delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2001/05/28/13:05:32

From: "Mark E." <snowball3 AT bigfoot DOT com>
To: djgpp-workers AT delorie DOT com
Date: Mon, 28 May 2001 13:05:08 -0400
MIME-Version: 1.0
Subject: getopt fix
Message-ID: <3B124D04.9747.794512@localhost>
X-mailer: Pegasus Mail for Win32 (v3.12c)
Reply-To: djgpp-workers AT delorie DOT com

getopt currently doesn't follow the posix standard because it returns '?' 
when a missing argument is encountered instead of ':'. The patch below fixes 
this:

Index: getopt.c
===================================================================
RCS file: /cvs/djgpp/djgpp/src/libc/posix/unistd/getopt.c,v
retrieving revision 1.1
diff -c -p -r1.1 getopt.c
*** getopt.c	1995/06/13 04:47:58	1.1
--- getopt.c	2001/05/28 17:01:43
***************
*** 1,3 ****
--- 1,4 ----
+ /* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */
  /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  #include <libc/stubs.h>
  #include <stdio.h>
***************
*** 8,14 ****
  int opterr = 1,	optind = 1, optopt = 0;
  char *optarg = 0;
  
! #define	BADCH	(int)'?'
  #define	EMSG	""
  
  int
--- 9,16 ----
  int opterr = 1,	optind = 1, optopt = 0;
  char *optarg = 0;
  
! #define	BADOPT	(int)'?'
! #define	BADARG	(int)':'
  #define	EMSG	""
  
  int
*************** getopt(int nargc, char *const nargv[], c
*** 52,58 ****
  	++p;
        fprintf(stderr, "%s: illegal option -- %c\n", p, optopt);
      }
!     return BADCH;
    }
    if (*++oli != ':')
    {		/* don't need argument */
--- 54,60 ----
  	++p;
        fprintf(stderr, "%s: illegal option -- %c\n", p, optopt);
      }
!     return BADOPT;
    }
    if (*++oli != ':')
    {		/* don't need argument */
*************** getopt(int nargc, char *const nargv[], c
*** 74,80 ****
        if (opterr)
  	fprintf(stderr, "%s: option requires an argument -- %c\n",
  		p, optopt);
!       return BADCH;
      }
      else			/* white space */
        optarg = nargv[optind];
--- 76,82 ----
        if (opterr)
  	fprintf(stderr, "%s: option requires an argument -- %c\n",
  		p, optopt);
!       return BADARG;
      }
      else			/* white space */
        optarg = nargv[optind];

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019