1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-23 05:31:53 +02:00
eda-tools/genkicat/run.c

71 lines
1.2 KiB
C
Raw Normal View History

/*
* 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) {
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);
}