home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume5 / s5last < prev    next >
Encoding:
Text File  |  1989-02-03  |  11.9 KB  |  409 lines

  1. Path: xanth!nic.MR.NET!hal!ncoast!allbery
  2. From: moran@tron.UUCP
  3. Newsgroups: comp.sources.misc
  4. Subject: v05i049: "last" command for System V
  5. Message-ID: <8811111227.AA24477@tron.WEC.COM>
  6. Date: 15 Nov 88 00:27:31 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: moran@tron.UUCP
  9. Lines: 397
  10. Approved: allbery@ncoast.UUCP
  11.  
  12. Posting-number: Volume 5, Issue 49
  13. Submitted-by: "A. Nonymous" <moran@tron.UUCP>
  14. Archive-name: s5last
  15.  
  16. #    This is a shell archive.
  17. #    Remove everything above and including the cut line.
  18. #    Then run the rest of the file through sh.
  19. #----cut here-----cut here-----cut here-----cut here----#
  20. #!/bin/sh
  21. # shar:    Shell Archiver
  22. #    Run the following text with /bin/sh to create:
  23. #    last.1
  24. #    Makefile
  25. #    last.c
  26. # This archive created: Thu Nov 10 00:50:45 1988
  27. echo shar: extracting last.1
  28. sed 's/^X //' << \SHAR_EOF > last.1
  29. X .\"  Copyright (c) 1988  Harvey R. Moran Jr.
  30. X .\"                      1936 Altavue Rd.
  31. X .\"                      Catonsville, Md., 21228
  32. X .\"
  33. X .\"                      moran%tron.UUCP@umbc3.UMD.EDU   Internet
  34. X .\"                      {wb3ffv,netsys}!hrmhpc!harvey   UUCP
  35. X .\"
  36. X .\"  This software may be freely distributed provided that this
  37. X .\"  copyright notice is left intact and provided that:
  38. X .\"     a) The source code in machine readable format 
  39. X .\"        is included with any binary distribution.
  40. X .\"     b) If a binary version of this program or a derived work is sold,
  41. X .\"        the source code must be provided in machine readable
  42. X .\"        format for no additional charge.
  43. X .\"     c) No derived works may impose restrictions limiting free
  44. X .\"        distribution of the source code.
  45. X .\"  Source code in this context includes the C language source code
  46. X .\"  provided here as file "last.c" and the manual page provide here as
  47. X .\"  file "last.1".  It also includes any works derived from either of these.
  48. X .TH last 1L LOCAL
  49. X .SH NAME
  50. X last \- indicate last logins of users
  51. X .SH SYNTAX
  52. X .B last
  53. X [
  54. X .I -N
  55. X ] [
  56. X .I name
  57. X ]
  58. X .SH DESCRIPTION
  59. X The
  60. X .I last
  61. X command
  62. X looks back in the
  63. X .I wtmp
  64. X file which records all logins and logouts for information about
  65. X a user, or all users in reverse time order.
  66. X The optional argument
  67. X .I -N
  68. X specifies the maximum number of lines to print.
  69. X The optional
  70. X .I name
  71. X specifies the name of a user of interest.
  72. X If the session is still continuing \fBlast\fR
  73. X so indicates.
  74. X .PP
  75. X The
  76. X .I last
  77. X command
  78. X with no arguments prints a record of all logins and logouts.
  79. X .SH ORIGIN
  80. X This command is modeled on the
  81. X .I last
  82. X command which comes with the
  83. X Berkeley Software Distribution (BSD) of UNIX(tm).   The Berkeley version
  84. X also permits specifying a terminal name.  It also includes the
  85. X field specifying the remote machine in the case of a network login.
  86. X No such field is present in the System V.2 version of /etc/wtmp.
  87. X .SH FILES
  88. X /etc/wtmp        login data base
  89. X .SH SEE\ ALSO
  90. X utmp(4) login data base format description
  91. SHAR_EOF
  92. if test 2013 -ne "`wc -c last.1`"
  93. then
  94. echo shar: error transmitting last.1 '(should have been 2013 characters)'
  95. fi
  96. echo shar: extracting Makefile
  97. sed 's/^X //' << \SHAR_EOF > Makefile
  98. X DEFS = -DMAXENTRIES=1024    # maximum number of entries in /etc/wtmp handled
  99. X CFLAGS = -O -s $(DEFS)        # -O(ptomize) -s(trip symbols)
  100. X last:    last.c
  101. X     cc $(CFLAGS) -o last last.c
  102. SHAR_EOF
  103. if test 172 -ne "`wc -c Makefile`"
  104. then
  105. echo shar: error transmitting Makefile '(should have been 172 characters)'
  106. fi
  107. echo shar: extracting last.c
  108. sed 's/^X //' << \SHAR_EOF > last.c
  109. X /*
  110. X  * last.c -- examine the wtmp file for last login information
  111. X  *
  112. X  * =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  113. X  *  Copyright (c) 1988  Harvey R. Moran Jr.
  114. X  *                      1936 Altavue Rd.
  115. X  *                      Catonsville, Md., 21228
  116. X  *
  117. X  *                      moran%tron.UUCP@umbc3.UMD.EDU   Internet
  118. X  *                      {wb3ffv,netsys}!hrmhpc!harvey   UUCP
  119. X  *
  120. X  *  This software may be freely distributed provided that this
  121. X  *  copyright notice is left intact and provided that:
  122. X  *     a) The source code in machine readable format 
  123. X  *        is included with any binary distribution.
  124. X  *     b) If a binary version of this program or a derived work is sold,
  125. X  *        the source code must be provided in machine readable
  126. X  *        format for no additional charge.
  127. X  *     c) No derived works may impose restrictions limiting free
  128. X  *        distribution of the source code.
  129. X  *  Source code in this context includes the C language source code
  130. X  *  provided here as file "last.c" and the manual page provide here as
  131. X  *  file "last.1".  It also includes any works derived from either of these.
  132. X  *  
  133. X  * =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  134. X  *  Developed using Microport System V/AT 2.3U
  135. X  *
  136. X  *   usage:
  137. X  *        last [ -N ] [ username ]
  138. X  *
  139. X  *  This will print the usage of the system as
  140. X  * recorded in WTMP_FILE (defined in /usr/include/utmp.h) for
  141. X  * all users except the ones listed in exclude[] (below).
  142. X  * The option -N specifies that a maximum of N lines of
  143. X  * output will be generated.
  144. X  * The optional username form will restrict information printed
  145. X  * to be logins by the specified user.
  146. X  *
  147. X  * This command is modeled on the "last" command which is distributed
  148. X  * with the Berkeley Software Distribution (BSD) of UNIX(tm).
  149. X  * The print formats are slightly different. The field which
  150. X  * specifies a network login identification are not provided with this
  151. X  * version because the information is not available in the System V.2
  152. X  * UNIX(tm) WTMP_FILE file.
  153. X  *
  154. X  * It will also quit (with error message) if there are more than
  155. X  * MAXENTRIES entries in your WTMP_FILE (/etc/wtmp on my system).
  156. X  *
  157. X  *  Harvey Moran  11/10/88
  158. X  */
  159. X #include <stdio.h>
  160. X #include <fcntl.h>
  161. X #include <sys/types.h>
  162. X #include <utmp.h>
  163. X #include <time.h>
  164. X #ifndef MAXENTRIES
  165. X #    define MAXENTRIES 1024    /* maximum entries in WTMP_FILE handled */
  166. X #endif
  167. X #define STILL_IN -1L
  168. X #define AVOID    -1L
  169. X typedef int BOOL;
  170. X #define YES 1
  171. X #define NO  0
  172. X typedef struct utmp WTMP_t;
  173. X WTMP_t proc_tab;
  174. X typedef struct {
  175. X     char user_name[9];
  176. X     short pid;
  177. X     time_t x_time;
  178. X     time_t y_time;
  179. X     } ENTRY;
  180. X ENTRY entries[MAXENTRIES];
  181. X /*
  182. X  * user process user names to be excluded from display
  183. X  * change per local installation.  Perhaps these should be
  184. X  * in an environment variable instead, but there are already
  185. X  * so many of them.
  186. X  */
  187. X char *exclude[] = { "rc2", "crtsaver", "LOGIN", NULL };
  188. X char *keep_list = (char *) NULL;
  189. X /*
  190. X  * forward declared functions
  191. X  */
  192. X char *delta_time();
  193. X void qsort();
  194. X int cmp_ENTRY(), cmp_ENTRY_1();
  195. X main(ac, av)
  196. X int ac;
  197. X char *av[];
  198. X {
  199. X     int i, j, fd;
  200. X     unsigned  count = (unsigned) -1;    /* BIG unsigned int. */
  201. X     char intime[27], outtime[27], elapsed[27];
  202. X     long time_tmp;
  203. X     if ( ac > 1 && (i = atoi(av[1])) < 0 ) {
  204. X         count = (unsigned) -i;
  205. X         for ( i = 2; i < ac; ++i )
  206. X             av[i-1] = av[i];
  207. X         av[i] = NULL;
  208. X         --ac;
  209. X         }
  210. X     if ( (fd=open(WTMP_FILE, (O_RDONLY|O_NDELAY))) == -1 ) {
  211. X         fprintf(stderr, "%s:Can't open %s\n", av[0], WTMP_FILE);
  212. X         exit(1);
  213. X         }
  214. X     i = 0;
  215. X     while ( read(fd, &proc_tab, sizeof(WTMP_t)) == sizeof(WTMP_t) ) {
  216. X         switch ( proc_tab.ut_type ) {
  217. X             case EMPTY:            /* Empty slot in the WTMP_FILE */
  218. X             case INIT_PROCESS:    /* Process started by "init" */
  219. X             case LOGIN_PROCESS:    /* A "getty" process waiting for login */
  220. X             case ACCOUNTING:
  221. X                 break;            /* IGNORE all these forever */
  222. X             case RUN_LVL:        /* Change of Run Level */
  223. X             case BOOT_TIME:        /* Boot time */
  224. X             case OLD_TIME:        /* Old time of Day was */
  225. X             case NEW_TIME:        /* New Time of Day set */
  226. X                 break;            /* IGNORE all these for now */
  227. X             case USER_PROCESS:    /* A user process was started */
  228. X             case DEAD_PROCESS:    /* A user (I think) process died */
  229. X                 strncpy(entries[i].user_name, proc_tab.ut_user, sizeof(proc_tab.ut_user));
  230. X                 if ( omit(entries[i].user_name, ac, av) )
  231. X                     break;
  232. X                 if ( ac == 2 && strcmp(av[1], entries[i].user_name) )
  233. X                     break;    /* not the user of interest */
  234. X                 entries[i].pid = proc_tab.ut_pid;
  235. X                 entries[i].x_time = proc_tab.ut_time;
  236. X                 if ( ++i > (sizeof(entries)/sizeof(ENTRY)) ) {
  237. X                     fprintf(stderr, "Too many entries in %s\n", WTMP_FILE);
  238. X                     fprintf(stderr, "Fix program %s\n", av[0]);
  239. X                     exit(2);
  240. X                     }
  241. X                 break;
  242. X             default:
  243. X                 fprintf(stderr, "Ignoring GARBAGE in file %s. (ut_type = 0x%02x)\n", WTMP_FILE, proc_tab.ut_type);
  244. X                 break;
  245. X             }
  246. X         }
  247. X     /*
  248. X      * sort by pid, x_time
  249. X      */
  250. X     qsort((char *) entries, i, sizeof(ENTRY), cmp_ENTRY);
  251. X     j = 0;
  252. X #define E entries    /* minimize line wrapping of source code */
  253. X     do {
  254. X         if ( j == (i-1) ) {
  255. X             /*
  256. X              * final login has no corresponding logout
  257. X              * avoid referencing array element with is empty (or does not exist)
  258. X              */
  259. X             E[j].y_time = STILL_IN;
  260. X             break;
  261. X             }
  262. X         else if ( E[j].pid != E[j+1].pid ) { /* login has no corresponding logout */
  263. X             E[j].y_time = STILL_IN;
  264. X             ++j;
  265. X             }
  266. X         else { /* E[j].pid == E[j+1].pid , i.e. login/logout pair */
  267. X             E[j].y_time = E[j+1].x_time;
  268. X             E[j+1].x_time = AVOID;
  269. X             j += 2;
  270. X             }
  271. X         } while ( j < i );
  272. X     /*
  273. X      * sort by descending x_time
  274. X      */
  275. X     qsort((char *) entries, i, sizeof(ENTRY), cmp_ENTRY_1);
  276. X     for ( j = 0; j < i && count; ++j ) {
  277. X         if ( E[j].x_time == AVOID )
  278. X             continue;
  279. X         else if ( E[j].y_time == STILL_IN ) {
  280. X             time_tmp = (long) E[j].x_time;
  281. X             (void) strcpy(intime, asctime(localtime(&time_tmp)));
  282. X             intime[20] = '\0';    /* strip year */
  283. X             printf("%-8s %s %s still-logged-in\n", E[j].user_name, intime, " ");
  284. X             --count;
  285. X             }
  286. X         else {
  287. X             time_tmp = (long) E[j].x_time;
  288. X             (void) strcpy(intime, asctime(localtime(&time_tmp)));
  289. X             time_tmp = (long) E[j].y_time;
  290. X             (void) strcpy(outtime, asctime(localtime(&time_tmp)));
  291. X             intime[20] = outtime[20] = '\0';    /* strip year */
  292. X             time_tmp = (long) (E[j].y_time - E[j].x_time);
  293. X             (void) strcpy(elapsed, delta_time(time_tmp));
  294. X             printf("%-8s %s-%s %s\n", E[j].user_name, intime, outtime+10, elapsed);
  295. X             --count;
  296. X             }
  297. X         }
  298. X #undef E
  299. X }
  300. X /*
  301. X  * return comparison of 2 ENTRY type records
  302. X  * suitable for sorting by: pid, x_time
  303. X  */
  304. X cmp_ENTRY(a, b)
  305. X ENTRY *a, *b;
  306. X {
  307. X     if ( a->pid < b->pid )
  308. X         return (-1);
  309. X     if ( a->pid > b->pid )
  310. X         return (1);
  311. X     if ( a->x_time < b->x_time )
  312. X         return (-1);
  313. X     if ( a->x_time > b->x_time )
  314. X         return (1);
  315. X     return (0);
  316. X }
  317. X /*
  318. X  * return comparison of 2 ENTRY type records
  319. X  * suitable for a descending sort by: x_time
  320. X  */
  321. X cmp_ENTRY_1(a,b)
  322. X ENTRY *a, *b;
  323. X {
  324. X     if ( a->x_time < b->x_time )
  325. X         return (1);
  326. X     if ( a->x_time > b->x_time )
  327. X         return (-1);
  328. X     return (0);
  329. X }
  330. X /*
  331. X  * return whether to omit this name
  332. X  * It is omitted if:
  333. X  *       a) the name matches something in the exclude list
  334. X  *       b) the name does not match something in the invocation argument list
  335. X  *          and the argument list is not null
  336. X  */
  337. X BOOL
  338. X omit(name, ac, av)
  339. X char *name;
  340. X int ac;
  341. X char *av[];
  342. X {
  343. X     char **p;
  344. X     int i;
  345. X     for ( p = &exclude[0]; *p != (char *) NULL; ++p ) {
  346. X         if ( strcmp(*p, name) == 0 )
  347. X             return (YES);
  348. X         }
  349. X     if ( ac == 1 )    /* empty argument list, do not omit any */
  350. X         return (NO);
  351. X     for ( i = 1; i < ac; ++i ) {
  352. X         if ( strcmp(name, av[i]) == 0 )
  353. X             return (NO);
  354. X         }
  355. X     return (YES);
  356. X }
  357. X char *
  358. X delta_time(t)
  359. X long t;
  360. X {
  361. X     static char AscTime[11];
  362. X     int sec, min, hr, da;
  363. X     long x, y;
  364. X     sec = t % 60L;
  365. X     x = (t - (long) sec)/60L;
  366. X     min = x % 60L;
  367. X     x = (x - (long) min)/60L;
  368. X     hr  = x % 24;
  369. X     x   = (x - (long) hr)/24L;
  370. X     da  = x;
  371. X     if ( da )
  372. X         (void) sprintf(AscTime, "%3d+%02d:%02d:%02d", da, hr, min, sec);
  373. X     else
  374. X         (void) sprintf(AscTime, "    %02d:%02d:%02d", hr, min, sec);
  375. X     return (AscTime);
  376. X }
  377. SHAR_EOF
  378. if test 7908 -ne "`wc -c last.c`"
  379. then
  380. echo shar: error transmitting last.c '(should have been 7908 characters)'
  381. fi
  382. #    End of shell archive
  383. exit 0
  384.