bbify to shrink size
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;
+	unsigned long *fl;
+	char opt = *arg++;
 
-	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:
+	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;
 }