/* * This function is adapted from vm_parse_memsize in * /lib/libvmmapi/vmmapi.c in the FreeBSD Source tree. */ static int bhyveParseMemsize(const char *arg, size_t *ret_memsize) { size_t val; int error; if (virStrToLong_ul(arg, NULL, 10, &val) == 0) { /* * For the sake of backward compatibility if the memory size * specified on the command line is less than a megabyte then * it is interpreted as being in units of MB. */ if (val < 1024 * 1024UL) val *= 1024 * 1024UL; *ret_memsize = val; error = 0; } else { error = expand_number(arg, ret_memsize); } /* use memory in KiB here */ *ret_memsize /= 1024UL; return error; }
static int expand_number_int(const char *buf, int *num) { int64_t num64; int rval; rval = expand_number(buf, &num64); if (rval < 0) return (rval); if (num64 > INT_MAX || num64 < INT_MIN) { errno = ERANGE; return (-1); } *num = (int)num64; return (0); }
/* * This routine replaces "human-readable" number with its expanded form. */ static char * expand_amount(char *rule) { uint64_t num; const char *subject, *subject_id, *resource, *action, *amount, *per; char *copy, *expanded; copy = strdup(rule); if (copy == NULL) err(1, "strdup"); subject = strsep(©, ":"); subject_id = strsep(©, ":"); resource = strsep(©, ":"); action = strsep(©, "=/"); amount = strsep(©, "/"); per = copy; if (amount == NULL || strlen(amount) == 0) { free(copy); return (rule); } assert(subject != NULL); assert(subject_id != NULL); assert(resource != NULL); assert(action != NULL); if (expand_number(amount, &num)) err(1, "expand_number"); if (per == NULL) asprintf(&expanded, "%s:%s:%s:%s=%ju", subject, subject_id, resource, action, (uintmax_t)num); else asprintf(&expanded, "%s:%s:%s:%s=%ju/%s", subject, subject_id, resource, action, (uintmax_t)num, per); if (expanded == NULL) err(1, "asprintf"); return (expanded); }
int main(int argc, char *argv[]) { int i=0; uint64_t number; if (argc!=2) return(1); if ( is_number(argv[1])==1) { //num num if (expand_number(argv[1], &number) == -1) { // invalid value for val argument // printf("Bad value\n"); exit(1); } else { printf("%lu",number); exit(0); } } else prthumanval(atol(argv[1])); return 0; }
int vm_parse_memsize(const char *optarg, size_t *ret_memsize) { char *endptr; size_t optval; int error; optval = strtoul(optarg, &endptr, 0); if (*optarg != '\0' && *endptr == '\0') { /* * For the sake of backward compatibility if the memory size * specified on the command line is less than a megabyte then * it is interpreted as being in units of MB. */ if (optval < MB) optval *= MB; *ret_memsize = optval; error = 0; } else error = expand_number(optarg, ret_memsize); return (error); }
int main(int argc, char **argv) { int ch; int error; char *ep, *p; setlocale(LC_ALL, ""); dflag = false; while ((ch = getopt(argc, argv, "0123456789a:b:dl:n:p:")) != -1) switch (ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': /* * Undocumented kludge: split was originally designed * to take a number after a dash. */ if (numlines == 0) { p = argv[optind - 1]; if (p[0] == '-' && p[1] == ch && !p[2]) numlines = strtol(++p, &ep, 10); else numlines = strtol(argv[optind] + 1, &ep, 10); if (numlines <= 0 || *ep) errx(EX_USAGE, "%s: illegal line count", optarg); } break; case 'a': /* Suffix length */ if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep) errx(EX_USAGE, "%s: illegal suffix length", optarg); break; case 'b': /* Byte count. */ errno = 0; error = expand_number(optarg, &bytecnt); if (error == -1) errx(EX_USAGE, "%s: offset too large", optarg); break; case 'd': /* Decimal suffix */ dflag = true; break; case 'l': /* Line count. */ if (numlines != 0) usage(); if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep) errx(EX_USAGE, "%s: illegal line count", optarg); break; case 'n': /* Chunks. */ if (!isdigit((unsigned char)optarg[0]) || (chunks = (size_t)strtoul(optarg, &ep, 10)) == 0 || *ep != '\0') { errx(EX_USAGE, "%s: illegal number of chunks", optarg); } break; case 'p': /* pattern matching. */ if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0) errx(EX_USAGE, "%s: illegal regexp", optarg); pflag = 1; break; default: usage(); } argv += optind; argc -= optind; if (*argv != NULL) { /* Input file. */ if (strcmp(*argv, "-") == 0) ifd = STDIN_FILENO; else if ((ifd = open(*argv, O_RDONLY, 0)) < 0) err(EX_NOINPUT, "%s", *argv); ++argv; } if (*argv != NULL) /* File name prefix. */ if (strlcpy(fname, *argv++, sizeof(fname)) >= sizeof(fname)) errx(EX_USAGE, "file name prefix is too long"); if (*argv != NULL) usage(); if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname)) errx(EX_USAGE, "suffix is too long"); if (pflag && (numlines != 0 || bytecnt != 0 || chunks != 0)) usage(); if (numlines == 0) numlines = DEFLINE; else if (bytecnt != 0 || chunks != 0) usage(); if (bytecnt && chunks) usage(); if (ifd == -1) /* Stdin by default. */ ifd = 0; if (bytecnt) { split1(); exit (0); } else if (chunks) { split3(); exit (0); } split2(); if (pflag) regfree(&rgx); exit(0); }
int main(int argc, char *argv[]) { FTS *fts; FTSENT *p; off_t savednumber, curblocks; off_t threshold, threshold_sign; int ftsoptions; int listall; int depth; int Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag; int hflag, lflag, ch, notused, rval; char **save; static char dot[] = "."; setlocale(LC_ALL, ""); Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag = lflag = Aflag = 0; save = argv; ftsoptions = 0; savednumber = 0; threshold = 0; threshold_sign = 1; cblocksize = DEV_BSIZE; blocksize = 0; depth = INT_MAX; SLIST_INIT(&ignores); while ((ch = getopt(argc, argv, "AB:HI:LPasd:chklmnrt:x")) != -1) switch (ch) { case 'A': Aflag = 1; break; case 'B': errno = 0; cblocksize = atoi(optarg); if (errno == ERANGE || cblocksize <= 0) { warnx("invalid argument to option B: %s", optarg); usage(); } break; case 'H': Hflag = 1; break; case 'I': ignoreadd(optarg); break; case 'L': if (Pflag) usage(); Lflag = 1; break; case 'P': if (Lflag) usage(); Pflag = 1; break; case 'a': aflag = 1; break; case 's': sflag = 1; break; case 'd': dflag = 1; errno = 0; depth = atoi(optarg); if (errno == ERANGE || depth < 0) { warnx("invalid argument to option d: %s", optarg); usage(); } break; case 'c': cflag = 1; break; case 'h': hflag = 1; break; case 'k': hflag = 0; blocksize = 1024; break; case 'l': lflag = 1; break; case 'm': hflag = 0; blocksize = 1048576; break; case 'n': nodumpflag = 1; break; case 'r': /* Compatibility. */ break; case 't' : if (expand_number(optarg, &threshold) != 0 || threshold == 0) { warnx("invalid threshold: %s", optarg); usage(); } else if (threshold < 0) threshold_sign = -1; break; case 'x': ftsoptions |= FTS_XDEV; break; case '?': default: usage(); /* NOTREACHED */ } argc -= optind; argv += optind; /* * XXX * Because of the way that fts(3) works, logical walks will not count * the blocks actually used by symbolic links. We rationalize this by * noting that users computing logical sizes are likely to do logical * copies, so not counting the links is correct. The real reason is * that we'd have to re-implement the kernel's symbolic link traversing * algorithm to get this right. If, for example, you have relative * symbolic links referencing other relative symbolic links, it gets * very nasty, very fast. The bottom line is that it's documented in * the man page, so it's a feature. */ if (Hflag + Lflag + Pflag > 1) usage(); if (Hflag + Lflag + Pflag == 0) Pflag = 1; /* -P (physical) is default */ if (Hflag) ftsoptions |= FTS_COMFOLLOW; if (Lflag) ftsoptions |= FTS_LOGICAL; if (Pflag) ftsoptions |= FTS_PHYSICAL; if (!Aflag && (cblocksize % DEV_BSIZE) != 0) cblocksize = howmany(cblocksize, DEV_BSIZE) * DEV_BSIZE; listall = 0; if (aflag) { if (sflag || dflag) usage(); listall = 1; } else if (sflag) { if (dflag) usage(); depth = 0; } if (!*argv) { argv = save; argv[0] = dot; argv[1] = NULL; } if (blocksize == 0) (void)getbsize(¬used, &blocksize); if (!Aflag) { cblocksize /= DEV_BSIZE; blocksize /= DEV_BSIZE; } if (threshold != 0) threshold = howmany(threshold / DEV_BSIZE * cblocksize, blocksize); rval = 0; (void)signal(SIGINFO, siginfo); if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL) err(1, "fts_open"); while ((p = fts_read(fts)) != NULL) { switch (p->fts_info) { case FTS_D: /* Ignore. */ if (ignorep(p)) fts_set(fts, p, FTS_SKIP); break; case FTS_DP: if (ignorep(p)) break; curblocks = Aflag ? howmany(p->fts_statp->st_size, cblocksize) : howmany(p->fts_statp->st_blocks, cblocksize); p->fts_parent->fts_bignum += p->fts_bignum += curblocks; if (p->fts_level <= depth && threshold <= threshold_sign * howmany(p->fts_bignum * cblocksize, blocksize)) { if (hflag) { prthumanval(p->fts_bignum); (void)printf("\t%s\n", p->fts_path); } else { (void)printf("%jd\t%s\n", (intmax_t)howmany(p->fts_bignum * cblocksize, blocksize), p->fts_path); } } if (info) { info = 0; (void)printf("\t%s\n", p->fts_path); } break; case FTS_DC: /* Ignore. */ break; case FTS_DNR: /* Warn, continue. */ case FTS_ERR: case FTS_NS: warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); rval = 1; break; default: if (ignorep(p)) break; if (lflag == 0 && p->fts_statp->st_nlink > 1 && linkchk(p)) break; curblocks = Aflag ? howmany(p->fts_statp->st_size, cblocksize) : howmany(p->fts_statp->st_blocks, cblocksize); if (listall || p->fts_level == 0) { if (hflag) { prthumanval(curblocks); (void)printf("\t%s\n", p->fts_path); } else { (void)printf("%jd\t%s\n", (intmax_t)howmany(curblocks * cblocksize, blocksize), p->fts_path); } } p->fts_parent->fts_bignum += curblocks; } savednumber = p->fts_parent->fts_bignum; } if (errno) err(1, "fts_read"); if (cflag) { if (hflag) { prthumanval(savednumber); (void)printf("\ttotal\n"); } else { (void)printf("%jd\ttotal\n", (intmax_t)howmany( savednumber * cblocksize, blocksize)); } } ignoreclean(); exit(rval); }
int main(int argc, char **argv) { struct stat sb; mode_t omode; off_t oflow, rsize, tsize; int64_t sz; int ch, error, fd, oflags; char *fname, *rname; fd = -1; rsize = tsize = sz = 0; error = 0; rname = NULL; while ((ch = getopt(argc, argv, "cr:s:")) != -1) switch (ch) { case 'c': no_create = 1; break; case 'r': do_refer = 1; rname = optarg; break; case 's': if (expand_number(optarg, &sz) == -1) errx(EXIT_FAILURE, "invalid size argument `%s'", optarg); if (*optarg == '+' || *optarg == '-') do_relative = 1; got_size = 1; break; default: usage(); /* NOTREACHED */ } argv += optind; argc -= optind; /* * Exactly one of do_refer or got_size must be specified. Since * do_relative implies got_size, do_relative and do_refer are * also mutually exclusive. See usage() for allowed invocations. */ if (do_refer + got_size != 1 || argc < 1) usage(); if (do_refer) { if (stat(rname, &sb) == -1) err(EXIT_FAILURE, "%s", rname); tsize = sb.st_size; } else if (do_relative) rsize = sz; else tsize = sz; if (no_create) oflags = O_WRONLY; else oflags = O_WRONLY | O_CREAT; omode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; while ((fname = *argv++) != NULL) { if (fd != -1) close(fd); if ((fd = open(fname, oflags, omode)) == -1) { if (errno != ENOENT) { warn("%s", fname); error++; } continue; } if (do_relative) { if (fstat(fd, &sb) == -1) { warn("%s", fname); error++; continue; } oflow = sb.st_size + rsize; if (oflow < (sb.st_size + rsize)) { errno = EFBIG; warn("%s", fname); error++; continue; } tsize = oflow; } if (tsize < 0) tsize = 0; if (ftruncate(fd, tsize) == -1) { warn("%s", fname); error++; continue; } } if (fd != -1) close(fd); return error ? EXIT_FAILURE : EXIT_SUCCESS; }
int main(int argc, char * const *argv) { int ch; int64_t size; int64_t recsize; int64_t reccnt; const char *s; recsize = 0; size = 0; reccnt = 0; while((ch = getopt(argc, argv, "l:r:s:")) != -1) { switch (ch) { case 'l': if (expand_number(optarg, &recsize)) err(1, "Couldn't parse -l argument"); break; case 'r': if (expand_number(optarg, &reccnt)) err(1, "Couldn't parse -r argument"); break; case 's': if (expand_number(optarg, &size)) err(1, "Couldn't parse -s argument"); break; default: usage(); } } argc -= optind; argv += optind; if (argc != 1) usage(); if (size != 0 && reccnt != 0 && recsize != 0) { /* N N N */ if (size != reccnt * recsize) errx(1, "Inconsistent -l, -r and -s values"); } else if (size != 0 && reccnt != 0 && recsize == 0) { /* N N Z */ if (size % reccnt) errx(1, "Inconsistent -r and -s values (gives remainder)"); recsize = size / reccnt; } else if (size != 0 && reccnt == 0 && recsize != 0) { /* N Z N */ if (size % recsize) errx(1, "-s arg not divisible by -l arg"); } else if (size != 0 && reccnt == 0 && recsize == 0) { /* N Z Z */ recsize = DEF_RECSIZE; if (size % recsize) errx(1, "-s arg not divisible by %jd", recsize); } else if (size == 0 && reccnt != 0 && recsize != 0) { /* Z N N */ size = reccnt * recsize; } else if (size == 0 && reccnt != 0 && recsize == 0) { /* Z N Z */ recsize = DEF_RECSIZE; size = reccnt * recsize; } else if (size == 0 && reccnt == 0 && recsize != 0) { /* Z Z N */ size = DEF_RECCNT * recsize; } else if (size == 0 && reccnt == 0 && recsize == 0) { /* Z Z Z */ recsize = DEF_RECSIZE; size = DEF_RECCNT * recsize; } s = fifolog_create(argv[0], size, recsize); if (s == NULL) return (0); err(1, "%s", s); }