mkdir.c (40537) | mkdir.c (40602) |
---|---|
1/* 2 * Copyright (c) 1983, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 28 unchanged lines hidden (view full) --- 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)mkdir.c 8.2 (Berkeley) 1/25/94"; 43#endif 44static const char rcsid[] = | 1/* 2 * Copyright (c) 1983, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 28 unchanged lines hidden (view full) --- 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)mkdir.c 8.2 (Berkeley) 1/25/94"; 43#endif 44static const char rcsid[] = |
45 "$Id: mkdir.c,v 1.12 1998/10/20 06:37:01 msmith Exp $"; | 45 "$Id: mkdir.c,v 1.13 1998/10/20 08:04:15 msmith Exp $"; |
46#endif /* not lint */ 47 48#include <sys/types.h> 49#include <sys/stat.h> 50 51#include <err.h> 52#include <errno.h> 53#include <stdio.h> --- 40 unchanged lines hidden (view full) --- 94 errx(1, "invalid file mode: %s", mode); 95 omode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO); 96 } 97 98 for (exitval = 0; *argv != NULL; ++argv) { 99 if (pflag) { 100 if (build(*argv, omode)) 101 exitval = 1; | 46#endif /* not lint */ 47 48#include <sys/types.h> 49#include <sys/stat.h> 50 51#include <err.h> 52#include <errno.h> 53#include <stdio.h> --- 40 unchanged lines hidden (view full) --- 94 errx(1, "invalid file mode: %s", mode); 95 omode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO); 96 } 97 98 for (exitval = 0; *argv != NULL; ++argv) { 99 if (pflag) { 100 if (build(*argv, omode)) 101 exitval = 1; |
102 continue; 103 } 104 if (mkdir(*argv, omode) < 0) { | 102 } else if (mkdir(*argv, omode) < 0) { |
105 warn("%s", *argv); 106 exitval = 1; 107 } 108 /* 109 * The mkdir() and umask() calls both honor only the low 110 * nine bits, so if you try to set a mode including the | 103 warn("%s", *argv); 104 exitval = 1; 105 } 106 /* 107 * The mkdir() and umask() calls both honor only the low 108 * nine bits, so if you try to set a mode including the |
111 * sticky, setuid, setgid bits you lose them. So chmod(). | 109 * sticky, setuid, setgid bits you lose them. Don't do 110 * this unless the user has specifically requested a mode, 111 * as chmod will (obviously) ignore the umask. |
112 */ | 112 */ |
113 if (chmod(*argv, omode) == -1) { 114 warn("%s", *argv); | 113 if ((exitval == 0) && 114 (mode != NULL) && (chmod(*argv, omode) == -1)) { 115 warn("chmod %s", *argv); |
115 exitval = 1; 116 } 117 } 118 exit(exitval); 119} 120 121int 122build(path, omode) --- 41 unchanged lines hidden (view full) --- 164 if (stat(path, &sb)) { 165 if (errno != ENOENT || 166 mkdir(path, last ? omode : 167 S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 168 warn("%s", path); 169 retval = 1; 170 break; 171 } | 116 exitval = 1; 117 } 118 } 119 exit(exitval); 120} 121 122int 123build(path, omode) --- 41 unchanged lines hidden (view full) --- 165 if (stat(path, &sb)) { 166 if (errno != ENOENT || 167 mkdir(path, last ? omode : 168 S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 169 warn("%s", path); 170 retval = 1; 171 break; 172 } |
172 /* 173 * The mkdir() and umask() calls both honor only the 174 * low nine bits, so if you try to set a mode 175 * including the sticky, setuid, setgid bits you lose 176 * them. So chmod() the last path component to try 177 * to do what the caller has asked for. 178 */ 179 if (last && (chmod(path, omode) == -1)) { 180 warn("%s", path); 181 retval = 1; 182 } | |
183 } 184 else if ((sb.st_mode & S_IFMT) != S_IFDIR) { 185 if (last) 186 errno = EEXIST; 187 else 188 errno = ENOTDIR; 189 warn("%s", path); 190 retval = 1; 191 break; 192 } | 173 } 174 else if ((sb.st_mode & S_IFMT) != S_IFDIR) { 175 if (last) 176 errno = EEXIST; 177 else 178 errno = ENOTDIR; 179 warn("%s", path); 180 retval = 1; 181 break; 182 } |
193 *p = '/'; | 183 if (!last) 184 *p = '/'; |
194 } 195 if (!first && !last) 196 (void)umask(oumask); 197 return (retval); 198} 199 200void 201usage() 202{ 203 (void)fprintf(stderr, "usage: mkdir [-p] [-m mode] directory ...\n"); 204 exit (1); 205} | 185 } 186 if (!first && !last) 187 (void)umask(oumask); 188 return (retval); 189} 190 191void 192usage() 193{ 194 (void)fprintf(stderr, "usage: mkdir [-p] [-m mode] directory ...\n"); 195 exit (1); 196} |