aboutsummaryrefslogtreecommitdiff
path: root/later.c
blob: 5f59577d90b4ebac0f62643a7ca56d53f3a12850 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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;
}