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 | |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 19 | /* Nov 28, 2006 Yoshinori Sato <ysato@users.sourceforge.jp>: Add SELinux Support. |
| 20 | */ |
| 21 | |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 22 | #include <getopt.h> /* struct option */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 23 | #include "libbb.h" |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 25 | /* This is a NOFORK applet. Be very careful! */ |
| 26 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 27 | #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame^] | 28 | static const char mkdir_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 29 | "mode\0" Required_argument "m" |
| 30 | "parents\0" No_argument "p" |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 31 | #if ENABLE_SELINUX |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 32 | "context\0" Required_argument "Z" |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 33 | #endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 34 | ; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 35 | #endif |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 37 | int mkdir_main(int argc, char **argv); |
Denis Vlasenko | 22dcd04 | 2006-11-19 13:53:50 +0000 | [diff] [blame] | 38 | int mkdir_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 40 | mode_t mode = (mode_t)(-1); |
| 41 | int status = EXIT_SUCCESS; |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 42 | int flags = 0; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 43 | unsigned opt; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 44 | char *smode; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 45 | #if ENABLE_SELINUX |
| 46 | security_context_t scontext; |
| 47 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 48 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 49 | #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 50 | applet_long_options = mkdir_longopts; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 51 | #endif |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 52 | opt = getopt32(argc, argv, "m:p" USE_SELINUX("Z:"), &smode USE_SELINUX(,&scontext)); |
Denis Vlasenko | 22dcd04 | 2006-11-19 13:53:50 +0000 | [diff] [blame] | 53 | if (opt & 1) { |
Denis Vlasenko | dca0b70 | 2006-10-27 09:05:02 +0000 | [diff] [blame] | 54 | mode = 0777; |
Denis Vlasenko | 22dcd04 | 2006-11-19 13:53:50 +0000 | [diff] [blame] | 55 | if (!bb_parse_mode(smode, &mode)) { |
| 56 | bb_error_msg_and_die("invalid mode '%s'", smode); |
Eric Andersen | fa0540f | 1999-10-22 18:18:31 +0000 | [diff] [blame] | 57 | } |
Eric Andersen | fa0540f | 1999-10-22 18:18:31 +0000 | [diff] [blame] | 58 | } |
Denis Vlasenko | 22dcd04 | 2006-11-19 13:53:50 +0000 | [diff] [blame] | 59 | if (opt & 2) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 60 | flags |= FILEUTILS_RECUR; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 61 | #if ENABLE_SELINUX |
| 62 | if (opt & 4) { |
| 63 | selinux_or_die(); |
Denis Vlasenko | 39c651e | 2007-03-12 18:22:55 +0000 | [diff] [blame] | 64 | setfscreatecon_or_die(scontext); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 65 | } |
| 66 | #endif |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 67 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 68 | if (optind == argc) { |
| 69 | bb_show_usage(); |
Glenn L McGrath | 924f93e | 2002-09-28 08:32:14 +0000 | [diff] [blame] | 70 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | argv += optind; |
| 73 | |
| 74 | do { |
| 75 | if (bb_make_directory(*argv, mode, flags)) { |
| 76 | status = EXIT_FAILURE; |
| 77 | } |
| 78 | } while (*++argv); |
| 79 | |
| 80 | return status; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 81 | } |