118 *p_link; /* hash table chain pointer */
119 };
120
121 /*
122 * define hash table for struct uproc
123 * Hash function uses process id
124 * and the size of the hash table(HSIZE)
125 * to determine process index into the table.
126 */
127 static struct uproc pr_htbl[HSIZE];
128
129 static struct uproc *findhash(pid_t);
130 static time_t findidle(char *);
131 static void clnarglist(char *);
132 static void showproc(struct uproc *);
133 static void showtotals(struct uproc *);
134 static void calctotals(struct uproc *);
135 static char *getty(dev_t);
136 static void prttime(time_t, char *);
137 static void prtat(time_t *);
138 static void checkampm(char *);
139
140 static char *prog;
141 static int header = 1; /* true if -h flag: don't print heading */
142 static int lflag = 0; /* true if -l flag: w command format */
143 static char *sel_user; /* login of particular user selected */
144 static time_t now; /* current time of day */
145 static time_t uptime; /* time of last reboot & elapsed time since */
146 static int nusers; /* number of users logged in now */
147 static time_t idle; /* number of minutes user is idle */
148 static time_t jobtime; /* total cpu time visible */
149 static char doing[520]; /* process attached to terminal */
150 static time_t proctime; /* cpu time of process in doing */
151 static int empty;
152 static pid_t curpid;
153
154 #if SIGQUIT > SIGINT
155 #define ACTSIZE SIGQUIT
156 #else
157 #define ACTSIZE SIGINT
158 #endif
274
275 (void) printf(dcgettext(NULL,
276 " up %d day(s), %d hr(s), "
277 "%d min(s)", LC_TIME),
278 days, hrs, mins);
279 }
280 }
281
282 ut = utmpbegin; /* rewind utmp data */
283 (void) printf(dcgettext(NULL,
284 " %d user(s)\n", LC_TIME), nusers);
285 (void) printf(dcgettext(NULL, "User tty "
286 "login@ idle JCPU PCPU what\n", LC_TIME));
287 } else { /* standard whodo header */
288 char date_buf[100];
289
290 /*
291 * print current time and date
292 */
293 (void) strftime(date_buf, sizeof (date_buf),
294 dcgettext(NULL, "%C", LC_TIME), localtime(&now));
295 (void) printf("%s\n", date_buf);
296
297 /*
298 * print system name
299 */
300 (void) uname(&uts);
301 (void) printf("%s\n", uts.nodename);
302 }
303 }
304
305 /*
306 * loop through /proc, reading info about each process
307 * and build the parent/child tree
308 */
309 if (!(dirp = opendir(PROCDIR))) {
310 (void) fprintf(stderr, gettext("%s: could not open %s: %s\n"),
311 prog, PROCDIR, strerror(errno));
312 exit(1);
313 }
314
751 else if (tim > 0)
752 (void) printf(dcgettext(NULL, " %2d", LC_TIME), (int)tim);
753 else
754 (void) printf(" ");
755 (void) printf("%s", tail);
756 }
757
758
759 /*
760 * prints a 12 hour time given a pointer to a time of day
761 */
762 static void
763 prtat(time_t *time)
764 {
765 struct tm *p;
766
767 p = localtime(time);
768 if (now - *time <= 18 * HR) {
769 char timestr[50];
770 (void) strftime(timestr, sizeof (timestr),
771 dcgettext(NULL, " %l:%M""%p", LC_TIME), p);
772 checkampm(timestr);
773 (void) printf("%s", timestr);
774 } else if (now - *time <= 7 * DAY) {
775 char weekdaytime[20];
776
777 (void) strftime(weekdaytime, sizeof (weekdaytime),
778 dcgettext(NULL, "%a%l%p", LC_TIME), p);
779 checkampm(weekdaytime);
780 (void) printf(" %s", weekdaytime);
781 } else {
782 char monthtime[20];
783
784 (void) strftime(monthtime, sizeof (monthtime),
785 dcgettext(NULL, "%e%b%y", LC_TIME), p);
786 (void) printf(" %s", monthtime);
787 }
788 }
789
790 /*
791 * find & return number of minutes current tty has been idle
792 */
793 static time_t
794 findidle(char *devname)
795 {
796 struct stat stbuf;
797 time_t lastaction, diff;
798 char ttyname[64];
799
800 (void) strcpy(ttyname, "/dev/");
801 (void) strcat(ttyname, devname);
802 if (stat(ttyname, &stbuf) != -1) {
803 lastaction = stbuf.st_atime;
804 diff = now - lastaction;
805 diff = DIV60(diff);
813 /*
814 * given a pointer to the argument string clean out unsavory characters.
815 */
816 static void
817 clnarglist(char *arglist)
818 {
819 char *c;
820 int err = 0;
821
822 /* get rid of unsavory characters */
823 for (c = arglist; *c == NULL; c++) {
824 if ((*c < ' ') || (*c > 0176)) {
825 if (err++ > 5) {
826 *arglist = NULL;
827 break;
828 }
829 *c = '?';
830 }
831 }
832 }
833
834 /* replaces all occurences of AM/PM with am/pm */
835 static void
836 checkampm(char *str)
837 {
838 char *ampm;
839 while ((ampm = strstr(str, "AM")) != NULL ||
840 (ampm = strstr(str, "PM")) != NULL) {
841 *ampm = tolower(*ampm);
842 *(ampm+1) = tolower(*(ampm+1));
843 }
844 }
|
118 *p_link; /* hash table chain pointer */
119 };
120
121 /*
122 * define hash table for struct uproc
123 * Hash function uses process id
124 * and the size of the hash table(HSIZE)
125 * to determine process index into the table.
126 */
127 static struct uproc pr_htbl[HSIZE];
128
129 static struct uproc *findhash(pid_t);
130 static time_t findidle(char *);
131 static void clnarglist(char *);
132 static void showproc(struct uproc *);
133 static void showtotals(struct uproc *);
134 static void calctotals(struct uproc *);
135 static char *getty(dev_t);
136 static void prttime(time_t, char *);
137 static void prtat(time_t *);
138
139 static char *prog;
140 static int header = 1; /* true if -h flag: don't print heading */
141 static int lflag = 0; /* true if -l flag: w command format */
142 static char *sel_user; /* login of particular user selected */
143 static time_t now; /* current time of day */
144 static time_t uptime; /* time of last reboot & elapsed time since */
145 static int nusers; /* number of users logged in now */
146 static time_t idle; /* number of minutes user is idle */
147 static time_t jobtime; /* total cpu time visible */
148 static char doing[520]; /* process attached to terminal */
149 static time_t proctime; /* cpu time of process in doing */
150 static int empty;
151 static pid_t curpid;
152
153 #if SIGQUIT > SIGINT
154 #define ACTSIZE SIGQUIT
155 #else
156 #define ACTSIZE SIGINT
157 #endif
273
274 (void) printf(dcgettext(NULL,
275 " up %d day(s), %d hr(s), "
276 "%d min(s)", LC_TIME),
277 days, hrs, mins);
278 }
279 }
280
281 ut = utmpbegin; /* rewind utmp data */
282 (void) printf(dcgettext(NULL,
283 " %d user(s)\n", LC_TIME), nusers);
284 (void) printf(dcgettext(NULL, "User tty "
285 "login@ idle JCPU PCPU what\n", LC_TIME));
286 } else { /* standard whodo header */
287 char date_buf[100];
288
289 /*
290 * print current time and date
291 */
292 (void) strftime(date_buf, sizeof (date_buf),
293 "%+", localtime(&now));
294 (void) printf("%s\n", date_buf);
295
296 /*
297 * print system name
298 */
299 (void) uname(&uts);
300 (void) printf("%s\n", uts.nodename);
301 }
302 }
303
304 /*
305 * loop through /proc, reading info about each process
306 * and build the parent/child tree
307 */
308 if (!(dirp = opendir(PROCDIR))) {
309 (void) fprintf(stderr, gettext("%s: could not open %s: %s\n"),
310 prog, PROCDIR, strerror(errno));
311 exit(1);
312 }
313
750 else if (tim > 0)
751 (void) printf(dcgettext(NULL, " %2d", LC_TIME), (int)tim);
752 else
753 (void) printf(" ");
754 (void) printf("%s", tail);
755 }
756
757
758 /*
759 * prints a 12 hour time given a pointer to a time of day
760 */
761 static void
762 prtat(time_t *time)
763 {
764 struct tm *p;
765
766 p = localtime(time);
767 if (now - *time <= 18 * HR) {
768 char timestr[50];
769 (void) strftime(timestr, sizeof (timestr),
770 " %R ", p);
771 (void) printf("%s", timestr);
772 } else if (now - *time <= 7 * DAY) {
773 char weekdaytime[20];
774
775 (void) strftime(weekdaytime, sizeof (weekdaytime),
776 "%a%H ", p);
777 (void) printf(" %s", weekdaytime);
778 } else {
779 char monthtime[20];
780
781 (void) strftime(monthtime, sizeof (monthtime),
782 "%e%b%y", p);
783 (void) printf(" %s", monthtime);
784 }
785 }
786
787 /*
788 * find & return number of minutes current tty has been idle
789 */
790 static time_t
791 findidle(char *devname)
792 {
793 struct stat stbuf;
794 time_t lastaction, diff;
795 char ttyname[64];
796
797 (void) strcpy(ttyname, "/dev/");
798 (void) strcat(ttyname, devname);
799 if (stat(ttyname, &stbuf) != -1) {
800 lastaction = stbuf.st_atime;
801 diff = now - lastaction;
802 diff = DIV60(diff);
810 /*
811 * given a pointer to the argument string clean out unsavory characters.
812 */
813 static void
814 clnarglist(char *arglist)
815 {
816 char *c;
817 int err = 0;
818
819 /* get rid of unsavory characters */
820 for (c = arglist; *c == NULL; c++) {
821 if ((*c < ' ') || (*c > 0176)) {
822 if (err++ > 5) {
823 *arglist = NULL;
824 break;
825 }
826 *c = '?';
827 }
828 }
829 }
|