示例#1
0
static void
quick(void)
{
	struct utmpx *utx;
	int col, ncols, num;

	ncols = ttywidth();
	col = num = 0;
	while ((utx = getutxent()) != NULL) {
		if (utx->ut_type != USER_PROCESS)
			continue;
		printf("%-16s", utx->ut_user);
		if (++col < ncols / (16 + 1))
			putchar(' ');
		else {
			col = 0;
			putchar('\n');
		}
		num++;
	}
	if (col != 0)
		putchar('\n');

	printf("# users = %d\n", num);
}
示例#2
0
文件: tabs.c 项目: 2asoft/freebsd
int
main(int argc __unused, char *argv[])
{
	long cols, i, inc, j, margin, nstops, stops[NSTOPS];
	const char *cr, *ct, *st, *ML;
	char area[1024], *ap, *arg, *end;

	setlocale(LC_ALL, "");

	inc = 8;
	margin = 0;
	nstops = -1;
	while ((arg = *++argv) != NULL && (*arg == '-' || *arg == '+')) {
		if (*arg == '+') {
			/* +m[n] or +[n] */
			if (*++arg == 'm')
				arg++;
			if (*arg != '\0') {
				errno = 0;
				margin = strtol(arg, &end, 10);
				if (errno != 0 || *end != '\0' || margin < 0)
					errx(1, "%s: invalid margin width",
					    arg);
			} else
				margin = 10;
		} else if (isdigit(arg[1])) {
			/* -n */
			errno = 0;
			inc = strtol(arg + 1, &end, 10);
			if (errno != 0 || *end != '\0' || inc < 0)
				errx(1, "%s: invalid increment", arg + 1);
		} else if (arg[1] == 'T') {
			/* -Ttype or -T type */
			if (arg[2] != '\0')
				setenv("TERM", arg + 2, 1);
			else {
				if ((arg = *++argv) == NULL)
					usage();
				setenv("TERM", arg, 1);
			}
		} else if (arg[1] == '-') {
			arg = *++argv;
			break;
		} else {
			/* Predefined format */
			for (i = 0; i < (int)NELEMS(formats); i++)
				if (strcmp(formats[i].name, arg + 1) == 0)
					break;
			if (i == NELEMS(formats))
				usage();
			for (j = nstops = 0; j < NSTOPS &&
			    formats[i].stops[j] != 0; j++)
				stops[nstops++] = formats[i].stops[j];
		}
	}	
	
	if (arg != NULL) {
		if (nstops != -1)
			usage();
		gettabs(arg, stops, &nstops);
	}

	/* Initialise terminal, get the strings we need */
	setupterm(NULL, 1, NULL);
	ap = area;
	if ((ct = tgetstr("ct", &ap)) == NULL)
		errx(1, "terminal cannot clear tabs");
	if ((st = tgetstr("st", &ap)) == NULL)
		errx(1, "terminal cannot set tabs");
	if ((cr = tgetstr("cr", &ap)) == NULL)
		cr = "\r";
	ML = tgetstr("ML", &ap);
	cols = ttywidth();

	/* Clear all tabs. */
	putp(cr);
	putp(ct);

	/*
	 * Set soft margin.
	 * XXX Does this actually work?
	 */
	if (ML != NULL) {
		printf("%*s", (int)margin, "");
		putp(ML);
	} else if (margin != 0)
		warnx("terminal cannot set left margin");

	/* Optionally output new tab stops. */
	if (nstops >= 0) {
		printf("%*s", (int)stops[0] - 1, "");
		putp(st);
		for (i = 1; i < nstops; i++) {
			printf("%*s", (int)(stops[i] - stops[i - 1]), "");
			putp(st);
		}
	} else if (inc > 0) {
		for (i = 0; i < cols / inc; i++) {
			putp(st);
			printf("%*s", (int)inc, "");
		}
		putp(st);
	}
	putp(cr);

	exit(0);
}