/* * run.c - Run helper scripts * * Copyright 2012 by Werner Almesberger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. */ #define _GNU_SOURCE /* for vasprintf */ #include #include #include #include #include "run.h" void run_cmd(const char *fmt, ...) { va_list ap; char *tmp; int res; va_start(ap, fmt); if (vasprintf(&tmp, fmt, ap) < 0) { perror("vasprintf"); exit(1); } res = system(tmp); if (res < 0) { perror("system"); exit(1); } if (res) { fprintf(stderr, "\"%s\" returned %d\n", tmp, res); exit(1); } } void cat(FILE *out, const char *name) { FILE *in; char buf[10000]; /* pick any good size */ size_t got, wrote; in = fopen(name, "r"); if (!in) { perror(name); exit(1); } while (1) { got = fread(buf, 1, sizeof(buf), in); if (!got) break; wrote = fwrite(buf, 1, got, out); if (wrote != got) { perror("fwrite"); exit(1); } } if (ferror(in)) { perror(name); exit(1); } fclose(in); }