home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / dec93 / os20 / util / multiuser.lha / MultiUser / C.src / Who.c < prev   
Encoding:
C/C++ Source or Header  |  1993-12-21  |  4.4 KB  |  179 lines

  1. /************************************************************
  2. * MultiUser - MultiUser Task/File Support System                *
  3. * ---------------------------------------------------------    *
  4. * Get a list of users who are logged in                            *
  5. * ---------------------------------------------------------    *
  6. * © Copyright 1993 by Geert Uytterhoeven                            *
  7. * All Rights Reserved.                                                    *
  8. ************************************************************/
  9.  
  10.  
  11. #include    <exec/memory.h>
  12. #include <exec/execbase.h>
  13. #include <proto/exec.h>
  14. #include <proto/dos.h>
  15. #include <string.h>
  16. #include <libraries/multiuser.h>
  17. #include <proto/multiuser.h>
  18.  
  19. #include "Who_rev.h"
  20.  
  21.  
  22. char __VersTag__[] = VERSTAG;
  23.  
  24.  
  25. static BOOL FillUsers(UWORD users[], ULONG maxusers, ULONG *numusers,
  26.                              struct ExecBase *SysBase, struct muBase *muBase);
  27. static BOOL AddUser(ULONG user, UWORD users[], ULONG maxusers,
  28.                           ULONG *numusers);
  29. static ULONG DumpUsers(UWORD users[], ULONG numusers, BOOL quick,
  30.                                struct DosLibrary *DOSBase, struct muBase *muBase);
  31.  
  32.  
  33. int __saveds Start(char *arg)
  34. {
  35.     struct ExecBase *SysBase;
  36.     struct DosLibrary *DOSBase;
  37.     struct muBase *muBase = NULL;
  38.     struct RDArgs *args;
  39.     LONG argarray[] = {
  40. #define argAM        0
  41. #define argI        1
  42. #define argQUICK    2
  43.         NULL, NULL, NULL
  44.     };
  45.     UWORD *users;
  46.     ULONG maxusers = 0, numusers;
  47.     LONG error = NULL;
  48.     int rc = RETURN_OK;
  49.     UWORD uid;
  50.  
  51.     SysBase = *(struct ExecBase **)4;
  52.     
  53.     if ((!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37))) ||
  54.          (!(muBase = (struct muBase *)OpenLibrary("multiuser.library", 39)))) {
  55.         rc = RETURN_FAIL;
  56.         goto Exit;
  57.     }
  58.  
  59.     args = ReadArgs("AM/S,I/S,Q=QUICK/S", argarray, NULL);
  60.     if (!args)
  61.         error = IoErr();
  62.     else if ((argarray[argAM] && !argarray[argI]) ||
  63.                 (!argarray[argAM] && argarray[argI]))
  64.         PutStr("Invalid options\n");
  65.     else if (argarray[argAM]) {
  66.         uid = muGetTaskOwner(NULL)>>16;
  67.         error = DumpUsers(&uid, 1, (BOOL)argarray[argQUICK], DOSBase, muBase);
  68.     } else do {
  69.         maxusers += 256;
  70.         if (users = AllocVec(maxusers*sizeof(UWORD), MEMF_CLEAR)) {
  71.             if (FillUsers(users, maxusers, &numusers, SysBase, muBase))
  72.                 error = DumpUsers(users, numusers, (BOOL)argarray[argQUICK], DOSBase,
  73.                                         muBase);
  74.             else
  75.                 rc = RETURN_ERROR;
  76.             FreeVec(users);
  77.         } else
  78.             error = IoErr();
  79.     } while (!error && (rc != RETURN_OK));
  80.     FreeArgs(args);
  81. Fail:
  82.     if (error) {
  83.         PrintFault(error, NULL);
  84.         rc = RETURN_ERROR;
  85.     }
  86.  
  87. Exit:
  88.     CloseLibrary((struct Library *)muBase);
  89.     CloseLibrary((struct Library *)DOSBase);
  90.  
  91.     return(rc);
  92. }    
  93.  
  94.  
  95.     /*
  96.      *        Fill in the uids
  97.      */
  98.  
  99. static BOOL FillUsers(UWORD users[], ULONG maxusers, ULONG *numusers,
  100.                              struct ExecBase *SysBase, struct muBase *muBase)
  101. {
  102.     BOOL rc;
  103.     struct Task *task;
  104.  
  105.     *numusers = 0;
  106.     Disable();
  107.     rc = AddUser(muGetTaskOwner(SysBase->ThisTask), users, maxusers, numusers);
  108.     for (task = (struct Task *)SysBase->TaskReady.lh_Head;
  109.           (task->tc_Node.ln_Succ) && rc;
  110.           task = (struct Task *)task->tc_Node.ln_Succ)
  111.         rc = AddUser(muGetTaskOwner(task), users, maxusers, numusers);
  112.     for (task = (struct Task *)SysBase->TaskWait.lh_Head;
  113.           (task->tc_Node.ln_Succ) && rc;
  114.           task = (struct Task *)task->tc_Node.ln_Succ)
  115.         rc = AddUser(muGetTaskOwner(task), users, maxusers, numusers);
  116.     Enable();
  117.     return(rc);
  118. }
  119.  
  120.  
  121. static BOOL AddUser(ULONG user, UWORD users[], ULONG maxusers, ULONG *numusers)
  122. {
  123.     ULONG i;
  124.     UWORD uid;
  125.     BOOL found = FALSE;
  126.  
  127.     if (user) {
  128.         uid = user>>16;
  129.         for (i = 0; !found && (i < *numusers); i++)
  130.             found = (users[i] == uid);
  131.         if (found)
  132.             return(TRUE);
  133.         else if (*numusers >= maxusers)
  134.             return(FALSE);
  135.         users[(*numusers)++] = uid;
  136.     }
  137.     return(TRUE);
  138. }
  139.  
  140.  
  141.     /*
  142.      *        Dump the information
  143.      */
  144.  
  145. static ULONG DumpUsers(UWORD users[], ULONG numusers, BOOL quick,
  146.                               struct DosLibrary *DOSBase, struct muBase *muBase)
  147. {
  148.     struct muUserInfo *info;
  149.     ULONG error = NULL;
  150.     ULONG i;
  151.     LONG stream[2];
  152.  
  153.     if (info = muAllocUserInfo()) {
  154.          for (i= 0; (i < numusers) && !CheckSignal(SIGBREAKF_CTRL_C); i++)
  155.             if (users[i]) {
  156.                 info->uid = users[i];
  157.                 if (muGetUserInfo(info, muKeyType_uid)) 
  158.                     if (quick) {
  159.                         stream[0] = (LONG)info->UserID;
  160.                         VPrintf("%s\n", stream);
  161.                     } else {
  162.                         stream[0] = (LONG)info->UserName;
  163.                         stream[1] = (LONG)info->UserID;
  164.                         VPrintf("%s (%s)\n", stream);
  165.                     }
  166.                 else {
  167.                     stream[0] = info->uid;
  168.                     VPrintf("Unknown user %04lx\n", stream);
  169.                 }
  170.             } else if (quick)
  171.                 PutStr("nobody\n");
  172.             else
  173.                 PutStr("Mister Anonymous (nobody)\n");
  174.         muFreeUserInfo(info);
  175.     } else
  176.         error = IoErr();
  177.     return(error);
  178. }
  179.