From af53bdb048c50577e2f648aa8af5cfed31d25aa1 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Tue, 17 Nov 2020 09:27:01 -0800 Subject: [PATCH] libc: Add opterr global variable defined here: https://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html Signed-off-by: Xiang Xiao --- include/unistd.h | 3 +++ libs/libc/libc.csv | 1 + libs/libc/unistd/Make.defs | 6 ++--- libs/libc/unistd/lib_getopt.c | 1 + libs/libc/unistd/lib_getopterrp.c | 45 +++++++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 libs/libc/unistd/lib_getopterrp.c diff --git a/include/unistd.h b/include/unistd.h index c4460ebb7f..627eb1a7b4 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -296,10 +296,12 @@ extern "C" #ifndef __NXFLAT__ EXTERN FAR char *optarg; /* Optional argument following option */ +EXTERN int opterr; /* Print error message */ EXTERN int optind; /* Index into argv */ EXTERN int optopt; /* Unrecognized option character */ #else # define optarg (*(getoptargp())) +# define opterr (*(getopterrp())) # define optind (*(getoptindp())) # define optopt (*(getoptoptp())) #endif @@ -389,6 +391,7 @@ int getopt(int argc, FAR char * const argv[], FAR const char *optstring); */ FAR char **getoptargp(void); /* Optional argument following option */ +FAR int *getopterrp(void); /* Print error message */ FAR int *getoptindp(void); /* Index into argv */ FAR int *getoptoptp(void); /* Unrecognized option character */ diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv index de0d74860a..a5395ed6fa 100644 --- a/libs/libc/libc.csv +++ b/libs/libc/libc.csv @@ -55,6 +55,7 @@ "gethostname","unistd.h","","int","FAR char *","size_t" "getopt","unistd.h","","int","int","FAR char * const []|FAR char * const *","FAR const char *" "getoptargp","unistd.h","","FAR char **" +"getopterrp","unistd.h","","FAR int *" "getoptindp","unistd.h","","FAR int *" "getoptoptp","unistd.h","","FAR int *" "gets","stdio.h","defined(CONFIG_FILE_STREAM)","FAR char *","FAR char *" diff --git a/libs/libc/unistd/Make.defs b/libs/libc/unistd/Make.defs index 40410dbd81..3955fe87e5 100644 --- a/libs/libc/unistd/Make.defs +++ b/libs/libc/unistd/Make.defs @@ -36,9 +36,9 @@ # Add the unistd C files to the build CSRCS += lib_access.c lib_daemon.c lib_swab.c lib_pathconf.c lib_sysconf.c -CSRCS += lib_getopt.c lib_getoptargp.c lib_getoptindp.c lib_getoptoptp.c -CSRCS += lib_alarm.c lib_fstatvfs.c lib_statvfs.c lib_sleep.c lib_usleep.c -CSRCS += lib_seteuid.c lib_setegid.c lib_geteuid.c lib_getegid.c +CSRCS += lib_getopt.c lib_getoptargp.c lib_getopterrp.c lib_getoptindp.c +CSRCS += lib_getoptoptp.c lib_alarm.c lib_fstatvfs.c lib_statvfs.c lib_sleep.c +CSRCS += lib_usleep.c lib_seteuid.c lib_setegid.c lib_geteuid.c lib_getegid.c CSRCS += lib_setreuid.c lib_setregid.c CSRCS += lib_getrusage.c lib_utimes.c CSRCS += lib_setrlimit.c lib_getrlimit.c diff --git a/libs/libc/unistd/lib_getopt.c b/libs/libc/unistd/lib_getopt.c index 9450a573f4..56e1afe7aa 100644 --- a/libs/libc/unistd/lib_getopt.c +++ b/libs/libc/unistd/lib_getopt.c @@ -52,6 +52,7 @@ ****************************************************************************/ FAR char *optarg; /* Optional argument following option */ +int opterr = 0; /* Print error message */ int optind = 1; /* Index into argv */ int optopt = '?'; /* unrecognized option character */ diff --git a/libs/libc/unistd/lib_getopterrp.c b/libs/libc/unistd/lib_getopterrp.c new file mode 100644 index 0000000000..333e82b131 --- /dev/null +++ b/libs/libc/unistd/lib_getopterrp.c @@ -0,0 +1,45 @@ +/**************************************************************************** + * libs/libc/unistd/lib_getopterrp.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: getopterrp + * + * Description: + * Returns a pointer to opterr. This function is only used for external + * modules that need to access the base, global variable, opterr. + * + ****************************************************************************/ + +FAR int *getopterrp(void) +{ + return &opterr; +}