aboutsummaryrefslogtreecommitdiffstats
path: root/usr/ln.c
blob: 6a98982a1b2a58040c47724c53528c83c7d0ee79 (plain) (blame)
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
#include "gbos.h"
/* ln TARGET NAME: give an existing file a second name.
 *
 * A real hard link, not a copy: both names share one inode, so the data is
 * stored once and `rm` only frees it when the last name goes. gbfs always had
 * the link count - nothing had ever incremented it.
 *
 * The syscall takes a request block (two paths, and the trap needs HL for its
 * dispatch table), and that block is a static living at $A000 - so the paths
 * are copied to the stack first, like every other program that has statics. */

static unsigned char req[4];

void main(void) {
    char paths[64];
    char *argv[4];
    unsigned char argc = argv_parse(argv, 4);
    unsigned char i, j = 0;
    char *tgt, *name;

    if (argc < 2) {
        puts("usage: ln TARGET NAME");
        nl();
        sexit(2);
    }
    tgt = paths;
    for (i = 0; argv[0][i] && j < 30; i++) paths[j++] = argv[0][i];
    paths[j++] = 0;
    name = paths + j;
    for (i = 0; argv[1][i] && j < 62; i++) paths[j++] = argv[1][i];
    paths[j] = 0;

    req[0] = (unsigned char)(unsigned int)tgt;
    req[1] = (unsigned char)(((unsigned int)tgt) >> 8);
    req[2] = (unsigned char)(unsigned int)name;
    req[3] = (unsigned char)(((unsigned int)name) >> 8);
    if (sys_link(req) != 0) {
        puts("ln: cannot link ");
        puts(name);
        nl();
        sexit(1);
    }
}