1: /*
   2:  * chown uid file ...
   3:  */
   4: 
   5: #include <stdio.h>
   6: #include <ctype.h>
   7: #include <sys/types.h>
   8: #include <sys/stat.h>
   9: #include <pwd.h>
  10: #include <grp.h>
  11: 
  12: struct  passwd  *pwd,*getpwnam();
  13: struct  group   *grp,*getgrnam();
  14: struct  stat    stbuf;
  15: int uid, gid;
  16: int status;
  17: 
  18: main(argc, argv)
  19: char *argv[];
  20: {
  21:     register c;
  22:     register char *group;
  23: 
  24:     if(argc < 3) {
  25:         printf("usage: chown uid file ...\n");
  26:         exit(4);
  27:     }
  28:     for (group = argv[1]; *group ; group++) {
  29:         if (*group == '.') {
  30:             *group = '\0';
  31:             group++;
  32:             if (isnumber(group))
  33:                 gid = atoi(group);
  34:             else {
  35:                 if ((grp=getgrnam(group)) == -1) {
  36:                     printf("unknown group: %s\n",group);
  37:                     exit(4);
  38:                 }
  39:                 gid = grp->gr_gid;
  40:                 endgrent();
  41:             }
  42:             goto owner;
  43:         }
  44:     }
  45:     group = NULL;
  46: owner:
  47:     if(isnumber(argv[1])) {
  48:         uid = atoi(argv[1]);
  49:     } else {
  50:         if((pwd=getpwnam(argv[1])) == NULL) {
  51:             printf("unknown user id: %s\n",argv[1]);
  52:             exit(4);
  53:         }
  54:         uid = pwd->pw_uid;
  55:     }
  56: 
  57:     for(c=2; c<argc; c++) {
  58:         if (group) {
  59:         if(chown(argv[c], uid, gid) < 0) {
  60:             perror(argv[c]);
  61:             status = 1;
  62:         }
  63:         } else {
  64:         stat(argv[c], &stbuf);
  65:         if(chown(argv[c], uid, stbuf.st_gid) < 0) {
  66:             perror(argv[c]);
  67:             status = 1;
  68:         }
  69:         }
  70:     }
  71:     exit(status);
  72: }
  73: 
  74: isnumber(s)
  75: char *s;
  76: {
  77:     register c;
  78: 
  79:     while(c = *s++)
  80:         if(!isdigit(c))
  81:             return(0);
  82:     return(1);
  83: }

Defined functions

isnumber defined in line 74; used 2 times
main defined in line 18; never used

Defined variables

gid defined in line 15; used 3 times
grp defined in line 13; used 2 times
pwd defined in line 12; used 2 times
status defined in line 16; used 3 times
stbuf defined in line 14; used 2 times
uid defined in line 15; used 4 times
Last modified: 1982-09-21
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 970
Valid CSS Valid XHTML 1.0 Strict