diff options
author | Marius Halden <marius.h@lden.org> | 2016-08-19 16:00:54 +0200 |
---|---|---|
committer | Marius Halden <marius.h@lden.org> | 2016-08-19 16:00:54 +0200 |
commit | b6125c9eac61eca81eae42fb63a26ce16a8dc8db (patch) | |
tree | 7633e9b588bc60b1e29a4b23b7f049cdfec0856b /newbatch.c | |
download | runq-b6125c9eac61eca81eae42fb63a26ce16a8dc8db.tar.gz runq-b6125c9eac61eca81eae42fb63a26ce16a8dc8db.tar.bz2 runq-b6125c9eac61eca81eae42fb63a26ce16a8dc8db.tar.xz |
Initial commit
Diffstat (limited to 'newbatch.c')
-rw-r--r-- | newbatch.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/newbatch.c b/newbatch.c new file mode 100644 index 0000000..6b27234 --- /dev/null +++ b/newbatch.c @@ -0,0 +1,70 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/time.h> +#include <errno.h> +#include <err.h> +#include <string.h> +#include <fcntl.h> + +#define DEFAULT_QUEUE_DIR "." + +int +main(int argc, char **argv) +{ + pid_t mypid; + struct timeval tv; + int64_t microtime; + char *timestr = NULL, *queuedir = NULL, *tmpfile = NULL, *newfile = NULL; + char buf[1024], *tmp; + int fd, l, m; + + mypid = getpid(); + gettimeofday(&tv, NULL); + microtime = tv.tv_sec * 1000000 + tv.tv_usec; + asprintf(×tr, "%li.%u", microtime, mypid); + + if (argc > 1) { + queuedir = strdup(argv[1]); + } else { + queuedir = strdup(DEFAULT_QUEUE_DIR); + } + + asprintf(&tmpfile, "%s/tmp/%s", queuedir, timestr); + asprintf(&newfile, "%s/new/%s", queuedir, timestr); + free(timestr); + free(queuedir); + + fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0660); + if (fd == -1) + err(1, "open()"); + + while ((l = read(STDIN_FILENO, buf, sizeof(buf))) != 0) { + if (l == -1) { + unlink(tmpfile); + err(1, NULL); + } + + tmp = buf; + while (l > 0) { + m = write(fd, tmp, l); + if (m == -1) { + unlink(tmpfile); + err(1, NULL); + } + l -= m; + tmp += m; + } + } + close(fd); + + if (rename(tmpfile, newfile) == -1) { + unlink(tmpfile); + err(1, NULL); + } + + free(tmpfile); + free(newfile); + + return 0; +} |