Home | History | Annotate | Line # | Download | only in pax
getoldopt.c revision 1.17
      1  1.17  christos /*	$NetBSD: getoldopt.c,v 1.17 2003/06/23 13:15:15 christos Exp $	*/
      2   1.3       cgd 
      3   1.1       jtc /*
      4   1.1       jtc  * Plug-compatible replacement for getopt() for parsing tar-like
      5   1.1       jtc  * arguments.  If the first argument begins with "-", it uses getopt;
      6   1.1       jtc  * otherwise, it uses the old rules used by tar, dump, and ps.
      7   1.1       jtc  *
      8   1.1       jtc  * Written 25 August 1985 by John Gilmore (ihnp4!hoptoad!gnu) and placed
      9   1.4       jtc  * in the Public Domain for your edification and enjoyment.
     10   1.1       jtc  */
     11   1.1       jtc 
     12   1.5  christos #include <sys/cdefs.h>
     13  1.14        tv #if defined(__RCSID) && !defined(lint)
     14  1.17  christos __RCSID("$NetBSD: getoldopt.c,v 1.17 2003/06/23 13:15:15 christos Exp $");
     15   1.1       jtc #endif /* not lint */
     16   1.1       jtc 
     17  1.14        tv #include <getopt.h>
     18   1.1       jtc #include <stdio.h>
     19   1.1       jtc #include <string.h>
     20  1.15  christos #include <stdlib.h>
     21   1.2       cgd #include <unistd.h>
     22   1.5  christos #include <sys/stat.h>
     23   1.5  christos #include "pax.h"
     24   1.5  christos #include "extern.h"
     25   1.1       jtc 
     26   1.1       jtc int
     27  1.11     lukem getoldopt(int argc, char **argv, const char *optstring,
     28  1.12     lukem 	struct option *longopts, int *idx)
     29   1.1       jtc {
     30   1.1       jtc 	static char	*key;		/* Points to next keyletter */
     31  1.17  christos 	static char	argv1[64];
     32   1.8     itohy 
     33   1.1       jtc 	if (key == NULL) {		/* First time */
     34   1.6     lukem 		if (argc < 2) return -1;
     35   1.1       jtc 		key = argv[1];
     36  1.17  christos 		if (*key != '-')
     37  1.17  christos 			(void)snprintf(argv[1] = argv1, sizeof(argv1), "-%s",
     38  1.17  christos 			    key);
     39   1.1       jtc 	}
     40   1.1       jtc 
     41  1.17  christos 	if (longopts != NULL) {
     42  1.17  christos 		return getopt_long(argc, argv, optstring,
     43  1.17  christos 		    longopts, idx);
     44  1.17  christos 	} else {
     45  1.17  christos 		return getopt(argc, argv, optstring);
     46   1.1       jtc 	}
     47   1.1       jtc }
     48