[PATCH 1/3] sysctl: Add a flags variable, and use it to pass output parameters

Jeremy Kerr jk at ozlabs.org
Wed Nov 28 03:11:24 UTC 2007


Signed-off-by: Jeremy Kerr <jk at ozlabs.org>

--
bloat-o-meter results on powerpc:
function                                             old     new   delta
sysctl_display_all                                   220     212      -8
sysctl_main                                          684     672     -12
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-20)             Total: -20 bytes


---

 procps/sysctl.c |   48 +++++++++++++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 21 deletions(-)

Index: busybox/procps/sysctl.c
===================================================================
--- busybox.orig/procps/sysctl.c
+++ busybox/procps/sysctl.c
@@ -19,10 +19,10 @@
 /*
  *    Function Prototypes
  */
-static int sysctl_read_setting(const char *setting, int output);
-static int sysctl_write_setting(const char *setting, int output);
-static int sysctl_preload_file(const char *filename, int output);
-static int sysctl_display_all(const char *path, int output, int show_table);
+static int sysctl_read_setting(const char *setting, int flags);
+static int sysctl_write_setting(const char *setting, int flags);
+static int sysctl_preload_file(const char *filename, int flags);
+static int sysctl_display_all(const char *path, int flags);
 
 /*
  *    Globals...
@@ -30,6 +30,10 @@ static int sysctl_display_all(const char
 static const char ETC_SYSCTL_CONF[] ALIGN1 = "/etc/sysctl.conf";
 static const char PROC_SYS[] ALIGN1 = "/proc/sys/";
 enum { strlen_PROC_SYS = sizeof(PROC_SYS) - 1 };
+enum {
+	FLAG_TABLE_FORMAT	= 0x1,
+	FLAG_SHOW_KEYS		= 0x2,
+};
 
 /* error messages */
 static const char ERR_UNKNOWN_PARAMETER[] ALIGN1 =
@@ -64,7 +68,7 @@ int sysctl_main(int argc, char **argv) M
 int sysctl_main(int argc, char **argv)
 {
 	int retval = 0;
-	int output = 1;
+	int flags = FLAG_SHOW_KEYS;
 	int write_mode = 0;
 	int switches_allowed = 1;
 
@@ -77,7 +81,7 @@ int sysctl_main(int argc, char **argv)
 		if (switches_allowed && **argv == '-') {	/* we have a switch */
 			switch ((*argv)[1]) {
 			case 'n':
-				output = 0;
+				flags &= ~FLAG_SHOW_KEYS;
 				break;
 			case 'w':
 				write_mode = 1;
@@ -86,11 +90,12 @@ int sysctl_main(int argc, char **argv)
 			case 'p':
 				argv++;
 				return sysctl_preload_file(((*argv /*&& **argv*/) ? *argv : ETC_SYSCTL_CONF),
-							output);
-			case 'a':
+							flags);
 			case 'A':
-				return sysctl_display_all(PROC_SYS, output,
-							((*argv)[1] == 'A'));
+				flags |= FLAG_TABLE_FORMAT;
+				/* fall through */
+			case 'a':
+				return sysctl_display_all(PROC_SYS, flags);
 			default:
 				bb_error_msg(ERR_UNKNOWN_PARAMETER, *argv);
 				/* fall through */
@@ -101,9 +106,9 @@ int sysctl_main(int argc, char **argv)
 		} else {
 			switches_allowed = 0;
 			if (write_mode)
-				retval |= sysctl_write_setting(*argv, output);
+				retval |= sysctl_write_setting(*argv, flags);
 			else
-				sysctl_read_setting(*argv, output);
+				sysctl_read_setting(*argv, flags);
 		}
 	}
 	return retval;
@@ -116,7 +121,7 @@ int sysctl_main(int argc, char **argv)
  */
 #define PRELOAD_BUF 256
 
-static int sysctl_preload_file(const char *filename, int output)
+static int sysctl_preload_file(const char *filename, int flags)
 {
 	int lineno;
 	char oneline[PRELOAD_BUF];
@@ -163,7 +168,7 @@ static int sysctl_preload_file(const cha
 
 		/* safe because sizeof(oneline) == sizeof(buffer) */
 		sprintf(buffer, "%s=%s", name, value);
-		sysctl_write_setting(buffer, output);
+		sysctl_write_setting(buffer, flags);
 	}
 	fclose(fp);
 	return 0;
@@ -172,7 +177,7 @@ static int sysctl_preload_file(const cha
 /*
  *     Write a single sysctl setting
  */
-static int sysctl_write_setting(const char *setting, int output)
+static int sysctl_write_setting(const char *setting, int flags)
 {
 	int retval = 0;
 	const char *name;
@@ -220,7 +225,7 @@ static int sysctl_write_setting(const ch
 	} else {
 		dwrite_str(fd, value);
 		close(fd);
-		if (output) {
+		if (flags & FLAG_SHOW_KEYS) {
 			dwrite_str(STDOUT_FILENO, outname);
 			dwrite_str(STDOUT_FILENO, " = ");
 		}
@@ -236,7 +241,7 @@ static int sysctl_write_setting(const ch
 /*
  *     Read a sysctl setting
  */
-static int sysctl_read_setting(const char *setting, int output)
+static int sysctl_read_setting(const char *setting, int flags)
 {
 	int retval = 0;
 	char *tmpname, *outname, *cptr;
@@ -272,7 +277,7 @@ static int sysctl_read_setting(const cha
 		retval = -1;
 	} else {
 		while (fgets(inbuf, sizeof(inbuf) - 1, fp)) {
-			if (output) {
+			if (flags & FLAG_SHOW_KEYS) {
 				dwrite_str(STDOUT_FILENO, outname);
 				dwrite_str(STDOUT_FILENO, " = ");
 			}
@@ -289,7 +294,7 @@ static int sysctl_read_setting(const cha
 /*
  *     Display all the sysctl settings
  */
-static int sysctl_display_all(const char *path, int output, int show_table)
+static int sysctl_display_all(const char *path, int flags)
 {
 	int retval = 0;
 	DIR *dp;
@@ -308,9 +313,10 @@ static int sysctl_display_all(const char
 		if (stat(tmpdir, &ts) != 0) {
 			bb_perror_msg(tmpdir);
 		} else if (S_ISDIR(ts.st_mode)) {
-			sysctl_display_all(tmpdir, output, show_table);
+			sysctl_display_all(tmpdir, flags);
 		} else {
-			retval |= sysctl_read_setting(tmpdir + strlen_PROC_SYS, output);
+			retval |= sysctl_read_setting(tmpdir + strlen_PROC_SYS,
+					flags);
 		}
 		free(tmpdir);
 	} /* end while */



More information about the busybox mailing list