Commit | Line | Data |
---|---|---|
bcf1365c | 1 | /* |
59d11f9a KB |
2 | * Copyright (c) 1983, 1992, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
0f38f750 | 4 | * |
27c71911 | 5 | * %sccs.include.redist.c% |
bcf1365c DF |
6 | */ |
7 | ||
8 | #ifndef lint | |
59d11f9a KB |
9 | static char copyright[] = |
10 | "@(#) Copyright (c) 1983, 1992, 1993\n\ | |
11 | The Regents of the University of California. All rights reserved.\n"; | |
0f38f750 | 12 | #endif /* not lint */ |
bcf1365c DF |
13 | |
14 | #ifndef lint | |
59d11f9a | 15 | static char sccsid[] = "@(#)mkdir.c 8.1 (Berkeley) %G%"; |
0f38f750 | 16 | #endif /* not lint */ |
bcf1365c | 17 | |
937fdd8f KB |
18 | #include <sys/types.h> |
19 | #include <sys/stat.h> | |
779eba17 | 20 | |
2de05b7b | 21 | #include <err.h> |
937fdd8f | 22 | #include <errno.h> |
a645d56b | 23 | #include <stdio.h> |
779eba17 | 24 | #include <stdlib.h> |
6ebcb998 | 25 | #include <string.h> |
fe19c180 | 26 | |
2de05b7b KB |
27 | int build __P((char *)); |
28 | void usage __P((void)); | |
ea857b71 | 29 | |
779eba17 | 30 | int |
fe19c180 | 31 | main(argc, argv) |
0f38f750 | 32 | int argc; |
779eba17 | 33 | char *argv[]; |
fe19c180 | 34 | { |
2de05b7b | 35 | int ch, exitval, pflag; |
fe19c180 | 36 | |
937fdd8f KB |
37 | pflag = 0; |
38 | while ((ch = getopt(argc, argv, "p")) != EOF) | |
39 | switch(ch) { | |
40 | case 'p': | |
41 | pflag = 1; | |
42 | break; | |
43 | case '?': | |
44 | default: | |
45 | usage(); | |
46 | } | |
47 | ||
48 | if (!*(argv += optind)) | |
49 | usage(); | |
50 | ||
2de05b7b | 51 | for (exitval = 0; *argv; ++argv) |
937fdd8f | 52 | if (pflag) |
2de05b7b KB |
53 | exitval |= build(*argv); |
54 | else if (mkdir(*argv, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { | |
55 | warn("%s", *argv); | |
56 | exitval = 1; | |
57 | } | |
937fdd8f KB |
58 | exit(exitval); |
59 | } | |
60 | ||
2de05b7b | 61 | int |
937fdd8f KB |
62 | build(path) |
63 | char *path; | |
64 | { | |
65 | register char *p; | |
66 | struct stat sb; | |
779eba17 | 67 | int create, savech; |
937fdd8f | 68 | |
779eba17 KB |
69 | p = path; |
70 | if (*p) /* Skip leading '/'. */ | |
71 | ++p; | |
72 | for (create = 0;; ++p) | |
73 | if (!*p || *p == '/') { | |
74 | savech = *p; | |
937fdd8f KB |
75 | *p = '\0'; |
76 | if (stat(path, &sb)) { | |
779eba17 KB |
77 | if (errno != ENOENT || mkdir(path, |
78 | S_IRWXU | S_IRWXG | S_IRWXO) < 0) { | |
2de05b7b KB |
79 | warn("%s", path); |
80 | return (1); | |
937fdd8f KB |
81 | } |
82 | create = 1; | |
83 | } | |
779eba17 | 84 | if (!(*p = savech)) |
937fdd8f KB |
85 | break; |
86 | } | |
2de05b7b KB |
87 | if (!create) { |
88 | warnx("%s: %s", path, strerror(EEXIST)); | |
89 | return (1); | |
90 | } | |
91 | return (0); | |
937fdd8f KB |
92 | } |
93 | ||
779eba17 | 94 | void |
937fdd8f KB |
95 | usage() |
96 | { | |
779eba17 | 97 | (void)fprintf(stderr, "usage: mkdir [-p] directory ...\n"); |
2de05b7b | 98 | exit (1); |
779eba17 | 99 | } |