-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstaticpagegen.c
409 lines (393 loc) · 12.3 KB
/
staticpagegen.c
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* For PATH_MAX, remove and define PATH_MAX if unwanted */
#define _POSIX_C_SOURCE 1
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <errno.h>
#include "xhtmlbasic1-0.h"
#include "staticpagegen.h"
static char *xhtmldirname = "xhtml";
static char *gopherdirname = "gopher";
char *pagetitle = "Page";
char *pagebaseaddr = "server";
enum Gentype gen = GEN_HG;
enum Htmlspec htmspec = HTM_XHTMLBASIC10;
enum Gopherspec gphspec = GPH_GEOMYIDAE;
int debuglvl = 0;
int ignoresymlinks = 0;
struct stat cwdstbuf;
/*
* staticpagegen_main - the main function.
* argpath - SOURCE directory given as an argument, from main.
* Initialises certain values and calls itersrc. Returns to main. See the
* manpage for program details.
*/
int
staticpagegen_main(char *argpath)
{
char cwd[PATH_MAX];
struct stat stbuf;
if (strlen(argpath) >= FILENAME_MAX) {
(void)fprintf(stderr, "SOURCE folder name too long\n");
return 1;
}
if (stat(argpath, &stbuf))
return staticpagegen_perror(argpath);
if (!S_ISDIR(stbuf.st_mode)) {
errno = ENOTDIR;
(void)fprintf(stderr, "staticpagegen: failed to use '%s': %s\n",
argpath, strerror(errno));
return 1;
}
if (gen != GEN_GONLY) {
if (mkdir(xhtmldirname, 0700))
return staticpagegen_perror(xhtmldirname);
if (debuglvl > 0)
(void)fprintf(stderr, "Created directory '%s'\n", xhtmldirname);
}
if (gen != GEN_HONLY) {
if (mkdir(gopherdirname, 0700))
return staticpagegen_perror(gopherdirname);
if (debuglvl > 0)
(void)fprintf(stderr, "Created directory '%s'\n", gopherdirname);
}
if (!getcwd(cwd, PATH_MAX))
return staticpagegen_perror(cwd);
if (stat(cwd, &cwdstbuf))
return staticpagegen_perror(cwd);
return itersrc(argpath, "");
}
/*
* itersrc - iterates through directories and calls appropriate functions
* depending on the file.
* path - directory to iterate.
* genpath - partial relative path for the directories to generate.
*/
int
itersrc(char *path, char *genpath)
{
struct stat pathstbuf;
DIR *d;
struct dirent *dir;
int wrpages_rval = 0;
if (stat(path, &pathstbuf))
return staticpagegen_perror(path);
if ((d = opendir(path))) {
char *subdirs[SUBDIRN_MAX];
char *subfils[SUBDIRN_MAX];
unsigned int subdirs_n = 0;
unsigned int subfils_n = 0;
if (debuglvl > 1)
(void)fprintf(stderr, "Iterating directory '%s'\n", path);
while ((dir = readdir(d))) {
struct stat stbuf;
char nextpath[PATH_MAX];
char nextgenpath[PATH_MAX];
if (snprintf(nextpath, PATH_MAX, "%s/%s", path, dir->d_name) >=
PATH_MAX) {
(void)fprintf(stderr, "staticpagegen: can't use '%s/%s': File"
" name too long'\n", path, dir->d_name);
return 1;
}
if (!ignoresymlinks) {
if (stat(nextpath, &stbuf))
return staticpagegen_perror(nextpath);
}
else {
if (lstat(nextpath, &stbuf))
return staticpagegen_perror(nextpath);
}
if (snprintf(nextgenpath, PATH_MAX, "%s/%s", genpath, dir->d_name)
>= PATH_MAX) {
(void)fprintf(stderr, "staticpagegen: can't use '%s/%s': File"
" name too long'\n", genpath, dir->d_name);
return 1;
}
if (S_ISDIR(stbuf.st_mode) && strcmp(dir->d_name, ".") &&
strcmp(dir->d_name, "..") && !(pathstbuf.st_ino ==
cwdstbuf.st_ino && (!strcmp(dir->d_name, xhtmldirname) ||
!strcmp(dir->d_name, gopherdirname)))) {
subdirs[subdirs_n] = dir->d_name;
subdirs_n += 1;
mkpagepaths(nextgenpath);
if (itersrc(nextpath, nextgenpath))
return -1;
} else if (S_ISREG(stbuf.st_mode)) {
subfils[subfils_n] = dir->d_name;
subfils_n += 1;
cpfiles(nextpath, nextgenpath);
} else {
if (debuglvl > 1)
(void)fprintf(stderr, "Ignoring node '%s'\n", dir->d_name);
}
}
if ((wrpages_rval = wrpages(genpath, subdirs, subdirs_n, subfils,
subfils_n)) != 0)
return wrpages_rval;
if (closedir(d))
return staticpagegen_perror(path);
} else
return staticpagegen_perror(path);
return 0;
}
/*
* mkpagepaths - makes the generated page directories.
* nextgenpath - partial relative path for the directories to generate.
*/
int
mkpagepaths(char *nextgenpath)
{
char xhtmlgenpath[PATH_MAX];
char gophergenpath[PATH_MAX];
if (gen != GEN_GONLY) {
if (snprintf(xhtmlgenpath, PATH_MAX, "%s%s", xhtmldirname, nextgenpath)
>= PATH_MAX) {
(void)fprintf(stderr, "staticpagegen: can't use '%s%s': File"
" name too long'\n", xhtmldirname, nextgenpath);
return 1;
}
if (mkdir(xhtmlgenpath, 0700))
return staticpagegen_perror(xhtmlgenpath);
if (debuglvl > 0)
(void)fprintf(stderr, "Created directory '%s'\n", xhtmlgenpath);
}
if (gen != GEN_HONLY) {
if (snprintf(gophergenpath, PATH_MAX, "%s%s", gopherdirname,
nextgenpath) >= PATH_MAX) {
(void)fprintf(stderr, "staticpagegen: can't use '%s%s': File"
" name too long'\n", gopherdirname, nextgenpath);
return 1;
}
if (mkdir(gophergenpath, 0700))
return staticpagegen_perror(gophergenpath);
if (debuglvl > 0)
(void)fprintf(stderr, "Created directory '%s'\n", gophergenpath);
}
return 0;
}
/*
* cpfiles - copies the files from the iterated directories to the
* generated directories.
* nextpath - path for the source file.
* nextgenpath - partial relative path for the destination files.
*/
int
cpfiles(char *nextpath, char *nextgenpath)
{
char xhtmlgenpath[PATH_MAX];
char gophergenpath[PATH_MAX];
char byte;
FILE *inptr;
FILE *xhtmloutptr;
FILE *gopheroutptr;
if (!(inptr = fopen(nextpath, "r")))
return staticpagegen_perror(nextpath);
if (gen != GEN_GONLY) {
if (snprintf(xhtmlgenpath, PATH_MAX, "%s%s", xhtmldirname, nextgenpath)
>= PATH_MAX) {
(void)fprintf(stderr, "staticpagegen: can't use '%s%s': File"
" name too long'\n", xhtmldirname, nextgenpath);
return 1;
}
if (!(xhtmloutptr = fopen(xhtmlgenpath, "w")))
return staticpagegen_perror(xhtmlgenpath);
if (debuglvl > 0)
(void)fprintf(stderr, "Copying file '%s' to '%s'\n", nextpath,
xhtmlgenpath);
while (1) {
if ((byte = fgetc(inptr)) == EOF && feof(inptr)) {
clearerr(inptr);
fseek(inptr, 0, SEEK_SET);
break;
} else if (byte == EOF && ferror(inptr))
return staticpagegen_perror(nextpath);
if (fputc(byte, xhtmloutptr) == EOF && ferror(xhtmloutptr))
return staticpagegen_perror(xhtmlgenpath);
}
if (fclose(xhtmloutptr))
return staticpagegen_perror(xhtmlgenpath);
}
if (gen != GEN_HONLY) {
if (snprintf(gophergenpath, PATH_MAX, "%s%s", gopherdirname,
nextgenpath) >= PATH_MAX) {
(void)fprintf(stderr, "staticpagegen: can't use '%s%s': File"
" name too long'\n", gopherdirname, nextgenpath);
return 1;
}
if (!(gopheroutptr = fopen(gophergenpath, "w")))
return staticpagegen_perror(gophergenpath);
if (debuglvl > 0)
(void)fprintf(stderr, "Copying file '%s' to '%s'\n", nextpath,
gophergenpath);
while (1) {
if ((byte = fgetc(inptr)) == EOF && feof(inptr)) {
clearerr(inptr);
fseek(inptr, 0, SEEK_SET);
break;
} else if (byte == EOF && ferror(inptr))
return 1;
if (fputc(byte, gopheroutptr) == EOF && ferror(gopheroutptr))
return staticpagegen_perror(gophergenpath);
}
if (fclose(gopheroutptr))
return staticpagegen_perror(gophergenpath);
}
if (fclose(inptr))
return staticpagegen_perror(nextpath);
return 0;
}
/*
* wrpages - writes the contents of the gopher and xhtml pages.
* genpath - partial relative path for the page files to be generated.
* subdirs - directories located in where the generated pages will be.
* subdirs_n - subdir count.
* subflis - files located in where the generated pages will be.
* subfils_n - subfil count.
*/
int
wrpages(char *genpath, char **subdirs, unsigned int
subdirs_n, char **subfils, unsigned int subfils_n)
{
if (gen != GEN_GONLY && htmspec == HTM_XHTMLBASIC10) {
unsigned int i;
FILE *idxptr;
char xhtmlindexfil[PATH_MAX];
if (snprintf(xhtmlindexfil, PATH_MAX, "%s%s/index.xhtml", xhtmldirname,
genpath) >= PATH_MAX) {
(void)fprintf(stderr, "staticpagegen: can't use '%s%s/index.xhtml':"
" File name too long'\n", xhtmldirname, genpath);
return 1;
}
if ((idxptr = fopen(xhtmlindexfil, "w")) == NULL)
return staticpagegen_perror(xhtmlindexfil);
if (debuglvl > 0)
(void)fprintf(stderr, "Created file '%s'\n", xhtmlindexfil);
if (fprintf(idxptr, "%s%s%s%s%s", xhtmlbasic10top, pagetitle,
xhtmlbasic10mid1, pagetitle, xhtmlbasic10mid2) !=
(int) (strlen(xhtmlbasic10top) + 2 * strlen(pagetitle) +
strlen(xhtmlbasic10mid1) + strlen(xhtmlbasic10mid2))) {
(void)fprintf(stderr, "staticpagegen: can't write to file '%s'\n",
xhtmlindexfil);
return 1;
}
if (strcmp(genpath, "")) {
char prevgenpath[PATH_MAX];
unsigned int lastslashi = 0;
for (i = 1; i < strlen(genpath); i++) {
if (genpath[i] == '/')
lastslashi = i;
}
(void)strncpy(prevgenpath, genpath, lastslashi);
prevgenpath[lastslashi] = '\0';
if (fprintf(idxptr, "<a href=\"%s/\">../</a>\n",
prevgenpath) != 20 + (int) strlen(prevgenpath)) {
(void)fprintf(stderr, "staticpagegen: can't write to file"
" '%s'\n", xhtmlindexfil);
return 1;
}
}
for (i = 0; i < subdirs_n; i++) {
if (fprintf(idxptr, "<a href=\"%s/%s/index.xhtml\">%s/</a>\n",
genpath, subdirs[i], subdirs[i]) != 30 + (int)
(strlen(genpath) + 2 * strlen(subdirs[i]))) {
(void)fprintf(stderr, "staticpagegen: can't write to file"
" '%s'\n", xhtmlindexfil);
return 1;
}
}
for (i = 0; i < subfils_n; i++) {
if (fprintf(idxptr, "<a href=\"%s/%s\">%s</a>\n",
genpath, subfils[i], subfils[i]) != 17 + (int)
(strlen(genpath) + 2 * strlen(subfils[i]))) {
(void)fprintf(stderr, "staticpagegen: can't write to file"
" '%s'\n", xhtmlindexfil);
return 1;
}
}
if (fprintf(idxptr, "%s", xhtmlbasic10bot) !=
(int) strlen(xhtmlbasic10bot)) {
(void)fprintf(stderr, "staticpagegen: can't write to file"
" '%s'\n", xhtmlindexfil);
return 1;
}
if (fclose(idxptr) != 0)
return staticpagegen_perror(xhtmlindexfil);
}
if (gen != GEN_HONLY && gphspec == GPH_GEOMYIDAE) {
unsigned int i;
FILE *idxptr;
char gopherindexfil[PATH_MAX];
if (snprintf(gopherindexfil, PATH_MAX, "%s%s/index.gph", gopherdirname,
genpath) >= PATH_MAX) {
(void)fprintf(stderr, "staticpagegen: can't use '%s%s/index.gph':"
" File name too long'\n", gopherdirname, genpath);
return 1;
}
if ((idxptr = fopen(gopherindexfil, "w")) == NULL)
return staticpagegen_perror(gopherindexfil);
if (debuglvl > 0)
(void)fprintf(stderr, "Created file '%s'\n", gopherindexfil);
if (fprintf(idxptr, "[1|%s|/|%s|port]\n", pagetitle,
pagebaseaddr) != 13 +
(int) (strlen(pagetitle) + strlen(pagebaseaddr))) {
(void)fprintf(stderr, "staticpagegen: can't write to file '%s'\n",
gopherindexfil);
return 1;
}
if (strcmp(genpath, "")) {
char prevgenpath[PATH_MAX];
unsigned int lastslashi = 0;
for (i = 1; i < strlen(genpath); i++) {
if (genpath[i] == '/')
lastslashi = i;
}
(void)strncpy(prevgenpath, genpath, lastslashi);
prevgenpath[lastslashi] = '\0';
if (fprintf(idxptr, "[1|../|%s/|%s|port]\n", prevgenpath,
pagebaseaddr) != 16 + (int) (strlen(prevgenpath) +
strlen(pagebaseaddr))) {
(void)fprintf(stderr, "staticpagegen: can't write to file '%s'\n",
gopherindexfil);
return 1;
}
}
for (i = 0; i < subdirs_n; i++) {
if (fprintf(idxptr, "[1|%s/|%s/%s/|%s|port]\n", subdirs[i], genpath,
subdirs[i], pagebaseaddr) != 15 + (int) (2 *
strlen(subdirs[i]) + strlen(genpath) +
strlen(pagebaseaddr))) {
(void)fprintf(stderr, "staticpagegen: can't write to file '%s'\n",
gopherindexfil);
return 1;
}
}
for (i = 0; i < subfils_n; i++) {
if (fprintf(idxptr, "[0|%s|%s/%s|%s|port]\n", subfils[i], genpath,
subfils[i], pagebaseaddr) != 13 + (int) (2 *
strlen(subfils[i]) + strlen(genpath) +
strlen(pagebaseaddr))) {
(void)fprintf(stderr, "staticpagegen: can't write to file '%s'\n",
gopherindexfil);
return 1;
}
}
if (fclose(idxptr) != 0)
return staticpagegen_perror(gopherindexfil);
}
return 0;
}
/*
* staticpagegen_perror - prints the program name alongside the error message.
* probstr - string related to the problem.
*/
int
staticpagegen_perror(char *probstr)
{
(void)fprintf(stderr, "staticpagegen: ");
perror(probstr);
return errno;
}