diff options
-rw-r--r-- | e2fsprogs/chattr.c | 326 | ||||
-rw-r--r-- | e2fsprogs/lsattr.c | 148 |
2 files changed, 177 insertions, 297 deletions
diff --git a/e2fsprogs/chattr.c b/e2fsprogs/chattr.c index 7abc831..5aaa14f 100644 --- a/e2fsprogs/chattr.c +++ b/e2fsprogs/chattr.c @@ -36,270 +36,200 @@ #define EXT2FS_ATTR(x) #endif -#ifndef S_ISLNK /* So we can compile even with gcc-warn */ -# ifdef __S_IFLNK -# define S_ISLNK(mode) __S_ISTYPE((mode), __S_IFLNK) -# else -# define S_ISLNK(mode) 0 -# endif -#endif - #include "e2fsbb.h" #include "e2p/e2p.h" -static int add; -static int rem; -static int set; -static int set_version; +#define OPT_ADD 1 +#define OPT_REM 2 +#define OPT_SET 4 +#define OPT_SET_VER 8 +static int flags; +static int recursive; static unsigned long version; -static int recursive; -static int verbose; - static unsigned long af; static unsigned long rf; static unsigned long sf; -#ifdef _LFS64_LARGEFILE -#define LSTAT lstat64 -#define STRUCT_STAT struct stat64 +#ifdef CONFIG_LFS +# define LSTAT lstat64 +# define STRUCT_STAT struct stat64 #else -#define LSTAT lstat -#define STRUCT_STAT struct stat -#endif - -#if 0 -static void fatal_error(const char * fmt_string, int errcode) -{ - fprintf (stderr, fmt_string, program_name); - exit (errcode); -} - -#define usage() fatal_error(_("usage: %s [-RV] [-+=AacDdijsSu] [-v version] files...\n"), \ - 1) +# define LSTAT lstat +# define STRUCT_STAT struct stat #endif struct flags_char { - unsigned long flag; - char optchar; + unsigned long flag; + char optchar; }; static const struct flags_char flags_array[] = { - { EXT2_NOATIME_FL, 'A' }, - { EXT2_SYNC_FL, 'S' }, - { EXT2_DIRSYNC_FL, 'D' }, - { EXT2_APPEND_FL, 'a' }, - { EXT2_COMPR_FL, 'c' }, - { EXT2_NODUMP_FL, 'd' }, - { EXT2_IMMUTABLE_FL, 'i' }, + { EXT2_NOATIME_FL, 'A' }, + { EXT2_SYNC_FL, 'S' }, + { EXT2_DIRSYNC_FL, 'D' }, + { EXT2_APPEND_FL, 'a' }, + { EXT2_COMPR_FL, 'c' }, + { EXT2_NODUMP_FL, 'd' }, + { EXT2_IMMUTABLE_FL, 'i' }, { EXT3_JOURNAL_DATA_FL, 'j' }, - { EXT2_SECRM_FL, 's' }, - { EXT2_UNRM_FL, 'u' }, - { EXT2_NOTAIL_FL, 't' }, - { EXT2_TOPDIR_FL, 'T' }, + { EXT2_SECRM_FL, 's' }, + { EXT2_UNRM_FL, 'u' }, + { EXT2_NOTAIL_FL, 't' }, + { EXT2_TOPDIR_FL, 'T' }, { 0, 0 } }; static unsigned long get_flag(char c) { const struct flags_char *fp; - - for (fp = flags_array; fp->flag != 0; fp++) { + for (fp = flags_array; fp->flag; fp++) if (fp->optchar == c) return fp->flag; - } + bb_show_usage(); return 0; } - -static int decode_arg (int * i, int argc, char ** argv) +static int decode_arg(char *arg) { - char * p; - char * tmp; - unsigned long fl; - - switch (argv[*i][0]) - { - case '-': - for (p = &argv[*i][1]; *p; p++) { - if (*p == 'R') { - recursive = 1; - continue; - } - if (*p == 'V') { - verbose = 1; - continue; - } - if (*p == 'v') { - (*i)++; - if (*i >= argc) - usage (); - version = strtol (argv[*i], &tmp, 0); - if (*tmp) { - com_err (program_name, 0, - _("bad version - %s\n"), - argv[*i]); - usage (); - } - set_version = 1; - continue; - } - if ((fl = get_flag(*p)) == 0) - usage(); - rf |= fl; - rem = 1; - } - break; - case '+': - add = 1; - for (p = &argv[*i][1]; *p; p++) { - if ((fl = get_flag(*p)) == 0) - usage(); - af |= fl; - } - break; - case '=': - set = 1; - for (p = &argv[*i][1]; *p; p++) { - if ((fl = get_flag(*p)) == 0) - usage(); - sf |= fl; - } - break; - default: + unsigned long *fl; + char opt = *arg++; + + if (opt == '-') { + flags |= OPT_REM; + fl = &rf; + } else if (opt == '+') { + flags |= OPT_ADD; + fl = ⁡ + } else if (opt == '=') { + flags |= OPT_SET; + fl = &sf; + } else return EOF; - break; - } + + for (; *arg ; ++arg) + (*fl) |= get_flag(*arg); + return 1; } -static int chattr_dir_proc (const char *, struct dirent *, void *); +static int chattr_dir_proc(const char *, struct dirent *, void *); -static void change_attributes (const char * name) +static void change_attributes(const char * name) { - unsigned long flags; + unsigned long fsflags; STRUCT_STAT st; - if (LSTAT (name, &st) == -1) { - com_err (program_name, errno, _("while trying to stat %s"), - name); + if (LSTAT(name, &st) == -1) { + bb_error_msg("while trying to stat %s", name); return; } if (S_ISLNK(st.st_mode) && recursive) return; /* Don't try to open device files, fifos etc. We probably - ought to display an error if the file was explicitly given - on the command line (whether or not recursive was - requested). */ - if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && - !S_ISDIR(st.st_mode)) + * ought to display an error if the file was explicitly given + * on the command line (whether or not recursive was + * requested). */ + if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode)) return; - if (set) { - if (verbose) { - printf (_("Flags of %s set as "), name); - print_flags (stdout, sf, 0); - printf ("\n"); - } - if (fsetflags (name, sf) == -1) - perror (name); + if (flags & OPT_SET_VER) + if (fsetversion(name, version) == -1) + bb_error_msg("while setting version on %s", name); + + if (flags & OPT_SET) { + fsflags = sf; } else { - if (fgetflags (name, &flags) == -1) - com_err (program_name, errno, - _("while reading flags on %s"), name); - else { - if (rem) - flags &= ~rf; - if (add) - flags |= af; - if (verbose) { - printf (_("Flags of %s set as "), name); - print_flags (stdout, flags, 0); - printf ("\n"); - } - if (!S_ISDIR(st.st_mode)) - flags &= ~EXT2_DIRSYNC_FL; - if (fsetflags (name, flags) == -1) - com_err (program_name, errno, - _("while setting flags on %s"), name); + if (fgetflags(name, &fsflags) == -1) { + bb_error_msg("while reading flags on %s", name); + goto skip_setflags; } + if (flags & OPT_REM) + fsflags &= ~rf; + if (flags & OPT_ADD) + fsflags |= af; + if (!S_ISDIR(st.st_mode)) + fsflags &= ~EXT2_DIRSYNC_FL; } - if (set_version) { - if (verbose) - printf (_("Version of %s set as %lu\n"), name, version); - if (fsetversion (name, version) == -1) - com_err (program_name, errno, - _("while setting version on %s"), name); - } + if (fsetflags(name, fsflags) == -1) + bb_error_msg("while setting flags on %s", name); + +skip_setflags: if (S_ISDIR(st.st_mode) && recursive) - iterate_on_dir (name, chattr_dir_proc, NULL); + iterate_on_dir(name, chattr_dir_proc, NULL); } -static int chattr_dir_proc (const char * dir_name, struct dirent * de, - void * private EXT2FS_ATTR((unused))) +static int chattr_dir_proc(const char *dir_name, struct dirent *de, + void *private EXT2FS_ATTR((unused))) { - if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) { - char *path; + /*if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {*/ + if (de->d_name[0] == '.' && (de->d_name[1] == '\0' || \ + (de->d_name[1] == '.' && de->d_name[2] == '\0'))) { - path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1); + char *path = malloc(strlen(dir_name) + 1 + strlen(de->d_name) + 1); if (!path) - fatal_error(_("Couldn't allocate path variable " - "in chattr_dir_proc"), 1); - sprintf (path, "%s/%s", dir_name, de->d_name); - change_attributes (path); + bb_error_msg_and_die("Couldn't allocate path variable in chattr_dir_proc"); + sprintf(path, "%s/%s", dir_name, de->d_name); + change_attributes(path); free(path); } return 0; } -int chattr_main (int argc, char ** argv) +int chattr_main(int argc, char **argv) { - int i, j; - int end_arg = 0; + int i; + char *arg; -#ifdef ENABLE_NLS - setlocale(LC_MESSAGES, ""); - setlocale(LC_CTYPE, ""); - bindtextdomain(NLS_CAT_NAME, LOCALEDIR); - textdomain(NLS_CAT_NAME); -#endif -#if 0 - if (argc && *argv) - program_name = *argv; -#endif - i = 1; - while (i < argc && !end_arg) { - /* '--' arg should end option processing */ - if (strcmp(argv[i], "--") == 0) { - i++; - end_arg = 1; - } else if (decode_arg (&i, argc, argv) == EOF) - end_arg = 1; - else - i++; + /* parse the args */ + for (i = 1; i < argc; ++i) { + arg = argv[i]; + + /* take care of -R and -v <version> */ + if (arg[0] == '-') { + if (arg[1] == 'R' && arg[2] == '\0') { + recursive = 1; + continue; + } else if (arg[1] == 'v' && arg[2] == '\0') { + char *tmp; + ++i; + if (i >= argc) + bb_show_usage(); + version = strtol(argv[i], &tmp, 0); + if (*tmp) { + bb_error_msg("bad version - %s", arg); + bb_show_usage(); + } + flags |= OPT_SET_VER; + continue; + } + } + + if (decode_arg(arg) == EOF) + break; } + + /* run sanity checks on all the arguments given us */ if (i >= argc) - usage (); - if (set && (add || rem)) { - fputs(_("= is incompatible with - and +\n"), stderr); - exit (1); + bb_show_usage(); + if ((flags & OPT_SET) && ((flags & OPT_ADD) || (flags & OPT_REM))) { + bb_error_msg("= is incompatible with - and +"); + return EXIT_FAILURE; } if ((rf & af) != 0) { - fputs("Can't both set and unset same flag.\n", stderr); - exit (1); + bb_error_msg("Can't both set and unset same flag"); + return EXIT_FAILURE; } - if (!(add || rem || set || set_version)) { - fputs(_("Must use '-v', =, - or +\n"), stderr); - exit (1); + if (!flags) { + bb_error_msg("Must use '-v', =, - or +"); + return EXIT_FAILURE; } -#if 0 - if (verbose) - fprintf (stderr, "chattr %s (%s)\n", - E2FSPROGS_VERSION, E2FSPROGS_DATE); -#endif - for (j = i; j < argc; j++) - change_attributes (argv[j]); - exit(0); + + /* now run chattr on all the files passed to us */ + while (i < argc) + change_attributes(argv[i++]); + + return EXIT_SUCCESS; } diff --git a/e2fsprogs/lsattr.c b/e2fsprogs/lsattr.c index 0f1c6f2..be5723c 100644 --- a/e2fsprogs/lsattr.c +++ b/e2fsprogs/lsattr.c @@ -39,99 +39,87 @@ #define EXT2FS_ATTR(x) #endif -static int all; -static int dirs_opt; -static unsigned pf_options; -static int recursive; -static int verbose; -static int generation_opt; - -#ifdef _LFS64_LARGEFILE -#define LSTAT lstat64 -#define STRUCT_STAT struct stat64 +#define OPT_RECUR 1 +#define OPT_ALL 2 +#define OPT_DIRS_OPT 4 +#define OPT_PF_LONG 8 +#define OPT_GENERATION 16 +static int flags; + +#ifdef CONFIG_LFS +# define LSTAT lstat64 +# define STRUCT_STAT struct stat64 #else -#define LSTAT lstat -#define STRUCT_STAT struct stat +# define LSTAT lstat +# define STRUCT_STAT struct stat #endif -#if 0 -static void usage(void) +static void list_attributes(const char *name) { - fprintf(stderr, _("Usage: %s [-RVadlv] [files...]\n"), program_name); - exit(1); -} -#endif - -static void list_attributes (const char * name) -{ - unsigned long flags; + unsigned long fsflags; unsigned long generation; - if (fgetflags (name, &flags) == -1) { - com_err (program_name, errno, _("While reading flags on %s"), - name); + if (fgetflags(name, &fsflags) == -1) { + bb_perror_msg("While reading flags on %s", name); return; } - if (generation_opt) { - if (fgetversion (name, &generation) == -1) { - com_err (program_name, errno, - _("While reading version on %s"), - name); + if (flags & OPT_GENERATION) { + if (fgetversion(name, &generation) == -1) { + bb_perror_msg("While reading version on %s", name); return; } - printf ("%5lu ", generation); + printf("%5lu ", generation); } - if (pf_options & PFOPT_LONG) { + if (flags & OPT_PF_LONG) { printf("%-28s ", name); - print_flags(stdout, flags, pf_options); + print_flags(stdout, fsflags, PFOPT_LONG); fputc('\n', stdout); } else { - print_flags(stdout, flags, pf_options); + print_flags(stdout, fsflags, 0); printf(" %s\n", name); } } -static int lsattr_dir_proc (const char *, struct dirent *, void *); +static int lsattr_dir_proc(const char *, struct dirent *, void *); -static void lsattr_args (const char * name) +static void lsattr_args(const char *name) { STRUCT_STAT st; - if (LSTAT (name, &st) == -1) - com_err (program_name, errno, _("while trying to stat %s"), - name); + if (LSTAT(name, &st) == -1) + bb_perror_msg("while trying to stat %s", name); else { - if (S_ISDIR(st.st_mode) && !dirs_opt) - iterate_on_dir (name, lsattr_dir_proc, NULL); + if (S_ISDIR(st.st_mode) && !(flags & OPT_DIRS_OPT)) + iterate_on_dir(name, lsattr_dir_proc, NULL); else - list_attributes (name); + list_attributes(name); } } -static int lsattr_dir_proc (const char * dir_name, struct dirent * de, - void * private EXT2FS_ATTR((unused))) +static int lsattr_dir_proc(const char *dir_name, struct dirent *de, + void *private EXT2FS_ATTR((unused))) { STRUCT_STAT st; char *path; int dir_len = strlen(dir_name); - path = malloc(dir_len + strlen (de->d_name) + 2); + path = malloc(dir_len + strlen(de->d_name) + 2); if (dir_len && dir_name[dir_len-1] == '/') - sprintf (path, "%s%s", dir_name, de->d_name); + sprintf(path, "%s%s", dir_name, de->d_name); else - sprintf (path, "%s/%s", dir_name, de->d_name); - if (LSTAT (path, &st) == -1) - perror (path); + sprintf(path, "%s/%s", dir_name, de->d_name); + if (LSTAT(path, &st) == -1) + bb_perror_msg(path); else { - if (de->d_name[0] != '.' || all) { - list_attributes (path); - if (S_ISDIR(st.st_mode) && recursive && + if (de->d_name[0] != '.' || (flags & OPT_ALL)) { + list_attributes(path); + if (S_ISDIR(st.st_mode) && (flags & OPT_RECUR) && strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) { - printf ("\n%s:\n", path); - iterate_on_dir (path, lsattr_dir_proc, NULL); - printf ("\n"); + printf("\n%s:\n", path); + iterate_on_dir(path, lsattr_dir_proc, NULL); + printf("\n"); } } } @@ -139,55 +127,17 @@ static int lsattr_dir_proc (const char * dir_name, struct dirent * de, return 0; } -int lsattr_main (int argc, char ** argv) +int lsattr_main(int argc, char **argv) { - int c; int i; -#ifdef ENABLE_NLS - setlocale(LC_MESSAGES, ""); - setlocale(LC_CTYPE, ""); - bindtextdomain(NLS_CAT_NAME, LOCALEDIR); - textdomain(NLS_CAT_NAME); -#endif -#if 0 - if (argc && *argv) - program_name = *argv; -#endif - while ((c = getopt (argc, argv, "Radlv")) != EOF) - switch (c) - { - case 'R': - recursive = 1; - break; - case 'V': - verbose = 1; - break; - case 'a': - all = 1; - break; - case 'd': - dirs_opt = 1; - break; - case 'l': - pf_options = PFOPT_LONG; - break; - case 'v': - generation_opt = 1; - break; - default: - usage(); - } + flags = bb_getopt_ulflags(argc, argv, "Radlv"); -#if 0 - if (verbose) - fprintf (stderr, "lsattr %s (%s)\n", - E2FSPROGS_VERSION, E2FSPROGS_DATE); -#endif if (optind > argc - 1) - lsattr_args ("."); + lsattr_args("."); else for (i = optind; i < argc; i++) - lsattr_args (argv[i]); - exit(0); + lsattr_args(argv[i]); + + return EXIT_SUCCESS; } |