/* gitindex - static repository index for /mnt/marlonivo.com/code * * usage: gitindex ROOT OUT * Scans ROOT for git repositories and writes a static HTML index to OUT. * Build: cc -O2 -o /usr/bin/gitindex gitindex.c */ #include #include #include #include #include #define nil NULL static char * esc(const char *s) { size_t n = strlen(s); char *b = malloc(n * 6 + 1); char *p = b; for(; *s; s++){ switch(*s){ case '&': p += sprintf(p, "&"); break; case '<': p += sprintf(p, "<"); break; case '>': p += sprintf(p, ">"); break; case '"': p += sprintf(p, """); break; default: *p++ = *s; } } *p = 0; return b; } static int cmpstr(const void *a, const void *b) { return strcmp(*(char **)a, *(char **)b); } int main(int argc, char **argv) { if(argc != 3){ fprintf(stderr, "usage: gitindex ROOT OUT\n"); return 1; } DIR *d = opendir(argv[1]); if(d == nil){ perror("opendir"); return 1; } char **repos = malloc(sizeof *repos * 512); int n = 0; struct dirent *e; while((e = readdir(d)) != nil){ if(e->d_name[0] == '.') continue; char path[4096]; snprintf(path, sizeof path, "%s/%s/.git", argv[1], e->d_name); struct stat st; if(stat(path, &st) != 0 || !S_ISDIR(st.st_mode)) continue; repos[n++] = strdup(e->d_name); } closedir(d); qsort(repos, n, sizeof *repos, cmpstr); FILE *f = fopen(argv[2], "w"); if(f == nil){ perror("fopen"); return 1; } fprintf(f, "\n\n\n\ncode\n\n\n\n
+
\n

Repositories

\n
    \n"); for(int i = 0; i < n; i++){ char *x = esc(repos[i]); fprintf(f, "
  • %s
  • \n", x, x); free(x); } fprintf(f, "
\n\n\n"); fclose(f); return 0; }