Using getopt

This commit is contained in:
Gitea 2020-12-13 10:22:44 -06:00
parent fb6226dd6b
commit 6731edbf57

17
pwd.c
View file

@ -6,19 +6,12 @@
int main(int argc, char * argv[]) { int main(int argc, char * argv[]) {
int mode = 01; /* 01: print PWD -L, 02: print physical path -P */ int mode = 01; /* 01: print PWD -L, 02: print physical path -P */
char c;
if(argc > 1) { while((c = getopt(argc, argv, "LP")) != -1) {
for(int i = 1; i < argc; i++) { switch(c) {
if(argv[i][1] == 'L' && argv[i][2] == '\0') { case 'L': mode = 01; break;
mode = 01; case 'P': mode = 02; break;
}
else if(argv[i][1] == 'P' && argv[i][2] == '\0') {
mode = 02;
}
else {
fprintf(stderr, "%s: invalid option: %s\n", argv[0], argv[i]);
exit(1);
}
} }
} }