Example #1
0
/* Re-read the proc list each time we traverse it, because
   sending signals may cause it to change. */
void slay_tree(int sig, pid_t pid, int include_children) {
	proc_t **procs;
	int i;

	procs = readproctab(PROC_FILLSTAT);
	slay_impl(procs, sig, pid, include_children);
	for (i = 0; procs[i]; i++) {
		free(procs[i]);
	}
	free(procs);
}
Example #2
0
/***** main */
int main(int argc, char **argv) {
    char *user = NULL;
    utmp_t *u;
    struct winsize win;
    int header=1, longform=1, from=1, args, maxcmd=80, ch;

#ifndef W_SHOWFROM
    from = 0;
#endif

    for (args=0; (ch = getopt(argc, argv, "hlusfV")) != EOF; args++)
	switch (ch) {
	  case 'h': header = 0;		break;
	  case 'l': longform = 1;	break;
	  case 's': longform = 0;	break;
	  case 'f': from = !from;	break;
	  case 'V': display_version();	exit(0);
	  case 'u': ignoreuser = 1;	break;
	  default:
	    printf("usage: w -hlsufV [user]\n"
		   "    -h    skip header\n"
		   "    -l    long listing (default)\n"
		   "    -s    short listing\n"
		   "    -u    ignore uid of processes\n"
		   "    -f    toggle FROM field (default %s)\n"
		   "    -V    display version\n", FROM_STRING);
	    exit(1);
	}

    if ((argv[optind]))
	user = (argv[optind]);

    if (ioctl(1, TIOCGWINSZ, &win) != -1 && win.ws_col > 0)
	maxcmd = win.ws_col;
    if (maxcmd < 71) {
	fprintf(stderr, "%d column window is too narrow\n", maxcmd);
	exit(1);
    }
    maxcmd -= 29 + (from ? 16 : 0) + (longform ? 20 : 0);
    if (maxcmd < 3)
	fprintf(stderr, "warning: screen width %d suboptimal.\n", win.ws_col);

    procs = readproctab(PROC_FILLCMD | PROC_FILLUSR);

    if (header) {				/* print uptime and headers */
	print_uptime();
	printf("USER     TTY      ");
	if (from)
	    printf("FROM            ");
	if (longform)
	    printf("  LOGIN@   IDLE   JCPU   PCPU  WHAT\n");
	else
	    printf("   IDLE  WHAT\n");
    }

    utmpname(UTMP_FILE);
    setutent();
    while ((u=getutent())) {
 	if (u->ut_type == USER_PROCESS &&
 	    (user ? !strncmp(u->ut_user, user, USERSZ) : *u->ut_user))
 	    showinfo(u, longform, maxcmd, from);
    }
    endutent();

    return 0;
}