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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
*(at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pages.h"
#include "config.h"
int
createfile(const char *file)
{
/*
* create file at location 'file' using the template
* overwrite if it exists
* assume the caller has put us in the base directory already
*/
struct stat sb = {0};
if(!stat(output_dir, &sb) == 0 && !S_ISDIR(sb.st_mode))
{
fprintf(stderr, "directory '%s' does not exist, trying to make directory..\n", output_dir);
if (mkdir(output_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
{
fprintf(stderr, "unable to mkdir '%s', unrecoverable failure\n", output_dir);
return 0;
}
fprintf(stderr, "OK\n");
}
char filename[512] = {0};
size_t length = 512;
strncat(filename, output_dir, length-1);
length = length - strlen(output_dir);
strncat(filename, file, length-1);
FILE *out = fopen(filename, "w");
if (!out)
{
fprintf(stderr, "unable to open file '%s', unrecoverable failure\n", filename);
return 0;
}
FILE *in = fopen(template_file, "r");
if (!in)
{
fprintf(stderr, "unable to open file '%s', unrecoverable failure\n", filename);
fclose(out);
return 0;
}
char c;
while ((c = fgetc(in)) != EOF)
{
fputc(c, out);
}
fclose(out);
fclose(in);
return 1;
}
long
findstring(const char *file, const char *str)
{
/*
* find first occurance of substring 'str' in file 'output_dir'/'file'
* return offset of string beginning
* -1 on error (including not found)
*
* return value is a long obtained by ftell(), set the position later using fseek()
* (use SEEK_SET as whence for beginning of file)
*/
long offset = -1;
char filename[512] = {0};
size_t length = 512;
strncat(filename, output_dir, length-1);
length = length - strlen(output_dir);
strncat(filename, file, length-1);
FILE *in = fopen(filename, "r");
if (!in)
{
fprintf(stderr, "unable to open file '%s', unrecoverable failure\n", filename);
return -1;
}
/* hacky */
char buffer[1024] = {0};
char *strpos = NULL;
while (fgets(buffer, 1024, in) != NULL)
{
strpos = strstr(buffer, str);
if (strpos)
{
/* found our substring */
break;
}
memset(buffer, 0, 1024);
continue;
}
if (!strpos)
{
fprintf(stderr, "unable to find substring '%s' in '%s'\n", str, filename);
fclose(in);
return -1;
}
/* we have the line that holds our substring, find out where it is in the file */
long curpos = ftell(in);
if (!curpos)
{
fprintf(stderr, "ftell() failed\n");
fclose(in);
return -1;
}
// strpos-buffer = first byte of str in line
offset = (curpos - strlen(buffer)+1) + strpos-buffer;
fclose(in);
return offset;
}
int
deletebytes(const char *file, long offset, size_t bytes)
{
/*
* delete 'bytes' bytes from 'output_dir'/'file' starting at offset 'offset'
* return 1 on success. 0 on failure
*/
char filename[512] = {0};
size_t length = 512;
strncat(filename, output_dir, length-1);
length = length - strlen(output_dir);
strncat(filename, file, length-1);
FILE *in = fopen(filename, "r");
if (!in)
{
fprintf(stderr, "unable to open file '%s', unrecoverable failure\n", filename);
return 0;
}
FILE *out = fopen("tmp.tmp", "w");
if (!out)
{
fprintf(stderr, "unable to open temporary file, unrecoverable failure\n");
fclose(in);
return 0;
}
long curpos = 1;
char c;
/* read bytes from 'in' and write them to 'out', skipping 'bytes' bytes starting at 'offset' */
while ((c = fgetc(in)) != EOF)
{
if (curpos >= offset && curpos <= ((offset-1)+bytes))
{
; // byte is one we want to skip
}
else
fputc(c, out);
curpos++;
}
/* delete 'filename' and move our temp file 'out' to take its place */
fclose(in);
if (remove(filename) == -1)
{
fprintf(stderr, "unable to delete file '%s', unrecoverable failure\n", filename);
fclose(out);
return 0;
}
if (rename("tmp.tmp", filename) == -1)
{
fprintf(stderr, "unable to rename tmp file to '%s', unrecoverable failure\n", filename);
fclose(out);
return 0;
}
fclose(out);
return 1;
}
int
writeatbyte(const char *dest, struct fileorstring *source, long offset)
{
/*
* write file 'source' into 'output_dir'/'file' starting at offset 'offset'
* return 1 on success. 0 on failure
*/
char filename[512] = {0};
size_t length = 512;
strncat(filename, output_dir, length-1);
length = length - strlen(output_dir);
strncat(filename, dest, length-1);
FILE *in = fopen(filename, "r");
if (!in)
{
fprintf(stderr, "unable to open file '%s', unrecoverable failure\n", filename);
return 0;
}
FILE *tmp = fopen("tmp.tmp", "w");
if (!tmp)
{
fprintf(stderr, "unable to open temp file, unrecoverable failure\n");
fclose(in);
return 0;
}
FILE *src = NULL;
if (source->file != NULL)
{
src = fopen(source->file, "r");
if (!src)
{
fprintf(stderr, "unable to open file '%s', unrecoverable failure\n", source->file);
fclose(in);
fclose(tmp);
return 0;
}
}
/*
* read file 'in' until byte 'offset', writing into file 'tmp'
* if source->file isn't NULL, write file 'src' into 'tmp'
* otherwise write 'source->str' into file 'tmp'
* finish writing file 'in' into 'tmp'
* delete file 'filename'
* rename file 'tmp' 'filename'
*/
char c, t, a;
long curpos = 1;
int i = 0;
while ((c = fgetc(in)) != EOF)
{
if (curpos == offset)
{
if (src != NULL)
{
while ((t = fgetc(src)) != EOF)
{
/* prevent writing end of line '\n' if the next char is EOF */
a = fgetc(src);
ungetc(a, src);
if (a == EOF && t == '\n')
continue;
fputc(t, tmp);
}
}
else
{
while ((t = source->str[i]) != '\0')
{
fputc(source->str[i], tmp);
i++;
}
}
}
fputc(c, tmp);
curpos++;
}
fclose(tmp);
fclose(in);
if (src)
fclose(src);
if (remove(filename) == -1)
{
fprintf(stderr, "unable to delete file '%s', unrecoverable failure\n", filename);
return 0;
}
if (rename("tmp.tmp", filename) == -1)
{
fprintf(stderr, "unable to rename tmp file to '%s', unrecoverable failure\n", filename);
return 0;
}
return 1;
}
int
replaceinpage(const char *outfile, const char *toreplace, struct fileorstring *source)
{
/*
* replace 'toreplace' in file 'outfile' taking 'infile' as input
* return 1 on success, 0 on failure
*/
long substrpos = -1;
substrpos = findstring(frontpage_index_output, toreplace);
if (substrpos < 0)
{
return 0;
}
if (!deletebytes(frontpage_index_output, substrpos, strlen(toreplace)))
{
return 0;
}
if (!writeatbyte(outfile, source, substrpos))
{
return 0;
}
return 1;
}
char
*gettime()
{
return "10/10/2019";
}
int
createtmpfile(const char *name, const char *content, size_t size)
{
FILE *tmp = fopen(name, "w");
if (!tmp)
{
fprintf(stderr, "unable to create temp file '%s', unrecoverable failure\n", name);
return 0;
}
fwrite(content, 1, size, tmp);
fclose(tmp);
return 1;
}
int
frontpage(int flags)
{
if (!createfile(frontpage_index_output))
{
fprintf(stderr, "unable to generate frontpage\n");
return 0;
}
struct fileorstring index = {frontpage_index, NULL};
struct fileorstring title = {NULL, frontpage_title};
struct fileorstring info = {NULL, frontpage_info};
struct fileorstring time = {NULL, gettime()};
if (!replaceinpage(frontpage_index_output, content_string, &index) ||
!replaceinpage(frontpage_index_output, title_string, &title) ||
!replaceinpage(frontpage_index_output, info_string, &info) ||
!replaceinpage(frontpage_index_output, time_string, &time))
{
fprintf(stderr, "unable to generate frontpage\n");
return 0;
}
//remove("title.tmp");
//remove("date.tmp");
//remove("info.tmp");
return 1;
}
|