Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini mkdir implementation for busybox |
| 4 | * |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 5 | * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu> |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 6 | * |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 10 | /* BB_AUDIT SUSv3 compliant */ |
| 11 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/mkdir.html */ |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 12 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 13 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
| 14 | * |
| 15 | * Fixed broken permission setting when -p was used; especially in |
| 16 | * conjunction with -m. |
| 17 | */ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <unistd.h> |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 21 | #include <getopt.h> /* struct option */ |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 22 | #include "busybox.h" |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 23 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 24 | #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 25 | static const struct option mkdir_long_options[] = { |
| 26 | { "mode", 1, NULL, 'm' }, |
| 27 | { "parents", 0, NULL, 'p' }, |
| 28 | { 0, 0, 0, 0 } |
| 29 | }; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 30 | #endif |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 31 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 32 | int mkdir_main (int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 33 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 34 | mode_t mode = (mode_t)(-1); |
| 35 | int status = EXIT_SUCCESS; |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 36 | int flags = 0; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 37 | unsigned long opt; |
| 38 | char *smode; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 40 | #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 41 | bb_applet_long_options = mkdir_long_options; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 42 | #endif |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 43 | opt = bb_getopt_ulflags(argc, argv, "m:p", &smode); |
| 44 | if(opt & 1) { |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 45 | mode = 0777; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 46 | if (!bb_parse_mode (smode, &mode)) { |
| 47 | bb_error_msg_and_die ("invalid mode `%s'", smode); |
Eric Andersen | fa0540f | 1999-10-22 18:18:31 +0000 | [diff] [blame] | 48 | } |
Eric Andersen | fa0540f | 1999-10-22 18:18:31 +0000 | [diff] [blame] | 49 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 50 | if(opt & 2) |
| 51 | flags |= FILEUTILS_RECUR; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 52 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 53 | if (optind == argc) { |
| 54 | bb_show_usage(); |
Glenn L McGrath | 924f93e | 2002-09-28 08:32:14 +0000 | [diff] [blame] | 55 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 57 | argv += optind; |
| 58 | |
| 59 | do { |
| 60 | if (bb_make_directory(*argv, mode, flags)) { |
| 61 | status = EXIT_FAILURE; |
| 62 | } |
| 63 | } while (*++argv); |
| 64 | |
| 65 | return status; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 66 | } |