diff options
| author | The Android Open Source Project <initial-contribution@android.com> | 2010-07-14 16:16:20 -0700 |
|---|---|---|
| committer | Android Git Automerger <android-git-automerger@android.com> | 2010-07-14 16:16:20 -0700 |
| commit | f4f7697f69a68abc755d9c4d1ef384984d81b164 (patch) | |
| tree | 1b5345d24526fc793ded3712ead66b85e1ff0d55 /toolbox | |
| parent | 335cc39928ba4ff8daca360376bc2d7fd241122d (diff) | |
| parent | 0bcda269708bbc4f4bd729765e3d4d84e0fd5e60 (diff) | |
| download | system_core-f4f7697f69a68abc755d9c4d1ef384984d81b164.tar.gz system_core-f4f7697f69a68abc755d9c4d1ef384984d81b164.tar.bz2 system_core-f4f7697f69a68abc755d9c4d1ef384984d81b164.zip | |
am 0bcda269: merge from open-source master
Merge commit '0bcda269708bbc4f4bd729765e3d4d84e0fd5e60'
* commit '0bcda269708bbc4f4bd729765e3d4d84e0fd5e60':
Add -R to chmod
Add -p support to mkdir
Diffstat (limited to 'toolbox')
| -rw-r--r-- | toolbox/chmod.c | 67 | ||||
| -rw-r--r-- | toolbox/mkdir.c | 61 |
2 files changed, 119 insertions, 9 deletions
diff --git a/toolbox/chmod.c b/toolbox/chmod.c index 31a53bf7..2a524e99 100644 --- a/toolbox/chmod.c +++ b/toolbox/chmod.c @@ -4,17 +4,74 @@ #include <sys/types.h> #include <dirent.h> #include <errno.h> +#include <sys/limits.h> +#include <sys/stat.h> #include <unistd.h> #include <time.h> +void recurse_chmod(char* path, int mode) +{ + struct dirent *dp; + DIR *dir = opendir(path); + if (dir == NULL) { + // not a directory, carry on + return; + } + char *subpath = malloc(sizeof(char)*PATH_MAX); + int pathlen = strlen(path); + + while ((dp = readdir(dir)) != NULL) { + if (strcmp(dp->d_name, ".") == 0 || + strcmp(dp->d_name, "..") == 0) continue; + + if (strlen(dp->d_name) + pathlen + 2/*NUL and slash*/ > PATH_MAX) { + fprintf(stderr, "Invalid path specified: too long\n"); + exit(1); + } + + strcpy(subpath, path); + strcat(subpath, "/"); + strcat(subpath, dp->d_name); + + if (chmod(subpath, mode) < 0) { + fprintf(stderr, "Unable to chmod %s: %s\n", subpath, strerror(errno)); + exit(1); + } + + recurse_chmod(subpath, mode); + } + free(subpath); + closedir(dir); +} + +static int usage() +{ + fprintf(stderr, "Usage: chmod [OPTION] <MODE> <FILE>\n"); + fprintf(stderr, " -R, --recursive change files and directories recursively\n"); + fprintf(stderr, " --help display this help and exit\n"); + + return 10; +} + int chmod_main(int argc, char **argv) { int i; - if (argc < 3) { - fprintf(stderr, "Usage: chmod <MODE> <FILE>\n"); - return 10; + if (argc < 3 || strcmp(argv[1], "--help") == 0) { + return usage(); + } + + int recursive = (strcmp(argv[1], "-R") == 0 || + strcmp(argv[1], "--recursive") == 0) ? 1 : 0; + + if (recursive && argc < 4) { + return usage(); + } + + if (recursive) { + argc--; + argv++; } int mode = 0; @@ -29,11 +86,15 @@ int chmod_main(int argc, char **argv) } s++; } + for (i = 2; i < argc; i++) { if (chmod(argv[i], mode) < 0) { fprintf(stderr, "Unable to chmod %s: %s\n", argv[i], strerror(errno)); return 10; } + if (recursive) { + recurse_chmod(argv[i], mode); + } } return 0; } diff --git a/toolbox/mkdir.c b/toolbox/mkdir.c index 121adab2..656970a7 100644 --- a/toolbox/mkdir.c +++ b/toolbox/mkdir.c @@ -2,10 +2,14 @@ #include <unistd.h> #include <string.h> #include <errno.h> +#include <sys/limits.h> +#include <sys/stat.h> static int usage() { - fprintf(stderr,"mkdir <target>\n"); + fprintf(stderr,"mkdir [OPTION] <target>\n"); + fprintf(stderr," --help display usage and exit\n"); + fprintf(stderr," -p, --parents create parent directories as needed\n"); return -1; } @@ -13,15 +17,60 @@ int mkdir_main(int argc, char *argv[]) { int symbolic = 0; int ret; - if(argc < 2) return usage(); + if(argc < 2 || strcmp(argv[1], "--help") == 0) { + return usage(); + } + + int recursive = (strcmp(argv[1], "-p") == 0 || + strcmp(argv[1], "--parents") == 0) ? 1 : 0; + + if(recursive && argc < 3) { + // -p specified without a path + return usage(); + } + + if(recursive) { + argc--; + argv++; + } + + char currpath[PATH_MAX], *pathpiece; + struct stat st; while(argc > 1) { argc--; argv++; - ret = mkdir(argv[0], 0777); - if(ret < 0) { - fprintf(stderr, "mkdir failed for %s, %s\n", argv[0], strerror(errno)); - return ret; + if(recursive) { + // reset path + strcpy(currpath, ""); + // create the pieces of the path along the way + pathpiece = strtok(argv[0], "/"); + if(argv[0][0] == '/') { + // prepend / if needed + strcat(currpath, "/"); + } + while(pathpiece != NULL) { + if(strlen(currpath) + strlen(pathpiece) + 2/*NUL and slash*/ > PATH_MAX) { + fprintf(stderr, "Invalid path specified: too long\n"); + return 1; + } + strcat(currpath, pathpiece); + strcat(currpath, "/"); + if(stat(currpath, &st) != 0) { + ret = mkdir(currpath, 0777); + if(ret < 0) { + fprintf(stderr, "mkdir failed for %s, %s\n", currpath, strerror(errno)); + return ret; + } + } + pathpiece = strtok(NULL, "/"); + } + } else { + ret = mkdir(argv[0], 0777); + if(ret < 0) { + fprintf(stderr, "mkdir failed for %s, %s\n", argv[0], strerror(errno)); + return ret; + } } } |
