aboutsummaryrefslogtreecommitdiff
path: root/ch5
diff options
context:
space:
mode:
Diffstat (limited to 'ch5')
-rw-r--r--ch5/5-01_getint-fixed.c1
-rw-r--r--ch5/5-02_getfloat.c1
-rw-r--r--ch5/5-06_pointer-funcs.c2
-rw-r--r--ch5/5-08_error-checking.c3
-rw-r--r--ch5/5-09_yearday-pointers.c5
-rw-r--r--ch5/5-10_expr.c9
-rw-r--r--ch5/5-11_detab-remixed.c5
-rw-r--r--ch5/5-11_entab-remixed.c8
-rw-r--r--ch5/5-12_detab-spec.c4
-rw-r--r--ch5/5-12_entab-spec.c4
-rw-r--r--ch5/5-13_tail.c3
-rw-r--r--ch5/5-14_rsort.c10
-rw-r--r--ch5/5-15_sort-fold.c12
-rw-r--r--ch5/5-16_dir-order.c8
-rw-r--r--ch5/5-17_field-sort.c9
-rw-r--r--ch5/5-18_dcl-error-recovery.c4
-rw-r--r--ch5/5-19_undcl-no-redundant-parens.c4
-rw-r--r--ch5/5-20_expanded-dcl.c4
18 files changed, 29 insertions, 67 deletions
diff --git a/ch5/5-01_getint-fixed.c b/ch5/5-01_getint-fixed.c
index b013f20..641251f 100644
--- a/ch5/5-01_getint-fixed.c
+++ b/ch5/5-01_getint-fixed.c
@@ -29,7 +29,6 @@ int main() {
int getint(int *pn) {
int c, sign;
-
while (isspace(c = getch())) {
}
if (!isdigit(c) && c != EOF && c != '+' && c != '-') {
diff --git a/ch5/5-02_getfloat.c b/ch5/5-02_getfloat.c
index e6f3347..b125976 100644
--- a/ch5/5-02_getfloat.c
+++ b/ch5/5-02_getfloat.c
@@ -32,7 +32,6 @@ int main() {
int getfloat(double *pn) {
int c, sign, places;
-
while (isspace(c = getch())) {
}
if (!isdigit(c) && c != EOF && c != '+' && c != '-') {
diff --git a/ch5/5-06_pointer-funcs.c b/ch5/5-06_pointer-funcs.c
index 7947ba6..a64a0fe 100644
--- a/ch5/5-06_pointer-funcs.c
+++ b/ch5/5-06_pointer-funcs.c
@@ -60,7 +60,7 @@ void reverse(char *s) {
int get_line(char *s, int lim) {
int c, i;
- for (i = 0; i < lim - 1 && (c=getchar()) != EOF && c != '\n'; ++i) {
+ for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
*s++ = c;
}
if (c == '\n') {
diff --git a/ch5/5-08_error-checking.c b/ch5/5-08_error-checking.c
index 61c15c7..b1bc5e7 100644
--- a/ch5/5-08_error-checking.c
+++ b/ch5/5-08_error-checking.c
@@ -19,7 +19,6 @@ int main() {
int m, d, doy;
m = d = 0;
doy = 0;
-
printf("Calling day_of_year(1973, 10, 32)\n> ");
if ((doy = day_of_year(1973, 10, 32)) > 0) {
printf("Oct 12nd, 1973 is day %d\n", doy);
@@ -52,7 +51,6 @@ int main() {
int day_of_year(int year, int month, int day) {
int i, leap;
leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
-
if (month < 1 || month > 12) {
return -1;
}
@@ -68,7 +66,6 @@ int day_of_year(int year, int month, int day) {
void month_day(int year, int yearday, int *pmonth, int *pday) {
int i, leap;
leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
-
// Check for edge case
if (yearday > 365 + leap || yearday < 1) {
*pmonth = 0; // Set to zero so the error is obvious
diff --git a/ch5/5-09_yearday-pointers.c b/ch5/5-09_yearday-pointers.c
index 7a28f88..df7197d 100644
--- a/ch5/5-09_yearday-pointers.c
+++ b/ch5/5-09_yearday-pointers.c
@@ -22,7 +22,6 @@ int main() {
};
m = d = 0;
doy = 0;
-
// It said nothing about avoiding indexing in main()!
for (i = 0; i < 3; i++) {
doy = day_of_year(tvals[i][0], tvals[i][1], tvals[i][2]);
@@ -40,7 +39,6 @@ int main() {
int day_of_year(int year, int month, int day) {
int i, leap;
leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
-
if (month < 1 || month > 12) {
return -1;
}
@@ -50,7 +48,7 @@ int day_of_year(int year, int month, int day) {
}
for (i = 1; i < month; i++) {
// here, too
- day += *(daytab + i) + ((i ==2) ? leap : 0);
+ day += *(daytab + i) + ((i == 2) ? leap : 0);
}
return day;
}
@@ -58,7 +56,6 @@ int day_of_year(int year, int month, int day) {
void month_day(int year, int yearday, int *pmonth, int *pday) {
int i, leap;
leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
-
if (yearday > 365 + leap || yearday < 1) {
*pmonth = 0;
*pday = 0;
diff --git a/ch5/5-10_expr.c b/ch5/5-10_expr.c
index cc88838..acaabca 100644
--- a/ch5/5-10_expr.c
+++ b/ch5/5-10_expr.c
@@ -27,7 +27,7 @@ double pop(void);
* calculator. Note that for most shells, you'll need to escape the
* multiplication symbol (*) or it will expand to every file in the current
* directory; NOT what you want in this situation. :)
- *
+ *
* So if you want 6 × 8 for example, you'll need to type `6 8 \*`
*/
int main(int argc, char *argv[]) {
@@ -35,7 +35,7 @@ int main(int argc, char *argv[]) {
double op2;
for (argp = 1; argp < argc; argp++) {
int argtype = opt_type(argv[argp]);
- switch(argtype) {
+ switch (argtype) {
case '*':
push(pop() * pop());
break;
@@ -80,7 +80,7 @@ double pop(void) {
int opt_type(char op[]) {
int i = 0;
- switch(op[i]) {
+ switch (op[i]) {
case '*':
return '*';
case '+':
@@ -88,12 +88,11 @@ int opt_type(char op[]) {
case '/':
return '/';
case '-':
- if (op[i+1] == '\0') {
+ if (op[i + 1] == '\0') {
return '-';
}
break;
}
-
while (isdigit(op[i])) {
i++;
}
diff --git a/ch5/5-11_detab-remixed.c b/ch5/5-11_detab-remixed.c
index 43bffb4..457d465 100644
--- a/ch5/5-11_detab-remixed.c
+++ b/ch5/5-11_detab-remixed.c
@@ -12,19 +12,17 @@
int main(int argc, char *argv[]) {
int column, c, tabnum, stop;
column = 0;
-
if (argc > 1) {
tabnum = 1;
stop = atoi(argv[tabnum]);
} else {
tabnum = 0;
}
-
while ((c = getchar()) != EOF) {
if (c == '\t') {
if (argc > 1) {
// advance the argument if we're ahead of the last one
- if (column > stop && tabnum < (argc -1)) {
+ if (column > stop && tabnum < (argc - 1)) {
stop = atoi(argv[++tabnum]);
}
// insert our spaces up to the tabstop
@@ -56,6 +54,5 @@ int main(int argc, char *argv[]) {
column++;
}
}
-
return 0;
}
diff --git a/ch5/5-11_entab-remixed.c b/ch5/5-11_entab-remixed.c
index 8f6f7d4..d517365 100644
--- a/ch5/5-11_entab-remixed.c
+++ b/ch5/5-11_entab-remixed.c
@@ -21,17 +21,14 @@
int main(int argc, char *argv[]) {
int column, c, spaces, tabnum, stop;
spaces = column = 0;
-
if (argc > 1) {
tabnum = 1;
stop = atoi(argv[tabnum]);
} else {
tabnum = 0;
}
-
while ((c = getchar()) != EOF) {
column++;
-
if (c == ' ') {
spaces++;
if (argc > 1) {
@@ -43,11 +40,11 @@ int main(int argc, char *argv[]) {
putchar('\t');
spaces = 0;
}
- } else if (column > stop && tabnum < (argc -1)) {
+ } else if (column > stop && tabnum < (argc - 1)) {
stop = atoi(argv[++tabnum]);
}
} else {
- // we need to do default tabstopping
+ // we need to do default tabstopping
if (column % TABWIDTH == 0 && spaces > 1) {
putchar('\t');
spaces = 0;
@@ -71,6 +68,5 @@ int main(int argc, char *argv[]) {
putchar(c);
}
}
-
return 0;
}
diff --git a/ch5/5-12_detab-spec.c b/ch5/5-12_detab-spec.c
index ba22619..84f81d5 100644
--- a/ch5/5-12_detab-spec.c
+++ b/ch5/5-12_detab-spec.c
@@ -16,7 +16,6 @@ int main(int argc, char *argv[]) {
int tabevery = (-1);
char arg[4];
column = 0;
-
if (argc > 1) {
int i = 1;
int j = 0;
@@ -47,10 +46,8 @@ int main(int argc, char *argv[]) {
if (tabevery == -1) {
tabevery = DEFAULTTAB;
}
-
while ((c = getchar()) != EOF) {
column++;
-
if (c == '\t') {
/* No point in processing our tabs until we're after the offset */
if (column > tabstart) {
@@ -70,6 +67,5 @@ int main(int argc, char *argv[]) {
putchar(c);
}
}
-
return 0;
}
diff --git a/ch5/5-12_entab-spec.c b/ch5/5-12_entab-spec.c
index c1f3f6b..d551f66 100644
--- a/ch5/5-12_entab-spec.c
+++ b/ch5/5-12_entab-spec.c
@@ -17,7 +17,6 @@ int main(int argc, char *argv[]) {
int tabevery = (-1);
char arg[4];
spaces = column = 0;
-
if (argc > 1) {
int i = 1;
int j = 0;
@@ -48,10 +47,8 @@ int main(int argc, char *argv[]) {
if (tabevery == -1) {
tabevery = DEFAULTTAB;
}
-
while ((c = getchar()) != EOF) {
column++;
-
if (c == ' ') {
/* No point in starting our tabs until we're after the offset */
if (column > tabstart) {
@@ -77,6 +74,5 @@ int main(int argc, char *argv[]) {
putchar(c);
}
}
-
return 0;
}
diff --git a/ch5/5-13_tail.c b/ch5/5-13_tail.c
index 3a8ea30..642c7c5 100644
--- a/ch5/5-13_tail.c
+++ b/ch5/5-13_tail.c
@@ -54,7 +54,7 @@ int readlines(char *lineptr[], int maxlines) {
if (nlines >= maxlines || (p = alloc(len)) == NULL) {
return -1;
} else {
- line[len-1] = '\0';
+ line[len - 1] = '\0';
strcpy(p, line);
lineptr[nlines++] = p;
}
@@ -90,7 +90,6 @@ void parse_args(int argc, char *argv[]) {
int main(int argc, char *argv[]) {
int nlines;
parse_args(argc, argv);
-
if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
writelines(lineptr, nlines - numlines, nlines);
return 0;
diff --git a/ch5/5-14_rsort.c b/ch5/5-14_rsort.c
index b39a20c..5b19e6d 100644
--- a/ch5/5-14_rsort.c
+++ b/ch5/5-14_rsort.c
@@ -29,7 +29,6 @@ int numcmp(const char *, const char *);
void my_qsort(void *v[], int left, int right, int (*comp)(const char *, const char *)) {
int i, last;
void swap(void *v[], int, int);
-
if (left >= right) {
return;
}
@@ -47,7 +46,6 @@ void my_qsort(void *v[], int left, int right, int (*comp)(const char *, const ch
int numcmp(const char *s1, const char *s2) {
double v1, v2;
-
v1 = atof(s1);
v2 = atof(s2);
if (v1 < v2) {
@@ -61,7 +59,6 @@ int numcmp(const char *s1, const char *s2) {
void swap(void *v[], int i, int j) {
void *temp;
-
temp = v[i];
v[i] = v[j];
v[j] = temp;
@@ -75,7 +72,7 @@ int readlines(char *lineptr[], int maxlines) {
if (nlines >= maxlines || (p = alloc(len)) == NULL) {
return -1;
} else {
- line[len-1] = '\0';
+ line[len - 1] = '\0';
strcpy(p, line);
lineptr[nlines++] = p;
}
@@ -123,17 +120,16 @@ char *alloc(int n) {
}
/* sort input lines */
-int main (int argc, char *argv[]) {
+int main(int argc, char *argv[]) {
int nlines; /* number of input lines read */
int numeric = 0; /* 1 if numeric sort */
int reverse = 0; /* 1 if reverse sort */
-
if (argc > 1) {
int i, j;
for (i = 1, j = 0; --argc; i++) {
if (argv[i][j++] == '-') {
while (argv[i][j] != '\0') {
- switch(argv[i][j]) {
+ switch (argv[i][j]) {
case 'n':
numeric = 1;
break;
diff --git a/ch5/5-15_sort-fold.c b/ch5/5-15_sort-fold.c
index 90b567b..90c658f 100644
--- a/ch5/5-15_sort-fold.c
+++ b/ch5/5-15_sort-fold.c
@@ -31,7 +31,6 @@ int istrcmp(const char *, const char *);
void my_qsort(void *v[], int left, int right, int (*comp)(const char *, const char *)) {
int i, last;
void swap(void *v[], int, int);
-
if (left >= right) {
return;
}
@@ -49,7 +48,6 @@ void my_qsort(void *v[], int left, int right, int (*comp)(const char *, const ch
int numcmp(const char *s1, const char *s2) {
double v1, v2;
-
v1 = atof(s1);
v2 = atof(s2);
if (v1 < v2) {
@@ -63,7 +61,6 @@ int numcmp(const char *s1, const char *s2) {
void swap(void *v[], int i, int j) {
void *temp;
-
temp = v[i];
v[i] = v[j];
v[j] = temp;
@@ -77,7 +74,7 @@ int readlines(char *lineptr[], int maxlines) {
if (nlines >= maxlines || (p = alloc(len)) == NULL) {
return -1;
} else {
- line[len-1] = '\0';
+ line[len - 1] = '\0';
strcpy(p, line);
lineptr[nlines++] = p;
}
@@ -104,7 +101,7 @@ void reverse_set(char *lineptr[], int nlines) {
/* Ignore character case, then send results to qsort */
int istrcmp(const char *s1, const char *s2) {
- for ( ; tolower(*s1) == tolower(*s2); s1++, s2++) {
+ for (; tolower(*s1) == tolower(*s2); s1++, s2++) {
if (*s1 == '\0') {
return 0;
}
@@ -135,18 +132,17 @@ char *alloc(int n) {
}
/* sort input lines */
-int main (int argc, char *argv[]) {
+int main(int argc, char *argv[]) {
int nlines; /* number of input lines read */
int numeric = 0; /* 1 if numeric sort */
int reverse = 0; /* 1 if reverse sort */
int fold = 0; /* 1 if folding upper and lower case */
-
if (argc > 1) {
int i, j;
for (i = 1, j = 0; --argc; i++) {
if (argv[i][j++] == '-') {
while (argv[i][j] != '\0') {
- switch(argv[i][j]) {
+ switch (argv[i][j]) {
case 'n':
numeric = 1;
break;
diff --git a/ch5/5-16_dir-order.c b/ch5/5-16_dir-order.c
index 4610ea6..2952e23 100644
--- a/ch5/5-16_dir-order.c
+++ b/ch5/5-16_dir-order.c
@@ -49,7 +49,6 @@ int mygetline(char *line, int lim) {
void my_qsort(void *v[], int left, int right, int (*comp)(const char *, const char *)) {
int i, last;
void swap(void *v[], int, int);
-
if (left >= right) {
return;
}
@@ -67,7 +66,6 @@ void my_qsort(void *v[], int left, int right, int (*comp)(const char *, const ch
int numcmp(const char *s1, const char *s2) {
double v1, v2;
-
v1 = atof(s1);
v2 = atof(s2);
if (v1 < v2) {
@@ -81,7 +79,6 @@ int numcmp(const char *s1, const char *s2) {
void swap(void *v[], int i, int j) {
void *temp;
-
temp = v[i];
v[i] = v[j];
v[j] = temp;
@@ -95,7 +92,7 @@ int readlines(char *lineptr[], int maxlines) {
if (nlines >= maxlines || (p = alloc(len)) == NULL) {
return -1;
} else {
- line[len-1] = '\0';
+ line[len - 1] = '\0';
strcpy(p, line);
lineptr[nlines++] = p;
}
@@ -160,13 +157,12 @@ char *alloc(int n) {
/* sort input lines */
int main(int argc, char *argv[]) {
int nlines; /* number of input lines read */
-
if (argc > 1) {
int i, j;
for (i = 1, j = 0; --argc; i++) {
if (argv[i][j++] == '-') {
while (argv[i][j] != '\0') {
- switch(argv[i][j]) {
+ switch (argv[i][j]) {
case 'n':
numeric = 1;
break;
diff --git a/ch5/5-17_field-sort.c b/ch5/5-17_field-sort.c
index c086053..078b347 100644
--- a/ch5/5-17_field-sort.c
+++ b/ch5/5-17_field-sort.c
@@ -64,7 +64,6 @@ int mygetline(char *line, int lim) {
void my_qsort(void *v[], int left, int right, int (*comp)(const char *, const char *)) {
int i, last;
void swap(void *v[], int, int);
-
if (left >= right) {
return;
}
@@ -108,7 +107,7 @@ int readlines(char *lineptr[], int maxlines) {
if (nlines >= maxlines || (p = alloc(len)) == NULL) {
return -1;
} else {
- line[len-1] = '\0';
+ line[len - 1] = '\0';
strcpy(p, line);
lineptr[nlines++] = p;
}
@@ -170,8 +169,8 @@ int field_start(const char *s, int fieldnum) {
if (s[i] == DELIMITER) {
fieldnum--;
if (fieldnum == 0) {
- if (s[i+1] != '\0') {
- return i+1;
+ if (s[i + 1] != '\0') {
+ return i + 1;
}
}
}
@@ -227,7 +226,7 @@ int main(int argc, char *argv[]) {
for (i = 1, j = 0; --argc; i++) {
if (argv[i][j++] == '-') {
while (argv[i][j] != '\0') {
- switch(argv[i][j]) {
+ switch (argv[i][j]) {
case 'n':
numeric = 1;
break;
diff --git a/ch5/5-18_dcl-error-recovery.c b/ch5/5-18_dcl-error-recovery.c
index 9a22764..03a7e3c 100644
--- a/ch5/5-18_dcl-error-recovery.c
+++ b/ch5/5-18_dcl-error-recovery.c
@@ -80,7 +80,7 @@ int gettoken(void) {
bufp++;
}
if (buf[bufp] == '/') {
- if (buf[bufp+1] == '/' || buf[bufp+1] == '*') {
+ if (buf[bufp + 1] == '/' || buf[bufp + 1] == '*') {
while (buf[bufp] != '\n') {
bufp++;
}
@@ -130,7 +130,7 @@ int gettoken(void) {
*/
int get_line(char *s, int lim) {
int c, i;
- for (i = 0; i < lim - 1 && (c=getchar()) != EOF && c != '\n'; ++i) {
+ for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
*s++ = c;
}
if (c == '\n') {
diff --git a/ch5/5-19_undcl-no-redundant-parens.c b/ch5/5-19_undcl-no-redundant-parens.c
index 4af4314..44d5f38 100644
--- a/ch5/5-19_undcl-no-redundant-parens.c
+++ b/ch5/5-19_undcl-no-redundant-parens.c
@@ -44,12 +44,12 @@ int gettoken(void) {
return tokentype = '(';
}
} else if (c == '[') {
- for (*p++ = c; (*p++ = getch()) != ']'; ) {
+ for (*p++ = c; (*p++ = getch()) != ']';) {
}
*p = '\0';
return tokentype = BRACKETS;
} else if (isalpha(c)) {
- for (*p++ = c; isalnum(c = getch()); ) {
+ for (*p++ = c; isalnum(c = getch());) {
*p++ = c;
}
*p = '\0';
diff --git a/ch5/5-20_expanded-dcl.c b/ch5/5-20_expanded-dcl.c
index d7d4c5c..a30fa3e 100644
--- a/ch5/5-20_expanded-dcl.c
+++ b/ch5/5-20_expanded-dcl.c
@@ -107,7 +107,7 @@ int gettoken(void) {
bufp++;
}
if (buf[bufp] == '/') {
- if (buf[bufp+1] == '/' || buf[bufp+1] == '*') {
+ if (buf[bufp + 1] == '/' || buf[bufp + 1] == '*') {
while (buf[bufp] != '\n') {
bufp++;
}
@@ -167,7 +167,7 @@ int gettoken(void) {
*/
int get_line(char *s, int lim) {
int c, i;
- for (i = 0; i < lim - 1 && (c=getchar()) != EOF && c != '\n'; ++i) {
+ for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
*s++ = c;
}
if (c == '\n') {