Просмотр исходного кода

Patch from Jean-Yves Avenard to move the getopt globals to their
own file, since at least on SH, weak variables with initial values
(.data stuff) were not working. Moving these to their own file
seems to be a good way to handle it.

Eric Andersen 24 лет назад
Родитель
Сommit
d7839f55b4
4 измененных файлов с 60 добавлено и 38 удалено
  1. 1 1
      libc/unistd/Makefile
  2. 14 5
      libc/unistd/getopt.c
  3. 40 0
      libc/unistd/getopt_vars.c
  4. 5 32
      libc/unistd/gnu_getopt.c

+ 1 - 1
libc/unistd/Makefile

@@ -26,7 +26,7 @@ LIBC=$(TOPDIR)libc.a
 DIRS:=
 DIRS:=
 
 
 CSRC=execl.c execlp.c execv.c execvep.c execvp.c getcwd.c getopt.c \
 CSRC=execl.c execlp.c execv.c execvep.c execvp.c getcwd.c getopt.c \
-	sleep.c getpass.c sysconf_src.c
+	sleep.c getpass.c sysconf_src.c getopt_vars.c
 
 
 # TESTING -- comment this out if it breaks for you
 # TESTING -- comment this out if it breaks for you
 ifeq ($(TARGET_ARCH), $(NATIVE_ARCH))
 ifeq ($(TARGET_ARCH), $(NATIVE_ARCH))

+ 14 - 5
libc/unistd/getopt.c

@@ -21,10 +21,10 @@
 #include <stdio.h>
 #include <stdio.h>
 #include <string.h>
 #include <string.h>
 
 
-int opterr __attribute__ ((__weak__)) = 1; /* error => print message */
+extern int opterr;	/* error => print message */
-int optind __attribute__ ((__weak__)) = 1; /* next argv[] index */
+extern int optind;	/* next argv[] index */
-int optopt __attribute__ ((__weak__)) = 1; /* Set for unknown arguments */
+extern int optopt;	/* Set for unknown arguments */
-char *optarg __attribute__ ((__weak__)) = NULL;	/* option parameter if any */
+extern char *optarg;	/* option parameter if any */
 
 
 static int Err(name, mess, c) /* returns '?' */
 static int Err(name, mess, c) /* returns '?' */
 char *name;						/* program name argv[0] */
 char *name;						/* program name argv[0] */
@@ -54,7 +54,16 @@ int getopt (int argc, char *const *argv, const char *optstring)
 	register char *cp;			/* -> option in `optstring' */
 	register char *cp;			/* -> option in `optstring' */
 
 
 	optarg = NULL;
 	optarg = NULL;
-
+	
+	/* initialise getopt vars */
+	if (optind == 0)
+	  {
+	    optind = 1;
+	    opterr = 1;
+	    optopt = 1;
+	    optarg = NULL;
+	  }
+	  
 	if (sp == 1) {				/* fresh argument */
 	if (sp == 1) {				/* fresh argument */
 		if (optind >= argc		/* no more arguments */
 		if (optind >= argc		/* no more arguments */
 			|| argv[optind][0] != '-'	/* no more options */
 			|| argv[optind][0] != '-'	/* no more options */

+ 40 - 0
libc/unistd/getopt_vars.c

@@ -0,0 +1,40 @@
+#include <stdio.h>
+
+/*
+ * Getopt vars shared between getopt and gnu_getopt
+ */
+ 
+/* For communication from `getopt' to the caller.
+   When `getopt' finds an option that takes an argument,
+   the argument value is returned here.
+   Also, when `ordering' is RETURN_IN_ORDER,
+   each non-option ARGV-element is returned here.  */
+
+char *optarg = NULL;
+
+/* Index in ARGV of the next element to be scanned.
+   This is used for communication to and from the caller
+   and for communication between successive calls to `getopt'.
+
+   On entry to `getopt', zero means this is the first call; initialize.
+
+   When `getopt' returns EOF, this is the index of the first of the
+   non-option elements that the caller should itself scan.
+
+   Otherwise, `optind' communicates from one call to the next
+   how much of ARGV has been scanned so far.  */
+
+/* XXX 1003.2 says this must be 1 before any call.  */
+int optind = 0;
+
+
+/* Callers store zero here to inhibit the error message
+   for unrecognized options.  */
+
+int opterr = 1;
+
+/* Set to an option character which was unrecognized.
+   This must be initialized on some systems to avoid linking in the
+   system's own getopt implementation.  */
+
+int optopt = '?';

+ 5 - 32
libc/unistd/gnu_getopt.c

@@ -52,28 +52,12 @@ extern int _getopt_internal (int argc, char *const *argv,
 
 
 #ifdef L__gnu_getopt_internal
 #ifdef L__gnu_getopt_internal
 
 
-/* For communication from `getopt' to the caller.
+/* external getopt vars */
-   When `getopt' finds an option that takes an argument,
-   the argument value is returned here.
-   Also, when `ordering' is RETURN_IN_ORDER,
-   each non-option ARGV-element is returned here.  */
 
 
-char *optarg = NULL;
+extern int optind;
-
+extern int opterr;
-/* Index in ARGV of the next element to be scanned.
+extern int optopt;
-   This is used for communication to and from the caller
+extern char *optarg;
-   and for communication between successive calls to `getopt'.
-
-   On entry to `getopt', zero means this is the first call; initialize.
-
-   When `getopt' returns EOF, this is the index of the first of the
-   non-option elements that the caller should itself scan.
-
-   Otherwise, `optind' communicates from one call to the next
-   how much of ARGV has been scanned so far.  */
-
-/* XXX 1003.2 says this must be 1 before any call.  */
-int optind = 0;
 
 
 /* The next char to be scanned in the option-element
 /* The next char to be scanned in the option-element
    in which the last option character we returned was found.
    in which the last option character we returned was found.
@@ -84,17 +68,6 @@ int optind = 0;
 
 
 static char *nextchar;
 static char *nextchar;
 
 
-/* Callers store zero here to inhibit the error message
-   for unrecognized options.  */
-
-int opterr = 1;
-
-/* Set to an option character which was unrecognized.
-   This must be initialized on some systems to avoid linking in the
-   system's own getopt implementation.  */
-
-int optopt = '?';
-
 /* Describe how to deal with options that follow non-option ARGV-elements.
 /* Describe how to deal with options that follow non-option ARGV-elements.
 
 
    If the caller did not specify anything,
    If the caller did not specify anything,