Ejemplo n.º 1
0
Archivo: du.c Proyecto: Zeke-OS/zeke
int
main(int argc, char *argv[])
{
    struct recursor r = { .fn = du, .hist = NULL, .depth = 0, .maxdepth = 0,
                          .follow = 'P', .flags = 0 };
    off_t n = 0;
    int kflag = 0, dflag = 0;
    char *bsize;

    ARGBEGIN {
    case 'a':
        aflag = 1;
        break;
    case 'd':
        dflag = 1;
        maxdepth = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
        break;
    case 'h':
        hflag = 1;
        break;
    case 'k':
        kflag = 1;
        break;
    case 's':
        sflag = 1;
        break;
    case 'x':
        r.flags |= SAMEDEV;
        break;
    case 'H':
    case 'L':
    case 'P':
        r.follow = ARGC();
        break;
    default:
        usage();
    } ARGEND;

    if ((aflag && sflag) || (dflag && sflag))
        usage();

    bsize = getenv("BLOCKSIZE");
    if (bsize)
        blksize = estrtonum(bsize, 1, MIN(LLONG_MAX, SIZE_MAX));
    if (kflag)
        blksize = 1024;

    if (!argc) {
        recurse(".", &n, &r);
        printpath(n, ".");
    } else {
        for (; *argv; argc--, argv++) {
            n = 0;
            recurse(*argv, &n, &r);
            printpath(n, *argv);
        }
    }

    return fshut(stdout, "<stdout>") || recurse_status;
}
Ejemplo n.º 2
0
int
main(int argc, char *argv[])
{
	FILE *fp[2] = { stdin, stdout };
	int ret = 0, i;
	char *fname[2] = { "<stdin>", "<stdout>" };

	ARGBEGIN {
	case 'c':
		countfmt = "%7ld ";
		break;
	case 'd':
		dflag = 1;
		break;
	case 'u':
		uflag = 1;
		break;
	case 'f':
		fskip = estrtonum(EARGF(usage()), 0, INT_MAX);
		break;
	case 's':
		sskip = estrtonum(EARGF(usage()), 0, INT_MAX);
		break;
	default:
		usage();
	} ARGEND;

	if (argc > 2)
		usage();

	for (i = 0; i < argc; i++) {
		if (strcmp(argv[i], "-")) {
			fname[i] = argv[i];
			if (!(fp[i] = fopen(argv[i], (i == 0) ? "r" : "w")))
				eprintf("fopen %s:", argv[i]);
		}
	}

	uniq(fp[0], fp[1]);
	uniqfinish(fp[1]);

	ret |= fshut(fp[0], fname[0]) | fshut(fp[1], fname[1]);

	return ret;
}
Ejemplo n.º 3
0
static void
line(char *arg, struct termios *m)
{
	const struct line *ln = lines;
	while (ln->str && strcmp(ln->str, arg))
		ln++;
	if (ln->str)
		m->c_line = ln->value;
	else
		m->c_line = estrtonum(arg, 0, 255);
}
Ejemplo n.º 4
0
int
main(int argc, char *argv[])
{
	FILE *fp;
	size_t len = 4;
	int ret = 0;
	char f;

	ARGBEGIN {
	case 'a':
		break;
	case 'n':
		len = estrtonum(EARGF(usage()), 1, LLONG_MAX);
		break;
	case 't':
		format = estrdup("%8l#: ");
		f = *EARGF(usage());
		if (f == 'd' || f == 'o' || f == 'x')
			format[3] = f;
		else
			usage();
		break;
	default:
		usage();
	} ARGEND;

	if (!argc) {
		strings(stdin, "<stdin>", len);
	} else {
		for (; *argv; argc--, argv++) {
			if (!strcmp(*argv, "-")) {
				*argv = "<stdin>";
				fp = stdin;
			} else if (!(fp = fopen(*argv, "r"))) {
				weprintf("fopen %s:", *argv);
				ret = 1;
				continue;
			}
			strings(fp, *argv, len);
			if (fp != stdin && fshut(fp, *argv))
				ret = 1;
		}
	}

	ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");

	return ret;
}
Ejemplo n.º 5
0
int
main(int argc, char *argv[])
{
	struct stat st;
	char *ref = NULL;
	clock_gettime(CLOCK_REALTIME, &times[0]);

	ARGBEGIN {
	case 'a':
		aflag = 1;
		break;
	case 'c':
		cflag = 1;
		break;
	case 'd':
	case 't':
		times[0].tv_sec = parsetime(EARGF(usage()), times[0].tv_sec);
		break;
	case 'm':
		mflag = 1;
		break;
	case 'r':
		ref = EARGF(usage());
		if (stat(ref, &st) < 0)
			eprintf("stat '%s':", ref);
		times[0] = st.st_atim;
		times[1] = st.st_mtim;
		break;
	case 'T':
		times[0].tv_sec = estrtonum(EARGF(usage()), 0, LLONG_MAX);
		break;
	default:
		usage();
	} ARGEND;

	if (!argc)
		usage();
	if (!aflag && !mflag)
		aflag = mflag = 1;
	if (!ref)
		times[1] = times[0];

	for (; *argv; argc--, argv++)
		touch(*argv);

	return 0;
}
Ejemplo n.º 6
0
static size_t
parselist(const char *s)
{
	size_t i;
	char  *p, *tmp;

	tmp = estrdup(s);
	for (i = 0; (p = strsep(&tmp, " ,")); i++) {
		if (*p == '\0')
			eprintf("empty field in tablist\n");
		tablist = ereallocarray(tablist, i + 1, sizeof(*tablist));
		tablist[i] = estrtonum(p, 1, MIN(LLONG_MAX, SIZE_MAX));
		if (i > 0 && tablist[i - 1] >= tablist[i])
			eprintf("tablist must be ascending\n");
	}
	tablist = ereallocarray(tablist, i + 1, sizeof(*tablist));

	return i;
}
Ejemplo n.º 7
0
static struct spec *
makespec(char *s)
{
	struct spec *sp;
	int fileno;
	size_t fldno;

	if (!strcmp(s, "0")) {   /* join field must be 0 and nothing else */
		fileno = 0;
		fldno = 0;
	} else if ((s[0] == '1' || s[0] == '2') && s[1] == '.') {
		fileno = s[0] - '0';
		fldno = estrtonum(&s[2], 1, MIN(LLONG_MAX, SIZE_MAX)) - 1;
	} else {
		eprintf("%s: invalid format\n", s);
	}

	sp = ereallocarray(NULL, INIT, sizeof(struct spec));
	sp->fileno = fileno;
	sp->fldno = fldno;
	return sp;
}
Ejemplo n.º 8
0
int
main(int argc, char *argv[])
{
	struct group *gr;
	struct passwd *pw;
	struct recursor r = { .fn = chownpwgr, .hist = NULL, .depth = 0, .maxdepth = 1,
	                      .follow = 'P', .flags = 0 };
	char *owner, *group;

	ARGBEGIN {
	case 'h':
		hflag = 1;
		break;
	case 'r':
	case 'R':
		r.maxdepth = 0;
		break;
	case 'H':
	case 'L':
	case 'P':
		r.follow = ARGC();
		break;
	default:
		usage();
	} ARGEND

	if (argc < 2)
		usage();

	owner = argv[0];
	if ((group = strchr(owner, ':')))
		*group++ = '\0';

	if (owner && *owner) {
		errno = 0;
		pw = getpwnam(owner);
		if (pw) {
			uid = pw->pw_uid;
		} else {
			if (errno)
				eprintf("getpwnam %s:", owner);
			uid = estrtonum(owner, 0, UINT_MAX);
		}
	}
	if (group && *group) {
		errno = 0;
		gr = getgrnam(group);
		if (gr) {
			gid = gr->gr_gid;
		} else {
			if (errno)
				eprintf("getgrnam %s:", group);
			gid = estrtonum(group, 0, UINT_MAX);
		}
	}
	if (uid == (uid_t)-1 && gid == (gid_t)-1)
		usage();

	for (argc--, argv++; *argv; argc--, argv++)
		recurse(*argv, NULL, &r);

	return ret || recurse_status;
}
Ejemplo n.º 9
0
Archivo: nl.c Proyecto: rovaughn/distro
int
main(int argc, char *argv[])
{
	FILE *fp = NULL;
	size_t l, s;
	int ret = 0;
	char *d, *formattype, *formatblit;

	ARGBEGIN {
	case 'd':
		d = EARGF(usage());
		l = utflen(d);

		switch (l) {
		case 0:
			break;
		case 1:
			s = strlen(d);
			delim = emalloc(s + 1 + 1);
			estrlcpy(delim, d, s + 1 + 1);
			estrlcat(delim, ":", s + 1 + 1);
			delimlen = s + 1;
			break;
		default:
			delim = d;
			delimlen = strlen(delim);
			break;
		}
		break;
	case 'f':
		type[0] = getlinetype(EARGF(usage()), preg);
		break;
	case 'b':
		type[1] = getlinetype(EARGF(usage()), preg + 1);
		break;
	case 'h':
		type[2] = getlinetype(EARGF(usage()), preg + 2);
		break;
	case 'i':
		incr = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
		break;
	case 'l':
		blines = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
		break;
	case 'n':
		formattype = EARGF(usage());
		estrlcpy(format, "%", sizeof(format));

		if (!strcmp(formattype, "ln")) {
			formatblit = "-";
		} else if (!strcmp(formattype, "rn")) {
			formatblit = "";
		} else if (!strcmp(formattype, "rz")) {
			formatblit = "0";
		} else {
			eprintf("%s: bad format\n", formattype);
		}

		estrlcat(format, formatblit, sizeof(format));
		estrlcat(format, "*ld%s", sizeof(format));
		break;
	case 'p':
		pflag = 1;
		break;
	case 's':
		sep = EARGF(usage());
		break;
	case 'v':
		startnum = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
		break;
	case 'w':
		width = estrtonum(EARGF(usage()), 1, INT_MAX);
		break;
	default:
		usage();
	} ARGEND;

	if (argc > 1)
		usage();

	if (!argc) {
		nl("<stdin>", stdin);
	} else {
		if (!strcmp(argv[0], "-")) {
			argv[0] = "<stdin>";
			fp = stdin;
		} else if (!(fp = fopen(argv[0], "r"))) {
			eprintf("fopen %s:", argv[0]);
		}
		nl(argv[0], fp);
	}

	ret |= fp && fp != stdin && fshut(fp, argv[0]);
	ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");

	return ret;
}
Ejemplo n.º 10
0
static void cols(char *arg, struct termios *m)   { setwinsize(-1, estrtonum(arg, 0, USHRT_MAX)); (void) m; }
Ejemplo n.º 11
0
static void stime(char *arg, struct termios *m)  { m->c_cc[VTIME] = estrtonum(arg, 0, CC_MAX); }
Ejemplo n.º 12
0
static void rows(char *arg, struct termios *m)   { setwinsize(estrtonum(arg, 0, USHRT_MAX), -1); (void) m; }
Ejemplo n.º 13
0
int
main(int argc, char *argv[])
{
	size_t jf[2] = { jfield, jfield, };
	FILE *fp[2];
	int ret = 0, n;
	char *fno;

	ARGBEGIN {
	case '1':
		jf[0] = estrtonum(EARGF(usage()), 1, MIN(LLONG_MAX, SIZE_MAX));
		break;
	case '2':
		jf[1] = estrtonum(EARGF(usage()), 1, MIN(LLONG_MAX, SIZE_MAX));
		break;
	case 'a':
		fno = EARGF(usage());
		if (strcmp(fno, "1") == 0)
			unpairsa = 1;
		else if (strcmp(fno, "2") == 0)
			unpairsb = 1;
		else
			usage();
		break;
	case 'e':
		replace = EARGF(usage());
		break;
	case 'o':
		oflag = 1;
		initolist(&output);
		makeolist(&output, EARGF(usage()));
		break;
	case 't':
		sep = EARGF(usage());
		break;
	case 'v':
		pairs = 0;
		fno = EARGF(usage());
		if (strcmp(fno, "1") == 0)
			unpairsa = 1;
		else if (strcmp(fno, "2") == 0)
			unpairsb = 1;
		else
			usage();
		break;
	default:
		usage();
	} ARGEND;

	if (sep)
		seplen = unescape(sep);

	if (argc != 2)
		usage();

	for (n = 0; n < 2; n++) {
		if (!strcmp(argv[n], "-")) {
			argv[n] = "<stdin>";
			fp[n] = stdin;
		} else if (!(fp[n] = fopen(argv[n], "r"))) {
			eprintf("fopen %s:", argv[n]);
		}
	}

	jf[0]--;
	jf[1]--;

	join(fp[0], fp[1], jf[0], jf[1]);

	if (oflag)
		freespecs(&output);

	if (fshut(fp[0], argv[0]) | (fp[0] != fp[1] && fshut(fp[1], argv[1])) |
	    fshut(stdout, "<stdout>"))
		ret = 2;

	return ret;
}
Ejemplo n.º 14
0
int
main(int argc, char *argv[])
{
	Rune *rarg;
	size_t i, j, argi, lastargi, formatlen, blen;
	long long num;
	double dou;
	int cooldown = 0, width, precision, ret = 0;
	char *format, *tmp, *arg, *fmt, flag;

	argv0 = argv[0];
	if (argc < 2)
		usage();

	format = argv[1];
	if ((tmp = strstr(format, "\\c"))) {
		*tmp = 0;
		cooldown = 1;
	}
	formatlen = unescape(format);
	if (formatlen == 0)
		return 0;
	lastargi = 0;
	for (i = 0, argi = 2; !cooldown || i < formatlen; i++, i = cooldown ? i : (i % formatlen)) {
		if (i == 0) {
			if (lastargi == argi)
				break;
			lastargi = argi;
		}
		if (format[i] != '%') {
			putchar(format[i]);
			continue;
		}

		/* flag */
		for (flag = '\0', i++; strchr("#-+ 0", format[i]); i++) {
			flag = format[i];
		}

		/* field width */
		width = -1;
		if (format[i] == '*') {
			if (argi < argc)
				width = estrtonum(argv[argi++], 0, INT_MAX);
			else
				cooldown = 1;
			i++;
		} else {
			j = i;
			for (; strchr("+-0123456789", format[i]); i++);
			if (j != i) {
				tmp = estrndup(format + j, i - j);
				width = estrtonum(tmp, 0, INT_MAX);
				free(tmp);
			} else {
				width = 0;
			}
		}

		/* field precision */
		precision = -1;
		if (format[i] == '.') {
			if (format[++i] == '*') {
				if (argi < argc)
					precision = estrtonum(argv[argi++], 0, INT_MAX);
				else
					cooldown = 1;
				i++;
			} else {
				j = i;
				for (; strchr("+-0123456789", format[i]); i++);
				if (j != i) {
					tmp = estrndup(format + j, i - j);
					precision = estrtonum(tmp, 0, INT_MAX);
					free(tmp);
				} else {
					precision = 0;
				}
			}
		}

		if (format[i] != '%') {
			if (argi < argc)
				arg = argv[argi++];
			else {
				arg = "";
				cooldown = 1;
			}
		} else {
			putchar('%');
			continue;
		}

		switch (format[i]) {
		case 'b':
			if ((tmp = strstr(arg, "\\c"))) {
				*tmp = 0;
				blen = unescape(arg);
				fwrite(arg, sizeof(*arg), blen, stdout);
				return 0;
			}
			blen = unescape(arg);
			fwrite(arg, sizeof(*arg), blen, stdout);
			break;
		case 'c':
			unescape(arg);
			rarg = ereallocarray(NULL, utflen(arg) + 1, sizeof(*rarg));
			utftorunestr(arg, rarg);
			efputrune(rarg, stdout, "<stdout>");
			free(rarg);
			break;
		case 's':
			printf("%*.*s", width, precision, arg);
			break;
		case 'd': case 'i': case 'o': case 'u': case 'x': case 'X':
			for (j = 0; isspace(arg[j]); j++);
			if (arg[j] == '\'' || arg[j] == '\"') {
				arg += j + 1;
				unescape(arg);
				rarg = ereallocarray(NULL, utflen(arg) + 1, sizeof(*rarg));
				utftorunestr(arg, rarg);
				num = rarg[0];
			} else if (arg[0]) {
				errno = 0;
				if (format[i] == 'd' || format[i] == 'i')
					num = strtol(arg, &tmp, 0);
				else
					num = strtoul(arg, &tmp, 0);

				if (tmp == arg || *tmp != '\0') {
					ret = 1;
					weprintf("%%%c %s: conversion error\n",
					    format[i], arg);
				}
				if (errno == ERANGE) {
					ret = 1;
					weprintf("%%%c %s: out of range\n",
					    format[i], arg);
				}
			} else {
					num = 0;
			}
			fmt = estrdup(flag ? "%#*.*ll#" : "%*.*ll#");
			if (flag)
				fmt[1] = flag;
			fmt[flag ? 7 : 6] = format[i];
			printf(fmt, width, precision, num);
			free(fmt);
			break;
		case 'a': case 'A': case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
			fmt = estrdup(flag ? "%#*.*#" : "%*.*#");
			if (flag)
				fmt[1] = flag;
			fmt[flag ? 5 : 4] = format[i];
			dou = (strlen(arg) > 0) ? estrtod(arg) : 0;
			printf(fmt, width, precision, dou);
			free(fmt);
			break;
		default:
			eprintf("Invalid format specifier '%c'.\n", format[i]);
		}
		if (argi >= argc)
			cooldown = 1;
	}

	return fshut(stdout, "<stdout>") | ret;
}
Ejemplo n.º 15
0
int
main(int argc, char *argv[])
{
	pid_t pid;
	size_t i;
	int ret = 0, sig = SIGTERM;

	argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;

	if (!argc)
		usage();

	if ((*argv)[0] == '-') {
		switch ((*argv)[1]) {
		case 'l':
			if ((*argv)[2])
				goto longopt;
			argc--, argv++;
			if (!argc) {
				for (i = 0; i < LEN(sigs); i++)
					puts(sigs[i].name);
			} else if (argc == 1) {
				sig = estrtonum(*argv, 0, INT_MAX);
				if (sig > 128)
					sig = WTERMSIG(sig);
				puts(sig2name(sig));
			} else {
				usage();
			}
			return fshut(stdout, "<stdout>");
		case 's':
			if ((*argv)[2])
				goto longopt;
			argc--, argv++;
			if (!argc)
				usage();
			sig = name2sig(*argv);
			argc--, argv++;
			break;
		case '-':
			if ((*argv)[2])
				goto longopt;
			argc--, argv++;
			break;
		default:
		longopt:
			/* XSI-extensions -argnum and -argname*/
			if (isdigit((*argv)[1])) {
				sig = estrtonum((*argv) + 1, 0, INT_MAX);
				sig2name(sig);
			} else {
				sig = name2sig((*argv) + 1);
			}
			argc--, argv++;
		}
	}

	if (argc && !strcmp(*argv, "--"))
		argc--, argv++;

	if (!argc)
		usage();

	for (; *argv; argc--, argv++) {
		pid = estrtonum(*argv, INT_MIN, INT_MAX);
		if (kill(pid, sig) < 0) {
			weprintf("kill %d:", pid);
			ret = 1;
		}
	}

	return ret;
}
Ejemplo n.º 16
0
int
main(int argc, char *argv[])
{
	int dflag = 0;
	char *gflag = 0;
	char *oflag = 0;
	char *mflag = 0;
	char *tflag = 0;
	struct group *gr;
	struct passwd *pw;
	struct stat st;
	char *p;

	ARGBEGIN {
	case 'd':
		dflag = 1;
		break;
	case 'D':
		Dflag = 1;
		break;
	case 's':
		sflag = 1;
		break;
	case 'g':
		gflag = EARGF(usage());
		break;
	case 'o':
		oflag = EARGF(usage());
		break;
	case 'm':
		mflag = EARGF(usage());
		break;
	case 't':
		tflag = EARGF(usage());
		break;
	default:
		usage();
	} ARGEND

	if (argc < 1 + (!tflag & !dflag) || dflag & (Dflag | sflag | !!tflag))
		usage();

	if (gflag) {
		errno = 0;
		gr = getgrnam(gflag);
		if (gr) {
			group = gr->gr_gid;
		} else {
			if (errno)
				eprintf("getgrnam %s:", gflag);
			group = estrtonum(gflag, 0, UINT_MAX);
		}
	} else {
		group = getgid();
	}

	if (oflag) {
		errno = 0;
		pw = getpwnam(oflag);
		if (pw) {
			owner = pw->pw_uid;
		} else {
			if (errno)
				eprintf("getpwnam %s:", oflag);
			owner = estrtonum(oflag, 0, UINT_MAX);
		}
	} else {
		owner = getuid();
	}

	if (mflag) {
		mode = parsemode(mflag, mode, 0);
		if (mode < 0)
			return 1;
	}

	if (tflag) {
		memmove(argv - 1, argv, argc);
		argv[argc++] = tflag;
	}
	if (tflag || argc > 2) {
		if (stat(argv[argc - 1], &st) < 0) {
			if ((errno == ENOENT) && Dflag) {
				make_dirs(argv[argc - 1], 1);
			} else {
				eprintf("stat %s:", argv[argc - 1]);
			}
		} else if (!S_ISDIR(st.st_mode)) {
			eprintf("%s: not a directory\n", argv[argc - 1]);
		}
	}

	if (dflag) {
		for (; *argv; argc--, argv++)
			make_dirs(*argv, 0);
	} else {
		if (stat(argv[argc - 1], &st) < 0) {
			if (errno != ENOENT)
				eprintf("stat %s:", argv[argc - 1]);
			if (tflag || Dflag || argc > 2) {
				if ((p = strrchr(argv[argc - 1], '/')) != NULL) {
					*p = '\0';
					make_dirs(argv[argc - 1], 1);
					*p = '/';
				}
			}
		}
		enmasse(argc, argv, install);
	}

	return 0;
}
Ejemplo n.º 17
0
static void min(char *arg, struct termios *m)    { m->c_cc[VMIN] = estrtonum(arg, 0, CC_MAX); }