Skip to contentSkip to conversation

conf.c: use return to exit from main()

PATCH In review Ladislav Michl 2 participants 3 messages Oct 31, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 conf.c: use return to exit from main()

Replace exit() with return and use standard exit values. Good for code size and readability, although I have to admit both has very little impact on real world... :-)

   text    data     bss     dec     hex filename
  60608    2084    5908   68600   10bf8 scripts/kconfig/conf_orig
  60528    2084    5908   68520   10ba8 scripts/kconfig/conf
scripts/kconfig/conf.c +11 -12
index 3e1057f..70f1f99 100644
@@ -70,7 +70,7 @@ static void check_stdin(void)
 		printf(_("aborted!\n\n"));
 		printf(_("Console input/output is redirected. "));
 		printf(_("Run 'make oldconfig' to update configuration.\n\n"));
-		exit(1);
+		exit(EXIT_FAILURE);
 	}
 }
 
@@ -469,16 +469,15 @@ int main(int ac, char **av)
 			break;
 		case 'h':
 			printf(_("See README for usage info\n"));
-			exit(0);
-			break;
+			return EXIT_SUCCESS;
 		default:
 			fprintf(stderr, _("See README for usage info\n"));
-			exit(1);
+			return EXIT_FAILURE;
 		}
 	}
 	if (ac == optind) {
 		printf(_("%s: Kconfig file missing\n"), av[0]);
-		exit(1);
+		return EXIT_FAILURE;
 	}
 	name = av[optind];
 	conf_parse(name);
@@ -492,7 +491,7 @@ int main(int ac, char **av)
 				"*** Please run some configurator (e.g. \"make oldconfig\" or\n"
 				"*** \"make menuconfig\" or \"make xconfig\").\n"
 				"***\n"));
-			exit(1);
+			return EXIT_FAILURE;
 		}
 	}
 
@@ -504,7 +503,7 @@ int main(int ac, char **av)
 			printf(_("***\n"
 				"*** Can't find default configuration \"%s\"!\n"
 				"***\n"), defconfig_file);
-			exit(1);
+			return EXIT_FAILURE;
 		}
 		break;
 	case ask_silent:
@@ -543,7 +542,7 @@ int main(int ac, char **av)
 			if (name && *name) {
 				fprintf(stderr,
 					_("\n*** Kernel configuration requires explicit update.\n\n"));
-				return 1;
+				return EXIT_FAILURE;
 			}
 		}
 		valid_stdin = isatty(0) && isatty(1) && isatty(2);
@@ -586,17 +585,17 @@ int main(int ac, char **av)
 		 */
 		if (conf_get_changed() && conf_write(NULL)) {
 			fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
-			exit(1);
+			return EXIT_FAILURE;
 		}
 		if (conf_write_autoconf()) {
 			fprintf(stderr, _("\n*** Error during update of the kernel configuration.\n\n"));
-			return 1;
+			return EXIT_FAILURE;
 		}
 	} else {
 		if (conf_write(NULL)) {
 			fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
-			exit(1);
+			return EXIT_FAILURE;
 		}
 	}
-	return 0;
+	return EXIT_SUCCESS;
 }
#1 lore raw
Quoted 7 lines · Ladislav Michl

Replace exit() with return and use standard exit values. Good for code size and readability, although I have to admit both has very little impact on real world... :-)

   text    data     bss     dec     hex filename
  60608    2084    5908   68600   10bf8 scripts/kconfig/conf_orig
  60528    2084    5908   68520   10ba8 scripts/kconfig/conf

replacing return is ok, but it is wrong to change the return values:

We are on POSIX (UNIX, Linux, etc), not on a very generic standard C implementation. The return value is important and it could be used by other scripts, which is not guarantee in generic standard C.

With your change, it is more difficult to add other errors and to test error values, because AFAIK the EXIT_FAILURE is not fix.

For these two reason, and because I don't think this program could be ported in a generic C platform, I think you should not uset EXIT_FAILURE.

ciao
cate

#2 lore raw
Quoted 6 lines · Giacomo A. Catenazzi

With your change, it is more difficult to add other errors and to test error values, because AFAIK the EXIT_FAILURE is not fix.

For these two reason, and because I don't think this program could be ported in a generic C platform, I think you should not uset EXIT_FAILURE.

Well, every POSIX platform I know about defines EXIT_FAILURE as 1. However POSIX itself requires EXIT_FAILURE to evaluate to a non-zero value so your statement is perfectly valid. Here it is:

Replace exit() with return.

scripts/kconfig/conf.c +7 -8
index 3e1057f..5bcea31 100644
@@ -469,16 +469,15 @@ int main(int ac, char **av)
 			break;
 		case 'h':
 			printf(_("See README for usage info\n"));
-			exit(0);
-			break;
+			return 0;
 		default:
 			fprintf(stderr, _("See README for usage info\n"));
-			exit(1);
+			return 1;
 		}
 	}
 	if (ac == optind) {
 		printf(_("%s: Kconfig file missing\n"), av[0]);
-		exit(1);
+		return 1;
 	}
 	name = av[optind];
 	conf_parse(name);
@@ -492,7 +491,7 @@ int main(int ac, char **av)
 				"*** Please run some configurator (e.g. \"make oldconfig\" or\n"
 				"*** \"make menuconfig\" or \"make xconfig\").\n"
 				"***\n"));
-			exit(1);
+			return 1;
 		}
 	}
 
@@ -504,7 +503,7 @@ int main(int ac, char **av)
 			printf(_("***\n"
 				"*** Can't find default configuration \"%s\"!\n"
 				"***\n"), defconfig_file);
-			exit(1);
+			return 1;
 		}
 		break;
 	case ask_silent:
@@ -586,7 +585,7 @@ int main(int ac, char **av)
 		 */
 		if (conf_get_changed() && conf_write(NULL)) {
 			fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
-			exit(1);
+			return 1;
 		}
 		if (conf_write_autoconf()) {
 			fprintf(stderr, _("\n*** Error during update of the kernel configuration.\n\n"));
@@ -595,7 +594,7 @@ int main(int ac, char **av)
 	} else {
 		if (conf_write(NULL)) {
 			fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
-			exit(1);
+			return 1;
 		}
 	}
 	return 0;