hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/user/save.c
blob: 056e160d6bb7db2dd030ee7d9d3b8fdf1cf6556b (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
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char** argv)
{
    if (argc < 2)
    {
        printf("usage: run save <path>\n");
        return 1;
    }

    const char* name = argv[1];
    int fd = open(name, O_CREAT | O_TRUNC | O_WRONLY);
    if (fd < 0)
    {
        printf("save: %s: cannot open\n", name);
        return 1;
    }

    printf("saving to %s -- type lines, a single . saves\n", name);

    for (;;)
    {
        char line[256];
        int bytes_read = readline(line, 256);
        printf("\n");
        if (bytes_read == 1 && line[0] == '.')
            break;
        write(fd, line, (unsigned long)bytes_read);
        write(fd, "\n", 1);
    }

    close(fd);
    printf("saved %s\n", name);
    return 0;
}