Browse Source

Use common symbols to avoid dragging in getopt.o unless a getopt-using

command is linked in.
tags/v0.9.3
Michael Brown 18 years ago
parent
commit
70d20c4e7a
3 changed files with 26 additions and 19 deletions
  1. 4
    0
      src/core/exec.c
  2. 5
    18
      src/core/getopt.c
  3. 17
    1
      src/include/getopt.h

+ 4
- 0
src/core/exec.c View File

@@ -36,6 +36,10 @@
36 36
 static struct command commands[0] __table_start ( commands );
37 37
 static struct command commands_end[0] __table_end ( commands );
38 38
 
39
+/* Avoid dragging in getopt.o unless a command really uses it */
40
+int optind;
41
+int nextchar;
42
+
39 43
 /**
40 44
  * Execute command
41 45
  *

+ 5
- 18
src/core/getopt.c View File

@@ -41,14 +41,14 @@ char *optarg;
41 41
  * This is an index into the argv[] array.  When getopt() returns -1,
42 42
  * @c optind is the index to the first element that is not an option.
43 43
  */
44
-int optind = 1;
44
+int optind;
45 45
 
46 46
 /**
47 47
  * Current option character index
48 48
  *
49 49
  * This is an index into the current element of argv[].
50 50
  */
51
-static int nextchar = 0;
51
+int nextchar;
52 52
 
53 53
 /**
54 54
  * Unrecognised option
@@ -58,22 +58,6 @@ static int nextchar = 0;
58 58
  */
59 59
 int optopt;
60 60
 
61
-/**
62
- * Reset getopt() internal state
63
- *
64
- * Due to a limitation of the POSIX getopt() API, it is necessary to
65
- * add a call to reset_getopt() before each set of calls to getopt()
66
- * or getopt_long().  This arises because POSIX assumes that each
67
- * process will parse command line arguments no more than once; this
68
- * assumption is not valid within Etherboot.  We work around the
69
- * limitation by arranging for execv() to call reset_getopt() before
70
- * executing the command.
71
- */
72
-void reset_getopt ( void ) {
73
-	optind = 1;
74
-	nextchar = 0;
75
-}
76
-
77 61
 /**
78 62
  * Get option argument from argv[] array
79 63
  *
@@ -231,6 +215,9 @@ static int match_short_option ( int argc, char * const argv[],
231 215
  * @ret longindex	Index of long option (or NULL)
232 216
  * @ret option		Option found, or -1 for no more options
233 217
  *
218
+ * Note that the caller must arrange for reset_getopt() to be called
219
+ * before each set of calls to getopt_long().  In Etherboot, this is
220
+ * done automatically by execv().
234 221
  */
235 222
 int getopt_long ( int argc, char * const argv[], const char *optstring,
236 223
 		  const struct option *longopts, int *longindex ) {

+ 17
- 1
src/include/getopt.h View File

@@ -49,9 +49,9 @@ struct option {
49 49
 
50 50
 extern char *optarg;
51 51
 extern int optind;
52
+extern int nextchar;
52 53
 extern int optopt;
53 54
 
54
-extern void reset_getopt();
55 55
 extern int getopt_long ( int argc, char * const argv[], const char *optstring,
56 56
 			 const struct option *longopts, int *longindex );
57 57
 
@@ -73,4 +73,20 @@ static inline int getopt ( int argc, char * const argv[],
73 73
 	return getopt_long ( argc, argv, optstring, no_options, NULL );
74 74
 }
75 75
 
76
+/**
77
+ * Reset getopt() internal state
78
+ *
79
+ * Due to a limitation of the POSIX getopt() API, it is necessary to
80
+ * add a call to reset_getopt() before each set of calls to getopt()
81
+ * or getopt_long().  This arises because POSIX assumes that each
82
+ * process will parse command line arguments no more than once; this
83
+ * assumption is not valid within Etherboot.  We work around the
84
+ * limitation by arranging for execv() to call reset_getopt() before
85
+ * executing the command.
86
+ */
87
+static inline void reset_getopt ( void ) {
88
+	optind = 1;
89
+	nextchar = 0;
90
+}
91
+
76 92
 #endif /* _GETOPT_H */

Loading…
Cancel
Save