# 2.11BSD#431 - /usr/src/usr.sbin/chown/chown.c

##### Functions

    Perror            X
    chownr            X
    error             X
    fatal             X
    isnumber          X
    main              X

##### Variables

    copyright         X
    fflag             X
    pwd               X
    rflag             X
    status            X
    stbuf             X
    uid               X

       1: /*
       2:  * Copyright (c) 1980 Regents of the University of California.
       3:  * All rights reserved.  The Berkeley software License Agreement
       4:  * specifies the terms and conditions for redistribution.
       5:  */
       6:
       7: #if !defined(lint) && defined(DOSCCS)
       8: char copyright[] =
       9: "@(#) Copyright (c) 1980 Regents of the University of California.\n\
      10:  All rights reserved.\n";
      11:
      12: static char sccsid[] = "@(#)chown.c	5.6.1 (2.11BSD GTE) 11/4/94";
      13: #endif
      14:
      15: /*
      16:  * chown [-fR] uid[.gid] file ...
      17:  */
      18:
      19: #include <stdio.h>
      20: #include <ctype.h>
      21: #include <sys/types.h>
      22: #include <sys/file.h>
      23: #include <sys/stat.h>
      24: #include <pwd.h>
      25: #include <sys/dir.h>
      26: #include <grp.h>
      27: #include <strings.h>
      28:
      29: static  char    *fchdirmsg = "Can't fchdir() back to starting directory";
      30: struct  passwd *pwd;
      31: struct  passwd *getpwnam();
      32: struct  stat stbuf;
      33: uid_t   uid;
      34: int status;
      35: int fflag;
      36: int rflag;
      37:
      38: main(argc, argv)
      39:     char *argv[];
      40: {
      41:     register int c;
      42:     register gid_t gid;
      43:     register char *cp, *group;
      44:     struct group *grp;
      45:     int fcurdir;
      46:
      47:     argc--, argv++;
      48:     while (argc > 0 && argv[0][0] == '-') {
      49:         for (cp = &argv[0][1]; *cp; cp++) switch (*cp) {
      50:
      51:         case 'f':
      52:             fflag++;
      53:             break;
      54:
      55:         case 'R':
      56:             rflag++;
      57:             break;
      58:
      59:         default:
      60:             fatal(255, "unknown option: %c", *cp);
      61:         }
      62:         argv++, argc--;
      63:     }
      64:     if (argc < 2) {
      65:         fprintf(stderr, "usage: chown [-fR] owner[.group] file ...\n");
      66:         exit(-1);
      67:     }
      68:     gid = -1;
      69:     group = index(argv[0], '.');
      70:     if (group != NULL) {
      71:         *group++ = '\0';
      72:         if (!isnumber(group)) {
      73:             if ((grp = getgrnam(group)) == NULL)
      74:                 fatal(255, "unknown group: %s",group);
      75:             gid = grp -> gr_gid;
      76:             (void) endgrent();
      77:         } else if (*group != '\0')
      78:             gid = atoi(group);
      79:     }
      80:     if (!isnumber(argv[0])) {
      81:         if ((pwd = getpwnam(argv[0])) == NULL)
      82:             fatal(255, "unknown user id: %s",argv[0]);
      83:         uid = pwd->pw_uid;
      84:     } else
      85:         uid = atoi(argv[0]);
      86:
      87:     fcurdir = open(".", O_RDONLY);
      88:     if  (fcurdir < 0)
      89:         fatal(255, "Can't open .");
      90:
      91:     for (c = 1; c < argc; c++) {
      92:         /* do stat for directory arguments */
      93:         if (lstat(argv[c], &stbuf) < 0) {
      94:             status += Perror(argv[c]);
      95:             continue;
      96:         }
      97:         if (rflag && ((stbuf.st_mode&S_IFMT) == S_IFDIR)) {
      98:             status += chownr(argv[c], uid, gid, fcurdir);
      99:             continue;
     100:         }
     101:         if (chown(argv[c], uid, gid)) {
     102:             status += Perror(argv[c]);
     103:             continue;
     104:         }
     105:     }
     106:     exit(status);
     107: }
     108:
     109: isnumber(s)
     110:     char *s;
     111: {
     112:     register c;
     113:
     114:     while(c = *s++)
     115:         if (!isdigit(c))
     116:             return (0);
     117:     return (1);
     118: }
     119:
     120: chownr(dir, uid, gid, savedir)
     121:     char *dir;
     122: {
     123:     register DIR *dirp;
     124:     register struct direct *dp;
     125:     struct stat st;
     126:     int ecode;
     127:
     128:     /*
     129: 	 * Change what we are given before doing it's contents.
     130: 	 */
     131:     if (chown(dir, uid, gid) < 0 && Perror(dir))
     132:         return (1);
     133:     if (chdir(dir) < 0) {
     134:         Perror(dir);
     135:         return (1);
     136:     }
     137:     if ((dirp = opendir(".")) == NULL) {
     138:         Perror(dir);
     139:         return (1);
     140:     }
     141:     dp = readdir(dirp);
     142:     dp = readdir(dirp); /* read "." and ".." */
     143:     ecode = 0;
     144:     for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
     145:         if (lstat(dp->d_name, &st) < 0) {
     146:             ecode = Perror(dp->d_name);
     147:             if (ecode)
     148:                 break;
     149:             continue;
     150:         }
     151:         if ((st.st_mode&S_IFMT) == S_IFDIR) {
     152:             ecode = chownr(dp->d_name, uid, gid, dirfd(dirp));
     153:             if (ecode)
     154:                 break;
     155:             continue;
     156:         }
     157:         if (chown(dp->d_name, uid, gid) < 0 &&
     158:             (ecode = Perror(dp->d_name)))
     159:             break;
     160:     }
     161:     if  (fchdir(savedir) < 0)
     162:         fatal(255, fchdirmsg);
     163:     closedir(dirp);
     164:     return (ecode);
     165: }
     166:
     167: error(fmt, a)
     168:     char *fmt, *a;
     169: {
     170:
     171:     if (!fflag) {
     172:         fprintf(stderr, "chown: ");
     173:         fprintf(stderr, fmt, a);
     174:         putc('\n', stderr);
     175:     }
     176:     return (!fflag);
     177: }
     178:
     179: fatal(status, fmt, a)
     180:     int status;
     181:     char *fmt, *a;
     182: {
     183:
     184:     fflag = 0;
     185:     (void) error(fmt, a);
     186:     exit(status);
     187: }
     188:
     189: Perror(s)
     190:     char *s;
     191: {
     192:
     193:     if (!fflag) {
     194:         fprintf(stderr, "chown: ");
     195:         perror(s);
     196:     }
     197:     return (!fflag);
     198: }

#### Defined functions

Perror defined in line 189; used 7 times

- in line 94, 102, 131-138(3), 146, 158

chownr defined in line 120; used 2 times

- in line 98, 152

error defined in line 167; used 1 times

- in line 185

fatal defined in line 179; used 5 times

- in line 60, 74, 82, 89, 162

isnumber defined in line 109; used 2 times

- in line 72, 80

main defined in line 38; never used

#### Defined variables

copyright defined in line 8; never used
fchdirmsg defined in line 29; used 1 times

- in line 162

fflag defined in line 35; used 6 times

- in line 52, 171-176(2), 184, 193-197(2)

pwd defined in line 30; used 2 times

- in line 81-83(2)

rflag defined in line 36; used 2 times

- in line 56, 97

sccsid defined in line 12; never used
status defined in line 34; used 7 times

- in line 94-106(4), 179-180(2), 186

stbuf defined in line 32; used 2 times

- in line 93-97(2)

uid defined in line 33; used 8 times

- in line 83-85(2), 98-101(2), 120, 131, 152-157(2)

Last modified: 1994-11-05 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 1645 |