1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include <ctype.h>
# include <limits.h>
/*
* See ../README.md
*/
/*
* Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file
*/
/*
* Copied from https://gist.github.com/w-vi/7e98342181776162b1a3
*/
char * slurpfile (char * filename) {
FILE * fp;
char * source;
long bufsize;
size_t len;
if ((fp = fopen (filename, "r")) == NULL) {
fprintf (stderr, "Failed to open %s\n", filename);
exit (1);
}
/*
* Get the size of the file:
* - seek to the end of the file
* - use ftell to get the offset from the start
* - seek back to the beginning
*/
if (fseek (fp, 0L, SEEK_END) != 0) {
fprintf (stderr, "seek() failed for %s\n", filename);
exit (1);
}
if ((bufsize = ftell (fp)) == -1) {
fprintf (stderr, "ftell() failed for %s\n", filename);
exit (1);
}
if (fseek (fp, 0L, SEEK_SET) != 0) {
fprintf (stderr, "seek() failed for %s\n", filename);
exit (1);
}
/*
* Allocate memory for content of the file
*/
if ((source = (char *) malloc (sizeof (char) * (bufsize + 1))) == NULL) {
fprintf (stderr, "malloc() failed for %s\n", filename);
exit (1);
}
/*
* And read it in
*/
if ((len = fread (source, sizeof (char), bufsize, fp)) == 0) {
fprintf (stderr, "fread() failed for %s\n", filename);
exit (1);
}
source [++ len] = '\0';
return source;
}
/*
* Use four arrays:
* - filenames: names of files read
* - content: pointers to content of files read
* - content_ptr: pointers to unreturned content
* - content_size: number of characters not returned yet
*/
size_t read_files = 0;
char ** filenames = NULL;
char ** content = NULL;
char ** content_ptr = NULL;
size_t * content_size = NULL;
char * readN (char * filename, int amount) {
ssize_t found = -1;
char * out;
/*
* Check whether we've already found the file --
* if so, set its position in "found".
*/
for (size_t i = 0; i < read_files; i ++) {
if (strncmp (filename, filenames [i], PATH_MAX) == 0) {
/*
* Found the file
*/
found = i;
break;
}
}
/*
* Not found; all filename, content of file, and size to
* the end of the arrays.
*/
if (found < 0) {
found = read_files ++;
/*
* Allocate memory
*/
if ((filenames = (char **)
realloc (filenames, read_files * sizeof (char *))) == NULL) {
fprintf (stderr, "malloc() failed for %s\n", filename);
exit (1);
}
if ((content = (char **)
realloc (content, read_files * sizeof (char *))) == NULL) {
fprintf (stderr, "malloc() failed for %s\n", filename);
exit (1);
}
if ((content_ptr = (char **)
realloc (content_ptr, read_files * sizeof (char *))) == NULL) {
fprintf (stderr, "malloc() failed for %s\n", filename);
exit (1);
}
if ((content_size = (size_t *)
realloc (content_size, read_files * sizeof (size_t))) == NULL) {
fprintf (stderr, "malloc() failed for %s\n", filename);
exit (1);
}
/*
* Set data
*/
if ((filenames [found] = (char *)
malloc (sizeof (char) * (strlen (filename) + 1))) == NULL) {
fprintf (stderr, "malloc() failed for %s\n", filename);
exit (1);
}
stpncpy (filenames [found], filename, strlen (filename));
filenames [found] [strlen (filename)] = '\0';
content [found] = slurpfile (filename);
content_ptr [found] = content [found];
content_size [found] = strlen (content [found]);
/*
* Remove any trailing new lines
*/
while (content [found] [content_size [found] - 1] == '\n') {
content [found] [-- content_size [found]] = '\0';
}
}
/*
* Now 'found' points to the place where the content
* of the file is.
*/
/*
* Cannot return more than we have.
*/
if (content_size [found] < amount) {
amount = content_size [found];
}
/*
* Allocate memory for the return value
*/
if ((out = (char *) malloc (sizeof (char) * (amount + 1))) == NULL) {
fprintf (stderr, "malloc() failed for %s\n", filename);
exit (1);
}
/*
* Copy the amount of data, make sure it's NUL terminated.
*/
stpncpy (out, content_ptr [found], amount);
out [amount] = '\0';
/*
* Adjust the pointer and size left
*/
content_ptr [found] += amount;
content_size [found] -= amount;
return out;
}
int main (void) {
char * line = NULL;
size_t len = 0;
size_t strlen;
/*
* Iterate over the lines of input; parse each line into
* a file name and an amount. Call readN for each line.
*/
while ((strlen = getline (&line, &len, stdin)) != -1) {
char * line_ptr = line;
char * filename;
int amount;
if ((filename = (char *) malloc (PATH_MAX * sizeof (char))) == NULL) {
fprintf (stderr, "malloc() failed\n");
exit (1);
}
if (sscanf (line_ptr, "%s %d", filename, &amount) != 2) {
fprintf (stderr, "Could not parse input\n");
exit (1);
}
printf ("%s\n", readN (filename, amount));
free (filename);
}
/*
* Release memory
*/
free (line);
for (size_t i = 0; i < read_files; i ++) {
free (filenames [i]);
free (content [i]);
}
free (filenames);
free (content);
free (content_ptr);
free (content_size);
return (0);
}
|