home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / MISC / chown.c < prev    next >
C/C++ Source or Header  |  2009-11-06  |  2KB  |  78 lines

  1. /*
  2.  *    c h o w n . c
  3.  *
  4.  *    Creation Date:    03-July-1991 by trn@warper.jhuapl.edu <Tony Nardo>
  5.  *    Last Modified:
  6.  *
  7.  *    Change file ownership #.  Somewhat dissimilar to UNIX in that it
  8.  *    forces group *and* file ownership to change, but I wanted to keep
  9.  *    this simple.
  10.  *
  11.  *    This software has been tested on an MVME147 OSK (Microware) system
  12.  *    implementing OS-9 level 2.
  13.  *
  14.  *    No warranty expressed or implied. :-)
  15.  */
  16.  
  17. #include <strings.h>
  18.  
  19. #define    HELP    "usage: chown group#.user# file [file2...]\n"
  20.  
  21. main( argc, argv )
  22. register int    argc;
  23. register char    **argv;
  24. {
  25.     unsigned long    owner;
  26.     unsigned short    gid, uid;
  27.     register int    i, error;
  28.     register char    *dot;
  29.  
  30.     /*
  31.      * Must be sufficiently privileged to use chown.
  32.      */
  33.     owner = getuid();
  34.     if( owner & 0xffff0000 ) {
  35.         printf( "No permission to use chown\n" );
  36.         exit( 0 );
  37.     }
  38.  
  39.     /*
  40.      * Too few arguments will force a HELP message.
  41.      */
  42.     if( argc < 3 ) {
  43.         printf( HELP );
  44.         exit( 0 );
  45.     }
  46.  
  47.     /*
  48.      * Lack of a "." in the "GID.UID" field will also force HELP.
  49.      */
  50.     dot = index( argv[1], '.' );
  51.     if( ! dot ) {
  52.         printf( HELP );
  53.         exit( 0 );
  54.     }
  55.  
  56.     /*
  57.      * Extract the GID and UID.  Don't bother with sanity checks.
  58.      */
  59.     *dot = 0;            /* erase period to terminate GID */
  60.     dot++;                /* and look beyond it for UID     */
  61.     uid = atoi( dot );        /* get UID, GID             */
  62.     gid = atoi( argv[1] );
  63.     owner = ( gid << 16 ) | uid;    /* OS9 format owner # format     */
  64.  
  65.     /*
  66.      * All seems OK, go ahead and try to change ownerships!
  67.      */
  68.     i = 2;
  69.     do {
  70.         error = chown( argv[ i ], owner );
  71.         if( error ) {
  72.             printf( "chown: %s: No such file or directory\n",
  73.                argv[ i ] );
  74.         }
  75.     } while( ++i < argc );
  76.  
  77. }
  78.