extern already declared

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

extern already declared

Jan Stary
Whenever unistd.h declares getopt(3), it also declares
the extern optind and optarg, so files including unistd.h
don't need to declare those themselves, right?

        Jan

Index: games/fortune/strfile/strfile.c
===================================================================
RCS file: /cvs/src/games/fortune/strfile/strfile.c,v
retrieving revision 1.29
diff -u -p -r1.29 strfile.c
--- games/fortune/strfile/strfile.c 4 Jun 2017 13:39:25 -0000 1.29
+++ games/fortune/strfile/strfile.c 9 Feb 2020 16:23:42 -0000
@@ -252,8 +252,6 @@ main(int ac, char *av[])
 void
 getargs(int argc, char *argv[])
 {
- extern char *optarg;
- extern int optind;
  int ch;
 
  while ((ch = getopt(argc, argv, "c:hiorsx")) != -1) {
Index: games/hunt/hunt/hunt.c
===================================================================
RCS file: /cvs/src/games/hunt/hunt/hunt.c,v
retrieving revision 1.22
diff -u -p -r1.22 hunt.c
--- games/hunt/hunt/hunt.c 8 Apr 2017 22:50:41 -0000 1.22
+++ games/hunt/hunt/hunt.c 9 Feb 2020 16:23:42 -0000
@@ -85,8 +85,6 @@ int
 main(int ac, char **av)
 {
  int c;
- extern int optind;
- extern char *optarg;
  long enter_status;
  int option;
  struct servent *se;
Index: games/hunt/huntd/driver.c
===================================================================
RCS file: /cvs/src/games/hunt/huntd/driver.c,v
retrieving revision 1.29
diff -u -p -r1.29 driver.c
--- games/hunt/huntd/driver.c 21 Jan 2017 08:22:57 -0000 1.29
+++ games/hunt/huntd/driver.c 9 Feb 2020 16:23:42 -0000
@@ -80,8 +80,6 @@ main(int ac, char **av)
  static fd_set read_fds;
  static FLAG first = TRUE;
  static FLAG server = FALSE;
- extern int optind;
- extern char *optarg;
  extern char *__progname;
  int c;
  static struct timeval linger = { 0, 0 };
Index: games/robots/main.c
===================================================================
RCS file: /cvs/src/games/robots/main.c,v
retrieving revision 1.28
diff -u -p -r1.28 main.c
--- games/robots/main.c 28 Jun 2019 13:32:52 -0000 1.28
+++ games/robots/main.c 9 Feb 2020 16:23:42 -0000
@@ -56,7 +56,6 @@ main(int ac, char *av[])
  int score_err = 0; /* hold errno from score file open */
  int ch;
  int ret;
- extern int optind;
  char *home;
 #ifdef FANCY
  char *sp;
Index: regress/lib/libc/db/dbtest.c
===================================================================
RCS file: /cvs/src/regress/lib/libc/db/dbtest.c,v
retrieving revision 1.16
diff -u -p -r1.16 dbtest.c
--- regress/lib/libc/db/dbtest.c 27 Jul 2017 15:08:37 -0000 1.16
+++ regress/lib/libc/db/dbtest.c 9 Feb 2020 16:23:48 -0000
@@ -76,8 +76,6 @@ int XXlineno; /* Fast breakpoint for
 int
 main(int argc, char *argv[])
 {
- extern int optind;
- extern char *optarg;
  enum S command, state;
  DB *dbp;
  DBT data, key, keydata;
Index: regress/lib/libc/getaddrinfo/gaitest.c
===================================================================
RCS file: /cvs/src/regress/lib/libc/getaddrinfo/gaitest.c,v
retrieving revision 1.6
diff -u -p -r1.6 gaitest.c
--- regress/lib/libc/getaddrinfo/gaitest.c 9 Jun 2009 18:15:08 -0000 1.6
+++ regress/lib/libc/getaddrinfo/gaitest.c 9 Feb 2020 16:23:48 -0000
@@ -119,8 +119,6 @@ main(argc, argv)
  struct addrinfo *res;
  int error, i;
  char *p, *q;
- extern int optind;
- extern char *optarg;
  int c;
  char nbuf[10];
 
Index: regress/lib/libc/regex/main.c
===================================================================
RCS file: /cvs/src/regress/lib/libc/regex/main.c,v
retrieving revision 1.10
diff -u -p -r1.10 main.c
--- regress/lib/libc/regex/main.c 13 Jul 2016 06:17:11 -0000 1.10
+++ regress/lib/libc/regex/main.c 9 Feb 2020 16:23:48 -0000
@@ -41,8 +41,6 @@ main(int argc, char *argv[])
  int c;
  int errflg = 0;
  register int i;
- extern int optind;
- extern char *optarg;
 
  progname = argv[0];
 
Index: regress/lib/libutil/fmt_scaled/fmt_test.c
===================================================================
RCS file: /cvs/src/regress/lib/libutil/fmt_scaled/fmt_test.c,v
retrieving revision 1.15
diff -u -p -r1.15 fmt_test.c
--- regress/lib/libutil/fmt_scaled/fmt_test.c 16 Mar 2017 02:42:31 -0000 1.15
+++ regress/lib/libutil/fmt_scaled/fmt_test.c 9 Feb 2020 16:23:49 -0000
@@ -36,8 +36,6 @@ __dead static void usage(int stat)
 int
 main(int argc, char **argv)
 {
- extern char *optarg;
- extern int optind;
  int i, ch;
 
  while ((ch = getopt(argc, argv, "hv")) != -1) {

Reply | Threaded
Open this post in threaded view
|

Re: extern already declared

Todd C. Miller-3
On Sun, 09 Feb 2020 17:46:51 +0100, Jan Stary wrote:

> Whenever unistd.h declares getopt(3), it also declares
> the extern optind and optarg, so files including unistd.h
> don't need to declare those themselves, right?

Correct.  Most of those date back from before optind and optarg
were defined for you by unistd.h.

 - todd

Reply | Threaded
Open this post in threaded view
|

Re: extern already declared

Jan Stary
On Feb 09 09:49:35, [hidden email] wrote:
> On Sun, 09 Feb 2020 17:46:51 +0100, Jan Stary wrote:
>
> > Whenever unistd.h declares getopt(3), it also declares
> > the extern optind and optarg, so files including unistd.h
> > don't need to declare those themselves, right?
>
> Correct.  Most of those date back from before optind and optarg
> were defined for you by unistd.h.

But the externs should be mentioned in the manpage, right?
That's the only place they are mentioned before being talked about
in the text; although I suspect that's why people keep declaring them
in source code - it's what SYNOPSIS said.

        Jan

Reply | Threaded
Open this post in threaded view
|

Re: extern already declared

Todd C. Miller-3
On Sun, 09 Feb 2020 19:05:28 +0100, Jan Stary wrote:

> But the externs should be mentioned in the manpage, right?
> That's the only place they are mentioned before being talked about
> in the text; although I suspect that's why people keep declaring them
> in source code - it's what SYNOPSIS said.

I don't think so, you don't copy the contents of the SYNOPSIS to
your program.  The reason we have code that declares these itself
is that is what you needed to do in 4.3BSD (and probably System V
where the API originated).

 - todd

Reply | Threaded
Open this post in threaded view
|

Re: extern already declared

Jan Stary
In reply to this post by Todd C. Miller-3
On Feb 09 17:46:51, [hidden email] wrote:

> Whenever unistd.h declares getopt(3), it also declares
> the extern optind and optarg, so files including unistd.h
> don't need to declare those themselves, right?
>
> Jan
>
> Index: games/fortune/strfile/strfile.c
> ===================================================================
> RCS file: /cvs/src/games/fortune/strfile/strfile.c,v
> retrieving revision 1.29
> diff -u -p -r1.29 strfile.c
> --- games/fortune/strfile/strfile.c 4 Jun 2017 13:39:25 -0000 1.29
> +++ games/fortune/strfile/strfile.c 9 Feb 2020 16:23:42 -0000
> @@ -252,8 +252,6 @@ main(int ac, char *av[])
>  void
>  getargs(int argc, char *argv[])
>  {
> - extern char *optarg;
> - extern int optind;
>   int ch;
>  
>   while ((ch = getopt(argc, argv, "c:hiorsx")) != -1) {
> Index: games/hunt/hunt/hunt.c
> ===================================================================
> RCS file: /cvs/src/games/hunt/hunt/hunt.c,v
> retrieving revision 1.22
> diff -u -p -r1.22 hunt.c
> --- games/hunt/hunt/hunt.c 8 Apr 2017 22:50:41 -0000 1.22
> +++ games/hunt/hunt/hunt.c 9 Feb 2020 16:23:42 -0000
> @@ -85,8 +85,6 @@ int
>  main(int ac, char **av)
>  {
>   int c;
> - extern int optind;
> - extern char *optarg;
>   long enter_status;
>   int option;
>   struct servent *se;
> Index: games/hunt/huntd/driver.c
> ===================================================================
> RCS file: /cvs/src/games/hunt/huntd/driver.c,v
> retrieving revision 1.29
> diff -u -p -r1.29 driver.c
> --- games/hunt/huntd/driver.c 21 Jan 2017 08:22:57 -0000 1.29
> +++ games/hunt/huntd/driver.c 9 Feb 2020 16:23:42 -0000
> @@ -80,8 +80,6 @@ main(int ac, char **av)
>   static fd_set read_fds;
>   static FLAG first = TRUE;
>   static FLAG server = FALSE;
> - extern int optind;
> - extern char *optarg;
>   extern char *__progname;
>   int c;
>   static struct timeval linger = { 0, 0 };
> Index: games/robots/main.c
> ===================================================================
> RCS file: /cvs/src/games/robots/main.c,v
> retrieving revision 1.28
> diff -u -p -r1.28 main.c
> --- games/robots/main.c 28 Jun 2019 13:32:52 -0000 1.28
> +++ games/robots/main.c 9 Feb 2020 16:23:42 -0000
> @@ -56,7 +56,6 @@ main(int ac, char *av[])
>   int score_err = 0; /* hold errno from score file open */
>   int ch;
>   int ret;
> - extern int optind;
>   char *home;
>  #ifdef FANCY
>   char *sp;
> Index: regress/lib/libc/db/dbtest.c
> ===================================================================
> RCS file: /cvs/src/regress/lib/libc/db/dbtest.c,v
> retrieving revision 1.16
> diff -u -p -r1.16 dbtest.c
> --- regress/lib/libc/db/dbtest.c 27 Jul 2017 15:08:37 -0000 1.16
> +++ regress/lib/libc/db/dbtest.c 9 Feb 2020 16:23:48 -0000
> @@ -76,8 +76,6 @@ int XXlineno; /* Fast breakpoint for
>  int
>  main(int argc, char *argv[])
>  {
> - extern int optind;
> - extern char *optarg;
>   enum S command, state;
>   DB *dbp;
>   DBT data, key, keydata;
> Index: regress/lib/libc/getaddrinfo/gaitest.c
> ===================================================================
> RCS file: /cvs/src/regress/lib/libc/getaddrinfo/gaitest.c,v
> retrieving revision 1.6
> diff -u -p -r1.6 gaitest.c
> --- regress/lib/libc/getaddrinfo/gaitest.c 9 Jun 2009 18:15:08 -0000 1.6
> +++ regress/lib/libc/getaddrinfo/gaitest.c 9 Feb 2020 16:23:48 -0000
> @@ -119,8 +119,6 @@ main(argc, argv)
>   struct addrinfo *res;
>   int error, i;
>   char *p, *q;
> - extern int optind;
> - extern char *optarg;
>   int c;
>   char nbuf[10];
>  
> Index: regress/lib/libc/regex/main.c
> ===================================================================
> RCS file: /cvs/src/regress/lib/libc/regex/main.c,v
> retrieving revision 1.10
> diff -u -p -r1.10 main.c
> --- regress/lib/libc/regex/main.c 13 Jul 2016 06:17:11 -0000 1.10
> +++ regress/lib/libc/regex/main.c 9 Feb 2020 16:23:48 -0000
> @@ -41,8 +41,6 @@ main(int argc, char *argv[])
>   int c;
>   int errflg = 0;
>   register int i;
> - extern int optind;
> - extern char *optarg;
>  
>   progname = argv[0];
>  
> Index: regress/lib/libutil/fmt_scaled/fmt_test.c
> ===================================================================
> RCS file: /cvs/src/regress/lib/libutil/fmt_scaled/fmt_test.c,v
> retrieving revision 1.15
> diff -u -p -r1.15 fmt_test.c
> --- regress/lib/libutil/fmt_scaled/fmt_test.c 16 Mar 2017 02:42:31 -0000 1.15
> +++ regress/lib/libutil/fmt_scaled/fmt_test.c 9 Feb 2020 16:23:49 -0000
> @@ -36,8 +36,6 @@ __dead static void usage(int stat)
>  int
>  main(int argc, char **argv)
>  {
> - extern char *optarg;
> - extern int optind;
>   int i, ch;
>  
>   while ((ch = getopt(argc, argv, "hv")) != -1) {
>
>

On Feb 09 09:49:35, [hidden email] wrote:
> On Sun, 09 Feb 2020 17:46:51 +0100, Jan Stary wrote:
>
> > Whenever unistd.h declares getopt(3), it also declares
> > the extern optind and optarg, so files including unistd.h
> > don't need to declare those themselves, right?
>
> Correct.  Most of those date back from before optind and optarg
> were defined for you by unistd.h.

So can they be removed now?

Reply | Threaded
Open this post in threaded view
|

Re: extern already declared

Todd C. Miller-3
On Thu, 13 Feb 2020 23:53:49 +0100, Jan Stary wrote:

> On Feb 09 09:49:35, [hidden email] wrote:
> > On Sun, 09 Feb 2020 17:46:51 +0100, Jan Stary wrote:
> >
> > > Whenever unistd.h declares getopt(3), it also declares
> > > the extern optind and optarg, so files including unistd.h
> > > don't need to declare those themselves, right?
> >
> > Correct.  Most of those date back from before optind and optarg
> > were defined for you by unistd.h.
>
> So can they be removed now?

Yes, they can be removed.

 - todd

Reply | Threaded
Open this post in threaded view
|

Re: extern already declared

Ingo Schwarze
In reply to this post by Todd C. Miller-3
Hi,

Todd C. Miller wrote on Sun, Feb 09, 2020 at 09:49:35AM -0700:
> On Sun, 09 Feb 2020 17:46:51 +0100, Jan Stary wrote:
 
>> Whenever unistd.h declares getopt(3), it also declares
>> the extern optind and optarg, so files including unistd.h
>> don't need to declare those themselves, right?

> Correct.  Most of those date back from before optind and optarg
> were defined for you by unistd.h.

Committed, thanks.
  Ingo