/* * runfile - rf * * a no-dependancy, minimal * POSIX C99 Make/justfile * alternative to run commands. * * czjstmax * */ #define _POSIX_C_SOURCE 200809L #define RUNFILE_VERSION 1 #include #include #include #include #include #include void usage(); int main(int argc, char **argv) { if (argc > 2) usage(); else if (argc >= 2) { for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) usage(); } } FILE *f = fopen("rfile", "r"); if (!f) { printf("no rfile found.\n"); return 1; } wordexp_t p; char *line = NULL; size_t len = 0; while (getline(&line, &len, f) != -1) { line[strcspn(line, "\n")] = '\0'; printf("$ %s\n", line); wordexp(line, &p, 0); pid_t pid = fork(); if (pid == 0) { execvp(p.we_wordv[0], p.we_wordv); perror("failed to run command"); wordfree(&p); free(line); fclose(f); _exit(1); } else if (pid > 0) { int status; waitpid(pid, &status, 0); } else { perror("fork failed"); return 1; } } wordfree(&p); free(line); fclose(f); return 0; } void usage() { printf( "< runfile usage >\n" "syn: rf [section]\n" ); }