GETOPT 22-SEP-1995 18:32:06 VAX C V3.2-044 Page 1 V1.0 15-JUN-1993 01:07:54 [GOPHER.G2.VMS2_13.OBJECT]GETOPT.C;1 (1) 1 /* 2 ** GETOPT PROGRAM AND LIBRARY ROUTINE 3 ** 4 ** I wrote main() and AT&T wrote getopt() and we both put our efforts into 5 ** the public domain via mod.sources. 6 ** Rich $alz 7 ** Mirror Systems 8 ** (mirror!rs, rs@mirror.TMC.COM) 9 ** August 10, 1986 10 ** 11 ** This is the public-domain AT&T getopt(3) code. Hacked by Rich and by Jim. 12 */ 13 14 #include "String.h" 896 897 /*LINTLIBRARY*/ 898 #include 1147 1148 #define ERR(s, c) if(opterr){\ 1149 extern int write();\ 1150 char errbuf[2];\ 1151 errbuf[0] = c; errbuf[1] = '\n';\ 1152 (void) write(2, argv[0], (unsigned)strlen(argv[0]));\ 1153 (void) write(2, s, (unsigned)strlen(s));\ 1154 (void) write(2, errbuf, 2);} 1155 1156 extern char *strchr(); 1157 1158 int opterr = 1; 1159 int optind = 1; 1160 int optopt; 1161 char *optarg; 1162 1163 int 1164 getopt(argc, argv, opts) 1165 int argc; 1166 char **argv, *opts; 1167 { 1168 1 static int sp = 1; 1169 1 register int c; 1170 1 register char *cp; 1171 1 1172 1 if(sp == 1) 1173 1 if(optind >= argc || 1174 1 argv[optind][0] != '-' || argv[optind][1] == '\0') 1175 1 return(EOF); 1176 1 else if(strcmp(argv[optind], "--") == 0) { 1177 2 optind++; 1178 2 return(EOF); 1179 2 } 1180 1 optopt = c = argv[optind][sp]; 1181 1 if(c == ':' || (cp=strchr(opts, c)) == NULL) { 1182 2 ERR(": illegal option -- ", c); 1183 2 if(argv[optind][++sp] == '\0') { 1184 3 optind++; GETOPT 22-SEP-1995 18:32:06 VAX C V3.2-044 Page 2 V1.0 15-JUN-1993 01:07:54 [GOPHER.G2.VMS2_13.OBJECT]GETOPT.C;1 (1) 1185 3 sp = 1; 1186 3 } 1187 2 return('?'); 1188 2 } 1189 1 if(*++cp == ':') { 1190 2 if(argv[optind][sp+1] != '\0') 1191 2 optarg = &argv[optind++][sp+1]; 1192 2 else if(++optind >= argc) { 1193 3 ERR(": option requires an argument -- ", c); 1194 3 sp = 1; 1195 3 return('?'); 1196 3 } else 1197 2 optarg = argv[optind++]; 1198 2 sp = 1; 1199 2 } else { 1200 2 if(argv[optind][++sp] == '\0') { 1201 3 sp = 1; 1202 3 optind++; 1203 3 } 1204 2 optarg = NULL; 1205 2 } 1206 1 return(c); 1207 1 } Command Line ------------ CC/INCL=([-],[-.OBJECT])/DEFINE=(MULTINET=1,DEBUGGING,__VMS)/DEBUG/NOOPT/OBJ=[.VAX.DBG]/LIS=[.VAX.LIS] GETOPT.C .