frag.c (916B)
1 #include <assert.h> 2 3 #include <sys/types.h> 4 #include <errno.h> 5 #include <fcntl.h> 6 #include <limits.h> 7 #include <stdarg.h> 8 #include <stdint.h> 9 #include <stdio.h> 10 #include <string.h> 11 #include <unistd.h> 12 13 #include <kcgi.h> 14 #include <kcgihtml.h> 15 16 #include "page.h" 17 #include "user.h" 18 #include "config.h" 19 20 int 21 frag(struct pagedata *pd, char *name) 22 { 23 int fd; 24 int len; 25 char buf[4096]; 26 char path[PATH_MAX]; 27 int errprint; 28 29 assert(!strchr(name, '/')); 30 31 errprint = snprintf(path, sizeof(path), DataDir "/frags/%s", name); 32 if (errprint < 0 || (unsigned int)errprint >= sizeof(path)) 33 return 1; 34 35 fd = open(path, O_RDONLY); 36 if (fd < 0) { 37 kutil_warn(&pd->req, pd->user ? pd->user->name : NULL, "%s: %s", 38 path, strerror(errno)); 39 return 1; 40 } 41 42 while ((len = read(fd, buf, sizeof(buf))) > 0) { 43 if (khttp_write(&pd->req, buf, len) != KCGI_OK) 44 return 1; 45 } 46 47 if (close(fd) || len < 0) 48 return 1; 49 return 0; 50 }