home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / mint / init_5 / utmp.c < prev    next >
C/C++ Source or Header  |  1993-08-03  |  2KB  |  80 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 <utmp.h>
  8.  
  9. #define UTMP_FILE    "/etc/utmp"
  10.  
  11. void write_utmp(line, name, host, time)
  12. char *line;
  13. char *name;
  14. char *host;
  15. unsigned long time;
  16. {
  17.     register int returned_val;
  18.     int counter;
  19.     struct utmp entry;
  20.     int fd;
  21.  
  22. #ifdef DEBUG
  23.     printf("Given parameters:- line = '%s' name = '%s' host = '%s'\n", line, name, host);
  24. #endif
  25.  
  26.     bzero(&entry, sizeof(struct utmp));
  27.  
  28.     if ((fd = open(UTMP_FILE, O_RDWR)) == -1)
  29.     {
  30.         perror("write_utmp");
  31.         return;
  32.     }
  33.  
  34.     for (counter = 0; ((returned_val = read(fd, &entry, sizeof(struct utmp))) != -1); counter++)
  35.     {
  36. #ifdef DEBUG
  37.         printf("Current line is '%s' (returned_val = %d)\n", entry.ut_line, returned_val);
  38. #endif
  39.         if (returned_val == 0)
  40.             break;
  41.         if (strncmp(line, entry.ut_line, 8) == 0)
  42.             break;
  43.     }
  44.  
  45.     if (lseek(fd, (counter * sizeof(struct utmp)), 0) == -1)
  46.     {
  47.         perror("write_utmp: lseek");
  48.         close(fd);
  49.         return;
  50.     }
  51.  
  52. /*
  53.  * Note, doing this in this order means that it doesn't matter about the Null
  54.  * bytes strncpy adds the the strings if they are greater than 8/16 bytes!
  55.  */
  56. #ifdef DEBUG
  57.     printf("counter = %d\nline = %s\nname = %s\nhost = %s\ntime = %lu\n",
  58.         counter, line, name, host, time);
  59. #endif
  60.     strncpy(entry.ut_line, line, 8);
  61.     strncpy(entry.ut_name, name, 8);
  62.     strncpy(entry.ut_host, host, 16);
  63.     entry.ut_time = time;
  64.  
  65. #ifdef DEBUG
  66.     printf("counter = %d\nline = %s\nname = %s\nhost = %s\ntime = %lu\n",
  67.         counter, line, name, host, time);
  68. #endif
  69.  
  70.     if ((returned_val = write(fd, &entry, sizeof(struct utmp))) == -1)
  71.         perror("write_utmp: write");
  72.     else
  73.         if (returned_val != sizeof(struct utmp))
  74.             fprintf(stderr, "write_utmp: write: wrote too few bytes!\n");
  75. #ifdef DEBUG
  76.     printf("write_utmp: wrote %d bytes\n", returned_val);
  77. #endif
  78.     close(fd);
  79. }
  80.