home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3252 / whoson.c < prev   
Encoding:
C/C++ Source or Header  |  1991-04-30  |  2.5 KB  |  99 lines

  1. /* Whoson - A Unix equivalent of the Emas command
  2. Copyright (c) 1991, Kenneth Cameron.
  3. This software has not been placed in the public domain
  4. but may be freely distributed/used provided this notice remains intact.
  5.   
  6. This program is provided with no warranty that it will behave
  7. in the way expected. Further, the author will not be responsible
  8. for any loss resulting from the use or mis-use of this software. */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <utmp.h>
  13.  
  14. #define MAXU    1000    /* How many users should we cope with */
  15.  
  16. struct utmp User;
  17. char users[MAXU][10];
  18. int nsers[MAXU],total,number,visited;
  19.  
  20. sortu()
  21. {
  22.     int a,b,c;
  23.     char temp[8],n;
  24.     for(a=0;a<number;a++){
  25.         b=a;
  26.         for(c=a;c<number;c++)if(strcmp(users[b],users[c])>0)b=c;
  27.         strcpy(temp,users[a]);strcpy(users[a],users[b]);
  28.         strcpy(users[b],temp);n=nsers[a];nsers[a]=nsers[b];
  29.         nsers[b]=n;
  30.     }
  31.     return;
  32. }
  33.  
  34. main(argc,argv)
  35. int argc;
  36. char *argv[];
  37. {
  38.     FILE *fin;
  39.     int x,y,z,n;
  40.     char buf[20];
  41.     /* check flags */
  42.     for(x=1;x<argc;x++){
  43.         if(!strcmp(argv[x],"-h")){
  44. printf("Usage: %s [-h|-s]\n\n",argv[0]);
  45. printf("Display a list of those logged on. If the -s option is not specified\n");
  46. printf("then users logged on more than once will have the number shown in\n");
  47. printf("brackets after their username. Number of users/logins also shown\n");
  48.             exit(0);
  49.         }
  50.         if(strcmp(argv[x],"-s")){
  51.             printf("Invalid option use -h for help.\n");
  52.             exit(-1);
  53.         }
  54.     }
  55.     /* clear the user info areas */
  56.     total=number=visited=0;
  57.     for(x=0;x<MAXU;x++){users[x][0]='\0';nsers[x]=0;}
  58.     /* fetch the data we want from /etc/utmp */
  59.     if((fin=fopen("/etc/utmp","rb"))==(FILE *)0){
  60.         fprintf(stderr,
  61.             "Can't open /etc/utmp. Your machine's F**KED.\n");
  62.         exit(-1);
  63.     }
  64.     while(!feof(fin)){
  65.         /*get file entry*/
  66.         if(fread(&User,sizeof(User),1,fin)!=1)break;
  67.         visited++;
  68.         if(User.ut_host[0]=='\0')continue;/* Empty Slot */
  69.         total++;
  70.         for(x=0;x<MAXU;x++){
  71.             if(users[x][0]=='\0'){
  72.                 strncpy(users[x],User.ut_name,8);
  73.                 users[x][8];
  74.                 number++;nsers[x]=1;
  75.                 break;
  76.             }
  77.             if(!strncmp(User.ut_name,users[x],8)){
  78.                 nsers[x]++;break;
  79.             }
  80.         }
  81.     }
  82.     fclose(fin);
  83.     sortu();/* Sort the users into alphabetic order */
  84.     printf("Users currently logged on are:\n");
  85.     for(x=y=0;x<number;x++){
  86.         if(y==0)printf("   ");y++;
  87.         sprintf(buf,"%s",users[x]);
  88.         if((argc==1)&&(nsers[x]>1))
  89.             sprintf(buf,"%s(%d)",users[x],nsers[x]);
  90.         n=strlen(buf);printf("%s",buf);
  91.         if(y==6){y=0;printf("\n");}
  92.         else for(z=n;z<12;z++)printf(" ");
  93.     }
  94.     if(y!=0)printf("\n");
  95.     printf("               Total users: %d\tTotal logins: %d[%d]\n",number,
  96.         total,visited);
  97. }
  98.  
  99.