X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=tools%2Ftools.c;h=b78702d03a6d1ff26df2a1d412f3bb2965d40b67;hp=2a776f062a354cea9f233b011ef1a4122038d3b1;hb=6aa2f4e347e5d66a392b879fe901bc582099a552;hpb=dc8042b42500f79f613b1197df6cdf739615a89f diff --git a/tools/tools.c b/tools/tools.c index 2a776f06..b78702d0 100644 --- a/tools/tools.c +++ b/tools/tools.c @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include #include @@ -25,42 +27,6 @@ bool tools_verbose = false; /* Ten minutes. */ const unsigned int default_timeout_ms = 10 * 60 * 1000; -char *tal_basename(const void *ctx, const char *dir) -{ - const char *p = strrchr(dir, '/'); - - if (!p) - return tal_strdup(ctx, dir); - return tal_strdup(ctx, p+1); -} - -char *tal_dirname(const void *ctx, const char *dir) -{ - const char *p = strrchr(dir, '/'); - - if (!p) - return tal_strdup(ctx, "."); - return tal_strndup(ctx, dir, p - dir); -} - -char *tal_getcwd(const void *ctx) -{ - unsigned int len; - char *cwd; - - /* *This* is why people hate C. */ - len = 32; - cwd = tal_arr(ctx, char, len); - while (!getcwd(cwd, len)) { - if (errno != ERANGE) { - tal_free(cwd); - return NULL; - } - tal_resize(&cwd, len *= 2); - } - return cwd; -} - static void killme(int sig) { kill(-getpid(), SIGKILL); @@ -73,7 +39,7 @@ char *run_with_timeout(const void *ctx, const char *cmd, int p[2]; struct rbuf in; int status, ms; - struct timespec start; + struct timeabs start; *ok = false; if (pipe(p) != 0) @@ -105,7 +71,7 @@ char *run_with_timeout(const void *ctx, const char *cmd, signal(SIGALRM, killme); itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0; - itim.it_value = timespec_to_timeval(time_from_msec(*timeout_ms)); + itim.it_value = timespec_to_timeval(time_from_msec(*timeout_ms).ts); setitimer(ITIMER_REAL, &itim, NULL); status = system(cmd); @@ -124,7 +90,7 @@ char *run_with_timeout(const void *ctx, const char *cmd, if (waitpid(pid, &status, 0) != pid) err(1, "Failed to wait for child"); - ms = time_to_msec(time_sub(time_now(), start)); + ms = time_to_msec(time_between(time_now(), start)); if (ms > *timeout_ms) *timeout_ms = 0; else @@ -229,35 +195,30 @@ void keep_temp_dir(void) char *temp_file(const void *ctx, const char *extension, const char *srcname) { - unsigned baselen; - char *f, *suffix = tal_strdup(ctx, ""); + char *f, *base, *suffix; struct stat st; unsigned int count = 0; - srcname = tal_basename(ctx, srcname); - if (strrchr(srcname, '.')) - baselen = strrchr(srcname, '.') - srcname; - else - baselen = strlen(srcname); + base = path_join(ctx, temp_dir(), take(path_basename(ctx, srcname))); + /* Trim extension. */ + base[path_ext_off(base)] = '\0'; + suffix = tal_strdup(ctx, extension); do { - f = tal_fmt(ctx, "%s/%.*s%s%s", - temp_dir(), baselen, srcname, suffix, extension); - tal_free(suffix); - suffix = tal_fmt(ctx, "-%u", ++count); + f = tal_strcat(ctx, base, suffix); + suffix = tal_fmt(base, "-%u%s", ++count, extension); } while (lstat(f, &st) == 0); if (tools_verbose) printf("Creating file %s\n", f); - tal_free(suffix); + tal_free(base); return f; } bool move_file(const char *oldname, const char *newname) { char *contents; - size_t size; int fd; bool ret; @@ -272,7 +233,7 @@ bool move_file(const char *oldname, const char *newname) } /* Try copy and delete: not atomic! */ - contents = tal_grab_file(NULL, oldname, &size); + contents = grab_file(NULL, oldname); if (!contents) { if (tools_verbose) printf("read failed: %s\n", strerror(errno)); @@ -287,7 +248,7 @@ bool move_file(const char *oldname, const char *newname) goto free; } - ret = write_all(fd, contents, size); + ret = write_all(fd, contents, tal_count(contents)-1); if (close(fd) != 0) ret = false; @@ -311,23 +272,3 @@ void *do_tal_realloc(void *p, size_t size) tal_resize((char **)&p, size); return p; } - -void *tal_grab_file(const void *ctx, const char *filename, size_t *size) -{ - struct rbuf rbuf; - char *buf = tal_arr(ctx, char, 0); - - if (!rbuf_open(&rbuf, filename, buf, 0)) - return tal_free(buf); - - if (!rbuf_fill_all(&rbuf, do_tal_realloc) && errno) - rbuf.buf = tal_free(rbuf.buf); - else { - rbuf.buf[rbuf.len] = '\0'; - if (size) - *size = rbuf.len; - } - close(rbuf.fd); - - return rbuf.buf; -}