Beispiel #1
0
int
main(int argc, char *argv[])
{
	struct winsize win;
	FILE *fp;
	int ch, tflag, xflag;
	char *p;
	const char *errstr;

	if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
		if ((p = getenv("COLUMNS")) && *p != '\0') {
			termwidth = strtonum(p, 1, INT_MAX, &errstr);
			if (errstr != NULL)
				errx(1, "%s: %s", errstr, p);
		}
	} else
		termwidth = win.ws_col;

	if (pledge("stdio rpath", NULL) == -1)
		err(1, "pledge");

	tflag = xflag = 0;
	while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
		switch(ch) {
		case 'c':
			termwidth = strtonum(optarg, 1, INT_MAX, &errstr);
			if (errstr != NULL)
				errx(1, "%s: %s", errstr, optarg);
			break;
		case 's':
			separator = optarg;
			break;
		case 't':
			tflag = 1;
			break;
		case 'x':
			xflag = 1;
			break;
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	if (!*argv) {
		input(stdin);
	} else {
		for (; *argv; ++argv) {
			if ((fp = fopen(*argv, "r"))) {
				input(fp);
				(void)fclose(fp);
			} else {
				warn("%s", *argv);
				eval = 1;
			}
		}
	}

	if (pledge("stdio", NULL) == -1)
		err(1, "pledge");

	if (!entries)
		exit(eval);

	if (tflag)
		maketbl();
	else if (maxlength >= termwidth)
		print();
	else if (xflag)
		c_columnate();
	else
		r_columnate();
	exit(eval);
}
Beispiel #2
0
int
main(int argc, char **argv)
{
    struct winsize win;
    FILE *fp;
    int ch, tflag, xflag;
    char *p;
    const char *src;
    wchar_t *newsep;
    size_t seplen;

    setlocale(LC_ALL, "");

    if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
        if ((p = getenv("COLUMNS")))
            termwidth = atoi(p);
    } else
        termwidth = win.ws_col;

    tflag = xflag = 0;
    while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
        switch(ch) {
        case 'c':
            termwidth = atoi(optarg);
            break;
        case 's':
            src = optarg;
            seplen = mbsrtowcs(NULL, &src, 0, NULL);
            if (seplen == (size_t)-1)
                err(1, "bad separator");
            newsep = malloc((seplen + 1) * sizeof(wchar_t));
            if (newsep == NULL)
                err(1, NULL);
            mbsrtowcs(newsep, &src, seplen + 1, NULL);
            separator = newsep;
            break;
        case 't':
            tflag = 1;
            break;
        case 'x':
            xflag = 1;
            break;
        case '?':
        default:
            usage();
        }
    argc -= optind;
    argv += optind;

    if (!*argv)
        input(stdin);
    else for (; *argv; ++argv)
            if ((fp = fopen(*argv, "r"))) {
                input(fp);
                (void)fclose(fp);
            } else {
                warn("%s", *argv);
                eval = 1;
            }

    if (!entries)
        exit(eval);

    maxlength = roundup(maxlength + 1, TAB);
    if (tflag)
        maketbl();
    else if (maxlength >= termwidth)
        print();
    else if (xflag)
        c_columnate();
    else
        r_columnate();
    exit(eval);
}