home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / cowen_tools.lzh / owner1.c < prev   
C/C++ Source or Header  |  1992-03-06  |  6KB  |  296 lines

  1. /*
  2. ** OWNER
  3. **  changes the ownership of files using the Cumana Password System
  4. **    owner <newgroup>.<newowner> <file> <file> ... 
  5. **     options are:
  6. **            -r = with recursion into directories
  7. **            -? = report usage
  8. **
  9. **  The password file is looked for on the following
  10. **            /DD/SYS/PASSWORD
  11. **            /H0/SYS/PASSWORD
  12. **            /D0/SYS/PASSWORD
  13. **
  14. ** 
  15. **  copyright 1991 by Cowen Software Ltd
  16. **  23 Bristol Ave, Levenshulme, Manchester,
  17. **  GB-M19 3NU, England, UK
  18. **  released to the public domain for non-commercial use
  19. ** 
  20. */
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <modes.h>
  25. #include <ctype.h>
  26. #define true 1
  27. #define false 0
  28. #define trace false
  29.  
  30. char qflag = false;
  31. int  pcount = 0;
  32. extern int  errno;
  33.  
  34. char  rflag = FALSE;                           /* recursive      */
  35.  
  36. char pbuff[140],
  37.      *ppntr;
  38.  
  39. char **arg;
  40.  
  41. char *username;
  42. union {short  userno;
  43.        struct { char userhi, userlo;} users;
  44.       } user;
  45.  
  46. int userint;       
  47.  
  48.  
  49. /* ------------Main Procedure--------------------------------------*/
  50.  
  51.  
  52. main(argc,argv)
  53. int   argc;
  54. char  *argv[];
  55.    {
  56.  
  57. #if trace
  58. fprintf(stderr,"main(%d parameters)\n",argc);
  59. fflush(stderr); 
  60. #endif
  61.  
  62.    getflags(argc,argv);
  63.    if    (pcount < 3)  help ();
  64.  
  65.    arg = argv;
  66.    
  67.    getowner();
  68.    printf("User %03u.%03u : %s\n",(int) user.users.userhi,
  69.                                   (int) user.users.userlo,
  70.                                   username);
  71.    userint = user.users.userhi * 65536 + user.users.userlo;
  72.         /*   This is because CHOWN demands an int as para    */
  73.                                   
  74.    while ( pcount-- > 0 ) newowner();
  75.  
  76. #if trace
  77. fprintf(stderr,"exit\n");
  78. fflush(stderr); 
  79. #endif
  80.  
  81.    exit(0);
  82.    }
  83.    
  84.  
  85. /* -----------------Get User from Password File----------------------*/
  86.  
  87. /*
  88. **    Scans down the PBUFF buffer to get the next parameter
  89. **    Terminates on "," (replacing it by \0)
  90. **    Errors on \n
  91. **    Uses the global pointer PPNTR, returns pointer to parameter
  92. */
  93.    
  94. char * getpass()
  95. {
  96. int pass, cnt;
  97. char *x1, *x2;
  98.  
  99. #if trace
  100. fprintf(stderr,"getpass(); ppntr = %-20s\n",ppntr);
  101. fflush(stderr); 
  102. #endif
  103.  
  104. if (*ppntr == '\0') return ppntr;
  105. for (x1 = ppntr; ( (*x1 != ',') & (*x1 != '\0') ) ;x1++);
  106.  
  107. *x1++ = '\0';
  108. x2 = ppntr;
  109. ppntr = x1;
  110. return x2;         
  111. }
  112.       
  113.  
  114. /*    get a User Number from a string pointer.
  115. **    called from getowner
  116. */
  117.  
  118. getno(ptr)
  119. char *ptr;
  120.       
  121. {
  122. char *p1, *p2;    
  123.  
  124. #if trace
  125. fprintf(stderr,"getno(%-8s)\n",ptr);
  126. fflush(stderr); 
  127. #endif
  128.  
  129. while (*ptr == ' ') ptr++;
  130. p1 = ptr;
  131. if (!isdigit(*ptr)) error ("No Group Number");
  132. while (isdigit(*ptr)) ptr++;
  133. if (*ptr != '.') error("No dot in User Number");
  134. *ptr++ = '\0';
  135. if (!isdigit(*ptr)) error ("No User Number");
  136. p2 = ptr;
  137. while (isdigit(*ptr)) ptr++;
  138. while (*ptr == ' ') ptr++;
  139. if (*ptr != '\0') error("Invalid User Number");
  140. return (atoi(p1) * 256 + atoi(p2) );
  141. }
  142.  
  143.  
  144.  
  145. /*
  146. **    Reads the new owner from the first parameter
  147. **    Called from MAIN procedure
  148. */
  149.    
  150. getowner()
  151.    {
  152.    int para_no, pass, cnt;
  153.    char x, *x1, *x2;
  154.  
  155. #if trace
  156. fprintf(stderr,"getowner()\n");
  157. fflush(stderr); 
  158. #endif
  159.  
  160.    pcount--;
  161.    while (**++arg == '-') ;
  162.    pass =  open("/DD/SYS/PASSWORD",_READ);
  163.    if ( pass == -1 ) pass =  open("/H0/SYS/PASSWORD",_READ);
  164.    if ( pass == -1 ) pass =  open("/D0/SYS/PASSWORD",_READ);
  165.    if ( pass == -1 )  error("Can't open password file");
  166.    x = **arg;
  167.    if (!isascii(x)) error("Corrupt Password File");
  168.  
  169.     para_no = (isdigit(x)) ? getno(*arg) : 0;
  170.    
  171.    while (cnt = readln(pass,pbuff,138) )
  172.       { 
  173.       if (cnt == -1) error("Error reading Password File");
  174.       pbuff[cnt-1] = '\0';
  175.       
  176. #if trace
  177. fprintf(stderr,"read: cnt = %d; pbuff = %-20s\n",cnt,pbuff);
  178. fflush(stderr); 
  179. #endif
  180.  
  181.       ppntr = pbuff;
  182.       while (*ppntr == ' ') ppntr++;
  183.       if (*ppntr == '\0') continue;
  184.       username = getpass();
  185.       getpass();              /* skip password */
  186.       x1 = getpass();
  187.       user.userno = getno(x1);
  188.  
  189. #if trace
  190. fprintf(stderr,"getno.loop para_no = %d; userno = %d\n",para_no,user.userno);
  191. fprintf(stderr,"      *arg = %-9s; username = %-9s\n",*arg,username);
  192. fflush(stderr); 
  193. #endif
  194.  
  195.       if (isdigit(x)) { if (para_no == user.userno) return 0; }
  196.       else            { if (!strcmp(*arg,username) ) return 0; }
  197.       }
  198.         
  199.    error("No such user");
  200.    exit(5);
  201.    }
  202.  
  203.  
  204. /* -----------------Change Ownership on named files-----------------*/
  205.     
  206.   
  207. /*
  208. **    Changes the File Ownership on named files
  209. **    Called from MAIN procedure
  210. */
  211.    
  212. newowner()
  213.    {
  214.  
  215. #if trace
  216. fprintf(stderr,"newowner()");
  217. fflush(stderr); 
  218. #endif
  219.  
  220.                                          while (**++arg == '-') ;
  221.    if (chown(*arg, userint) != 0) 
  222.               exit(errno);
  223.    printf("Owner changed for file : %s \n",*arg);
  224.  
  225.    ; 
  226.    }
  227.       
  228.  
  229. /* -------------------Help, Error, Etc------------------------------*/
  230.  
  231.  
  232. getflags(carg,varg)
  233.  int   carg;
  234. char  **varg;
  235.    {
  236.    char  *p, c;
  237.    while ( (--carg) )
  238.       {
  239.       if (*(p = *++varg) == '-')
  240.          {
  241.          while (c = *++p)
  242.             {
  243.             switch (c)
  244.                {
  245.                case '?' :
  246.                      help();
  247.                      break; 
  248.                case 'r' :
  249.                      rflag = TRUE;                    /* recursive*/
  250.                      break; 
  251.                default :
  252.                      help();
  253.                      break; 
  254.                }
  255.             }
  256.          }
  257.       else pcount++;      
  258.       }
  259.    }   
  260.  
  261.  
  262.   
  263. /*
  264. ** provide usage info for this command
  265. */
  266.  
  267.  
  268. help()
  269.    {
  270.    fputs("owner: change ownership of files\n", stderr);
  271.    fputs("         (super user only)\n", stderr);
  272.    fputs("Usage: owner user file file ...\n", stderr);
  273.    fputs("Where:\n", stderr);
  274.    fputs("User    = group.userno or username\n", stderr);
  275.    fputs("Options = -r - process directories recursively \n", stderr);
  276.    fputs("          -? - help\n", stderr);
  277.    exit (0);
  278.    }
  279.    
  280.   
  281. /*
  282. ** write an error message, by 
  283. ** writing the string passed and then a <cr>
  284. */
  285.  
  286. error(s1)
  287. char  *s1;
  288.    {
  289.    fputs(s1,stderr);
  290.    fputs("\n",stderr);
  291.    exit(5);
  292.    }
  293.  
  294.  
  295.  
  296.