/** * This CGI program renders the oper.io blog * Copyright (C) 2024 Aaron Ball * * 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 . */ #include "common.h" int catfile(char* path) { char c = 0; FILE* fd = fopen(path, "r"); if(!fd) return 2; while((c = fgetc(fd)) != EOF) putchar(c); fclose(fd); return 0; } int html_read_title(char* path, char* out) { char buf[TITLE_MAX]; int start = 0; int end = 0; FILE* fd; fd = fopen(path, "r"); if(!fd) return 2; fgets(buf, TITLE_MAX, fd); fclose(fd); if(strncmp(buf, "", 5) == 0) { strncpy(out, &buf[start], end - start); out[end] = '\0'; return 1; } end++; } return -1; } char* runsh(char* cmd) { char buf[2048] = {'\0'}; char* out = malloc(1); FILE* fd = NULL; unsigned long len = 0; fd = popen(cmd, "r"); while(fgets(buf, 2048, fd) != NULL) { len += strlen(buf); out = realloc(out, len + 1); strcat(out, buf); } pclose(fd); if(out[len - 1] == '\n') out[len-1] = '\0'; return out; }