home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3404 < prev    next >
Internet Message Format  |  1991-05-22  |  6KB

  1. From: tin@smsc.sony.com (Tin Le)
  2. Newsgroups: alt.sources
  3. Subject: Re: lastlog for SVR4
  4. Message-ID: <1991May22.210955.5199@smsc.sony.com>
  5. Date: 22 May 91 21:09:55 GMT
  6.  
  7. I ported this from Tom C.'s posting in just a few minutes.  There is also
  8. a lastlogin for System V (I have it running on ISC v2.0.2).  With this,
  9. you could have the same functionalities as with BSD systems.
  10.  
  11. Submitted-by: tin@rn31
  12. Archive-name: lastlog/part01
  13.  
  14. ---- Cut Here and feed the following to sh ----
  15. #!/bin/sh
  16. # This is lastlog, a shell archive (produced by shar 3.49)
  17. # To extract the files from this archive, save it to a file, remove
  18. # everything above the "!/bin/sh" line above, and type "sh file_name".
  19. #
  20. # made 05/22/1991 21:05 UTC by tin@rn31
  21. # Source directory /usr1/tin/News/last
  22. #
  23. # existing files will NOT be overwritten unless -c is specified
  24. # This format requires very little intelligence at unshar time.
  25. # "if test", "echo", "true", and "sed" may be needed.
  26. #
  27. # This shar contains:
  28. # length  mode       name
  29. # ------ ---------- ------------------------------------------
  30. #    282 -rw-r--r-- Makefile
  31. #   3458 -rw-r--r-- lastlog.c
  32. #
  33. # ============= Makefile ==============
  34. if test -f 'Makefile' -a X"$1" != X"-c"; then
  35.     echo 'x - skipping Makefile (File already exists)'
  36. else
  37. echo 'x - extracting Makefile (Text)'
  38. sed 's/^X//' << 'SHAR_EOF' > 'Makefile' &&
  39. X#
  40. X#  Makefile for last
  41. X#
  42. X# 5/22/91 Tin
  43. X#
  44. XTARGET = last
  45. XOBJECTS = lastlog.o 
  46. XCFLAGS = -O -DSVR4
  47. XLDFLAGS = -s
  48. XLIBS = 
  49. XINCLDIR = /usr/include
  50. X
  51. X$(TARGET): $(OBJECTS)
  52. X    $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(OBJECTS) $(LIBS)
  53. X
  54. Xclean:
  55. X    -rm -f $(OBJECTS) core
  56. X
  57. Xclobber: clean
  58. X    -rm -f $(TARGET)
  59. X
  60. SHAR_EOF
  61. true || echo 'restore of Makefile failed'
  62. fi
  63. # ============= lastlog.c ==============
  64. if test -f 'lastlog.c' -a X"$1" != X"-c"; then
  65.     echo 'x - skipping lastlog.c (File already exists)'
  66. else
  67. echo 'x - extracting lastlog.c (Text)'
  68. sed 's/^X//' << 'SHAR_EOF' > 'lastlog.c' &&
  69. X/*
  70. X * lastlog - print last login time for all users, based on times
  71. X *            stored in /usr/adm/lastlog.
  72. X *
  73. X * Lines are printed oldest first, with name, date/time, and gecos
  74. X * field on each line.
  75. X *
  76. X * No command line options. Runs on VAX/4.2 BSD Unix, Sony RISC SVR4.
  77. X *
  78. X * compile with:  cc -O -o lastlog lastlog.c
  79. X *
  80. X * Tin Le, Sony Microsystems, San Jose 5/22/91
  81. X * - Adapted for SVR4.
  82. X *
  83. X * Rex Sanders, US Geological Survey, Pacific Marine Geology, 12/19/85
  84. X */
  85. X
  86. X#include <stdio.h>
  87. X#include <sys/types.h>
  88. X#ifdef SVR4
  89. X#include <fcntl.h>
  90. X#include <string.h>
  91. X#else
  92. X#include <sys/file.h>
  93. X#include <strings.h>
  94. X#endif
  95. X#include <lastlog.h>
  96. X#include <pwd.h>
  97. X#ifndef SVR4
  98. X#include <sysexits.h>
  99. X#else
  100. X#define EX_OSFILE    1
  101. X#define EX_SOFTWARE    2
  102. X#endif
  103. X
  104. X/* In case your UNIX put lastlog somewhere other than */adm/lastlog */
  105. X#define LASTLOG        "/usr/adm/lastlog"
  106. X
  107. X/* maximum number of users/entries in /etc/passwd */
  108. X#define MAXU       5000
  109. X/* maximum length of the gecos field in /etc/passwd */
  110. X#define MAXG        100
  111. X
  112. Xchar   *ctime ();
  113. Xlong    lseek ();
  114. X
  115. Xstruct info_s {
  116. X    int     time;
  117. X#ifdef SVR4    /* Actually there is no hard limit in SVR4 struct passwd
  118. X        ** Change this to char *name, and malloc your string if you
  119. X        ** really want to get complicated.
  120. X        ** 5/22/91 Tin
  121. X        */
  122. X    char    name[15];
  123. X#else
  124. X    char    name[9];
  125. X#endif
  126. X    char    gecos[MAXG];
  127. X};
  128. Xstruct info_s   info[MAXU];
  129. X
  130. Xmain (argc, argv) char **argv; {
  131. X    int     infocmp ();
  132. X    struct lastlog  ll;
  133. X    struct passwd  *pw;
  134. X    char    lastdate[25];
  135. X    int     llfd;
  136. X    register int    nusers = 0;
  137. X    register int    i;
  138. X
  139. X    if ((llfd = open (LASTLOG, O_RDONLY)) < 0) {
  140. X    perror("open()");
  141. X    fprintf(stderr, "lastlog: %s\n", LASTLOG);
  142. X    exit (EX_OSFILE);
  143. X    }
  144. X
  145. X/*
  146. X * For each user in password file, grab password info
  147. X */
  148. X    while (pw = getpwent ()) {
  149. X    /* 
  150. X     * Grab info from lastlog file
  151. X     */
  152. X     if (!strcmp(pw->pw_passwd,"*"))
  153. X        continue;
  154. X         if (lseek (llfd, (long) pw -> pw_uid * sizeof ll, 0) == -1) {
  155. X        fprintf(stderr,"%s: lseek for uid %d failed\n", *argv, pw->pw_uid);
  156. X        continue;
  157. X     } 
  158. X         if (read (llfd, (char *) & ll, sizeof ll) != sizeof ll) {
  159. X        fprintf(stderr, "%s: read for uid %d (%s) failed\n",
  160. X        *argv, pw->pw_uid, pw->pw_name);
  161. X        continue;
  162. X     } 
  163. X
  164. X
  165. X         info[nusers].time = ll.ll_time;
  166. X#ifdef SVR4
  167. X         strcpy (info[nusers].name, pw -> pw_name);
  168. X#else
  169. X         strncpy (info[nusers].name, pw -> pw_name, 9);
  170. X#endif
  171. X         strncpy (info[nusers].gecos, pw -> pw_gecos, MAXG);
  172. X         if (nusers++ == MAXU) {
  173. X        fprintf(stderr, "%s: recompile with MAXU > %d\n",
  174. X        *argv, MAXU);
  175. X        exit(EX_SOFTWARE);
  176. X     } 
  177. X    }
  178. X
  179. X/*
  180. X * Sort users by last login time
  181. X */
  182. X    qsort ((char *) info, nusers, sizeof (struct info_s), infocmp);
  183. X
  184. X/*
  185. X * Print info for each user
  186. X */
  187. X    for (i = 0; i < nusers; i++) {
  188. X         if (info[i].time) {
  189. X             strncpy (lastdate, ctime (&info[i].time), 24);
  190. X             lastdate[24] = '\0';
  191. X         }
  192. X         else
  193. X             strcpy (lastdate, "never logged in");
  194. X
  195. X         printf ("%-12s %-24s    %s\n", info[i].name, lastdate,
  196. X                   info[i].gecos);
  197. X    }
  198. X
  199. X    close (llfd);
  200. X    endpwent ();
  201. X}
  202. X
  203. X/*
  204. X * infocmp - compare 2 info entries for qsort
  205. X */
  206. X
  207. Xinfocmp (info1, info2)
  208. Xstruct info_s  *info1,
  209. X               *info2;
  210. X{
  211. X    register int    r;
  212. X
  213. X    if (info1 -> time == info2 -> time)
  214. X         r = 0;
  215. X    else
  216. X         r = (info1 -> time > info2 -> time) ? 1 : -1;
  217. X
  218. X    return (r);
  219. X}
  220. X
  221. SHAR_EOF
  222. true || echo 'restore of lastlog.c failed'
  223. fi
  224. exit 0
  225. -- 
  226. .----------------------------------------------------------------------
  227. . Tin Le                    Work Internet: tin@smsc.Sony.COM
  228. . Sony Microsystems              UUCP: {uunet,mips}!sonyusa!tin
  229. . Work: (408) 944-4157      Home Internet: tin@szebra.uu.net
  230.