Print this page
2849 uptime should use locale settings for current time
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/whodo/whodo.c
+++ new/usr/src/cmd/whodo/whodo.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2013 Gary Mills
23 23 *
24 24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25 25 * Use is subject to license terms.
26 26 */
27 27
28 28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 29 /* All Rights Reserved */
30 30
31 31 /*
32 32 * University Copyright- Copyright (c) 1982, 1986, 1988
33 33 * The Regents of the University of California
34 34 * All Rights Reserved
35 35 *
36 36 * University Acknowledgment- Portions of this document are derived from
37 37 * software developed by the University of California, Berkeley, and its
38 38 * contributors.
39 39 */
40 40
41 41 /*
42 42 * This is the new whodo command which takes advantage of
43 43 * the /proc interface to gain access to the information
44 44 * of all the processes currently on the system.
45 45 *
46 46 * Maintenance note:
47 47 *
48 48 * Much of this code is replicated in w.c. If you're
49 49 * fixing bugs here, then you should probably fix 'em there too.
50 50 */
51 51
52 52 #include <stdio.h>
53 53 #include <string.h>
54 54 #include <stdlib.h>
55 55 #include <ctype.h>
56 56 #include <fcntl.h>
57 57 #include <time.h>
58 58 #include <errno.h>
59 59 #include <sys/types.h>
60 60 #include <utmpx.h>
61 61 #include <sys/utsname.h>
62 62 #include <sys/stat.h>
63 63 #include <sys/mkdev.h>
64 64 #include <dirent.h>
65 65 #include <procfs.h> /* /proc header file */
66 66 #include <sys/wait.h>
67 67 #include <locale.h>
68 68 #include <unistd.h>
69 69 #include <limits.h>
70 70 #include <priv_utils.h>
71 71
72 72 /*
73 73 * Use the full lengths from utmpx for user and line.
74 74 */
75 75 #define NMAX (sizeof (((struct utmpx *)0)->ut_user))
76 76 #define LMAX (sizeof (((struct utmpx *)0)->ut_line))
77 77
78 78 /* Print minimum field widths. */
79 79 #define LOGIN_WIDTH 8
80 80 #define LINE_WIDTH 12
81 81
82 82 #define DIV60(t) ((t+30)/60) /* x/60 rounded */
83 83
84 84 #ifdef ERR
85 85 #undef ERR
86 86 #endif
87 87 #define ERR (-1)
88 88
89 89 #define DEVNAMELEN 14
90 90 #define HSIZE 256 /* size of process hash table */
91 91 #define PROCDIR "/proc"
92 92 #define INITPROCESS (pid_t)1 /* init process pid */
93 93 #define NONE 'n' /* no state */
94 94 #define RUNNING 'r' /* runnable process */
95 95 #define ZOMBIE 'z' /* zombie process */
96 96 #define VISITED 'v' /* marked node as visited */
97 97
98 98 static int ndevs; /* number of configured devices */
99 99 static int maxdev; /* slots for configured devices */
100 100 #define DNINCR 100
101 101 static struct devl { /* device list */
102 102 char dname[DEVNAMELEN]; /* device name */
103 103 dev_t ddev; /* device number */
104 104 } *devl;
105 105
106 106 struct uproc {
107 107 pid_t p_upid; /* user process id */
108 108 char p_state; /* numeric value of process state */
109 109 dev_t p_ttyd; /* controlling tty of process */
110 110 time_t p_time; /* ticks of user & system time */
111 111 time_t p_ctime; /* ticks of child user & system time */
112 112 int p_igintr; /* 1=ignores SIGQUIT and SIGINT */
113 113 char p_comm[PRARGSZ+1]; /* command */
114 114 char p_args[PRARGSZ+1]; /* command line arguments */
115 115 struct uproc *p_child, /* first child pointer */
116 116 *p_sibling, /* sibling pointer */
117 117 *p_pgrplink, /* pgrp link */
118 118 *p_link; /* hash table chain pointer */
119 119 };
120 120
121 121 /*
122 122 * define hash table for struct uproc
123 123 * Hash function uses process id
124 124 * and the size of the hash table(HSIZE)
125 125 * to determine process index into the table.
126 126 */
127 127 static struct uproc pr_htbl[HSIZE];
↓ open down ↓ |
127 lines elided |
↑ open up ↑ |
128 128
129 129 static struct uproc *findhash(pid_t);
130 130 static time_t findidle(char *);
131 131 static void clnarglist(char *);
132 132 static void showproc(struct uproc *);
133 133 static void showtotals(struct uproc *);
134 134 static void calctotals(struct uproc *);
135 135 static char *getty(dev_t);
136 136 static void prttime(time_t, char *);
137 137 static void prtat(time_t *);
138 -static void checkampm(char *);
139 138
140 139 static char *prog;
141 140 static int header = 1; /* true if -h flag: don't print heading */
142 141 static int lflag = 0; /* true if -l flag: w command format */
143 142 static char *sel_user; /* login of particular user selected */
144 143 static time_t now; /* current time of day */
145 144 static time_t uptime; /* time of last reboot & elapsed time since */
146 145 static int nusers; /* number of users logged in now */
147 146 static time_t idle; /* number of minutes user is idle */
148 147 static time_t jobtime; /* total cpu time visible */
149 148 static char doing[520]; /* process attached to terminal */
150 149 static time_t proctime; /* cpu time of process in doing */
151 150 static int empty;
152 151 static pid_t curpid;
153 152
154 153 #if SIGQUIT > SIGINT
155 154 #define ACTSIZE SIGQUIT
156 155 #else
157 156 #define ACTSIZE SIGINT
158 157 #endif
159 158
160 159 int
161 160 main(int argc, char *argv[])
162 161 {
163 162 struct utmpx *ut;
164 163 struct utmpx *utmpbegin;
165 164 struct utmpx *utmpend;
166 165 struct utmpx *utp;
167 166 struct tm *tm;
168 167 struct uproc *up, *parent, *pgrp;
169 168 struct psinfo info;
170 169 struct sigaction actinfo[ACTSIZE];
171 170 struct pstatus statinfo;
172 171 size_t size;
173 172 struct stat sbuf;
174 173 struct utsname uts;
175 174 DIR *dirp;
176 175 struct dirent *dp;
177 176 char pname[64];
178 177 char *fname;
179 178 int procfd;
180 179 int i;
181 180 int days, hrs, mins;
182 181 int entries;
183 182
184 183 /*
185 184 * This program needs the proc_owner privilege
186 185 */
187 186 (void) __init_suid_priv(PU_CLEARLIMITSET, PRIV_PROC_OWNER,
188 187 (char *)NULL);
189 188
190 189 (void) setlocale(LC_ALL, "");
191 190 #if !defined(TEXT_DOMAIN)
192 191 #define TEXT_DOMAIN "SYS_TEST"
193 192 #endif
194 193 (void) textdomain(TEXT_DOMAIN);
195 194
196 195 prog = argv[0];
197 196
198 197 while (argc > 1) {
199 198 if (argv[1][0] == '-') {
200 199 for (i = 1; argv[1][i]; i++) {
201 200 switch (argv[1][i]) {
202 201
203 202 case 'h':
204 203 header = 0;
205 204 break;
206 205
207 206 case 'l':
208 207 lflag++;
209 208 break;
210 209
211 210 default:
212 211 (void) printf(gettext(
213 212 "usage: %s [ -hl ] [ user ]\n"),
214 213 prog);
215 214 exit(1);
216 215 }
217 216 }
218 217 } else {
219 218 if (!isalnum(argv[1][0]) || argc > 2) {
220 219 (void) printf(gettext(
221 220 "usage: %s [ -hl ] [ user ]\n"), prog);
222 221 exit(1);
223 222 } else
224 223 sel_user = argv[1];
225 224 }
226 225 argc--; argv++;
227 226 }
228 227
229 228 /*
230 229 * read the UTMPX_FILE (contains information about
231 230 * each logged in user)
232 231 */
233 232 if (stat(UTMPX_FILE, &sbuf) == ERR) {
234 233 (void) fprintf(stderr, gettext("%s: stat error of %s: %s\n"),
235 234 prog, UTMPX_FILE, strerror(errno));
236 235 exit(1);
237 236 }
238 237 entries = sbuf.st_size / sizeof (struct futmpx);
239 238 size = sizeof (struct utmpx) * entries;
240 239
241 240 if ((ut = malloc(size)) == NULL) {
242 241 (void) fprintf(stderr, gettext("%s: malloc error of %s: %s\n"),
243 242 prog, UTMPX_FILE, strerror(errno));
244 243 exit(1);
245 244 }
246 245
247 246 (void) utmpxname(UTMPX_FILE);
248 247
249 248 utmpbegin = ut;
250 249 /* LINTED pointer cast may result in improper alignment */
251 250 utmpend = (struct utmpx *)((char *)utmpbegin + size);
252 251
253 252 setutxent();
254 253 while ((ut < utmpend) && ((utp = getutxent()) != NULL))
255 254 (void) memcpy(ut++, utp, sizeof (*ut));
256 255 endutxent();
257 256
258 257 (void) time(&now); /* get current time */
259 258
260 259 if (header) { /* print a header */
261 260 if (lflag) { /* w command format header */
262 261 prtat(&now);
263 262 for (ut = utmpbegin; ut < utmpend; ut++) {
264 263 if (ut->ut_type == USER_PROCESS) {
265 264 nusers++;
266 265 } else if (ut->ut_type == BOOT_TIME) {
267 266 uptime = now - ut->ut_xtime;
268 267 uptime += 30;
269 268 days = uptime / (60*60*24);
270 269 uptime %= (60*60*24);
271 270 hrs = uptime / (60*60);
272 271 uptime %= (60*60);
273 272 mins = uptime / 60;
274 273
275 274 (void) printf(dcgettext(NULL,
276 275 " up %d day(s), %d hr(s), "
277 276 "%d min(s)", LC_TIME),
278 277 days, hrs, mins);
279 278 }
280 279 }
281 280
282 281 ut = utmpbegin; /* rewind utmp data */
283 282 (void) printf(dcgettext(NULL,
↓ open down ↓ |
135 lines elided |
↑ open up ↑ |
284 283 " %d user(s)\n", LC_TIME), nusers);
285 284 (void) printf(dcgettext(NULL, "User tty "
286 285 "login@ idle JCPU PCPU what\n", LC_TIME));
287 286 } else { /* standard whodo header */
288 287 char date_buf[100];
289 288
290 289 /*
291 290 * print current time and date
292 291 */
293 292 (void) strftime(date_buf, sizeof (date_buf),
294 - dcgettext(NULL, "%C", LC_TIME), localtime(&now));
293 + "%+", localtime(&now));
295 294 (void) printf("%s\n", date_buf);
296 295
297 296 /*
298 297 * print system name
299 298 */
300 299 (void) uname(&uts);
301 300 (void) printf("%s\n", uts.nodename);
302 301 }
303 302 }
304 303
305 304 /*
306 305 * loop through /proc, reading info about each process
307 306 * and build the parent/child tree
308 307 */
309 308 if (!(dirp = opendir(PROCDIR))) {
310 309 (void) fprintf(stderr, gettext("%s: could not open %s: %s\n"),
311 310 prog, PROCDIR, strerror(errno));
312 311 exit(1);
313 312 }
314 313
315 314 while ((dp = readdir(dirp)) != NULL) {
316 315 if (dp->d_name[0] == '.')
317 316 continue;
318 317 retry:
319 318 (void) snprintf(pname, sizeof (pname),
320 319 "%s/%s/", PROCDIR, dp->d_name);
321 320 fname = pname + strlen(pname);
322 321 (void) strcpy(fname, "psinfo");
323 322 if ((procfd = open(pname, O_RDONLY)) < 0)
324 323 continue;
325 324 if (read(procfd, &info, sizeof (info)) != sizeof (info)) {
326 325 int err = errno;
327 326 (void) close(procfd);
328 327 if (err == EAGAIN)
329 328 goto retry;
330 329 if (err != ENOENT)
331 330 (void) fprintf(stderr, gettext(
332 331 "%s: read() failed on %s: %s\n"),
333 332 prog, pname, strerror(err));
334 333 continue;
335 334 }
336 335 (void) close(procfd);
337 336
338 337 up = findhash(info.pr_pid);
339 338 up->p_ttyd = info.pr_ttydev;
340 339 up->p_state = (info.pr_nlwp == 0? ZOMBIE : RUNNING);
341 340 up->p_time = 0;
342 341 up->p_ctime = 0;
343 342 up->p_igintr = 0;
344 343 (void) strncpy(up->p_comm, info.pr_fname,
345 344 sizeof (info.pr_fname));
346 345 up->p_args[0] = 0;
347 346
348 347 if (up->p_state != NONE && up->p_state != ZOMBIE) {
349 348 (void) strcpy(fname, "status");
350 349
351 350 /* now we need the proc_owner privilege */
352 351 (void) __priv_bracket(PRIV_ON);
353 352
354 353 procfd = open(pname, O_RDONLY);
355 354
356 355 /* drop proc_owner privilege after open */
357 356 (void) __priv_bracket(PRIV_OFF);
358 357
359 358 if (procfd < 0)
360 359 continue;
361 360
362 361 if (read(procfd, &statinfo, sizeof (statinfo))
363 362 != sizeof (statinfo)) {
364 363 int err = errno;
365 364 (void) close(procfd);
366 365 if (err == EAGAIN)
367 366 goto retry;
368 367 if (err != ENOENT)
369 368 (void) fprintf(stderr, gettext(
370 369 "%s: read() failed on %s: %s \n"),
371 370 prog, pname, strerror(err));
372 371 continue;
373 372 }
374 373 (void) close(procfd);
375 374
376 375 up->p_time = statinfo.pr_utime.tv_sec +
377 376 statinfo.pr_stime.tv_sec;
378 377 up->p_ctime = statinfo.pr_cutime.tv_sec +
379 378 statinfo.pr_cstime.tv_sec;
380 379
381 380 (void) strcpy(fname, "sigact");
382 381
383 382 /* now we need the proc_owner privilege */
384 383 (void) __priv_bracket(PRIV_ON);
385 384
386 385 procfd = open(pname, O_RDONLY);
387 386
388 387 /* drop proc_owner privilege after open */
389 388 (void) __priv_bracket(PRIV_OFF);
390 389
391 390 if (procfd < 0)
392 391 continue;
393 392 if (read(procfd, actinfo, sizeof (actinfo))
394 393 != sizeof (actinfo)) {
395 394 int err = errno;
396 395 (void) close(procfd);
397 396 if (err == EAGAIN)
398 397 goto retry;
399 398 if (err != ENOENT)
400 399 (void) fprintf(stderr, gettext(
401 400 "%s: read() failed on %s: %s \n"),
402 401 prog, pname, strerror(err));
403 402 continue;
404 403 }
405 404 (void) close(procfd);
406 405
407 406 up->p_igintr =
408 407 actinfo[SIGINT-1].sa_handler == SIG_IGN &&
409 408 actinfo[SIGQUIT-1].sa_handler == SIG_IGN;
410 409
411 410 up->p_args[0] = 0;
412 411
413 412 /*
414 413 * Process args if there's a chance we'll print it.
415 414 */
416 415 if (lflag) { /* w command needs args */
417 416 clnarglist(info.pr_psargs);
418 417 (void) strcpy(up->p_args, info.pr_psargs);
419 418 if (up->p_args[0] == 0 ||
420 419 up->p_args[0] == '-' &&
421 420 up->p_args[1] <= ' ' ||
422 421 up->p_args[0] == '?') {
423 422 (void) strcat(up->p_args, " (");
424 423 (void) strcat(up->p_args, up->p_comm);
425 424 (void) strcat(up->p_args, ")");
426 425 }
427 426 }
428 427
429 428 }
430 429
431 430 /*
432 431 * link pgrp together in case parents go away
433 432 * Pgrp chain is a single linked list originating
434 433 * from the pgrp leader to its group member.
435 434 */
436 435 if (info.pr_pgid != info.pr_pid) { /* not pgrp leader */
437 436 pgrp = findhash(info.pr_pgid);
438 437 up->p_pgrplink = pgrp->p_pgrplink;
439 438 pgrp->p_pgrplink = up;
440 439 }
441 440 parent = findhash(info.pr_ppid);
442 441
443 442 /* if this is the new member, link it in */
444 443 if (parent->p_upid != INITPROCESS) {
445 444 if (parent->p_child) {
446 445 up->p_sibling = parent->p_child;
447 446 up->p_child = 0;
448 447 }
449 448 parent->p_child = up;
450 449 }
451 450
452 451 }
453 452
454 453 /* revert to non-privileged user */
455 454 (void) __priv_relinquish();
456 455
457 456 (void) closedir(dirp);
458 457 (void) time(&now); /* get current time */
459 458
460 459 /*
461 460 * loop through utmpx file, printing process info
462 461 * about each logged in user
463 462 */
464 463 for (ut = utmpbegin; ut < utmpend; ut++) {
465 464 time_t tim;
466 465
467 466 if (ut->ut_type != USER_PROCESS)
468 467 continue;
469 468 if (sel_user && strncmp(ut->ut_name, sel_user, NMAX) != 0)
470 469 continue; /* we're looking for somebody else */
471 470 if (lflag) { /* -l flag format (w command) */
472 471 /* print login name of the user */
473 472 (void) printf("%-*.*s ", LOGIN_WIDTH, (int)NMAX,
474 473 ut->ut_name);
475 474
476 475 /* print tty user is on */
477 476 (void) printf("%-*.*s", LINE_WIDTH, (int)LMAX,
478 477 ut->ut_line);
479 478
480 479 /* print when the user logged in */
481 480 tim = ut->ut_xtime;
482 481 (void) prtat(&tim);
483 482
484 483 /* print idle time */
485 484 idle = findidle(ut->ut_line);
486 485 if (idle >= 36 * 60)
487 486 (void) printf(dcgettext(NULL, "%2ddays ",
488 487 LC_TIME), (idle + 12 * 60) / (24 * 60));
489 488 else
490 489 prttime(idle, " ");
491 490 showtotals(findhash((pid_t)ut->ut_pid));
492 491 } else { /* standard whodo format */
493 492 tim = ut->ut_xtime;
494 493 tm = localtime(&tim);
495 494 (void) printf("\n%-*.*s %-*.*s %2.1d:%2.2d\n",
496 495 LINE_WIDTH, (int)LMAX, ut->ut_line,
497 496 LOGIN_WIDTH, (int)NMAX, ut->ut_name, tm->tm_hour,
498 497 tm->tm_min);
499 498 showproc(findhash((pid_t)ut->ut_pid));
500 499 }
501 500 }
502 501
503 502 return (0);
504 503 }
505 504
506 505 /*
507 506 * Used for standard whodo format.
508 507 * This is the recursive routine descending the process
509 508 * tree starting from the given process pointer(up).
510 509 * It used depth-first search strategy and also marked
511 510 * each node as printed as it traversed down the tree.
512 511 */
513 512 static void
514 513 showproc(struct uproc *up)
515 514 {
516 515 struct uproc *zp;
517 516
518 517 if (up->p_state == VISITED) /* we already been here */
519 518 return;
520 519 /* print the data for this process */
521 520 if (up->p_state == ZOMBIE)
522 521 (void) printf(" %-*.*s %5d %4.1ld:%2.2ld %s\n",
523 522 LINE_WIDTH, (int)LMAX, " ?", (int)up->p_upid, 0L, 0L,
524 523 "<defunct>");
525 524 else if (up->p_state != NONE) {
526 525 (void) printf(" %-*.*s %5d %4.1ld:%2.2ld %s\n",
527 526 LINE_WIDTH, (int)LMAX, getty(up->p_ttyd), (int)up->p_upid,
528 527 up->p_time / 60L, up->p_time % 60L,
529 528 up->p_comm);
530 529 }
531 530 up->p_state = VISITED;
532 531
533 532 /* descend for its children */
534 533 if (up->p_child) {
535 534 showproc(up->p_child);
536 535 for (zp = up->p_child->p_sibling; zp; zp = zp->p_sibling) {
537 536 showproc(zp);
538 537 }
539 538 }
540 539
541 540 /* print the pgrp relation */
542 541 if (up->p_pgrplink)
543 542 showproc(up->p_pgrplink);
544 543 }
545 544
546 545
547 546 /*
548 547 * Used for -l flag (w command) format.
549 548 * Prints the CPU time for all processes & children,
550 549 * and the cpu time for interesting process,
551 550 * and what the user is doing.
552 551 */
553 552 static void
554 553 showtotals(struct uproc *up)
555 554 {
556 555 jobtime = 0;
557 556 proctime = 0;
558 557 empty = 1;
559 558 curpid = -1;
560 559 (void) strcpy(doing, "-"); /* default act: normally never prints */
561 560 calctotals(up);
562 561
563 562 /* print CPU time for all processes & children */
564 563 /* and need to convert clock ticks to seconds first */
565 564 prttime((time_t)jobtime, " ");
566 565
567 566 /* print cpu time for interesting process */
568 567 /* and need to convert clock ticks to seconds first */
569 568 prttime((time_t)proctime, " ");
570 569
571 570 /* what user is doing, current process */
572 571 (void) printf(" %-.32s\n", doing);
573 572 }
574 573
575 574 /*
576 575 * Used for -l flag (w command) format.
577 576 * This recursive routine descends the process
578 577 * tree starting from the given process pointer(up).
579 578 * It used depth-first search strategy and also marked
580 579 * each node as visited as it traversed down the tree.
581 580 * It calculates the process time for all processes &
582 581 * children. It also finds the "interesting" process
583 582 * and determines its cpu time and command.
584 583 */
585 584 static void
586 585 calctotals(struct uproc *up)
587 586 {
588 587 struct uproc *zp;
589 588
590 589 if (up->p_state == VISITED)
591 590 return;
592 591 up->p_state = VISITED;
593 592 if (up->p_state == NONE || up->p_state == ZOMBIE)
594 593 return;
595 594 jobtime += up->p_time + up->p_ctime;
596 595 proctime += up->p_time;
597 596
598 597 if (empty && !up->p_igintr) {
599 598 empty = 0;
600 599 curpid = -1;
601 600 }
602 601
603 602 if (up->p_upid > curpid && (!up->p_igintr || empty)) {
604 603 curpid = up->p_upid;
605 604 (void) strcpy(doing, up->p_args);
606 605 }
607 606
608 607 /* descend for its children */
609 608 if (up->p_child) {
610 609 calctotals(up->p_child);
611 610 for (zp = up->p_child->p_sibling; zp; zp = zp->p_sibling)
612 611 calctotals(zp);
613 612 }
614 613 }
615 614
616 615 static char *
617 616 devadd(char *name, dev_t ddev)
618 617 {
619 618 struct devl *dp;
620 619 int leng, start, i;
621 620
622 621 if (ndevs == maxdev) {
623 622 maxdev += DNINCR;
624 623 dp = realloc(devl, maxdev * sizeof (struct devl));
625 624 if (!dp) {
626 625 (void) fprintf(stderr,
627 626 gettext("%s: out of memory!: %s\n"),
628 627 prog, strerror(errno));
629 628 exit(1);
630 629 }
631 630 devl = dp;
632 631 }
633 632 dp = &devl[ndevs++];
634 633
635 634 dp->ddev = ddev;
636 635 if (name == NULL) {
637 636 (void) strcpy(dp->dname, " ? ");
638 637 return (dp->dname);
639 638 }
640 639
641 640 leng = strlen(name);
642 641 if (leng < DEVNAMELEN + 4) {
643 642 /* strip off "/dev/" */
644 643 (void) strcpy(dp->dname, &name[5]);
645 644 } else {
646 645 /* strip enough off the front to fit */
647 646 start = leng - DEVNAMELEN - 1;
648 647
649 648 for (i = start; i < leng && name[i] != '/'; i++)
650 649 ;
651 650 if (i == leng)
652 651 (void) strncpy(dp->dname, &name[start], DEVNAMELEN);
653 652 else
654 653 (void) strncpy(dp->dname, &name[i+1], DEVNAMELEN);
655 654 }
656 655 return (dp->dname);
657 656 }
658 657
659 658 static char *
660 659 devlookup(dev_t ddev)
661 660 {
662 661 struct devl *dp;
663 662 int i;
664 663
665 664 for (dp = devl, i = 0; i < ndevs; dp++, i++) {
666 665 if (dp->ddev == ddev)
667 666 return (dp->dname);
668 667 }
669 668 return (NULL);
670 669 }
671 670
672 671 /*
673 672 * This routine gives back a corresponding device name
674 673 * from the device number given.
675 674 */
676 675 static char *
677 676 getty(dev_t dev)
678 677 {
679 678 extern char *_ttyname_dev(dev_t, char *, size_t);
680 679 char devname[TTYNAME_MAX];
681 680 char *retval;
682 681
683 682 if (dev == PRNODEV)
684 683 return (" ? ");
685 684
686 685 if ((retval = devlookup(dev)) != NULL)
687 686 return (retval);
688 687
689 688 retval = _ttyname_dev(dev, devname, sizeof (devname));
690 689 return (devadd(retval, dev));
691 690 }
692 691
693 692 /*
694 693 * Findhash finds the appropriate entry in the process
695 694 * hash table (pr_htbl) for the given pid in case that
696 695 * pid exists on the hash chain. It returns back a pointer
697 696 * to that uproc structure. If this is a new pid, it allocates
698 697 * a new node, initializes it, links it into the chain (after
699 698 * head) and returns a structure pointer.
700 699 */
701 700 static struct uproc *
702 701 findhash(pid_t pid)
703 702 {
704 703 struct uproc *up, *tp;
705 704
706 705 tp = up = &pr_htbl[(int)pid % HSIZE];
707 706 if (up->p_upid == 0) { /* empty slot */
708 707 up->p_upid = pid;
709 708 up->p_state = NONE;
710 709 up->p_child = up->p_sibling = up->p_pgrplink = up->p_link = 0;
711 710 return (up);
712 711 }
713 712 if (up->p_upid == pid) { /* found in hash table */
714 713 return (up);
715 714 }
716 715 for (tp = up->p_link; tp; tp = tp->p_link) { /* follow chain */
717 716 if (tp->p_upid == pid) {
718 717 return (tp);
719 718 }
720 719 }
721 720 tp = malloc(sizeof (*tp)); /* add new node */
722 721 if (!tp) {
723 722 (void) fprintf(stderr, gettext("%s: out of memory!: %s\n"),
724 723 prog, strerror(errno));
725 724 exit(1);
726 725 }
727 726 (void) memset((char *)tp, 0, sizeof (*tp));
728 727 tp->p_upid = pid;
729 728 tp->p_state = NONE;
730 729 tp->p_child = tp->p_sibling = tp->p_pgrplink = (pid_t)0;
731 730 tp->p_link = up->p_link; /* insert after head */
732 731 up->p_link = tp;
733 732 return (tp);
734 733 }
735 734
736 735 #define HR (60 * 60)
737 736 #define DAY (24 * HR)
738 737 #define MON (30 * DAY)
739 738
740 739 /*
741 740 * prints a time in hours and minutes or minutes and seconds.
742 741 * The character string 'tail' is printed at the end, obvious
743 742 * strings to pass are "", " ", or "am".
744 743 */
745 744 static void
746 745 prttime(time_t tim, char *tail)
747 746 {
748 747 if (tim >= 60)
749 748 (void) printf(dcgettext(NULL, "%3d:%02d", LC_TIME),
750 749 (int)tim/60, (int)tim%60);
751 750 else if (tim > 0)
752 751 (void) printf(dcgettext(NULL, " %2d", LC_TIME), (int)tim);
753 752 else
754 753 (void) printf(" ");
755 754 (void) printf("%s", tail);
756 755 }
757 756
758 757
759 758 /*
760 759 * prints a 12 hour time given a pointer to a time of day
↓ open down ↓ |
456 lines elided |
↑ open up ↑ |
761 760 */
762 761 static void
763 762 prtat(time_t *time)
764 763 {
765 764 struct tm *p;
766 765
767 766 p = localtime(time);
768 767 if (now - *time <= 18 * HR) {
769 768 char timestr[50];
770 769 (void) strftime(timestr, sizeof (timestr),
771 - dcgettext(NULL, " %l:%M""%p", LC_TIME), p);
772 - checkampm(timestr);
770 + " %R ", p);
773 771 (void) printf("%s", timestr);
774 772 } else if (now - *time <= 7 * DAY) {
775 773 char weekdaytime[20];
776 774
777 775 (void) strftime(weekdaytime, sizeof (weekdaytime),
778 - dcgettext(NULL, "%a%l%p", LC_TIME), p);
779 - checkampm(weekdaytime);
776 + "%a%H ", p);
780 777 (void) printf(" %s", weekdaytime);
781 778 } else {
782 779 char monthtime[20];
783 780
784 781 (void) strftime(monthtime, sizeof (monthtime),
785 - dcgettext(NULL, "%e%b%y", LC_TIME), p);
782 + "%e%b%y", p);
786 783 (void) printf(" %s", monthtime);
787 784 }
788 785 }
789 786
790 787 /*
791 788 * find & return number of minutes current tty has been idle
792 789 */
793 790 static time_t
794 791 findidle(char *devname)
795 792 {
796 793 struct stat stbuf;
797 794 time_t lastaction, diff;
798 795 char ttyname[64];
799 796
800 797 (void) strcpy(ttyname, "/dev/");
801 798 (void) strcat(ttyname, devname);
802 799 if (stat(ttyname, &stbuf) != -1) {
803 800 lastaction = stbuf.st_atime;
804 801 diff = now - lastaction;
805 802 diff = DIV60(diff);
806 803 if (diff < 0)
807 804 diff = 0;
808 805 } else
809 806 diff = 0;
810 807 return (diff);
811 808 }
812 809
813 810 /*
814 811 * given a pointer to the argument string clean out unsavory characters.
815 812 */
816 813 static void
817 814 clnarglist(char *arglist)
818 815 {
819 816 char *c;
820 817 int err = 0;
821 818
822 819 /* get rid of unsavory characters */
↓ open down ↓ |
27 lines elided |
↑ open up ↑ |
823 820 for (c = arglist; *c == NULL; c++) {
824 821 if ((*c < ' ') || (*c > 0176)) {
825 822 if (err++ > 5) {
826 823 *arglist = NULL;
827 824 break;
828 825 }
829 826 *c = '?';
830 827 }
831 828 }
832 829 }
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 -}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX