~vdupras/collapseos

ref: ac3629b817115b1af75096856ee9499cfbbd3392 collapseos/tools/exec.c -rw-r--r-- 1.1 KiB
ac3629b8Virgil Dupras Make BLK@* and BLK!* into ialiases 1 year, 10 months ago
                                                                                
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
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#include "common.h"

/* Execute code from target file on the target machine.
   fname can be "-" for stdin.
 */

int main(int argc, char **argv)
{
    if (argc != 3) {
        fprintf(stderr, "Usage: ./exec device fname\n");
        return 1;
    }
    FILE *fp = stdin;
    if (strcmp(argv[2], "-") != 0) {
        fp = fopen(argv[2], "r");
        if (fp == NULL) {
            fprintf(stderr, "Could not open %s\n", argv[2]);
            return 1;
        }
    }
    int fd = ttyopen(argv[1]);
    if (fd < 0) {
        fprintf(stderr, "Could not open %s\n", argv[1]);
        return 1;
    }
    set_blocking(fd, 0);
    int c = getc(fp);
    while (c != EOF) {
        if (c == '\n') c = '\r';
        write(fd, &c, 1);
        usleep(1000); // let it breathe
        while (read(fd, &c, 1) == 1) {
            putchar(c);
            fflush(stdout);
        }
        c = getc(fp);
    }
    if (fd > 0) {
        close(fd);
    }
    if (strcmp(argv[2], "-") != 0) {
        fclose(fp);
    }
    printf("Done!\n");
    return 0;
}