aboutsummaryrefslogtreecommitdiff
path: root/ch5
diff options
context:
space:
mode:
authorzlg <zlg@zlg.space>2014-05-21 05:54:51 -0500
committerzlg <zlg@zlg.space>2014-05-21 05:54:51 -0500
commitcb197e459c381591f9cb0a92a0cbd720bdbda5a7 (patch)
tree6894d07c67403933b7d48fd71d5c49cfcc639a41 /ch5
parentSolve Exercise 5-16: directory-ordered `tail` (diff)
downloadknr-cb197e459c381591f9cb0a92a0cbd720bdbda5a7.tar.gz
knr-cb197e459c381591f9cb0a92a0cbd720bdbda5a7.tar.bz2
knr-cb197e459c381591f9cb0a92a0cbd720bdbda5a7.tar.xz
knr-cb197e459c381591f9cb0a92a0cbd720bdbda5a7.zip
Refactor flag handling
Diffstat (limited to 'ch5')
-rw-r--r--ch5/5-14_rsort.c20
-rw-r--r--ch5/5-15_sort-fold.c25
-rw-r--r--ch5/5-16_dir-order.c30
3 files changed, 51 insertions, 24 deletions
diff --git a/ch5/5-14_rsort.c b/ch5/5-14_rsort.c
index 4c03191..b39a20c 100644
--- a/ch5/5-14_rsort.c
+++ b/ch5/5-14_rsort.c
@@ -129,12 +129,20 @@ int main (int argc, char *argv[]) {
int reverse = 0; /* 1 if reverse sort */
if (argc > 1) {
- int i;
- for (i = 1; --argc; i++) {
- if (strcmp(argv[i], "-n") == 0) {
- numeric = 1;
- } else if (strcmp(argv[i], "-r") == 0) {
- reverse = 1;
+ int i, j;
+ for (i = 1, j = 0; --argc; i++) {
+ if (argv[i][j++] == '-') {
+ while (argv[i][j] != '\0') {
+ switch(argv[i][j]) {
+ case 'n':
+ numeric = 1;
+ break;
+ case 'r':
+ reverse = 1;
+ break;
+ }
+ j++;
+ }
}
}
}
diff --git a/ch5/5-15_sort-fold.c b/ch5/5-15_sort-fold.c
index 3b508b9..90b567b 100644
--- a/ch5/5-15_sort-fold.c
+++ b/ch5/5-15_sort-fold.c
@@ -142,14 +142,23 @@ int main (int argc, char *argv[]) {
int fold = 0; /* 1 if folding upper and lower case */
if (argc > 1) {
- int i;
- for (i = 1; --argc; i++) {
- if (strcmp(argv[i], "-n") == 0) {
- numeric = 1;
- } else if (strcmp(argv[i], "-r") == 0) {
- reverse = 1;
- } else if (strcmp(argv[i], "-f") == 0) {
- fold = 1;
+ int i, j;
+ for (i = 1, j = 0; --argc; i++) {
+ if (argv[i][j++] == '-') {
+ while (argv[i][j] != '\0') {
+ switch(argv[i][j]) {
+ case 'n':
+ numeric = 1;
+ break;
+ case 'r':
+ reverse = 1;
+ break;
+ case 'f':
+ fold = 1;
+ break;
+ }
+ j++;
+ }
}
}
}
diff --git a/ch5/5-16_dir-order.c b/ch5/5-16_dir-order.c
index eabf755..7533b3b 100644
--- a/ch5/5-16_dir-order.c
+++ b/ch5/5-16_dir-order.c
@@ -162,16 +162,26 @@ int main (int argc, char *argv[]) {
int nlines; /* number of input lines read */
if (argc > 1) {
- int i;
- for (i = 1; --argc; i++) {
- if (strcmp(argv[i], "-n") == 0) {
- numeric = 1;
- } else if (strcmp(argv[i], "-r") == 0) {
- reverse = 1;
- } else if (strcmp(argv[i], "-f") == 0) {
- fold = 1;
- } else if (strcmp(argv[i], "-d") == 0) {
- dir = 1;
+ int i, j;
+ for (i = 1, j = 0; --argc; i++) {
+ if (argv[i][j++] == '-') {
+ while (argv[i][j] != '\0') {
+ switch(argv[i][j]) {
+ case 'n':
+ numeric = 1;
+ break;
+ case 'r':
+ reverse = 1;
+ break;
+ case 'f':
+ fold = 1;
+ break;
+ case 'd':
+ dir = 1;
+ break;
+ }
+ j++;
+ }
}
}
}