handle 'chown a b/c' right; 'chdir ..' isn't enough
[unix-history] / usr / src / usr.sbin / chown / chown.c
index b5b0b9a..e37265f 100644 (file)
@@ -3,11 +3,16 @@
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms are permitted
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at Berkeley. The name of the University
- * may not be used to endorse or promote products derived from this
- * software without specific prior written permission. This software
- * is provided ``as is'' without express or implied warranty.
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -17,7 +22,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)chown.c    5.10 (Berkeley) %G%";
+static char sccsid[] = "@(#)chown.c    5.12 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -39,7 +44,7 @@ main(argc, argv)
        extern int optind;
        register char *cp;
        int ch;
        extern int optind;
        register char *cp;
        int ch;
-       char *index(), *rindex();
+       char curpath[MAXPATHLEN], *reset, *index(), *rindex();
 
        myname = (cp = rindex(*argv, '/')) ? cp + 1 : *argv;
        ischown = myname[2] == 'o';
 
        myname = (cp = rindex(*argv, '/')) ? cp + 1 : *argv;
        ischown = myname[2] == 'o';
@@ -76,8 +81,17 @@ main(argc, argv)
                setgid(*argv);
        }
 
                setgid(*argv);
        }
 
-       while (*++argv)
+       while (*++argv) {
+               if (reset = index(*argv, '/'))
+                       (void)getwd(curpath);
                change(*argv);
                change(*argv);
+               if (reset && chdir(curpath)) {
+                       if (fflag)
+                               exit(0);
+                       err(curpath);
+                       exit(-1);
+               }
+       }
        exit(retval);
 }
 
        exit(retval);
 }
 
@@ -203,10 +217,12 @@ static
 err(s)
        char *s;
 {
 err(s)
        char *s;
 {
+       extern int errno;
+       char *strerror();
+
        if (fflag)
                return;
        if (fflag)
                return;
-       fprintf(stderr, "%s: ", myname);
-       perror(s);
+       fprintf(stderr, "%s: %s: %s", myname, s, strerror(errno));
        retval = -1;
 }
 
        retval = -1;
 }