Cleaned up getopt

Getopt is provided as an alternative to unistd for windows support -
just needed a little bi of cleaning up since last time I used this code.
master
WHPThomas 2013-04-17 22:24:05 +10:00
parent cc5b5aa561
commit 81701abcc0
4 changed files with 24 additions and 98 deletions

63
getopt.c Executable file → Normal file
View File

@ -9,7 +9,7 @@ UNIFORUM conference in Dallas. I obtained it by electronic mail
directly from AT&T. The people there assure me that it is indeed
in the public domain.
25/1/2005 Henry Thomas <me(at)henri.net>
25/1/2005 Henry Thomas <me(at)henri(dot)net>
Ported this original AT&T version of 'getopt' to windows.
@ -19,8 +19,7 @@ unix/linux platforms.
*/
#include "321/getopt.h"
#ifndef HAS_GETOPT_H
#include "getopt.h"
#include <string.h>
#if defined(_MSC_VER)
@ -29,30 +28,6 @@ unix/linux platforms.
#include <stdio.h>
#include <stdlib.h>
#ifdef HAS_WIN_GUI
/* The windows gui version reports command line errors using MessageBox */
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define ERR(s, c) if(opterr) {\
char buffer[64];\
_snprintf(buffer, 64, "%s%s%c", argv[0], s, c);\
MessageBox(NULL, buffer, "Command Line Error", MB_OK | MB_ICONERROR);\
}
#else
#define ERR(s, c) if(opterr) {\
char errbuf[2];\
errbuf[0] = c; errbuf[1] = '\n';\
(void) write(2, argv[0], (unsigned)strlen(argv[0]));\
(void) write(2, s, (unsigned)strlen(s));\
(void) write(2, errbuf, 2);\
}
#endif
int opterr = 1;
int optind = 1;
int optopt;
@ -76,7 +51,7 @@ int getopt(int argc, char **argv, char *opts)
}
optopt = c = argv[optind][sp];
if(c == ':' || (cp = strchr(opts, c)) == NULL) {
ERR(": illegal option -- ", c);
fprintf(stderr, "Command line error: illegal option -- %c\r\n", c);
if(argv[optind][++sp] == '\0') {
optind++;
sp = 1;
@ -88,7 +63,7 @@ int getopt(int argc, char **argv, char *opts)
optarg = &argv[optind++][sp+1];
}
else if(++optind >= argc) {
ERR(": option requires an argument -- ", c);
fprintf(stderr, "Command line error: option requires an argument -- %c\r\n", c);
sp = 1;
return('?');
}
@ -106,33 +81,3 @@ int getopt(int argc, char **argv, char *opts)
}
return(c);
}
#ifdef HAS_WIN_GUI
char **getargv(char *cl, int *argc)
{
static char *argv[MAX_ARGC];
int i;
for(i = 0; i < MAX_ARGC; i++) {
argv[i] = cl;
LOOP:
cl++;
if(cl[0] == ' ') {
while(cl[1] == ' ') cl++;
*cl = 0;
cl++;
}
else {
if(cl[0]) {
goto LOOP;
}
i++;
break;
}
}
*argc = i;
return argv;
}
#endif
#endif /* HAS_GETOPT_H */

47
getopt.h Executable file → Normal file
View File

@ -9,7 +9,7 @@ UNIFORUM conference in Dallas. I obtained it by electronic mail
directly from AT&T. The people there assure me that it is indeed
in the public domain.
25/1/2005 Henry Thomas <me(at)henri.net>
25/1/2005 Henry Thomas <me(at)henri(dot)net>
Ported this original AT&T version of 'getopt' to windows.
@ -17,19 +17,14 @@ Added documentation to this header, so know what everything does.
*/
#ifndef GETOPT_H_
#define GETOPT_H_
#ifdef HAS_GETOPT_H
# include <getopt.h>
#else
#ifndef getopt_h
#define getopt_h
#ifdef __cplusplus
extern "C" {
#endif
extern int opterr;
/* If the value of the 'opterr' variable is nonzero, then getopt prints an
error message to the standard error stream if it encounters an unknown option
character or an option with a missing required argument. The windows version
@ -37,25 +32,29 @@ reports the error using a message box. This is the default behavior. If you
set this variable to zero, getopt does not print any messages, but it still
returns the character ? to indicate an error. */
extern int optopt;
extern int opterr;
/* When getopt encounters an unknown option character or an option with a
missing required argument, it stores that option character the 'optopt'
variable. You can use this for providing your own diagnostic messages. */
extern int optind;
extern int optopt;
/* The 'optind' variable is set by getopt to the index of the next element of
the argv array to be processed. Once getopt has found all of the option
arguments, you can use this variable to determine where the remaining
non-option arguments begin. The initial value of this variable is 1. */
extern char *optarg;
extern int optind;
/* The 'optarg' variable is set by getopt to point at the value of the
option argument, for those options that accept arguments. */
int getopt(int argc, char **argv, char *opts);
extern char *optarg;
/* The getopt function gets the next option argument from the argument list
specified by the argv and argc arguments. Normally these values come directly
@ -65,29 +64,11 @@ The options argument is a string that specifies the option characters that are
valid for this program. An option character in this string can be followed
by a colon (':') to indicate that it takes a required argument. */
#ifdef WIN32
int getopt(int argc, char **argv, char *opts);
#ifndef HAS_WIN_CLI
#define HAS_WIN_GUI 1
#endif
/* Define 'HAS_WIN_CLI' if you plan to use this version of getopt in a
windows console application */
#define MAX_ARGC 32
/* This is the maximum argument count that 'getargv' will return. */
char **getargv(char *cl, int *argc);
/* Parse a single command line sting and convert it into an argc/argv pair
compatible with getopt */
#endif
#ifdef __cplusplus
}
#endif
#endif /* HAS_GETOPT_H */
#endif /* GETOPT_H_ */
#endif

6
gpx.c
View File

@ -1,7 +1,7 @@
//
// gpx.c
//
// Created by WHPThomas on 1/04/13.
// Created by WHPThomas <me(at)henri(dot)net> on 1/04/13.
//
// Copyright (c) 2013 WHPThomas, All rights reserved.
//
@ -34,9 +34,9 @@
#include <strings.h>
#ifdef _WIN32
#include "getopt.h"
# include "getopt.h"
#else
#include <unistd.h>
# include <unistd.h>
#endif
#include "gpx.h"

6
gpx.h
View File

@ -1,7 +1,7 @@
//
// gpx.c
//
// Created by WHPThomas on 1/04/13.
// Created by WHPThomas <me(at)henri(dot)net> on 1/04/13.
//
// Copyright (c) 2013 WHPThomas, All rights reserved.
//
@ -24,8 +24,8 @@
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#ifndef gpx_gpx_h
#define gpx_gpx_h
#ifndef gpx_h
#define gpx_h
#include <limits.h>