示例#1
0
文件: xfs.c 项目: will-Do/libguestfs
guestfs_int_xfsinfo *
do_xfs_info (const char *pathordevice)
{
  int r;
  CLEANUP_FREE char *buf = NULL;
  CLEANUP_FREE char *out = NULL, *err = NULL;
  CLEANUP_FREE_STRING_LIST char **lines = NULL;
  int is_dev;

  is_dev = STREQLEN (pathordevice, "/dev/", 5);
  buf = is_dev ? strdup (pathordevice)
               : sysroot_path (pathordevice);
  if (buf == NULL) {
    reply_with_perror ("malloc");
    return NULL;
  }

  r = command (&out, &err, str_xfs_info, buf, NULL);
  if (r == -1) {
    reply_with_error ("%s", err);
    return NULL;
  }

  lines = split_lines (out);
  if (lines == NULL)
    return NULL;

  return parse_xfs_info (lines);
}
示例#2
0
文件: shttpd.c 项目: sakti/shttpd
http_request parse_http_request(ssize_t len, char *buffer)
{
    http_request tmp;
    int max_size = 100;
    char *lines[max_size];
    char *line[max_size];

    split_lines("\n", buffer, lines, max_size);
    puts(lines[0]);
    split_lines(" ", lines[0], line, max_size);
    tmp.method = line[0];
    tmp.path = line[1];
    tmp.http_version = line[2];
    tmp.len = len;
    return tmp;
}
示例#3
0
void lower_case(int input){
    fprintf(stderr, "B\n");
    int status;
    int p[2];
    if(pipe(p) == -1){
        printf("Could not create pipe\n");
        exit(1);
    }
    pid_t pid = fork();
    if(pid == (pid_t)0){
        //Replace tr stdin with stdout of last call
        dup2(input, 0);
        //Replace stdout of tr with write end of pipe.
        dup2(p[1], 1);
        close(input);
        close(p[1]);
        close(p[0]);
        split_lines(p[1]);
        if(execlp("tr", "tr", "A-Z", "a-z", NULL) == -1){
            printf("tr did not work!\n%s\n", strerror(errno));
            exit(1);
        }
    } else {
        close(p[1]);
        close(p[0]);
        wait(&status);
    }
}
示例#4
0
static guestfs_int_isoinfo *
isoinfo (const char *path)
{
  char *out = NULL, *err = NULL;
  int r;
  char **lines = NULL;
  guestfs_int_isoinfo *ret = NULL;

  /* --debug is necessary to get additional fields, in particular
   * the date & time fields.
   */
  r = command (&out, &err, str_isoinfo, "--debug", "-d", "-i", path, NULL);
  if (r == -1) {
    reply_with_error ("%s", err);
    goto done;
  }

  lines = split_lines (out);
  if (lines == NULL)
    goto done;

  ret = parse_isoinfo (lines);
  if (ret == NULL)
    goto done;

 done:
  free (out);
  free (err);
  if (lines)
    free_strings (lines);

  return ret;
}
示例#5
0
char *
do_part_get_name (const char *device, int partnum)
{
  CLEANUP_FREE char *parttype;

  parttype = do_part_get_parttype (device);
  if (parttype == NULL)
    return NULL;

  if (STREQ (parttype, "gpt")) {
    CLEANUP_FREE char *out = print_partition_table (device, true);
    if (!out)
      return NULL;

    CLEANUP_FREE_STRING_LIST char **lines = split_lines (out);

    if (!lines)
      return NULL;

    if (lines[0] == NULL || STRNEQ (lines[0], "BYT;")) {
      reply_with_error ("unknown signature, expected \"BYT;\" as first line of the output: %s",
                        lines[0] ? lines[0] : "(signature was null)");
      return NULL;
    }

    if (lines[1] == NULL) {
      reply_with_error ("parted didn't return a line describing the device");
      return NULL;
    }

    size_t row;
    int pnum;
    for (row = 2; lines[row] != NULL; ++row) {
      if (sscanf (lines[row], "%d:", &pnum) != 1) {
        reply_with_error ("could not parse row from output of parted print command: %s", lines[row]);
        return NULL;
      }
      if (pnum == partnum)
        break;
    }

    if (lines[row] == NULL) {
      reply_with_error ("partition number %d not found", partnum);
      return NULL;
    }

    char *name = get_table_field (lines[row], 5);
    if (name == NULL)
      reply_with_error ("cannot get the name field from '%s'", lines[row]);

    return name;
  } else {
    reply_with_error ("part-get-name can only be used on GUID Partition Tables");
    return NULL;
  }
}
示例#6
0
int
do_part_get_bootable (const char *device, int partnum)
{
  if (partnum <= 0) {
    reply_with_error ("partition number must be >= 1");
    return -1;
  }

  CLEANUP_FREE char *out = print_partition_table (device, true);
  if (!out)
    return -1;

  CLEANUP_FREE_STRING_LIST char **lines = split_lines (out);

  if (!lines)
    return -1;

  /* Partitions may not be in any order, so we have to look for
   * the matching partition number (RHBZ#602997).
   */
  if (lines[0] == NULL || STRNEQ (lines[0], "BYT;")) {
    reply_with_error ("unknown signature, expected \"BYT;\" as first line of the output: %s",
                      lines[0] ? lines[0] : "(signature was null)");
    return -1;
  }

  if (lines[1] == NULL) {
    reply_with_error ("parted didn't return a line describing the device");
    return -1;
  }

  size_t row;
  int pnum;
  for (row = 2; lines[row] != NULL; ++row) {
    if (sscanf (lines[row], "%d:", &pnum) != 1) {
      reply_with_error ("could not parse row from output of parted print command: %s", lines[row]);
      return -1;
    }
    if (pnum == partnum)
      break;
  }

  if (lines[row] == NULL) {
    reply_with_error ("partition number %d not found", partnum);
    return -1;
  }

  CLEANUP_FREE char *boot = get_table_field (lines[row], 6);
  if (boot == NULL)
    return -1;

  return strstr (boot, "boot") != NULL;
}
示例#7
0
文件: ext2.c 项目: will-Do/libguestfs
int64_t
ext_minimum_size (const char *device)
{
    CLEANUP_FREE char *err = NULL, *out = NULL;
    CLEANUP_FREE_STRING_LIST char **lines = NULL;
    int r;
    size_t i;
    int64_t ret;
    long block_size;
    const char *pattern = "Estimated minimum size of the filesystem: ";

    r = command (&out, &err, str_resize2fs, "-P", "-f", device, NULL);
    if (r == -1) {
        reply_with_error ("%s", err);
        return -1;
    }

    lines = split_lines (out);
    if (lines == NULL)
        return -1;

#if __WORDSIZE == 64
#define XSTRTOD64 xstrtol
#else
#define XSTRTOD64 xstrtoll
#endif

    for (i = 0; lines[i] != NULL; ++i) {
        if (STRPREFIX (lines[i], pattern)) {
            if (XSTRTOD64 (lines[i] + strlen (pattern),
                           NULL, 10, &ret, NULL) != LONGINT_OK) {
                reply_with_error ("cannot parse minimum size");
                return -1;
            }
            if ((block_size = get_block_size (device)) == -1)
                return -1;
            if (verbose) {
                fprintf (stderr, "Minimum size in blocks: %" SCNd64 \
                         "\nBlock count: %ld\n", ret, block_size);
            }
            if (INT64_MAX / block_size < ret) {
                reply_with_error ("filesystem size too big: overflow");
                return -1;
            }
            return ret * block_size;
        }
    }

#undef XSTRTOD64

    reply_with_error ("minimum size not found. Check output format:\n%s", out);
    return -1;
}
  void client::info(server_info & out)
  {
    send_(makecmd("INFO", true));
    string response = recv_bulk_reply_();

    if (response.empty())
      throw protocol_error("empty");

    string_vector lines;
    split_lines(response, lines);
    if (lines.empty())
      throw protocol_error("empty line for info");

    for (string_vector::const_iterator it = lines.begin();
         it != lines.end(); ++it)
    {
      const string & line = *it;
      string_vector line_parts;
      split(line, ':', line_parts);
      if (line_parts.size() != 2)
        throw protocol_error("unexpected line format for info");

      const string & key = line_parts[0];
      const string & val = line_parts[1];

      if (key == server_info_key_version)
        out.version = val;
      else if (key == server_info_key_bgsave_in_progress)
        out.bgsave_in_progress = value_from_string<unsigned long>(val) == 1;
      else if (key == server_info_key_connected_clients)
        out.connected_clients = value_from_string<unsigned long>(val);
      else if (key == server_info_key_connected_slaves)
        out.connected_slaves = value_from_string<unsigned long>(val);
      else if (key == server_info_key_used_memory)
        out.used_memory = value_from_string<unsigned long>(val);
      else if (key == server_info_key_changes_since_last_save)
        out.changes_since_last_save = value_from_string<unsigned long>(val);
      else if (key == server_info_key_last_save_time)
        out.last_save_time = value_from_string<unsigned long>(val);
      else if (key == server_info_key_total_connections_received)
        out.total_connections_received = value_from_string<unsigned long>(val);
      else if (key == server_info_key_total_commands_processed)
        out.total_commands_processed = value_from_string<unsigned long>(val);
      else if (key == server_info_key_uptime_in_seconds)
        out.uptime_in_seconds = value_from_string<unsigned long>(val);
      else if (key == server_info_key_uptime_in_days)
        out.uptime_in_days = value_from_string<unsigned long>(val);
      else if (key == server_info_key_role)
        out.role = val == server_info_value_role_master ? role_master : role_slave;
      else
        throw protocol_error(string("unexpected info key '") + key + "'");
    }
  }
示例#9
0
char *
do_part_get_disk_guid (const char *device)
{
  const char *pattern = "Disk identifier (GUID):";
  size_t i;

  CLEANUP_FREE char *err = NULL;
  int r = commandf (NULL, &err, COMMAND_FLAG_FOLD_STDOUT_ON_STDERR,
                    "sgdisk", device, "-p", NULL);
  if (r == -1) {
    reply_with_error ("%s %s -p: %s", "sgdisk", device, err);
    return NULL;
  }

  CLEANUP_FREE_STRING_LIST char **lines = split_lines (err);
  if (lines == NULL) {
    reply_with_error ("'%s %s -p' returned no output",
                      "sgdisk", device);
    return NULL;
  }

  for (i = 0; lines[i] != NULL; ++i) {
    if (STRPREFIX (lines[i], pattern)) {
      char *value = lines[i] + strlen (pattern);

      /* Skip any leading whitespace */
      value += strspn (value, " \t");

      /* Extract the actual information from the field. */
      char *ret = extract_uuid (value);
      if (ret == NULL) {
        /* The extraction function already sends the error. */
        return NULL;
      }

      return ret;
    }
  }

  /* If we got here it means we didn't find the field */
  reply_with_error ("sgdisk output did not contain disk GUID. "
                    "See LIBGUESTFS_DEBUG output for more details");
  return NULL;
}
示例#10
0
static guestfs_int_isoinfo *
isoinfo (const char *path)
{
  int r;
  CLEANUP_FREE char *out = NULL, *err = NULL;
  CLEANUP_FREE_STRING_LIST char **lines = NULL;

  /* --debug is necessary to get additional fields, in particular
   * the date & time fields.
   */
  r = command (&out, &err, "isoinfo", "--debug", "-d", "-i", path, NULL);
  if (r == -1) {
    reply_with_error ("%s", err);
    return NULL;
  }

  lines = split_lines (out);
  if (lines == NULL)
    return NULL;

  return parse_isoinfo (lines);
}
示例#11
0
文件: xfs.c 项目: msmhrt/libguestfs
guestfs_int_xfsinfo *
do_xfs_info (const char *pathordevice)
{
  int r;
  char *buf;
  char *out = NULL, *err = NULL;
  char **lines = NULL;
  guestfs_int_xfsinfo *ret = NULL;
  int is_dev;

  is_dev = STREQLEN (pathordevice, "/dev/", 5);
  buf = is_dev ? strdup (pathordevice)
               : sysroot_path (pathordevice);
  if (buf == NULL) {
    reply_with_perror ("malloc");
    return NULL;
  }

  r = command (&out, &err, "xfs_info", buf, NULL);
  free (buf);
  if (r == -1) {
    reply_with_error ("%s", err);
    goto error;
  }

  lines = split_lines (out);
  if (lines == NULL)
    goto error;

  ret = parse_xfs_info (lines);

error:
  free (err);
  free (out);
  if (lines)
    free_strings (lines);
  return ret;
}
示例#12
0
int64_t
ntfs_minimum_size (const char *device)
{
  CLEANUP_FREE char *err = NULL, *out = NULL;
  CLEANUP_FREE_STRING_LIST char **lines = NULL;
  int r;
  size_t i;
  int64_t volume_size = 0;
  const char *size_pattern = "You might resize at ",
             *full_pattern = "Volume is full",
             *cluster_size_pattern = "Cluster size",
             *volume_size_pattern = "Current volume size:";
  int is_full = 0;
  int32_t cluster_size = 0;

  /* FS may be marked for check, so force ntfsresize */
  r = command (&out, &err, str_ntfsresize, "--info", "-ff", device, NULL);

  lines = split_lines (out);
  if (lines == NULL)
    return -1;

  if (verbose) {
    for (i = 0; lines[i] != NULL; ++i)
      fprintf (stderr, "ntfs_minimum_size: lines[%zu] = \"%s\"\n", i, lines[i]);
  }

#if __WORDSIZE == 64
#define XSTRTOD64 xstrtol
#else
#define XSTRTOD64 xstrtoll
#endif

  if (r == -1) {
    /* If volume is full, ntfsresize returns error. */
    for (i = 0; lines[i] != NULL; ++i) {
      if (strstr (lines[i], full_pattern))
        is_full = 1;
      else if (STRPREFIX (lines[i], cluster_size_pattern)) {
        if (sscanf (lines[i] + strlen (cluster_size_pattern),
                    "%*[ ]:%" SCNd32, &cluster_size) != 1) {
          reply_with_error ("cannot parse cluster size");
          return -1;
        }
      }
      else if (STRPREFIX (lines[i], volume_size_pattern)) {
        if (XSTRTOD64 (lines[i] + strlen (volume_size_pattern),
                       NULL, 10, &volume_size, NULL) != LONGINT_OK) {
          reply_with_error ("cannot parse volume size");
          return -1;
        }
      }
    }
    if (is_full) {
      if (cluster_size == 0) {
        reply_with_error ("bad cluster size");
        return -1;
      }
      /* In case of a full filesystem, we estimate minimum size
       * as volume size rounded up to cluster size.
       */
      return (volume_size + cluster_size - 1) / cluster_size * cluster_size;
    }

    reply_with_error ("%s", err);
    return -1;
  }

  for (i = 0; lines[i] != NULL; ++i) {
    if (STRPREFIX (lines[i], size_pattern)) {
      int64_t ret;
      if (XSTRTOD64 (lines[i] + strlen (size_pattern),
                     NULL, 10, &ret, NULL) != LONGINT_OK) {
        reply_with_error ("cannot parse minimum size");
        return -1;
      }
      return ret;
    }
  }

#undef XSTRTOD64

  reply_with_error ("minimum size not found. Check output format:\n%s", out);
  return -1;
}
示例#13
0
__private_extern__ int
__part_load_locale(const char *name,
		unsigned char *using_locale,
		char **locale_buf,
		const char *category_filename,
		int locale_buf_size_max,
		int locale_buf_size_min,
		const char **dst_localebuf)
{
	int		saverr, fd, i, num_lines;
	char		*lbuf, *p;
	const char	*plim;
	char		filename[PATH_MAX];
	struct stat	st;
	size_t		namesize, bufsize;

	/*
	 * Slurp the locale file into the cache.
	 */
	namesize = strlen(name) + 1;

	/* 'PathLocale' must be already set & checked. */

	/* Range checking not needed, 'name' size is limited */
	strcpy(filename, _PathLocale);
	strcat(filename, "/");
	strcat(filename, name);
	strcat(filename, "/");
	strcat(filename, category_filename);
	if ((fd = _open(filename, O_RDONLY)) < 0)
		return (_LDP_ERROR);
	if (_fstat(fd, &st) != 0)
		goto bad_locale;
	if (st.st_size <= 0) {
		errno = EFTYPE;
		goto bad_locale;
	}
	bufsize = namesize + st.st_size;
	if ((lbuf = malloc(bufsize)) == NULL) {
		errno = ENOMEM;
		goto bad_locale;
	}
	(void)strcpy(lbuf, name);
	p = lbuf + namesize;
	plim = p + st.st_size;
	if (_read(fd, p, (size_t) st.st_size) != st.st_size)
		goto bad_lbuf;
	/*
	 * Parse the locale file into localebuf.
	 */
	if (plim[-1] != '\n') {
		errno = EFTYPE;
		goto bad_lbuf;
	}
	num_lines = split_lines(p, plim);
	if (num_lines >= locale_buf_size_max)
		num_lines = locale_buf_size_max;
	else if (num_lines < locale_buf_size_min) {
		errno = EFTYPE;
		goto bad_lbuf;
	}
	(void)_close(fd);
	/*
	 * Record the successful parse in the cache.
	 */
	if (*locale_buf != NULL)
		free(*locale_buf);
	*locale_buf = lbuf;
	for (p = *locale_buf, i = 0; i < num_lines; i++)
		dst_localebuf[i] = (p += strlen(p) + 1);
	for (i = num_lines; i < locale_buf_size_max; i++)
		dst_localebuf[i] = NULL;
	*using_locale = 1;

	return (_LDP_LOADED);

bad_lbuf:
	saverr = errno;
	free(lbuf);
	errno = saverr;
bad_locale:
	saverr = errno;
	(void)_close(fd);
	errno = saverr;

	return (_LDP_ERROR);
}
示例#14
0
char *
do_command (char *const *argv)
{
  char *out;
  CLEANUP_FREE char *err = NULL;
  int r;
  CLEANUP_BIND_STATE struct bind_state bind_state = { .mounted = false };
  CLEANUP_RESOLVER_STATE struct resolver_state resolver_state =
    { .mounted = false };

  /* We need a root filesystem mounted to do this. */
  NEED_ROOT (, return NULL);

  /* Conveniently, argv is already a NULL-terminated argv-style array
   * of parameters, so we can pass it straight in to our internal
   * commandv.  We just have to check the list is non-empty.
   */
  if (argv[0] == NULL) {
    reply_with_error ("passed an empty list");
    return NULL;
  }

  if (bind_mount (&bind_state) == -1)
    return NULL;
  if (enable_network) {
    if (set_up_etc_resolv_conf (&resolver_state) == -1)
      return NULL;
  }

  CHROOT_IN;
  r = commandv (&out, &err, (const char * const *) argv);
  CHROOT_OUT;

  free_bind_state (&bind_state);
  free_resolver_state (&resolver_state);

  if (r == -1) {
    reply_with_error ("%s", err);
    free (out);
    return NULL;
  }

  return out;			/* Caller frees. */
}

char **
do_command_lines (char *const *argv)
{
  CLEANUP_FREE char *out = NULL;
  char **lines;

  out = do_command (argv);
  if (out == NULL)
    return NULL;

  lines = split_lines (out);

  if (lines == NULL)
    return NULL;

  return lines;			/* Caller frees. */
}

char *
do_sh (const char *cmd)
{
  const char *argv[] = { "/bin/sh", "-c", cmd, NULL };

  return do_command ((char **) argv);
}

char **
do_sh_lines (const char *cmd)
{
  const char *argv[] = { "/bin/sh", "-c", cmd, NULL };

  return do_command_lines ((char **) argv);
}
示例#15
0
int
__part_load_locale(const char *name,
		int *using_locale,
		char *locale_buf,
		const char *category_filename,
		int locale_buf_size_max,
		int locale_buf_size_min,
		const char **dst_localebuf) {

	static char		locale_buf_C[] = "C";
	static int		num_lines;



	int			 fd;
	char			*lbuf;
	char			*p;
	const char 		*plim;
	char                     filename[PATH_MAX];
#ifdef __USE_INTERNAL_STAT64
	struct stat64		 st;
#else
	struct stat		 st;
#endif
	size_t			 namesize;
	size_t			 bufsize;
	int                      save_using_locale;
        char *nptr;

	save_using_locale = *using_locale;
	*using_locale = 0;

	if (name == NULL)
		goto no_locale;

	if (!strcmp(name, "C") || !strcmp(name, "POSIX"))
		return 0;

	/* XXX: when adding cxx support the function failed to compile, so
	 *      the missing functions below were commented out.
	 *      adding this message here to abort and warn the user */
    printf("__part_load_locale not implemented!\n");
    abort();

	/*
	 * If the locale name is the same as our cache, use the cache.
	 */
	lbuf = locale_buf;
	if (lbuf != NULL && strcmp(name, lbuf) == 0) {
		set_from_buf(lbuf, num_lines, dst_localebuf);
		*using_locale = 1;
		return 0;
	}

	/*
	 * Slurp the locale file into the cache.
	 */
	namesize = strlen(name) + 1;

	if (!_PathLocale)
		goto no_locale;
	/* Range checking not needed, 'name' size is limited */
	strcpy(filename, _PathLocale);
	strcat(filename, "/");
	strcat(filename, name);
	strcat(filename, "/");
	strcat(filename, category_filename);
	//fd = open(filename, O_RDONLY);
	if (fd < 0)
		goto no_locale;
#ifdef __USE_INTERNAL_STAT64
	if (fstat64(fd, &st) != 0)
#else
	//if (fstat(fd, &st) != 0)
#endif
	//	goto bad_locale;
	if (st.st_size <= 0)
		goto bad_locale;
	bufsize = namesize + st.st_size + 1;
	locale_buf = NULL;

        if (lbuf == NULL || lbuf == locale_buf_C)
          {
            lbuf = malloc(bufsize);
          }
        else
          {
            nptr = realloc(lbuf, bufsize);
            if (!nptr && lbuf)
              free (lbuf);
            lbuf = nptr;
          }

	if (lbuf == NULL)
		goto bad_locale;
	(void) strcpy(lbuf, name);
	p = lbuf + namesize;
	plim = p + st.st_size;
	//if (read(fd, p, (size_t) st.st_size) != st.st_size)
	//	goto bad_lbuf;
	//if (close(fd) != 0)
	//	goto bad_lbuf;
	/*
	 * Parse the locale file into localebuf.
	 */
	p[st.st_size] = '\0';
	if (plim[-1] != '\n')
		goto bad_lbuf;
	num_lines = split_lines(p, plim);
	if (num_lines >= locale_buf_size_max)
		num_lines = locale_buf_size_max;
	else if (num_lines >= locale_buf_size_min)
		num_lines = locale_buf_size_min;
	else
		goto reset_locale;
	set_from_buf(lbuf, num_lines, dst_localebuf);
	/*
	 * Record the successful parse in the cache.
	 */
	locale_buf = lbuf;

	*using_locale = 1;
	return 0;

reset_locale:
	locale_buf = locale_buf_C;
	save_using_locale = 0;
bad_lbuf:
	free(lbuf);
bad_locale:
	//(void)close(fd);
no_locale:
	*using_locale = save_using_locale;
	return -1;
}
示例#16
0
文件: dtrack.cpp 项目: 4og/schism
int DTrack::receive_udp_ascii(
	unsigned long* framenr, double* timestamp,
	int* nbodycal, int* nbody, dtrack_body_type* body, int max_nbody,
	int* nflystick, dtrack_flystick_type* flystick, int max_nflystick,
	int* nmeatool, dtrack_meatool_type* meatool, int max_nmeatool,
	int* nmarker, dtrack_marker_type* marker, int max_nmarker
){
	char* strs[PROT_MAX_LINES];
	char* s;
	int iline, nlines;
	int i, len, n;
	unsigned long ul, ularr[2];

	// Defaults:

	*framenr = 0;
	*timestamp = -1;   // i.e. not available
	*nbodycal = -1;    // i.e. not available
	*nbody = 0;
	*nflystick = 0;
	*nmeatool = 0;
	*nmarker = 0;

	// Receive udp packet:

	len = udp_receive(_udpsock, _udpbuf, _udpbufsize, _udptimeout_us);

	if(len == -1){
		return DTRACK_ERR_TIMEOUT;
	}
	if(len <= 0){
		return DTRACK_ERR_UDP;
	}

	// Split packet in lines:

	if((nlines = split_lines(_udpbuf, len, strs, PROT_MAX_LINES)) == 0){
		return DTRACK_ERR_PCK;
	}

	// Process lines:

	for(iline=0; iline<nlines; iline++){
		s = strs[iline];

		// Line for frame counter:

		if(!strncmp(s, "fr ", 3)){
			s += 3;
			
			if(!(s = get_ul(s, framenr))){       // get frame counter
				*framenr = 0;
				return DTRACK_ERR_PCK;
			}
			continue;
		}

		// Line for timestamp:

		if(!strncmp(s, "ts ", 3)){
			s += 3;
			
			if(!(s = get_d(s, timestamp))){      // get timestamp
				*timestamp = 0;
				return DTRACK_ERR_PCK;
			}
			continue;
		}
		
		// Line for additional information about number of calibrated bodies:

		if(!strncmp(s, "6dcal ", 6)){
			if(max_nbody <= 0){
				continue;
			}
			
			s += 6;
			
			if(!(s = get_ul(s, &ul))){            // get number of bodies
				return DTRACK_ERR_PCK;
			}

			*nbodycal = (int )ul;
			continue;
		}

		// Line for 6d data:

		if(!strncmp(s, "6d ", 3)){
			if(max_nbody <= 0){
				continue;
			}
			
			s += 3;
			
			if(!(s = get_ul(s, &ul))){            // get number of bodies
				return DTRACK_ERR_PCK;
			}

			*nbody = n = (int )ul;
			if(n > max_nbody){
				n = max_nbody;
			}

			for(i=0; i<n; i++){                  // get data of body
				if(!(s = get_block(s, "uf", &body[i].id, &body[i].quality))){
					return DTRACK_ERR_PCK;
				}
				
				if(!(s = get_block(s, "ffffff", NULL, body[i].loc))){
					return DTRACK_ERR_PCK;
				}
				
				if(!(s = get_block(s, "fffffffff", NULL, body[i].rot))){
					return DTRACK_ERR_PCK;
				}
			}
			
			continue;
		}
		
		// Line for flystick data:

		if(!strncmp(s, "6df ", 4)){
			if(max_nflystick <= 0){
				continue;
			}
			
			s += 4;
			
			if(!(s = get_ul(s, &ul))){            // get number of flysticks
				return DTRACK_ERR_PCK;
			}

			*nflystick = n = (int )ul;
			if(n > max_nflystick){
				n = max_nflystick;
			}

			for(i=0; i<n; i++){                  // get data of body
				if(!(s = get_block(s, "ufu", ularr, &flystick[i].quality))){
					return DTRACK_ERR_PCK;
				}
				flystick[i].id = ularr[0];
				flystick[i].bt = ularr[1];
				
				if(!(s = get_block(s, "ffffff", NULL, flystick[i].loc))){
					return DTRACK_ERR_PCK;
				}
				
				if(!(s = get_block(s, "fffffffff", NULL, flystick[i].rot))){
					return DTRACK_ERR_PCK;
				}
			}
			
			continue;
		}
		
		// Line for measurement tool data:

		if(!strncmp(s, "6dmt ", 5)){
			if(max_nmeatool <= 0){
				continue;
			}
			
			s += 5;
			
			if(!(s = get_ul(s, &ul))){            // get number of flysticks
				return DTRACK_ERR_PCK;
			}

			*nmeatool = n = (int )ul;
			if(n > max_nmeatool){
				n = max_nmeatool;
			}

			for(i=0; i<n; i++){                  // get data of body
				if(!(s = get_block(s, "ufu", ularr, &meatool[i].quality))){
					return DTRACK_ERR_PCK;
				}
				meatool[i].id = ularr[0];
				meatool[i].bt = ularr[1];
				
				if(!(s = get_block(s, "fff", NULL, meatool[i].loc))){
					return DTRACK_ERR_PCK;
				}
				
				if(!(s = get_block(s, "fffffffff", NULL, meatool[i].rot))){
					return DTRACK_ERR_PCK;
				}
			}
			
			continue;
		}
		
		// Line for single markers:

		if(!strncmp(s, "3d ", 3)){
			if(max_nmarker <= 0){
				continue;
			}
			
			s += 3;
			
			if(!(s = get_ul(s, &ul))){            // get number of markers
				return DTRACK_ERR_PCK;
			}

			*nmarker = n = (int )ul;
			if(n > max_nmarker){
				n = max_nmarker;
			}

			for(i=0; i<n; i++){                  // get marker data
				if(!(s = get_block(s, "uf", &marker[i].id, &marker[i].quality))){
					return DTRACK_ERR_PCK;
				}

				if(!(s = get_block(s, "fff", NULL, marker[i].loc))){
					return DTRACK_ERR_PCK;
				}
			}
			
			continue;
		}

		// ignore invalid line identifier
	}

	return DTRACK_ERR_NONE;
}
示例#17
0
char *
do_part_get_gpt_type(const char *device, int partnum)
{
  if (partnum <= 0) {
    reply_with_error ("partition number must be >= 1");
    return NULL;
  }

  CLEANUP_FREE char *partnum_str = NULL;
  if (asprintf (&partnum_str, "%i", partnum) == -1) {
    reply_with_perror ("asprintf");
    return NULL;
  }

  CLEANUP_FREE char *err = NULL;
  int r = commandf (NULL, &err, COMMAND_FLAG_FOLD_STDOUT_ON_STDERR,
                    str_sgdisk, device, "-i", partnum_str, NULL);

  if (r == -1) {
    reply_with_error ("%s %s -i %s: %s", str_sgdisk, device, partnum_str, err);
    return NULL;
  }

  char **lines = split_lines (err);
  if (lines == NULL) {
    reply_with_error ("'%s %s -i %i' returned no output",
                      str_sgdisk, device, partnum);
    return NULL;
  }

  /* Parse the output of sgdisk -i:
   * Partition GUID code: 21686148-6449-6E6F-744E-656564454649 (BIOS boot partition)
   * Partition unique GUID: 19AEC5FE-D63A-4A15-9D37-6FCBFB873DC0
   * First sector: 2048 (at 1024.0 KiB)
   * Last sector: 411647 (at 201.0 MiB)
   * Partition size: 409600 sectors (200.0 MiB)
   * Attribute flags: 0000000000000000
   * Partition name: 'EFI System Partition'
   */
  for (char **i = lines; *i != NULL; i++) {
    char *line = *i;

    /* Skip blank lines */
    if (line[0] == '\0') continue;

    /* Split the line in 2 at the colon */
    char *colon = strchr (line, ':');
    if (colon) {
#define SEARCH "Partition GUID code"
      if (colon - line == strlen(SEARCH) &&
          memcmp (line, SEARCH, strlen(SEARCH)) == 0)
      {
#undef SEARCH
        /* The value starts after the colon */
        char *value = colon + 1;

        /* Skip any leading whitespace */
        value += strspn (value, " \t");

        /* The value contains only valid GUID characters */
        size_t value_len = strspn (value, "-0123456789ABCDEF");

        char *ret = malloc (value_len + 1);
        if (ret == NULL) {
          reply_with_perror ("malloc");
          return NULL;
        }

        memcpy (ret, value, value_len);
        ret[value_len] = '\0';
        return ret;
      }
    } else {
      /* Ignore lines with no colon. Log to stderr so it will show up in
       * LIBGUESTFS_DEBUG. */
      if (verbose) {
        fprintf (stderr, "get-gpt-type: unexpected sgdisk output ignored: %s\n",
                 line);
      }
    }
  }

  /* If we got here it means we didn't find the Partition GUID code */
  reply_with_error ("sgdisk output did not contain Partition GUID code. "
                    "See LIBGUESTFS_DEBUG output for more details");
  return NULL;
}
示例#18
0
int
do_part_get_bootable (const char *device, int partnum)
{
  if (partnum <= 0) {
    reply_with_error ("partition number must be >= 1");
    return -1;
  }

  int parted_has_m_opt = test_parted_m_opt ();
  if (parted_has_m_opt == -1)
    return -1;

  char *out = print_partition_table (device, parted_has_m_opt);
  if (!out)
    return -1;

  char **lines = split_lines (out);
  free (out);

  if (!lines)
    return -1;

  if (parted_has_m_opt) {
    /* New-style parsing using the "machine-readable" format from
     * 'parted -m'.
     *
     * Partitions may not be in any order, so we have to look for
     * the matching partition number (RHBZ#602997).
     */
    if (lines[0] == NULL || STRNEQ (lines[0], "BYT;")) {
      reply_with_error ("unknown signature, expected \"BYT;\" as first line of the output: %s",
                        lines[0] ? lines[0] : "(signature was null)");
      free_strings (lines);
      return -1;
    }

    if (lines[1] == NULL) {
      reply_with_error ("parted didn't return a line describing the device");
      free_strings (lines);
      return -1;
    }

    size_t row;
    int pnum;
    for (row = 2; lines[row] != NULL; ++row) {
      if (sscanf (lines[row], "%d:", &pnum) != 1) {
        reply_with_error ("could not parse row from output of parted print command: %s", lines[row]);
        free_strings (lines);
        return -1;
      }
      if (pnum == partnum)
        break;
    }

    if (lines[row] == NULL) {
      reply_with_error ("partition number %d not found", partnum);
      free_strings (lines);
      return -1;
    }

    char *boot = get_table_field (lines[row], 6);
    if (boot == NULL) {
      free_strings (lines);
      return -1;
    }

    int r = STREQ (boot, "boot");

    free (boot);
    free_strings (lines);

    return r;
  }
  else {
    /* Old-style: First look for the line matching "^Number". */
    size_t start = 0, header, row;

    for (row = 0; lines[row] != NULL; ++row)
      if (STRPREFIX (lines[row], "Number")) {
        start = row+1;
        header = row;
        break;
      }

    if (start == 0) {
      reply_with_error ("parted output has no \"Number\" line");
      free_strings (lines);
      return -1;
    }

    /* Now we have to look at the column number of the "Flags" field.
     * This is because parted's output has no way to represent a
     * missing field except as whitespace, so we cannot just count
     * fields from the left.  eg. The "File system" field is often
     * missing in the output.
     */
    char *p = strstr (lines[header], "Flags");
    if (!p) {
      reply_with_error ("parted output has no \"Flags\" field");
      free_strings (lines);
      return -1;
    }
    size_t col = p - lines[header];

    /* Look for the line corresponding to this partition number. */
    row = start + partnum - 1;
    if (row >= count_strings (lines) || !STRPREFIX (lines[row], " ")) {
      reply_with_error ("partition number out of range: %d", partnum);
      free_strings (lines);
      return -1;
    }

    int r = STRPREFIX (&lines[row][col], "boot");
    free_strings (lines);
    return r;
  }
}
示例#19
0
guestfs_int_partition_list *
do_part_list (const char *device)
{
  int parted_has_m_opt = test_parted_m_opt ();
  if (parted_has_m_opt == -1)
    return NULL;

  char *out = print_partition_table (device, parted_has_m_opt);
  if (!out)
    return NULL;

  char **lines = split_lines (out);
  free (out);

  if (!lines)
    return NULL;

  guestfs_int_partition_list *r;

  if (parted_has_m_opt) {
    /* New-style parsing using the "machine-readable" format from
     * 'parted -m'.
     *
     * lines[0] is "BYT;", lines[1] is the device line which we ignore,
     * lines[2..] are the partitions themselves.  Count how many.
     */
    size_t nr_rows = 0, row;
    for (row = 2; lines[row] != NULL; ++row)
      ++nr_rows;

    r = malloc (sizeof *r);
    if (r == NULL) {
      reply_with_perror ("malloc");
      goto error1;
    }
    r->guestfs_int_partition_list_len = nr_rows;
    r->guestfs_int_partition_list_val =
      malloc (nr_rows * sizeof (guestfs_int_partition));
    if (r->guestfs_int_partition_list_val == NULL) {
      reply_with_perror ("malloc");
      goto error2;
    }

    /* Now parse the lines. */
    size_t i;
    for (i = 0, row = 2; lines[row] != NULL; ++i, ++row) {
      if (sscanf (lines[row], "%d:%" SCNi64 "B:%" SCNi64 "B:%" SCNi64 "B",
                  &r->guestfs_int_partition_list_val[i].part_num,
                  &r->guestfs_int_partition_list_val[i].part_start,
                  &r->guestfs_int_partition_list_val[i].part_end,
                  &r->guestfs_int_partition_list_val[i].part_size) != 4) {
        reply_with_error ("could not parse row from output of parted print command: %s", lines[row]);
        goto error3;
      }
    }
  }
  else {
    /* Old-style.  Start at the line following "^Number", up to the
     * next blank line.
     */
    size_t start = 0, end = 0, row;

    for (row = 0; lines[row] != NULL; ++row)
      if (STRPREFIX (lines[row], "Number")) {
        start = row+1;
        break;
      }

    if (start == 0) {
      reply_with_error ("parted output has no \"Number\" line");
      goto error1;
    }

    for (row = start; lines[row] != NULL; ++row)
      if (STREQ (lines[row], "")) {
        end = row;
        break;
      }

    if (end == 0) {
      reply_with_error ("parted output has no blank after end of table");
      goto error1;
    }

    size_t nr_rows = end - start;

    r = malloc (sizeof *r);
    if (r == NULL) {
      reply_with_perror ("malloc");
      goto error1;
    }
    r->guestfs_int_partition_list_len = nr_rows;
    r->guestfs_int_partition_list_val =
      malloc (nr_rows * sizeof (guestfs_int_partition));
    if (r->guestfs_int_partition_list_val == NULL) {
      reply_with_perror ("malloc");
      goto error2;
    }

    /* Now parse the lines. */
    size_t i;
    for (i = 0, row = start; row < end; ++i, ++row) {
      if (sscanf (lines[row], " %d %" SCNi64 "B %" SCNi64 "B %" SCNi64 "B",
                  &r->guestfs_int_partition_list_val[i].part_num,
                  &r->guestfs_int_partition_list_val[i].part_start,
                  &r->guestfs_int_partition_list_val[i].part_end,
                  &r->guestfs_int_partition_list_val[i].part_size) != 4) {
        reply_with_error ("could not parse row from output of parted print command: %s", lines[row]);
        goto error3;
      }
    }
  }

  free_strings (lines);
  return r;

 error3:
  free (r->guestfs_int_partition_list_val);
 error2:
  free (r);
 error1:
  free_strings (lines);
  return NULL;
}
示例#20
0
char *
do_part_get_parttype (const char *device)
{
  int parted_has_m_opt = test_parted_m_opt ();
  if (parted_has_m_opt == -1)
    return NULL;

  char *out = print_partition_table (device, parted_has_m_opt);
  if (!out)
    return NULL;

  if (parted_has_m_opt) {
    /* New-style parsing using the "machine-readable" format from
     * 'parted -m'.
     */
    char **lines = split_lines (out);
    free (out);

    if (!lines)
      return NULL;

    if (lines[0] == NULL || STRNEQ (lines[0], "BYT;")) {
      reply_with_error ("unknown signature, expected \"BYT;\" as first line of the output: %s",
                        lines[0] ? lines[0] : "(signature was null)");
      free_strings (lines);
      return NULL;
    }

    if (lines[1] == NULL) {
      reply_with_error ("parted didn't return a line describing the device");
      free_strings (lines);
      return NULL;
    }

    /* lines[1] is something like:
     * "/dev/sda:1953525168s:scsi:512:512:msdos:ATA Hitachi HDT72101;"
     */
    char *r = get_table_field (lines[1], 5);
    if (r == NULL) {
      free_strings (lines);
      return NULL;
    }

    free_strings (lines);

    /* If "loop" return an error (RHBZ#634246). */
    if (STREQ (r, "loop")) {
      free (r);
      reply_with_error ("not a partitioned device");
      return NULL;
    }

    return r;
  }
  else {
    /* Old-style.  Look for "\nPartition Table: <str>\n". */
    char *p = strstr (out, "\nPartition Table: ");
    if (!p) {
      reply_with_error ("parted didn't return Partition Table line");
      free (out);
      return NULL;
    }

    p += 18;
    char *q = strchr (p, '\n');
    if (!q) {
      reply_with_error ("parted Partition Table has no end of line char");
      free (out);
      return NULL;
    }

    *q = '\0';

    p = strdup (p);
    free (out);
    if (!p) {
      reply_with_perror ("strdup");
      return NULL;
    }

    /* If "loop" return an error (RHBZ#634246). */
    if (STREQ (p, "loop")) {
      free (p);
      reply_with_error ("not a partitioned device");
      return NULL;
    }

    return p;                   /* caller frees */
  }
}
示例#21
0
int GUILabel::SplitLinesForDrawing()
{
    numlines = 0;
    split_lines(_textToDraw, Width, Font);
    return numlines;
}
示例#22
0
文件: ldpart.c 项目: ikonst/crystax
int
__part_load_locale(const char *name,
		int *using_locale,
		char **locale_buf,
		const char *category_filename,
		int locale_buf_size_max,
		int locale_buf_size_min,
		const char **dst_localebuf)
{
    __crystax_locale_data_t *ld;
	int		saverr, i, num_lines;
	char		*lbuf, *p;
	const char	*plim;
	char		filename[PATH_MAX];
	size_t		namesize, bufsize;

	/* 'name' must be already checked. */
	if (strcmp(name, "C") == 0 || strcmp(name, "POSIX") == 0) {
		*using_locale = 0;
		return (_LDP_CACHE);
	}

	/*
	 * If the locale name is the same as our cache, use the cache.
	 */
	if (*locale_buf != NULL && strcmp(name, *locale_buf) == 0) {
		*using_locale = 1;
		return (_LDP_CACHE);
	}

	/*
	 * Slurp the locale file into the cache.
	 */
	namesize = strlen(name) + 1;

    ld = __crystax_locale_get_part_data(name, category_filename);
    if (ld == NULL)
        return (_LDP_ERROR);
	bufsize = namesize + ld->size;
	if ((lbuf = malloc(bufsize)) == NULL) {
		errno = ENOMEM;
		goto bad_locale;
	}
	(void)strcpy(lbuf, name);
	p = lbuf + namesize;
	plim = p + ld->size;
    memmove(p, ld->data, ld->size);
	/*
	 * Parse the locale file into localebuf.
	 */
	if (plim[-1] != '\n') {
		errno = EFTYPE;
		goto bad_lbuf;
	}
	num_lines = split_lines(p, plim);
	if (num_lines >= locale_buf_size_max)
		num_lines = locale_buf_size_max;
	else if (num_lines >= locale_buf_size_min)
		num_lines = locale_buf_size_min;
	else {
		errno = EFTYPE;
		goto bad_lbuf;
	}
	/*
	 * Record the successful parse in the cache.
	 */
	if (*locale_buf != NULL)
		free(*locale_buf);
	*locale_buf = lbuf;
	for (p = *locale_buf, i = 0; i < num_lines; i++)
		dst_localebuf[i] = (p += strlen(p) + 1);
	for (i = num_lines; i < locale_buf_size_max; i++)
		dst_localebuf[i] = NULL;
	*using_locale = 1;

	return (_LDP_LOADED);

bad_lbuf:
	saverr = errno;
	free(lbuf);
	errno = saverr;
bad_locale:

	return (_LDP_ERROR);
}
int main(int argc, char *argv[])
{
	int fd[3] = { -1, -1, -1 };
	pid_t child = 0;
	int i, r, len, status;
	fd_set read_fds;
	struct timeval tv;
	char buf[1024], line[2][1024];
	const char *password = "";

	if (argc < 5 || (!strcmp(argv[4], "-p") && argc < 7)) {
		printf("usage: %s ip port username [-p password] "
		    "command [args]\n", argv[0]);
		return (1);
	}
	if (!strcmp(argv[4], "-p"))
		password = argv[5];

	signal(SIGPIPE, SIG_IGN);
	if (!(child = bpopen(argv + (password[0] ? 6 : 4), &fd[1], &fd[2])))
		goto done;
	sleep(2); /* allow child to properly startup... */
	if (waitpid(child, &status, WNOHANG)) {
		if (WIFEXITED(status))
			printf("command terminated with exit status %d\n",
			    WEXITSTATUS(status));
		else if (WIFSIGNALED(status))
			printf("command terminated by signal %d\n",
			    WTERMSIG(status));
		else
			printf("command terminated (status %d)\n", status);
		goto done;
	}

	if ((fd[0] = tcp_connect(argv[1], atoi(argv[2]))) < 0)
		goto done;
	if (password[0])
		snprintf(buf, sizeof(buf), "USER %s PASS %s\n",
		    argv[3], password);
	else
		snprintf(buf, sizeof(buf), "USER %s\n", argv[3]);
	write(fd[0], buf, strlen(buf));

	printf("connected to %s:%s, waiting for game\n", argv[1], argv[2]);
	fflush(stdout);
	line[0][0] = line[1][0] = 0;
	while (1) {
		FD_ZERO(&read_fds);
		FD_SET(fd[0], &read_fds);
		FD_SET(fd[1], &read_fds);
		tv.tv_sec = 0;
		tv.tv_usec = 1000;
		r = select(((fd[0] > fd[1]) ? fd[0] : fd[1]) + 1, &read_fds,
		    NULL, NULL, &tv);
		if (r < 0) {
			if (errno != EINTR) {
				printf("select: %s\n", strerror(errno));
				goto done;
			}
			continue;
		}
		if (r == 0)
			continue;
		for (i = 0; i < 2; ++i) {
			if (!FD_ISSET(fd[i], &read_fds))
				continue;
			len = read(fd[i], buf, sizeof(buf) - 1);
			if (len < 0) {
				if (errno != EINTR) {
					printf("read: %s\n", strerror(errno));
					goto done;
				}
				continue;
			}
			if (len == 0)
				goto done;
			buf[len] = 0;
			split_lines(buf, line[i], sizeof(line[i]),
			    i ? fd[0] : fd[2]);
		}
	}
	
done:
	for (i = 0; i < 3; ++i)
		if (fd[i] != -1)
			close(fd[i]);
	if (child && !waitpid(child, &status, WNOHANG)) {
		sleep(1);
		if (kill(child, SIGKILL))
			printf("kill: %s\n", strerror(errno));
		wait(NULL);
	}
	return (0);
}