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

  1. /************************************************************
  2. * MultiUser - MultiUser Task/File Support System                *
  3. * ---------------------------------------------------------    *
  4. * Set the owner of a file - AmigaOS 2.04 (V37+) 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 "SetOwner37_rev.h"
  22.  
  23.  
  24. char __VersTag__[] = VERSTAG;
  25.  
  26.  
  27. static BOOL MySetOwner(char *name, LONG owner, struct DosLibrary *DOSBase);
  28.  
  29.  
  30. int __saveds Start(char *arg)
  31. {
  32.     struct ExecBase *SysBase;
  33.     struct DosLibrary *DOSBase;
  34.     struct muBase *muBase = NULL;
  35.     struct RDArgs *args;
  36.     LONG argarray[] = {
  37. #define argFILE    0
  38. #define argUSER    1
  39. #define argGROUP    2
  40. #define argNOBODY    3
  41. #define argALL        4
  42. #define argQUIET    5
  43.         NULL, NULL, NULL, NULL, NULL, NULL
  44.     };
  45.     ULONG user = NULL;
  46.     struct muUserInfo *uinfo;
  47.     struct muGroupInfo *ginfo;
  48.     struct AnchorPath *anchor;
  49.     int rc = RETURN_OK;
  50.     LONG error = NULL;
  51.     BPTR dir;
  52.  
  53.     SysBase = *(struct ExecBase **)4;
  54.     
  55.     if ((!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37))) ||
  56.          (!(muBase = (struct muBase *)OpenLibrary("multiuser.library", 39)))) {
  57.         rc = RETURN_FAIL;
  58.         goto Exit;
  59.     }
  60.  
  61.     args = ReadArgs("FILE/A,USER,GROUP,NOBODY/S,ALL/S,QUIET/S", argarray, NULL);
  62.     if (!args)
  63.         error = IoErr();
  64.     else if (argarray[argUSER] && argarray[argNOBODY]) {
  65.         PutStr("Invalid options\n");
  66.         rc = RETURN_ERROR;
  67.     } else {
  68.         if (argarray[argUSER]) {
  69.             if (uinfo = muAllocUserInfo()) {
  70.                 strncpy(uinfo->UserID, (char *)argarray[argUSER], muUSERIDSIZE-1);
  71.                 uinfo->UserID[muUSERIDSIZE-1] = '\0';
  72.                 if (muGetUserInfo(uinfo, muKeyType_UserID))
  73.                     user = (uinfo->uid<<16)|uinfo->gid;
  74.                 muFreeUserInfo(uinfo);
  75.             } else {
  76.                 PrintFault(IoErr(), NULL);
  77.                 goto Exit;
  78.             }
  79.             if (!user) {
  80.                 VPrintf("Unknown User '%s'\n", &argarray[argUSER]);
  81.                 rc = RETURN_ERROR;
  82.             }
  83.         } else if (!argarray[argNOBODY])
  84.             user = muGetTaskOwner(NULL);
  85.         if (argarray[argGROUP]) {
  86.             if (ginfo = muAllocGroupInfo()) {
  87.                 strncpy(ginfo->GroupID, (char *)argarray[argGROUP],
  88.                           muGROUPIDSIZE-1);
  89.                 ginfo->GroupID[muGROUPIDSIZE-1] = '\0';
  90.                 if (muGetGroupInfo(ginfo, muKeyType_GroupID))
  91.                     user = user & muMASK_UID | ginfo->gid;
  92.                 else {
  93.                     VPrintf("Unknown Group '%s'\n", &argarray[argGROUP]);
  94.                     rc = RETURN_ERROR;
  95.                 }
  96.                 muFreeGroupInfo(ginfo);                
  97.             } else {
  98.                 PrintFault(IoErr(), NULL);
  99.                 goto Exit;
  100.             }
  101.         }
  102.         if (!rc)
  103.             if (anchor = (struct AnchorPath *)AllocVec(sizeof(struct AnchorPath)+1024,
  104.                                                                      MEMF_CLEAR)) {
  105.                 anchor->ap_BreakBits = SIGBREAKF_CTRL_C;
  106.                 anchor->ap_Flags = APF_DOWILD;
  107.                 anchor->ap_Strlen = 1024;
  108.                 if (!(error = MatchFirst((char *)argarray[argFILE], anchor))) {
  109.                     do
  110.                         if (anchor->ap_Flags & APF_DIDDIR)
  111.                             anchor->ap_Flags &= ~APF_DIDDIR;
  112.                         else {
  113.                             if (argarray[argALL] && (anchor->ap_Info.fib_DirEntryType > 0))
  114.                                 anchor->ap_Flags |= APF_DODIR;
  115.                             dir = CurrentDir(DupLock(anchor->ap_Last->an_Lock));
  116.                             if (!MySetOwner(anchor->ap_Info.fib_FileName, user, DOSBase)) {
  117.                                 PutStr(anchor->ap_Buf);
  118.                                 PrintFault(IoErr(), " ");
  119.                             } else if (!argarray[argQUIET]) {
  120.                                 PutStr(anchor->ap_Buf);
  121.                                 if (anchor->ap_Info.fib_DirEntryType > 0)
  122.                                     PutStr(" (dir)");
  123.                                 PutStr("...Done\n");
  124.                             }
  125.                             UnLock(CurrentDir(dir));
  126.                         }
  127.                     while (!(error = MatchNext(anchor)));
  128.                     if (error == ERROR_NO_MORE_ENTRIES)
  129.                         error = NULL;
  130.                 } else if (error == ERROR_NO_MORE_ENTRIES)
  131.                     error = ERROR_OBJECT_NOT_FOUND;
  132.                 MatchEnd(anchor);
  133.                 FreeVec(anchor);
  134.             } else
  135.                 error = IoErr();
  136.     }
  137.     FreeArgs(args);
  138.     if (error) {
  139.         PrintFault(error, NULL);
  140.         rc = RETURN_ERROR;
  141.     }
  142.  
  143. Exit:
  144.     CloseLibrary((struct Library *)muBase);
  145.     CloseLibrary((struct Library *)DOSBase);
  146.  
  147.     return(rc);
  148. }
  149.  
  150.  
  151. static BOOL MySetOwner(char *name, LONG owner, struct DosLibrary *DOSBase)
  152. {
  153.     BOOL rc = FALSE;
  154.  
  155.     struct DevProc *dp;
  156.     BPTR fl;
  157.  
  158.     if (dp = GetDeviceProc(name, NULL)) {
  159.         if (fl = Lock(name, ACCESS_READ)) {
  160.             rc = DoPkt(dp->dvp_Port, ACTION_SET_OWNER, NULL, fl, MKBADDR("\0"),
  161.                           owner, NULL);
  162.             UnLock(fl);
  163.         }
  164.         FreeDeviceProc(dp);
  165.     }
  166.     return(rc);
  167. }
  168.