static void parse_int_param(prop_dictionary_t props, const char *name, const char *value) { int64_t intvalue; assert(name != NULL); assert(value != NULL); if (dehumanize_number(value, &intvalue) != 0) err(EXIT_FAILURE, "Invalid integer value `%s'", value); prop_dictionary_set(props, name, prop_number_create_integer(intvalue)); }
int string_to_capacity(const char *string, long *capacity) { int error; int64_t result; if (!strcmp(string, "*")) { *capacity = -1; return(1); } error = dehumanize_number(string, &result); if (error != 0) return(0); result /= 1024 * 1024; if (result == 0) return(0); *capacity = result; return(1); }
void mount_tmpfs_parseargs(int argc, char *argv[], struct tmpfs_args *args, int *mntflags, char *canon_dev, char *canon_dir) { int gidset, modeset, uidset; /* Ought to be 'bool'. */ int ch; gid_t gid; uid_t uid; mode_t mode; int64_t tmpnumber; mntoptparse_t mp; struct stat sb; /* Set default values for mount point arguments. */ memset(args, 0, sizeof(*args)); args->ta_version = TMPFS_ARGS_VERSION; args->ta_size_max = 0; args->ta_nodes_max = 0; *mntflags = 0; gidset = 0; gid = 0; uidset = 0; uid = 0; modeset = 0; mode = 0; optind = optreset = 1; while ((ch = getopt(argc, argv, "g:m:n:o:s:u:")) != -1 ) { switch (ch) { case 'g': gid = a_gid(optarg); gidset = 1; break; case 'm': mode = a_mask(optarg); modeset = 1; break; case 'n': if (dehumanize_number(optarg, &tmpnumber) == -1) err(EXIT_FAILURE, "failed to parse nodes `%s'", optarg); args->ta_nodes_max = tmpnumber; break; case 'o': mp = getmntopts(optarg, mopts, mntflags, 0); if (mp == NULL) err(EXIT_FAILURE, "getmntopts"); freemntopts(mp); break; case 's': if (dehumanize_number(optarg, &tmpnumber) == -1) err(EXIT_FAILURE, "failed to parse size `%s'", optarg); args->ta_size_max = tmpnumber; break; case 'u': uid = a_uid(optarg); uidset = 1; break; case '?': default: usage(); } } argc -= optind; argv += optind; if (argc != 2) usage(); strlcpy(canon_dev, argv[0], MAXPATHLEN); pathadj(argv[1], canon_dir); if (stat(canon_dir, &sb) == -1) err(EXIT_FAILURE, "cannot stat `%s'", canon_dir); args->ta_root_uid = uidset ? uid : sb.st_uid; args->ta_root_gid = gidset ? gid : sb.st_gid; args->ta_root_mode = modeset ? mode : sb.st_mode; }
void parse_pid(char *c, node_t *node, int nn, int pid) { char *p, *q, *n, buf[80]; size_t s; int i, col=0; procdata[nn][pid].node = node; n = q = strdup(c); s = strspn(q, " "); q += s; p = strsep(&q, " "); while (p != NULL) { DPRINTF("got data column %d %s\n", col, p); switch (col) { case 0: /* pid */ procdata[nn][pid].pid = atoi(p); DPRINTF("pid = %d\n", procdata[nn][pid].pid); break; case 1: strncpy(procdata[nn][pid].username, p, 9); DPRINTF("user=%s\n", procdata[nn][pid].username); break; case 4: if (topmode == TOP_NORMAL_THR) break; procdata[nn][pid].size = dehumanize_number(p); if (topmode == TOP_PROCPS) procdata[nn][pid].size *= 1024; DPRINTF("size=%d\n", procdata[nn][pid].size); break; case 5: if (topmode == TOP_NORMAL_THR) procdata[nn][pid].size = dehumanize_number(p); else procdata[nn][pid].res = dehumanize_number(p); if (topmode == TOP_PROCPS) procdata[nn][pid].res *= 1024; DPRINTF("res=%d\n", procdata[nn][pid].res); DPRINTF("size=%d\n", procdata[nn][pid].size); break; case 6: if (topmode == TOP_NORMAL_THR) procdata[nn][pid].res = dehumanize_number(p); DPRINTF("res=%d\n", procdata[nn][pid].res); DPRINTF("size=%d\n", procdata[nn][pid].size); break; case 8: if (topmode == TOP_PROCPS) { sscanf(p, "%f%%", &procdata[nn][pid].cpu); DPRINTF("cpu=%f\n", procdata[nn][pid].cpu); } case 9: if (topmode != TOP_PROCPS) { sscanf(p, "%f%%", &procdata[nn][pid].cpu); DPRINTF("cpu=%f\n", procdata[nn][pid].cpu); } break; case 10: if (topmode != TOP_PROCPS) { strncpy(procdata[nn][pid].command, p, 30); for (i=0; i < 30; i++) if (procdata[nn][pid].command[i] == '\n') procdata[nn][pid].command[i] = '\0'; DPRINTF("comm=%s\n", procdata[nn][pid].command); } break; case 12: if (topmode == TOP_PROCPS) { strncpy(procdata[nn][pid].command, p, 30); for (i=0; i < 30; i++) if (procdata[nn][pid].command[i] == '\n') procdata[nn][pid].command[i] = '\0'; DPRINTF("comm=%s\n", procdata[nn][pid].command); } break; default: break; } if (q != NULL) { s = strspn(q, " "); q += s; } p = strsep(&q, " "); col++; } free(n); }
void parse_mem(char *c, nodedata_t *nd, int which) { char *p, *q, *r, *n, buf[30]; n = q = strdup(c); q = strchr(q, ':'); q++; p = strsep(&q, ","); while (p != NULL) { DPRINTF("got %s\n", p); if (strstr(p, "inactive") != NULL) { sscanf(p, "%s inactive", buf); nd->inactmem = dehumanize_number(buf); DPRINTF(" inactive final %lld\n", nd->inactmem); } else if (strstr(p, "swap free") != NULL) { sscanf(p, "%s swap free", buf); nd->swapfree = dehumanize_number(buf); nd->swap += nd->swapfree; } else if (strstr(p, "swap in use") != NULL) { sscanf(p, "%s swap in use", buf); nd->swap = dehumanize_number(buf); } else if (strstr(p, "active") != NULL) { sscanf(p, "%s active", buf); nd->actmem = dehumanize_number(buf); DPRINTF(" active final %lld\n", nd->actmem); } else if (strstr(p, "used") != NULL && !which) { sscanf(p, "%s used", buf); nd->wiredmem = dehumanize_number(buf); DPRINTF(" wired final %lld\n", nd->wiredmem); } else if (strstr(p, "shared") != NULL) { sscanf(p, "%s shared", buf); nd->execmem = dehumanize_number(buf); DPRINTF(" exec final %lld\n", nd->execmem); } else if (strstr(p, "free") != NULL && !which) { sscanf(p, "%s free", buf); nd->freemem = dehumanize_number(buf); DPRINTF(" memfree final %lld\n", nd->freemem); } else if (strstr(p, "cached") != NULL) { r = strstr(p, "free"); sscanf(p, "%s free", buf); if (which) { nd->swapfree = dehumanize_number(buf); DPRINTF(" swapfree final %lld\n", nd->swapfree); } DPRINTF("FOO: %s %s\n", r, p); if (r != NULL) { while (!isdigit(*r)) r++; sscanf(r, "%s cached", buf); } else sscanf(p, "%s cached", buf); nd->filemem = dehumanize_number(buf); DPRINTF(" cached final %lld\n", nd->filemem); } else if (strstr(p, "av") != NULL && which) { sscanf(p, "%s av", buf); nd->swap = dehumanize_number(buf); DPRINTF(" swaptotal final %lld\n", nd->swap); } else if (strstr(p, "Act") != NULL) { sscanf(p, "%s Act", buf); nd->actmem = dehumanize_number(buf); DPRINTF(" final %lld\n", nd->actmem); } else if (strstr(p, "Inact") != NULL) { sscanf(p, "%s Inact", buf); nd->inactmem = dehumanize_number(buf); DPRINTF(" final %lld\n", nd->inactmem); } else if (strstr(p, "Wired") != NULL) { sscanf(p, "%s Wired", buf); nd->wiredmem = dehumanize_number(buf); DPRINTF(" final %lld\n", nd->wiredmem); } else if (strstr(p, "Exec") != NULL) { sscanf(p, "%s Exec", buf); nd->execmem = dehumanize_number(buf); DPRINTF(" final %lld\n", nd->execmem); } else if (strstr(p, "File") != NULL) { sscanf(p, "%s File", buf); nd->filemem = dehumanize_number(buf); DPRINTF(" final %lld\n", nd->filemem); } else if (strstr(p, "Free") != NULL) { sscanf(p, "%s Free", buf); if (which) { nd->swapfree = dehumanize_number(buf); DPRINTF(" final %lld\n", nd->swapfree); } else { nd->freemem = dehumanize_number(buf); DPRINTF(" final %lld\n", nd->freemem); } } else if (strstr(p, "Total") != NULL) { sscanf(p, "%s Total", buf); nd->swap = dehumanize_number(buf); DPRINTF(" final %lld\n", nd->swap); } p = strsep(&q, ","); } free(n); }