Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d20df83

Browse files
Merge branch 'experimental'
2 parents aca173a + 33c7fa1 commit d20df83

File tree

6 files changed

+42
-33
lines changed

6 files changed

+42
-33
lines changed

‎chapter05/5-19.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ int needsParens()
6060
void undcl(void)
6161
{
6262
int type;
63-
char temp[MAXTOKEN];
63+
char temp[MAXLEN];
6464

6565
while ((type = gettoken()) != '\n')
6666
if (type == PARENS || type == BRACKETS)

‎chapter07/7-6.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ void openfile(const char *filename, File *file)
3434
exit(EXIT_FAILURE);
3535
}
3636
strcpy(file->name, filename);
37+
*file->line = '0円';
3738
file->lineno = 0;
3839
}
3940

‎chapter07/7-7.c

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44
* the standard input. Should the file name be printed when a matching line is
55
* found?
66
*
7-
* Answer: of course!
8-
*
97
* By Faisal Saadatmand
108
*/
119

10+
/* Answer: only when files are named as arguments */
11+
1212
#include <stdio.h>
13-
#include <string.h>
1413
#include <stdlib.h>
14+
#include <string.h>
1515

16-
#define MAXLINE 1000
16+
#define MAXLEN 1000
1717

1818
/* globals */
19-
char *progName;
19+
char *prog; /* program name */
2020

2121
/* functions */
2222
int getLine(char *, size_t, FILE *);
2323
FILE* loadFile(char *);
24-
int findPattern(FILE *, char *, char *, int, int);
24+
int findPattern(FILE *, constchar *, constchar *, constint,const int);
2525

2626
/* getLine: read line, return length - file pointer version */
2727
int getLine(char *line, size_t max, FILE *fp)
@@ -35,38 +35,44 @@ FILE* loadFile(char *fileName)
3535
{
3636
FILE* fp;
3737
if (!(fp = fopen(fileName, "r"))) {
38-
fprintf(stderr, "%s: can't open %s\n", progName, fileName);
38+
fprintf(stderr, "%s: can't open %s\n", prog, fileName);
3939
exit(EXIT_FAILURE);
4040
}
4141
return fp;
4242
}
4343

44-
int findPattern(FILE *fp, char *fileName, char *pattern, int except, int number)
44+
int findPattern(FILE *fp, const char *fileName, const char *pattern,
45+
const int except, const int number)
4546
{
46-
char line[MAXLINE];
47-
long int lineno, found = 0;
47+
char line[MAXLEN];
48+
long lineno;
49+
int found;
4850

49-
for (lineno = 1; getLine(line, MAXLINE, fp) > 0; lineno++)
51+
lineno = found = 0;
52+
while (getLine(line, MAXLEN, fp) > 0) {
53+
++lineno;
5054
if ((strstr(line, pattern) != NULL) != except) {
5155
if (fileName)
52-
printf("%s:", fileName);
56+
fprintf(stdout, "%s:", fileName);
5357
if (number)
54-
printf ("%ld:", lineno);
55-
printf("%s", line);
56-
found++;
58+
fprintf (stdout, "%ld:", lineno);
59+
fprintf(stdout, "%s", line);
60+
++found;
5761
}
62+
}
5863
return found;
5964
}
6065

6166
/* find: print lines that match pattern from 1s arg */
6267
int main(int argc, char *argv[])
6368
{
64-
int c, except=0, number=0, found=0;
65-
char *pattern=NULL;
66-
FILE *file=NULL;
69+
int c, except, number, found;
70+
char *pattern;
71+
FILE *file;
6772

68-
progName = argv[0];
69-
while (--argc > 0 && (*++argv)[0] == '-') /* check for flags */
73+
prog = argv[0];
74+
except = number = found = 0;
75+
while (--argc > 0 && (*++argv)[0] == '-') /* check for flags */
7076
while ((c = *++argv[0]))
7177
switch (c) {
7278
case 'x':
@@ -76,20 +82,19 @@ int main(int argc, char *argv[])
7682
number = 1;
7783
break;
7884
default:
79-
printf("%s: illegal option %c\n", progName, c);
85+
fprintf(stderr, "%s: illegal option %c\n", prog, c);
8086
argc = 0;
8187
found = -1;
8288
break;
8389
}
84-
85-
pattern = *argv; /* save a pointer to the pattern */
90+
pattern = *argv++; /* save a pointer to the pattern */
8691
if (argc < 1)
87-
printf("Usage: %s -x -n pattern\n", progName);
88-
else if (argc == 1) /* input from stdin */
92+
fprintf(stderr, "Usage: %s [-xn] PATTERN [FILE...]\n", prog);
93+
else if (argc == 1) /* input from stdin */
8994
found += findPattern(stdin, NULL, pattern, except, number);
90-
else /* input from file or set of files */
95+
else /* input from file or set of files */
9196
while (argc-- > 1) {
92-
file = loadFile(*++argv);
97+
file = loadFile(*argv++);
9398
found += findPattern(file, *argv, pattern, except, number);
9499
fclose(file);
95100
}

‎chapter08/8-1.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
* Exercise 8-1. Rewrite the program cat from Chapter 7 using read, write, open
33
* and close instead of their standard library equivalents. Perform experiments
44
* to determine the relative speeds of the two version.
5-
* Note: this version of cat is faster.
5+
*
66
* By Faisal Saadatmand
77
*/
88

9+
/* Note: this version of cat is faster */
10+
911
#include <unistd.h> /* for read and write */
1012
#include <fcntl.h> /* for open and close */
1113
#include <stdio.h> /* also needed for BUFSIZ */

‎chapter08/8-2.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Exercise 8-2. Rewrite fopen and _fillbuf with fields instead of explicit bit
33
* operations. Compare code size and execution speed.
4+
*
45
* By Faisal Saadatmand
56
*/
67

‎chapter08/8-8.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ int main(void)
195195
printf("*** Add array's memory block to free list ***\n");
196196
nblocks++;
197197
for (p = &base, i = 1; i <= nblocks; p = p->s.ptr, i++)
198-
printf("block %i (address: %p size: %u ptr: %p)\n"
199-
, i, p, p->s.size, p->s.ptr);
198+
printf("block %i (address: %p size: %u ptr: %p)\n", i, (void*) p,
199+
p->s.size, (void*) p->s.ptr);
200200
printf("\n");
201201
}
202202

@@ -206,8 +206,8 @@ int main(void)
206206
else {
207207
printf("*** allocate freed memory with knr_malloc ***\n");
208208
for (p = &base, i = 1; i <= nblocks; p = p->s.ptr, i++)
209-
printf("block %i (address: %p size: %u ptr: %p)\n"
210-
, i, p, p->s.size, p->s.ptr);
209+
printf("block %i (address: %p size: %u ptr: %p)\n", i, (void*) p,
210+
p->s.size, (void*) p->s.ptr);
211211
printf("\n");
212212
}
213213

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /