2012-07-12 05:07:59 +03:00
|
|
|
/*
|
|
|
|
* 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 <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#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) {
|
2012-07-12 23:09:11 +03:00
|
|
|
fprintf(stderr, "\"%s\" returned %d\n", tmp, res);
|
2012-07-12 05:07:59 +03:00
|
|
|
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);
|
|
|
|
}
|