Root/genkicat/run.c

1/*
2 * run.c - Run helper scripts
3 *
4 * Copyright 2012 by Werner Almesberger
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#define _GNU_SOURCE /* for vasprintf */
13#include <stdarg.h>
14#include <stdlib.h>
15#include <stdio.h>
16#include <sys/types.h>
17
18#include "run.h"
19
20
21void run_cmd(const char *fmt, ...)
22{
23    va_list ap;
24    char *tmp;
25    int res;
26
27    va_start(ap, fmt);
28    if (vasprintf(&tmp, fmt, ap) < 0) {
29        perror("vasprintf");
30        exit(1);
31    }
32    res = system(tmp);
33    if (res < 0) {
34        perror("system");
35        exit(1);
36    }
37    if (res) {
38        fprintf(stderr, "\"%s\" returned %d\n", tmp, res);
39        exit(1);
40    }
41}
42
43
44void cat(FILE *out, const char *name)
45{
46    FILE *in;
47    char buf[10000]; /* pick any good size */
48    size_t got, wrote;
49
50    in = fopen(name, "r");
51    if (!in) {
52        perror(name);
53        exit(1);
54    }
55    while (1) {
56        got = fread(buf, 1, sizeof(buf), in);
57        if (!got)
58            break;
59        wrote = fwrite(buf, 1, got, out);
60        if (wrote != got) {
61            perror("fwrite");
62            exit(1);
63        }
64    }
65    if (ferror(in)) {
66        perror(name);
67        exit(1);
68    }
69    fclose(in);
70}
71

Archive Download this file

Branches:
master



interactive