diff options
| author | Ben Winston | 2025-11-10 22:29:09 -0500 |
|---|---|---|
| committer | Ben Winston | 2025-11-10 22:52:42 -0500 |
| commit | e5fbef8e416fe89d1fe2f7840383c26528b6b8d4 (patch) | |
| tree | cb286f986064be72d0d46d764aab4783050604ad /later.c | |
initial commit, writes args and pwd into a file
Diffstat (limited to 'later.c')
| -rw-r--r-- | later.c | 61 |
1 files changed, 61 insertions, 0 deletions
@@ -0,0 +1,61 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + + +int main(int argc, char** argv) { + /* how it could work + * - read in tokens + * - wait for -- + * - after --, read everything in and save to the LATERFILE + * - make sure to also save the PWD + * + * Format is: + * FROM <pwd> + * <command> + * -- + * + */ + char* laterfile_path = getenv("LATERFILE"); + if (!laterfile_path || strlen(laterfile_path) == 0) { + printf("where's your LATERFILE?\n"); + return 1; + } + + FILE* laterfile = fopen(laterfile_path, "a"); + + // loop through the arguments + int argi = 1; + while (argi < argc && strncmp(argv[argi], "--", 2) != 0) + argi++; + + if (argi == argc) { + printf("unrecognized arguments\n"); + return 1; + } + + argi++; // go past the -- + + // determine the current working directory + char pwd[255]; + getcwd(pwd, 254); + + // TODO check err + + // write the output + fwrite("FROM ", 1, 5, laterfile); + fwrite(pwd, 1, strlen(pwd), laterfile); + fwrite("\n", 1, 1, laterfile); + + // write out the command + for (int i = argi; i < argc; i++) { + fwrite(argv[i], 1, strlen(argv[i]), laterfile); + fwrite(" ", 1, 1, laterfile); + } + + // write the terminal + fwrite("\n--\n", 1, 4, laterfile); + + return 0; +} |
