home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / A / PS / PROCPS-0.000 / PROCPS-0 / procps-0.97 / utmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-25  |  4.8 KB  |  179 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/ioctl.h>
  5. #include <time.h>
  6. #include <utmp.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <sys/stat.h>
  10.  
  11. /* examine and fix utmp entries.  Note that the code for fixing entries
  12.    is not complete, and indeed does nothing at all at this time.  No bug
  13.    reports, please, as I am still actively working on this.  It is not
  14.    here for general use, but only so that I can ferret out any bugs that
  15.    exist on other peoples systems without having to log in to their systems
  16.    myself ;-)  */
  17.  
  18.  
  19. int main (int argc, char **argv) {
  20.  
  21.   FILE *ut; /* /etc/utmp */
  22.   struct utmp uts; /* utmp record */
  23.   char user[UT_NAMESIZE + 1];
  24.   char host[17];
  25.   char ch;
  26.   int print_all = 0, list = 0, fix = 0;
  27.  
  28. /* get options */
  29.   while ((ch = getopt(argc, argv, "laf")) != EOF)
  30.     switch (ch) {
  31.     case 'a':
  32.       print_all = 1;
  33.       break;
  34.     case 'l':
  35.       list = 1;
  36.       break;
  37.     case 'f':
  38.       fix = 1;
  39.       break;
  40.     }
  41.  
  42. /* check argument options */
  43.   if ( (!list && !print_all && !fix)) {
  44.     fprintf(stderr, "You must specify a command line option:\n\tl = list\n\
  45. \tf = fix\n\ta = all (requires l or f)\n");
  46.     exit(1);
  47.   }
  48.  
  49.   
  50.   if (list) {
  51.     ut = fopen(UTMP_FILE, "r");
  52.     while (fread(&uts, sizeof(uts), 1, ut))
  53.       if (((uts.ut_type == USER_PROCESS) && (uts.ut_name[0] != '\000'))
  54.       || print_all) {
  55.     strncpy(user, uts.ut_user, UT_NAMESIZE);
  56.     user[UT_NAMESIZE]=0;
  57.     strncpy(host, uts.ut_host, 16);
  58.     host[16]=0;
  59.     printf("ut_type: %d\n", uts.ut_type);
  60.     printf("ut_pid:  %d\n", uts.ut_pid);
  61.     printf("ut_line: %s\n", uts.ut_line);
  62.     printf("ut_id:   %2s\n", uts.ut_id);
  63.     printf("ut_time: %d\n", uts.ut_time);
  64.     printf("ut_user: %s\n", user);
  65.     printf("ut_host: %s\n", host);
  66.     printf("ut_addr: %d\n\n", uts.ut_addr);
  67.       }
  68.     fclose(ut);
  69.   }
  70.  
  71.  
  72.   if (fix) {
  73.     ut = fopen(UTMP_FILE, "r");
  74.     while (fread(&uts, sizeof(uts), 1, ut)) 
  75.       if (((uts.ut_type == USER_PROCESS) && (uts.ut_name[0] != '\000'))
  76.       || print_all) {
  77.     /* Display entry in utmp */
  78.     strncpy(user, uts.ut_user, UT_NAMESIZE);
  79.     user[UT_NAMESIZE]=0;
  80.     strncpy(host, uts.ut_host, 16);
  81.     host[16]=0;
  82.     printf("ut_type: %d\n", uts.ut_type);
  83.     printf("ut_pid:  %d\n", uts.ut_pid);
  84.     printf("ut_line: %s\n", uts.ut_line);
  85.     printf("ut_id:   %s2\n", uts.ut_id);
  86.     printf("ut_time: %d\n", uts.ut_time);
  87.     printf("ut_user: %s\n", user);
  88.     printf("ut_host: %s\n", host);
  89.     printf("ut_addr: %d\n\n", uts.ut_addr);
  90.       
  91.     printf("Modify this record? (y/N): "); fflush(stdout);
  92.     /* Ask if to delete or no */
  93.     if ((ch = getchar()) == 'y' || ch == 'Y') {
  94.       while (getchar() != '\n');
  95.       printf("Change ut_type? "); fflush(stdout);
  96.       if ((ch = getchar()) == 'y' || ch == 'Y') {
  97.         while (getchar() != '\n');
  98.         printf("INIT, LOGIN, USER, or DEAD_PROCESS? (I/L/U/D): ");
  99.         fflush(stdout);
  100.         ch = getchar();
  101.         switch (ch) {
  102.         case 'i':
  103.         case 'I':
  104.           uts.ut_type = INIT_PROCESS;
  105.           break;
  106.         case 'l':
  107.         case 'L':
  108.           uts.ut_type = LOGIN_PROCESS;
  109.           break;
  110.         case 'u':
  111.         case 'U':
  112.           uts.ut_type = USER_PROCESS;
  113.           break;
  114.         case 'd':
  115.         case 'D':
  116.           uts.ut_type = DEAD_PROCESS;
  117.           break;
  118.         default:
  119.           printf("Invalid choice: %c\n", ch);
  120.         }
  121.         if (ch != '\n') while ((ch = getchar()) != '\n');
  122.       }
  123.       if (ch != '\n') while ((ch = getchar()) != '\n');
  124.       printf("Change ut_id field? (y/N): "); fflush(stdout);
  125.       if ((ch = getchar()) == 'y' || ch == 'Y') {
  126.         while (getchar() != '\n');
  127.         printf("Please enter the two characters for ut_id: ");
  128.         fflush(stdout);
  129.         uts.ut_id[0] = getchar();
  130.         uts.ut_id[1] = getchar();
  131.         while ((ch = getchar()) != '\n');
  132.       }
  133.       if (ch != '\n') while ((ch = getchar()) != '\n');
  134.       printf("Change the ut_user field? (y/N): "); fflush(stdout);
  135.       if ((ch = getchar()) == 'y' || ch == 'Y') {
  136.         int i;
  137.         while (getchar() != '\n');
  138.         printf("Please enter the new ut_name, up to %c characters: ",
  139.            UT_NAMESIZE);
  140.         fflush(stdout);
  141.         for (i=0; i<UT_NAMESIZE; i++) {
  142.           ch = getchar();
  143.           uts.ut_user[i] = (ch != '\n') ? ch : i = UT_NAMESIZE, (char) 0;
  144.         }
  145.       }
  146.       if (ch != '\n') while ((ch = getchar()) != '\n');
  147.       printf("Change the ut_host field? (y/N): "); fflush(stdout);
  148.       if ((ch = getchar()) == 'y' || ch == 'Y') {
  149.         int i;
  150.         while (getchar() != '\n');
  151.         printf("Please enter the new ut_host, up to 16 characters: ");
  152.         fflush(stdout);
  153.         for (i=0; i<16; i++) {
  154.           ch = getchar();
  155.           uts.ut_user[i] = (ch != '\n') ? ch : i = 16, (char) 0;
  156.         }
  157.         if (ch != '\n') while ((ch = getchar()) != '\n');
  158.       }
  159.  
  160.       /* Here go the changes...*/
  161. /*      utmpname(UTMP_FILE);
  162.       setutent();
  163.       pututline(&uts);
  164.       endutent(); */
  165. /* But they don't work... */
  166.  
  167.     }
  168.     if (ch != '\n') while ((ch = getchar()) != '\n');
  169.     /* here we should write the utmp entry */
  170.       }
  171.     fclose(ut);
  172.   }
  173.  
  174.  
  175.   return 0;
  176.  
  177.  
  178. }
  179.