Exemplo n.º 1
0
extern int get_mount_size(const char *mount_point, u64 *total_kilobytes, u64 *used_kilobytes){
	char *mount_info, *partition_info;
	char *follow_info, line[PATH_MAX];
	char target[PATH_MAX], *ptr;
	char device[8];
	u64 total_size;
disk_dbg("ARM: get_mount_size!\n");

	if(mount_point == NULL || total_kilobytes == NULL || used_kilobytes == NULL)
		return 0;

	*total_kilobytes = 0;
	*used_kilobytes = 0;

	mount_info = read_whole_file(MOUNT_FILE);
	if(mount_info == NULL)
		return 0;
	follow_info = mount_info;

	memset(target, 0, PATH_MAX);
	sprintf(target, " %s ", mount_point);
	memset(device, 0, 8);
	while(get_line_from_buffer(follow_info, line, PATH_MAX) != NULL){
		follow_info += strlen(line);

		if((ptr = strstr(line, target)) != NULL){
			*ptr = '\0';
			strncpy(device, line+5, 8);
			break;
		}
	}
	free(mount_info);

	if(strlen(device) <= 0)
		return 0;

	partition_info = read_whole_file(PARTITION_FILE);
	if(partition_info == NULL)
		return 0;
	follow_info = partition_info;

	memset(target, 0, PATH_MAX);
	while(get_line_from_buffer(follow_info, line, PATH_MAX) != NULL){
		follow_info += strlen(line);

		if(sscanf(line, "%*u %*u %llu %[^\n ]", &total_size, target) == 2){
			if(strcmp(device, target))
				continue;

			*total_kilobytes = total_size;
			break;
		}
	}
	free(partition_info);

	return 1;
}
Exemplo n.º 2
0
/**
 * @brief read process stderr
 * @param c the child to read stderr from
 * @returns 0 on success, -1 on error.
 */
int read_stderr(child_node *c) {
  buffer bigbuff;
  char buff[255];
  char *line;
  int count, ret;
  
  ret = 0;
  memset(&(bigbuff), 0, sizeof(buffer));
  
  while((count=read(c->stderr_fd, buff, 255)) > 0) {
    if(append_to_buffer(&(bigbuff), buff, count)) {
      ret = -1;
      goto exit;
    }
    
    while((line = get_line_from_buffer(&(bigbuff)))) {
      ret = on_cmd_stderr(c, line);
      free(line);
      
      if(ret)
        goto exit;
    }
  }
  
  exit:
  
  if(count<0) {
    print( ERROR, "read: %s", strerror(errno));
    ret = -1;
  }
  
  release_buffer(&(bigbuff));
  
  return ret;
}
Exemplo n.º 3
0
static void		justify(const char *orig, char *dest, unsigned int width)
{
	dest[0] = '\0';
	if (width < 1)
		return ;

	t_line				line;
	unsigned int		size = strlen(orig);
	unsigned int		n_buffer = 0;
	int					end_dest = 0;

	line.s = (char *)malloc((width + 1) * sizeof(char));
	get_line_from_buffer(orig, &n_buffer, width + 1, &line);
	while (n_buffer < size)
	{
		unsigned int		n_last_separator;

		/*To trim end of line*/
		n_last_separator = find_last_occ(line.s, width);
		/*Case 1: All words can be copied*/
		if (IS_SEP(line.s[width]))
		{
			end_dest += ft_strncat_endl(dest + end_dest, &line, n_last_separator, width - n_last_separator);
			n_buffer += width + 1;
			goto next;
		}
		/*Case 2: End of file, this is last line*/
		else if (line.s[0] != '\0' && line.s[width - 1] == '\0')
		{
			ft_strncat_endl(dest + end_dest, &line, width, 0);
			break ;
		}
		/*Case 3: Last word is cutted so we remove from this line*/
		else
		{
			--line.n_gaps;
			/*Empty line*/
			if (line.s[0] != '\0' || n_last_separator != width)
				end_dest += ft_strncat_endl(dest + end_dest, &line, n_last_separator, width - n_last_separator);
			n_buffer += n_last_separator;
		}
		next:
		get_line_from_buffer(orig, &n_buffer, width + 1, &line);
	}
	free(line.s);
}
Exemplo n.º 4
0
extern int read_mount_data(const char *device_name
		, char *mount_point, int mount_len
		, char *type, int type_len
		, char *right, int right_len
		){
	char *mount_info = read_whole_file(MOUNT_FILE);
	char *start, line[256];
	char target[8];

	if(mount_point == NULL || mount_len <= 0
			|| type == NULL || type_len <= 0
			|| right == NULL || right_len <= 0
			){
		usb_dbg("Bad input!!\n");
		return 0;
	}

	if(mount_info == NULL){
		usb_dbg("Failed to open \"%s\"!!\n", MOUNT_FILE);
		return 0;
	}

	memset(target, 0, 8);
	sprintf(target, "%s ", device_name);

	if((start = strstr(mount_info, target)) == NULL){
		//usb_dbg("disk_initial:: %s: Failed to execute strstr()!\n", device_name);
		free(mount_info);
		return 0;
	}

	start += strlen(target);

	if(get_line_from_buffer(start, line, 256) == NULL){
		usb_dbg("%s: Failed to execute get_line_from_buffer()!\n", device_name);
		free(mount_info);
		return 0;
	}

	memset(mount_point, 0, mount_len);
	memset(type, 0, type_len);
	memset(right, 0, right_len);

	if(sscanf(line, "%s %s %[^\n ]", mount_point, type, right) != 3){
		usb_dbg("%s: Failed to execute sscanf()!\n", device_name);
		free(mount_info);
		return 0;
	}

	right[2] = 0;

	free(mount_info);
	return 1;
}
Exemplo n.º 5
0
/**
 * @brief read from a child stdout.
 * @param c the child from read to
 * @returns 0 on success, -1 on error
 */
int read_stdout(child_node *c) {
  int count;
  int ret;
  char *buff;
  char *line;
  message *m;
  
  buff = malloc(STDOUT_BUFF_SIZE);
    
  if(!buff) {
    print( ERROR, "malloc: %s", strerror(errno));
    return -1;
  }
  
  ret = 0;
  
  if(c->handler->raw_output_parser) { // parse raw bytes
    while((count=read(c->stdout_fd, buff, STDOUT_BUFF_SIZE)) > 0) {
      if(c->handler->raw_output_parser(c, buff, count)) {
        ret = -1;
        break;
      }
    }
  } else if(c->handler->output_parser) { // parse lines
    while(!ret && (count=read(c->stdout_fd, buff, STDOUT_BUFF_SIZE)) > 0) {
      if(append_to_buffer(&(c->output_buff), buff, count)) {
        ret = -1;
        continue;
      }
      while((line = get_line_from_buffer(&(c->output_buff)))) {
        m = c->handler->output_parser(line);
        if(m) {
          m->head.id = c->id;
          m->head.seq = c->seq + 1;
          
#ifdef BROADCAST_EVENTS
          if(broadcast_message(m)) {
            print( ERROR, "cannot broadcast messages.");
#else
          if(enqueue_message(&(c->conn->outcoming), m)) {
            print( ERROR, "cannot enqueue messages.");
#endif
            dump_message(m);
            free_message(m);
            ret = -1;
            // events not sent are not fatal, just a missed event.
            // while in raw connection a missed message will de-align the output/input.
            // this is why a "break;" is missing here
          } else {
            c->seq++;
          }
        }
        free(line);
      }
    }
  } else { // send raw bytes
    while((count=read(c->stdout_fd, buff, STDOUT_BUFF_SIZE)) > 0) {
      m = create_message(c->seq + 1, count, c->id);
      if(!m) {
        buff[MIN(count, STDOUT_BUFF_SIZE -1)] = '\0';
        print( ERROR, "cannot send the following output: '%s'", buff);
        ret = -1;
        break;
      }
      memcpy(m->data, buff, count);
      if(enqueue_message(&(c->conn->outcoming), m)) {
        print( ERROR, "cannot enqueue messages.");
        dump_message(m);
        free_message(m);
        ret = -1;
        break;
      } else {
        c->seq++;
      }
    }
  }
  
  if(count<0) {
    print( ERROR, "read: %s", strerror(errno));
    ret = -1;
  }
  
  free(buff);
  
  release_buffer(&(c->output_buff));
  
  return ret;
}
Exemplo n.º 6
0
disk_info_t *read_disk_data(){
	disk_info_t *disk_info_list = NULL, *new_disk_info, **follow_disk_info_list;
	char *partition_info = read_whole_file(PARTITION_FILE);
	char *follow_info;
	char line[64], device_name[16];
	u32 major;
	disk_info_t *parent_disk_info;
	partition_info_t *new_partition_info, **follow_partition_list;
	u64 device_size;

	if(partition_info == NULL){
		usb_dbg("Failed to open \"%s\"!!\n", PARTITION_FILE);
		return disk_info_list;
	}
	follow_info = partition_info;

	memset(device_name, 0, 16);
	while(get_line_from_buffer(follow_info, line, 64) != NULL){
		follow_info += strlen(line);

		if(sscanf(line, "%u %*u %llu %[^\n ]", &major, &device_size, device_name) != 3)
			continue;
		if(major != USB_DISK_MAJOR)
			continue;
		if(device_size == 1) // extend partition.
			continue;

		if(is_disk_name(device_name)){ // Disk
			follow_disk_info_list = &disk_info_list;
			while(*follow_disk_info_list != NULL)
				follow_disk_info_list = &((*follow_disk_info_list)->next);

			new_disk_info = create_disk(device_name, follow_disk_info_list);
		}
		else if(is_partition_name(device_name, NULL)){ // Partition
			// Found a partition device.
			// Find the parent disk.
			parent_disk_info = disk_info_list;
			while(1){
				if(parent_disk_info == NULL){
					usb_dbg("Error while parsing %s: found "
									"partition '%s' but haven't seen the disk device "
									"of which it is a part.\n", PARTITION_FILE, device_name);
					free(partition_info);
					return disk_info_list;
				}

				if(!strncmp(device_name, parent_disk_info->device, 3))
					break;

				parent_disk_info = parent_disk_info->next;
			}

			follow_partition_list = &(parent_disk_info->partitions);
			while(*follow_partition_list != NULL){
				if((*follow_partition_list)->partition_order == 0){
					free_partition_data(follow_partition_list);
					parent_disk_info->partitions = NULL;
					follow_partition_list = &(parent_disk_info->partitions);
				}
				else
					follow_partition_list = &((*follow_partition_list)->next);
			}

			new_partition_info = create_partition(device_name, follow_partition_list);
			if(new_partition_info != NULL)
				new_partition_info->disk = parent_disk_info;
		}
	}

	free(partition_info);
	return disk_info_list;
}