home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / utmp.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  2KB  |  73 lines

  1. /*
  2.  * BSD style utmp updating routine Version 1.0 (c) S.R.Usher 1991.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <utmp.h>
  10.  
  11. #define UTMP_FILE    "/etc/utmp"
  12.  
  13. void write_utmp(line, name, host, time)
  14. char *line;
  15. char *name;
  16. char *host;
  17. unsigned long time;
  18. {
  19.     register int returned_val;
  20.     int counter;
  21.     struct utmp entry;
  22.     int fd;
  23.  
  24.     bzero(&entry, sizeof(struct utmp));
  25.  
  26.     if ((fd = open(UTMP_FILE, O_RDWR)) == -1)
  27.     {
  28.         perror("write_utmp");
  29.         return;
  30.     }
  31.  
  32.     for (counter = 0; ((returned_val = read(fd, &entry, (unsigned) sizeof(struct utmp))) != -1); counter++)
  33.     {
  34. #ifdef DEBUG
  35.         printf("Current line is '%s' (returned_val = %d)\n", entry.ut_line, returned_val);
  36. #endif
  37.         if (returned_val == 0)
  38.             break;
  39.         if (strncmp(line, entry.ut_line, 8) == 0)
  40.             break;
  41.     }
  42.  
  43.     if (lseek(fd, (long)(counter * sizeof(struct utmp)), 0) == -1)
  44.     {
  45.         perror("write_utmp: lseek");
  46.         close(fd);
  47.         return;
  48.     }
  49.  
  50. /*
  51.  * Note, doing this in this order means that it doesn't matter about the Null
  52.  * bytes strncpy adds the the strings if they are greater than 8/16 bytes!
  53.  */
  54. #ifdef DEBUG
  55.     printf("counter = %d\nline = %s\nname = %s\nhost = %s\ntime = %lu\n",
  56.         counter, line, name, host, time);
  57. #endif
  58.     strncpy(entry.ut_line, line, 8);
  59.     strncpy(entry.ut_name, name, 8);
  60.     strncpy(entry.ut_host, host, 16);
  61.     entry.ut_time = time;
  62.  
  63.     if ((returned_val = write(fd, &entry, (unsigned) sizeof(struct utmp))) == -1)
  64.         perror("write_utmp: write");
  65.     else
  66.         if (returned_val != sizeof(struct utmp))
  67.             fprintf(stderr, "write_utmp: write: wrote too few bytes!\n");
  68. #ifdef DEBUG
  69.     printf("write_utmp: wrote %d bytes\n", returned_val);
  70. #endif
  71.     close(fd);
  72. }
  73.