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

  1. /************************************************************
  2. * MultiUser - MultiUser Task/File Support System                *
  3. * ---------------------------------------------------------    *
  4. * Set the owner of a file - AmigaOS 3.0 (V39+) version        *
  5. * ---------------------------------------------------------    *
  6. * © Copyright 1993 by Geert Uytterhoeven                            *
  7. * All Rights Reserved.                                                    *
  8. ************************************************************/
  9.  
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <dos/dos.h>
  14. #include <dos/dosasl.h>
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17. #include <string.h>
  18. #include <libraries/multiuser.h>
  19. #include <proto/multiuser.h>
  20.  
  21. #include "SetOwner_rev.h"
  22.  
  23.  
  24. char __VersTag__[] = VERSTAG;
  25.  
  26.  
  27. int __saveds Start(char *arg)
  28. {
  29.     struct ExecBase *SysBase;
  30.     struct DosLibrary *DOSBase;
  31.     struct muBase *muBase = NULL;
  32.     struct RDArgs *args;
  33.     LONG argarray[] = {
  34. #define argFILE    0
  35. #define argUSER    1
  36. #define argGROUP    2
  37. #define argNOBODY    3
  38. #define argALL        4
  39. #define argQUIET    5
  40.         NULL, NULL, NULL, NULL, NULL, NULL
  41.     };
  42.     ULONG user = NULL;
  43.     struct muUserInfo *uinfo;
  44.     struct muGroupInfo *ginfo;
  45.     struct AnchorPath *anchor;
  46.     int rc = RETURN_OK;
  47.     LONG error = NULL;
  48.     BPTR dir;
  49.  
  50.     SysBase = *(struct ExecBase **)4;
  51.     
  52.     if ((!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 39))) ||
  53.          (!(muBase = (struct muBase *)OpenLibrary("multiuser.library", 39)))) {
  54.         rc = RETURN_FAIL;
  55.         goto Exit;
  56.     }
  57.  
  58.     args = ReadArgs("FILE/A,USER,GROUP,NOBODY/S,ALL/S,QUIET/S", argarray, NULL);
  59.     if (!args)
  60.         error = IoErr();
  61.     else if (argarray[argUSER] && argarray[argNOBODY]) {
  62.         PutStr("Invalid options\n");
  63.         rc = RETURN_ERROR;
  64.     } else {
  65.         if (argarray[argUSER]) {
  66.             if (uinfo = muAllocUserInfo()) {
  67.                 strncpy(uinfo->UserID, (char *)argarray[argUSER], muUSERIDSIZE-1);
  68.                 uinfo->UserID[muUSERIDSIZE-1] = '\0';
  69.                 if (muGetUserInfo(uinfo, muKeyType_UserID))
  70.                     user = (uinfo->uid<<16)|uinfo->gid;
  71.                 muFreeUserInfo(uinfo);
  72.             } else {
  73.                 PrintFault(IoErr(), NULL);
  74.                 goto Exit;
  75.             }
  76.             if (!user) {
  77.                 VPrintf("Unknown User '%s'\n", &argarray[argUSER]);
  78.                 rc = RETURN_ERROR;
  79.             }
  80.         } else if (!argarray[argNOBODY])
  81.             user = muGetTaskOwner(NULL);
  82.         if (argarray[argGROUP]) {
  83.             if (ginfo = muAllocGroupInfo()) {
  84.                 strncpy(ginfo->GroupID, (char *)argarray[argGROUP],
  85.                           muGROUPIDSIZE-1);
  86.                 ginfo->GroupID[muGROUPIDSIZE-1] = '\0';
  87.                 if (muGetGroupInfo(ginfo, muKeyType_GroupID))
  88.                     user = user & muMASK_UID | ginfo->gid;
  89.                 else {
  90.                     VPrintf("Unknown Group '%s'\n", &argarray[argGROUP]);
  91.                     rc = RETURN_ERROR;
  92.                 }
  93.                 muFreeGroupInfo(ginfo);                
  94.             } else {
  95.                 PrintFault(IoErr(), NULL);
  96.                 goto Exit;
  97.             }
  98.         }
  99.         if (!rc)
  100.             if (anchor = (struct AnchorPath *)AllocVec(sizeof(struct AnchorPath)+1024,
  101.                                                                      MEMF_CLEAR)) {
  102.                 anchor->ap_BreakBits = SIGBREAKF_CTRL_C;
  103.                 anchor->ap_Flags = APF_DOWILD;
  104.                 anchor->ap_Strlen = 1024;
  105.                 if (!(error = MatchFirst((char *)argarray[argFILE], anchor))) {
  106.                     do
  107.                         if (anchor->ap_Flags & APF_DIDDIR)
  108.                             anchor->ap_Flags &= ~APF_DIDDIR;
  109.                         else {
  110.                             if (argarray[argALL] && (anchor->ap_Info.fib_DirEntryType > 0))
  111.                                 anchor->ap_Flags |= APF_DODIR;
  112.                             dir = CurrentDir(DupLock(anchor->ap_Last->an_Lock));
  113.                             if (!SetOwner(anchor->ap_Info.fib_FileName, user)) {
  114.                                 PutStr(anchor->ap_Buf);
  115.                                 PrintFault(IoErr(), " ");
  116.                             } else if (!argarray[argQUIET]) {
  117.                                 PutStr(anchor->ap_Buf);
  118.                                 if (anchor->ap_Info.fib_DirEntryType > 0)
  119.                                     PutStr(" (dir)");
  120.                                 PutStr("...Done\n");
  121.                             }
  122.                             UnLock(CurrentDir(dir));
  123.                         }
  124.                     while (!(error = MatchNext(anchor)));
  125.                     if (error == ERROR_NO_MORE_ENTRIES)
  126.                         error = NULL;
  127.                 } else if (error == ERROR_NO_MORE_ENTRIES)
  128.                     error = ERROR_OBJECT_NOT_FOUND;
  129.                 MatchEnd(anchor);
  130.                 FreeVec(anchor);
  131.             } else
  132.                 error = IoErr();
  133.     }
  134.     FreeArgs(args);
  135.     if (error) {
  136.         PrintFault(error, NULL);
  137.         rc = RETURN_ERROR;
  138.     }
  139.  
  140. Exit:
  141.     CloseLibrary((struct Library *)muBase);
  142.     CloseLibrary((struct Library *)DOSBase);
  143.  
  144.     return(rc);
  145. }
  146.