Date: Sun, 22 Jun 2014 20:13:58 +0000 (UTC) From: "Pedro F. Giffuni" <pfg@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r267745 - head/lib/libc/stdlib Message-ID: <201406222013.s5MKDwHw045376@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: pfg Date: Sun Jun 22 20:13:57 2014 New Revision: 267745 URL: http://svnweb.freebsd.org/changeset/base/267745 Log: getopt(3): recognize option:: as GNU extension for "optional options". Also ANSIfy a function declaration. While here update the OpenBSD patch level in getopt_long.c as we already have the corresponding change. Obtained from: NetBSD MFC after: 2 weeks Modified: head/lib/libc/stdlib/getopt.3 head/lib/libc/stdlib/getopt.c head/lib/libc/stdlib/getopt_long.c Modified: head/lib/libc/stdlib/getopt.3 ============================================================================== --- head/lib/libc/stdlib/getopt.3 Sun Jun 22 19:10:55 2014 (r267744) +++ head/lib/libc/stdlib/getopt.3 Sun Jun 22 20:13:57 2014 (r267745) @@ -1,4 +1,4 @@ -.\" $NetBSD: getopt.3,v 1.31 2003/09/23 10:26:54 wiz Exp $ +.\" $NetBSD: getopt.3,v 1.34 2014/06/05 22:09:50 wiz Exp $ .\" .\" Copyright (c) 1988, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -30,7 +30,7 @@ .\" @(#)getopt.3 8.5 (Berkeley) 4/27/95 .\" $FreeBSD$ .\" -.Dd April 27, 1995 +.Dd June 5, 2014 .Dt GETOPT 3 .Os .Sh NAME @@ -65,6 +65,17 @@ The option string may contain the following elements: individual characters, and characters followed by a colon to indicate an option argument is to follow. +If an individual character is followed by two colons, then the +option argument is optional; +.Va optarg +is set to the rest of the current +.Va argv +word, or +.Dv NULL +if there were no more characters in the current word. +This is a +.Nx +extension. For example, an option string .Li \&"x" recognizes an option Modified: head/lib/libc/stdlib/getopt.c ============================================================================== --- head/lib/libc/stdlib/getopt.c Sun Jun 22 19:10:55 2014 (r267744) +++ head/lib/libc/stdlib/getopt.c Sun Jun 22 20:13:57 2014 (r267745) @@ -1,4 +1,4 @@ -/* $NetBSD: getopt.c,v 1.26 2003/08/07 16:43:40 agc Exp $ */ +/* $NetBSD: getopt.c,v 1.29 2014/06/05 22:00:22 christos Exp $ */ /* * Copyright (c) 1987, 1993, 1994 @@ -59,10 +59,7 @@ char *optarg; /* argument associated wi * Parse argc/argv argument vector. */ int -getopt(nargc, nargv, ostr) - int nargc; - char * const nargv[]; - const char *ostr; +getopt(int nargc, char * const nargv[], const char *ostr) { static char *place = EMSG; /* option letter processing */ char *oli; /* option letter list index */ @@ -115,6 +112,12 @@ getopt(nargc, nargv, ostr) entire next argument. */ if (*place) optarg = place; + else if (oli[2] == ':') + /* + * GNU Extension, for optional arguments if the rest of + * the argument is empty, we return NULL + */ + optarg = NULL; else if (nargc > ++optind) optarg = nargv[optind]; else { Modified: head/lib/libc/stdlib/getopt_long.c ============================================================================== --- head/lib/libc/stdlib/getopt_long.c Sun Jun 22 19:10:55 2014 (r267744) +++ head/lib/libc/stdlib/getopt_long.c Sun Jun 22 20:13:57 2014 (r267745) @@ -1,4 +1,4 @@ -/* $OpenBSD: getopt_long.c,v 1.22 2006/10/04 21:29:04 jmc Exp $ */ +/* $OpenBSD: getopt_long.c,v 1.23 2007/10/31 12:34:57 chl Exp $ */ /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */ /*
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201406222013.s5MKDwHw045376>