File indexing completed on 2018-03-02 18:44:47 UTC
view on githubraw file Latest commit ec6cf3b0 on 2003-08-26 20:45:25 UTC
ec6cf3b09d Ed H*0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 #include <stdio.h>
0026
0027 #ifdef __OS2__
0028 # include <io.h>
0029 #endif
0030
0031 #if defined(__MSDOS__) || defined(__OS2__)
0032 #define ERR(s, c) \
0033 if (opterr) { \
0034 char buff[3]; \
0035 buff[0] = c; buff[1] = '\r'; buff[2] = '\n'; \
0036 (void)write(2, av[0], strlen(av[0])); \
0037 (void)write(2, s, strlen(s)); \
0038 (void)write(2, buff, 3); \
0039 }
0040 #else
0041 #define ERR(s, c) \
0042 if (opterr) { \
0043 char buff[2]; \
0044 buff[0] = c; buff[1] = '\n'; \
0045 (void)write(2, av[0], strlen(av[0])); \
0046 (void)write(2, s, strlen(s)); \
0047 (void)write(2, buff, 2); \
0048 }
0049 #endif
0050
0051 int opterr = 1;
0052 int optind = 1;
0053 int optopt;
0054 char *optarg;
0055
0056
0057
0058
0059
0060
0061
0062 int
0063 getopt(int ac, char **av, char *opts)
0064 {
0065 extern char *strchr(const char *, int);
0066 static int i = 1;
0067 char *p;
0068
0069
0070 if (i == 1) {
0071 if (optind >= ac ||
0072 #if defined(__MSDOS__) || defined(__OS2__)
0073 (av[optind][0] != '-' && av[optind][0] != '/')
0074 #else
0075 av[optind][0] != '-'
0076 #endif
0077 || av[optind][1] == '\0')
0078 return EOF;
0079 if (strcmp(av[optind], "--") == 0) {
0080 optind++;
0081 return EOF;
0082 }
0083 }
0084
0085
0086 if ((optopt = av[optind][i]) == ':'
0087 || (p = strchr(opts, optopt)) == NULL) {
0088 ERR(": illegal option -- ", optopt);
0089 if (av[optind][++i] == '\0') {
0090 optind++;
0091 i = 1;
0092 }
0093 return '?';
0094 }
0095
0096
0097 if (*++p == ':') {
0098 if (av[optind][i + 1] != '\0')
0099 optarg = &av[optind++][i + 1];
0100 else {
0101 if (++optind >= ac) {
0102 ERR(": option requires an argument -- ", optopt);
0103 i = 1;
0104 return '?';
0105 }
0106 optarg = av[optind++];
0107 }
0108 i = 1;
0109 }
0110 else {
0111 if (av[optind][++i] == '\0') {
0112 i = 1;
0113 optind++;
0114 }
0115 optarg = NULL;
0116 }
0117
0118 return optopt;
0119 }