From 651babfefa21536112cbcd799932b3595aeb2da4 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Wed, 19 Jul 2017 11:29:27 +0300 Subject: Rename struct filecapture to stream_capture; portability fixes --- Makefile | 3 + getc.c | 22 +++--- getl.c | 9 +-- runcap.c | 232 ++++++++++++++++++++++++++++++------------------------------- runcap.h | 39 ++++++----- seek.c | 26 +++---- t/Makefile | 8 ++- t/rt.c | 40 ++++++----- tell.c | 14 ++-- 9 files changed, 198 insertions(+), 195 deletions(-) diff --git a/Makefile b/Makefile index 03b87d8..36cec3e 100644 --- a/Makefile +++ b/Makefile @@ -39,6 +39,9 @@ CFLAGS = $(O) LDFLAGS = ARFLAGS = cru +.c.o: + $(CC) -c -o$@ $(CPPFLAGS) $(CFLAGS) $< + all: libruncap.a $(OBJECTS): $(HEADERS) diff --git a/getc.c b/getc.c index fce14ea..ed6713d 100644 --- a/getc.c +++ b/getc.c @@ -21,34 +21,34 @@ #include "runcap.h" int -runcap_getc(struct runcap *rc, int stream, char *cp) +runcap_getc(struct runcap *rc, int sd, char *cp) { - struct filecapture *fp; + struct stream_capture *cap; if (!cp) { errno = EINVAL; return -1; } - fp = runcap_filecapture(rc, stream); - if (!fp) + cap = runcap_stream_capture(rc, sd); + if (!cap) return -1; - if (fp->fc_level == fp->fc_cur) { - if (fp->fc_tmpfd != -1) { - ssize_t r = read(fp->fc_tmpfd, fp->fc_base, - fp->fc_size); + if (cap->sc_level == cap->sc_cur) { + if (cap->sc_storfd != -1) { + ssize_t r = read(cap->sc_storfd, cap->sc_base, + cap->sc_size); if (r < 0) return -1; else if (r == 0) return 0; - fp->fc_level = r; - fp->fc_cur = 0; + cap->sc_level = r; + cap->sc_cur = 0; } else { return 0; } } - *cp = fp->fc_base[fp->fc_cur++]; + *cp = cap->sc_base[cap->sc_cur++]; return 1; } diff --git a/getl.c b/getl.c index d989aa9..4bd352a 100644 --- a/getl.c +++ b/getl.c @@ -21,9 +21,8 @@ #include "runcap.h" ssize_t -runcap_getline(struct runcap *rc, int stream, char **pstr, size_t *psize) +runcap_getline(struct runcap *rc, int sd, char **pstr, size_t *psize) { - struct filecapture *fp; char *str; size_t size; size_t n; @@ -38,10 +37,6 @@ runcap_getline(struct runcap *rc, int stream, char **pstr, size_t *psize) str = *pstr; size = *psize; - fp = runcap_filecapture(rc, stream); - if (!fp) - return -1; - if (!str || size == 0) { /* Initial allocation */ size = 16; @@ -53,7 +48,7 @@ runcap_getline(struct runcap *rc, int stream, char **pstr, size_t *psize) } n = 0; - while ((res = runcap_getc(rc, stream, &c)) == 1) { + while ((res = runcap_getc(rc, sd, &c)) == 1) { if (n == size) { char *p; size_t sz; diff --git a/runcap.c b/runcap.c index 9abc492..de2f3a6 100644 --- a/runcap.c +++ b/runcap.c @@ -27,66 +27,66 @@ #include "runcap.h" static int -filecapture_alloc(struct filecapture *fc, size_t size) +stream_capture_alloc(struct stream_capture *cap, size_t size) { if (size) { - fc->fc_base = malloc(size); - if (!fc->fc_base) + cap->sc_base = malloc(size); + if (!cap->sc_base) return -1; } else - fc->fc_base = NULL; - fc->fc_size = size; - fc->fc_leng = 0; - fc->fc_level = 0; - fc->fc_nlines = 0; - fc->fc_cur = 0; - fc->fc_tmpfd = -1; - fc->fc_fd = -1; + cap->sc_base = NULL; + cap->sc_size = size; + cap->sc_leng = 0; + cap->sc_level = 0; + cap->sc_nlines = 0; + cap->sc_cur = 0; + cap->sc_storfd = -1; + cap->sc_fd = -1; return 0; } int -filecapture_init(struct filecapture *fc, size_t size) +stream_capture_init(struct stream_capture *cap, size_t size) { - if (!fc) { + if (!cap) { errno = EINVAL; return -1; } - if (filecapture_alloc(fc, size)) + if (stream_capture_alloc(cap, size)) return -1; return 0; } static void -filecapture_reset(struct filecapture *fc) +stream_capture_reset(struct stream_capture *cap) { - fc->fc_leng = 0; - fc->fc_level = 0; - fc->fc_nlines = 0; - fc->fc_cur = 0; - - if (fc->fc_tmpfd >= 0) { - close(fc->fc_tmpfd); - fc->fc_tmpfd = -1; + cap->sc_leng = 0; + cap->sc_level = 0; + cap->sc_nlines = 0; + cap->sc_cur = 0; + + if (cap->sc_storfd >= 0) { + close(cap->sc_storfd); + cap->sc_storfd = -1; } - if (fc->fc_fd >= 0) { - close(fc->fc_fd); - fc->fc_fd = -1; + if (cap->sc_fd >= 0) { + close(cap->sc_fd); + cap->sc_fd = -1; } } void -filecapture_free(struct filecapture *fc) +stream_capture_free(struct stream_capture *cap) { - filecapture_reset(fc); - free(fc->fc_base); - fc->fc_base = NULL; - fc->fc_size = 0; + stream_capture_reset(cap); + free(cap->sc_base); + cap->sc_base = NULL; + cap->sc_size = 0; - fc->fc_linemon = NULL; - fc->fc_monarg = NULL; + cap->sc_linemon = NULL; + cap->sc_monarg = NULL; } static int @@ -107,41 +107,41 @@ full_write(int fd, char *buf, size_t size) } static int -filecapture_flush(struct filecapture *fc) +stream_capture_flush(struct stream_capture *cap) { int res; - if (fc->fc_level == 0) + if (cap->sc_level == 0) return 0; - if (fc->fc_tmpfd == -1) { + if (cap->sc_storfd == -1) { int fd; char tmpl[] = "/tmp/rcXXXXXX"; fd = mkstemp(tmpl); if (fd == -1) return -1; unlink(tmpl); - fc->fc_tmpfd = fd; + cap->sc_storfd = fd; } - res = full_write(fc->fc_tmpfd, fc->fc_base, fc->fc_level); + res = full_write(cap->sc_storfd, cap->sc_base, cap->sc_level); if (res) return -1; - fc->fc_level = 0; - fc->fc_cur = 0; + cap->sc_level = 0; + cap->sc_cur = 0; return 0; } static int -filecapture_get(struct filecapture *fc, int *feof) +stream_capture_get(struct stream_capture *cap, int *feof) { char c; int rc; - if (fc->fc_level == fc->fc_size) { - if (filecapture_flush(fc)) + if (cap->sc_level == cap->sc_size) { + if (stream_capture_flush(cap)) return -1; } - rc = read(fc->fc_fd, &c, 1); + rc = read(cap->sc_fd, &c, 1); if (rc == -1) return -1; if (rc == 0) { @@ -149,43 +149,43 @@ filecapture_get(struct filecapture *fc, int *feof) return 0; } else *feof = 0; - fc->fc_base[fc->fc_level] = c; - fc->fc_level++; - fc->fc_leng++; + cap->sc_base[cap->sc_level] = c; + cap->sc_level++; + cap->sc_leng++; if (c == '\n') { - if (fc->fc_linemon) - fc->fc_linemon(fc->fc_base + fc->fc_cur, - fc->fc_level - fc->fc_cur, - fc->fc_monarg); - fc->fc_cur = fc->fc_level; - fc->fc_nlines++; + if (cap->sc_linemon) + cap->sc_linemon(cap->sc_base + cap->sc_cur, + cap->sc_level - cap->sc_cur, + cap->sc_monarg); + cap->sc_cur = cap->sc_level; + cap->sc_nlines++; } return 0; } static int -filecapture_put(struct filecapture *fc, int *feof) +stream_capture_put(struct stream_capture *cap, int *feof) { - if (fc->fc_cur < fc->fc_level) { - int n = write(fc->fc_fd, &fc->fc_base[fc->fc_cur], 1); + if (cap->sc_cur < cap->sc_level) { + int n = write(cap->sc_fd, &cap->sc_base[cap->sc_cur], 1); if (n == -1) return -1; if (n == 0) { errno = ENOSPC; return -1; } - fc->fc_cur++; + cap->sc_cur++; } - *feof = fc->fc_cur == fc->fc_level; + *feof = cap->sc_cur == cap->sc_level; return 0; } void runcap_free(struct runcap *rc) { - filecapture_free(&rc->rc_cap[RUNCAP_STDIN]); - filecapture_free(&rc->rc_cap[RUNCAP_STDOUT]); - filecapture_free(&rc->rc_cap[RUNCAP_STDERR]); + stream_capture_free(&rc->rc_cap[RUNCAP_STDIN]); + stream_capture_free(&rc->rc_cap[RUNCAP_STDOUT]); + stream_capture_free(&rc->rc_cap[RUNCAP_STDERR]); } static inline int @@ -219,7 +219,7 @@ runcap_start(struct runcap *rc) int i; for (i = 0; i < RUNCAP_NBUF; i++) - if (rc->rc_cap[i].fc_size) { + if (rc->rc_cap[i].sc_size) { if (pipe(p[i])) { goto err; } @@ -230,8 +230,8 @@ runcap_start(struct runcap *rc) if (p[RUNCAP_STDIN][0] >= 0) { dup2(p[RUNCAP_STDIN][0], RUNCAP_STDIN); close(p[RUNCAP_STDIN][1]); - } else if (rc->rc_cap[RUNCAP_STDIN].fc_fd >= 0) { - dup2(rc->rc_cap[RUNCAP_STDIN].fc_fd, RUNCAP_STDIN); + } else if (rc->rc_cap[RUNCAP_STDIN].sc_fd >= 0) { + dup2(rc->rc_cap[RUNCAP_STDIN].sc_fd, RUNCAP_STDIN); } if (p[RUNCAP_STDOUT][0] >= 0) { @@ -263,15 +263,15 @@ runcap_start(struct runcap *rc) default: if (p[RUNCAP_STDIN][0] >= 0) { close(p[RUNCAP_STDIN][0]); - rc->rc_cap[RUNCAP_STDIN].fc_fd = p[RUNCAP_STDIN][1]; + rc->rc_cap[RUNCAP_STDIN].sc_fd = p[RUNCAP_STDIN][1]; } if (p[RUNCAP_STDOUT][0] >= 0) { close(p[RUNCAP_STDOUT][1]); - rc->rc_cap[RUNCAP_STDOUT].fc_fd = p[RUNCAP_STDOUT][0]; + rc->rc_cap[RUNCAP_STDOUT].sc_fd = p[RUNCAP_STDOUT][0]; } if (p[RUNCAP_STDERR][0] >= 0) { close(p[RUNCAP_STDERR][1]); - rc->rc_cap[RUNCAP_STDERR].fc_fd = p[RUNCAP_STDERR][0]; + rc->rc_cap[RUNCAP_STDERR].sc_fd = p[RUNCAP_STDERR][0]; } return 0; } @@ -302,20 +302,20 @@ runcap_loop(struct runcap *rc) FD_ZERO(&rds); FD_ZERO(&wrs); - if (rc->rc_cap[RUNCAP_STDIN].fc_size - && rc->rc_cap[RUNCAP_STDIN].fc_fd >= 0) { - nfd = rc->rc_cap[RUNCAP_STDIN].fc_fd; - FD_SET(rc->rc_cap[RUNCAP_STDIN].fc_fd, &wrs); + if (rc->rc_cap[RUNCAP_STDIN].sc_size + && rc->rc_cap[RUNCAP_STDIN].sc_fd >= 0) { + nfd = rc->rc_cap[RUNCAP_STDIN].sc_fd; + FD_SET(rc->rc_cap[RUNCAP_STDIN].sc_fd, &wrs); } - if (rc->rc_cap[RUNCAP_STDOUT].fc_fd >= 0) { - if (rc->rc_cap[RUNCAP_STDOUT].fc_fd > nfd) - nfd = rc->rc_cap[RUNCAP_STDOUT].fc_fd; - FD_SET(rc->rc_cap[RUNCAP_STDOUT].fc_fd, &rds); + if (rc->rc_cap[RUNCAP_STDOUT].sc_fd >= 0) { + if (rc->rc_cap[RUNCAP_STDOUT].sc_fd > nfd) + nfd = rc->rc_cap[RUNCAP_STDOUT].sc_fd; + FD_SET(rc->rc_cap[RUNCAP_STDOUT].sc_fd, &rds); } - if (rc->rc_cap[RUNCAP_STDERR].fc_fd >= 0) { - if (rc->rc_cap[RUNCAP_STDERR].fc_fd > nfd) - nfd = rc->rc_cap[RUNCAP_STDERR].fc_fd; - FD_SET(rc->rc_cap[RUNCAP_STDERR].fc_fd, &rds); + if (rc->rc_cap[RUNCAP_STDERR].sc_fd >= 0) { + if (rc->rc_cap[RUNCAP_STDERR].sc_fd > nfd) + nfd = rc->rc_cap[RUNCAP_STDERR].sc_fd; + FD_SET(rc->rc_cap[RUNCAP_STDERR].sc_fd, &rds); } nfd++; @@ -369,39 +369,39 @@ runcap_loop(struct runcap *rc) continue; } - if (rc->rc_cap[RUNCAP_STDIN].fc_fd >= 0 - && FD_ISSET(rc->rc_cap[RUNCAP_STDIN].fc_fd, &wrs)) { - if (filecapture_put(&rc->rc_cap[RUNCAP_STDIN], &eof)) { + if (rc->rc_cap[RUNCAP_STDIN].sc_fd >= 0 + && FD_ISSET(rc->rc_cap[RUNCAP_STDIN].sc_fd, &wrs)) { + if (stream_capture_put(&rc->rc_cap[RUNCAP_STDIN], &eof)) { rc->rc_errno = errno; break; } if (eof) { /* FIXME: */ - close(rc->rc_cap[RUNCAP_STDIN].fc_fd); - rc->rc_cap[RUNCAP_STDIN].fc_fd = -1; + close(rc->rc_cap[RUNCAP_STDIN].sc_fd); + rc->rc_cap[RUNCAP_STDIN].sc_fd = -1; } } - if (rc->rc_cap[RUNCAP_STDOUT].fc_fd >= 0 - && FD_ISSET(rc->rc_cap[RUNCAP_STDOUT].fc_fd, &rds)) { - if (filecapture_get(&rc->rc_cap[RUNCAP_STDOUT], &eof)) { + if (rc->rc_cap[RUNCAP_STDOUT].sc_fd >= 0 + && FD_ISSET(rc->rc_cap[RUNCAP_STDOUT].sc_fd, &rds)) { + if (stream_capture_get(&rc->rc_cap[RUNCAP_STDOUT], &eof)) { rc->rc_errno = errno; break; } if (eof) { - close(rc->rc_cap[RUNCAP_STDOUT].fc_fd); - rc->rc_cap[RUNCAP_STDOUT].fc_fd = -1; + close(rc->rc_cap[RUNCAP_STDOUT].sc_fd); + rc->rc_cap[RUNCAP_STDOUT].sc_fd = -1; } } - if (rc->rc_cap[RUNCAP_STDERR].fc_fd >= 0 - && FD_ISSET(rc->rc_cap[RUNCAP_STDERR].fc_fd, &rds)) { - if (filecapture_get(&rc->rc_cap[RUNCAP_STDERR], &eof)) { + if (rc->rc_cap[RUNCAP_STDERR].sc_fd >= 0 + && FD_ISSET(rc->rc_cap[RUNCAP_STDERR].sc_fd, &rds)) { + if (stream_capture_get(&rc->rc_cap[RUNCAP_STDERR], &eof)) { rc->rc_errno = errno; break; } if (eof) { - close(rc->rc_cap[RUNCAP_STDERR].fc_fd); - rc->rc_cap[RUNCAP_STDERR].fc_fd = -1; + close(rc->rc_cap[RUNCAP_STDERR].sc_fd); + rc->rc_cap[RUNCAP_STDERR].sc_fd = -1; } } } @@ -423,41 +423,41 @@ runcap_init(struct runcap *rc, int flags) if (!(flags & RCF_TIMEOUT)) rc->rc_timeout = 0; if (flags & RCF_STDIN) { - if (rc->rc_cap[RUNCAP_STDIN].fc_size > 0 - && rc->rc_cap[RUNCAP_STDIN].fc_fd != -1) { + if (rc->rc_cap[RUNCAP_STDIN].sc_size > 0 + && rc->rc_cap[RUNCAP_STDIN].sc_fd != -1) { errno = EINVAL; return -1; } - rc->rc_cap[RUNCAP_STDIN].fc_level = - rc->rc_cap[RUNCAP_STDIN].fc_size; - rc->rc_cap[RUNCAP_STDIN].fc_cur = 0; - } else if (filecapture_init(&rc->rc_cap[RUNCAP_STDIN], 0)) + rc->rc_cap[RUNCAP_STDIN].sc_level = + rc->rc_cap[RUNCAP_STDIN].sc_size; + rc->rc_cap[RUNCAP_STDIN].sc_cur = 0; + } else if (stream_capture_init(&rc->rc_cap[RUNCAP_STDIN], 0)) return -1; if (flags & RCF_STDOUT_SIZE) - res = filecapture_alloc(&rc->rc_cap[RUNCAP_STDOUT], - rc->rc_cap[RUNCAP_STDOUT].fc_size); + res = stream_capture_alloc(&rc->rc_cap[RUNCAP_STDOUT], + rc->rc_cap[RUNCAP_STDOUT].sc_size); else - res = filecapture_init(&rc->rc_cap[RUNCAP_STDOUT], FC_BUFSIZE); + res = stream_capture_init(&rc->rc_cap[RUNCAP_STDOUT], STRCAP_BUFSIZE); if (res) return res; if (!(flags & RCF_STDOUT_LINEMON)) { - rc->rc_cap[RUNCAP_STDOUT].fc_linemon = NULL; - rc->rc_cap[RUNCAP_STDOUT].fc_monarg = NULL; + rc->rc_cap[RUNCAP_STDOUT].sc_linemon = NULL; + rc->rc_cap[RUNCAP_STDOUT].sc_monarg = NULL; } if (flags & RCF_STDERR_SIZE) - res = filecapture_alloc(&rc->rc_cap[RUNCAP_STDERR], - rc->rc_cap[RUNCAP_STDERR].fc_size); + res = stream_capture_alloc(&rc->rc_cap[RUNCAP_STDERR], + rc->rc_cap[RUNCAP_STDERR].sc_size); else - res = filecapture_init(&rc->rc_cap[RUNCAP_STDERR], FC_BUFSIZE); + res = stream_capture_init(&rc->rc_cap[RUNCAP_STDERR], STRCAP_BUFSIZE); if (res) return res; if (!(flags & RCF_STDERR_LINEMON)) { - rc->rc_cap[RUNCAP_STDERR].fc_linemon = NULL; - rc->rc_cap[RUNCAP_STDERR].fc_monarg = NULL; + rc->rc_cap[RUNCAP_STDERR].sc_linemon = NULL; + rc->rc_cap[RUNCAP_STDERR].sc_monarg = NULL; } rc->rc_pid = (pid_t) -1; @@ -516,13 +516,13 @@ runcap(struct runcap *rc, int flags) runcap_loop(rc); restore_signals(NUMSIGNALS, sig, oldact); if (rc->rc_errno == 0) { - if (rc->rc_cap[RUNCAP_STDOUT].fc_tmpfd != -1) { - filecapture_flush(&rc->rc_cap[RUNCAP_STDOUT]); - lseek(rc->rc_cap[RUNCAP_STDOUT].fc_tmpfd, 0, SEEK_SET); + if (rc->rc_cap[RUNCAP_STDOUT].sc_storfd != -1) { + stream_capture_flush(&rc->rc_cap[RUNCAP_STDOUT]); + lseek(rc->rc_cap[RUNCAP_STDOUT].sc_storfd, 0, SEEK_SET); } - if (rc->rc_cap[RUNCAP_STDERR].fc_tmpfd != -1) { - filecapture_flush(&rc->rc_cap[RUNCAP_STDERR]); - lseek(rc->rc_cap[RUNCAP_STDERR].fc_tmpfd, 0, SEEK_SET); + if (rc->rc_cap[RUNCAP_STDERR].sc_storfd != -1) { + stream_capture_flush(&rc->rc_cap[RUNCAP_STDERR]); + lseek(rc->rc_cap[RUNCAP_STDERR].sc_storfd, 0, SEEK_SET); } } return rc->rc_errno == 0 ? 0 : -1; diff --git a/runcap.h b/runcap.h index c57f2e1..9537eee 100644 --- a/runcap.h +++ b/runcap.h @@ -17,22 +17,23 @@ #ifndef _RUNCAP_H_ # define _RUNCAP_H_ 1 -struct filecapture +struct stream_capture { - size_t fc_size; /* size of the buffer */ - off_t fc_leng; /* total length of captured data */ - size_t fc_nlines; /* number of captured lines */ - size_t fc_cur; /* current offset in buffer */ - char * fc_base; /* buffer space */ - size_t fc_level; /* number of characters currently in buffer */ - void (*fc_linemon) (const char *, size_t, void *); + int sc_fd; /* Input descriptor */ + char *sc_base; /* buffer space */ + size_t sc_size; /* size of the buffer */ + size_t sc_level; /* number of characters currently in buffer */ + size_t sc_cur; /* current offset in buffer */ + off_t sc_leng; /* total length of captured data */ + size_t sc_nlines; /* number of captured lines */ + int sc_storfd; /* Storage file descriptor */ + + void (*sc_linemon) (const char *, size_t, void *); /* Line monitor function */ - void *fc_monarg; /* Line monitor argument */ - int fc_tmpfd; /* Storage file descriptor */ - int fc_fd; /* Input descriptor */ + void *sc_monarg; /* Line monitor argument */ }; -#define FC_BUFSIZE 4096 +#define STRCAP_BUFSIZE 4096 enum { RUNCAP_STDIN, @@ -46,7 +47,7 @@ struct runcap char *rc_program; /* [IN] (Path)name of the program to run */ char **rc_argv; /* [IN] Argument vector */ unsigned rc_timeout; /* [IN] Execution timeout */ - struct filecapture rc_cap[RUNCAP_NBUF]; + struct stream_capture rc_cap[RUNCAP_NBUF]; /* rc_cap[RUNCAP_STDIN] - [IN], rest - [OUT] */ pid_t rc_pid; /* [OUT] - PID of the process */ int rc_status; /* [OUT] - Termination status */ @@ -61,16 +62,16 @@ struct runcap #define RCF_STDERR_SIZE 0x0020 #define RCF_STDERR_LINEMON 0x0040 -int filecapture_init(struct filecapture *fc, size_t size); -void filecapture_free(struct filecapture *fc); +int stream_capture_init(struct stream_capture *fc, size_t size); +void stream_capture_free(struct stream_capture *fc); int runcap(struct runcap *rc, int flags); void runcap_free(struct runcap *rc); -static inline struct filecapture * -runcap_filecapture(struct runcap *rc, int stream) +static inline struct stream_capture * +runcap_stream_capture(struct runcap *rc, int stream) { - struct filecapture *fp; + struct stream_capture *fp; if (stream != RUNCAP_STDOUT && stream != RUNCAP_STDERR) { errno = EINVAL; @@ -79,7 +80,7 @@ runcap_filecapture(struct runcap *rc, int stream) fp = &rc->rc_cap[stream]; - if (!fp->fc_base || fp->fc_size == 0) { + if (!fp->sc_base || fp->sc_size == 0) { errno = EINVAL; return NULL; } diff --git a/seek.c b/seek.c index b69cfe0..2ff6b48 100644 --- a/seek.c +++ b/seek.c @@ -20,23 +20,23 @@ #include "runcap.h" int -runcap_seek(struct runcap *rc, int stream, off_t off, int whence) +runcap_seek(struct runcap *rc, int sd, off_t off, int whence) { - struct filecapture *fp; + struct stream_capture *cap; off_t cur; - fp = runcap_filecapture(rc, stream); - if (!fp) + cap = runcap_stream_capture(rc, sd); + if (!cap) return -1; - cur = runcap_tell(rc, stream); + cur = runcap_tell(rc, sd); switch (whence) { case SEEK_CUR: off = cur; break; case SEEK_END: - off = fp->fc_leng + off; + off = cap->sc_leng + off; break; case SEEK_SET: @@ -52,15 +52,15 @@ runcap_seek(struct runcap *rc, int stream, off_t off, int whence) return -1; } - cur -= fp->fc_level; + cur -= cap->sc_level; - if (cur <= off && off <= cur + fp->fc_level) { - fp->fc_cur = off - cur; - } else if (fp->fc_tmpfd != -1) { - if (lseek(fp->fc_tmpfd, off, SEEK_SET) == -1) + if (cur <= off && off <= cur + cap->sc_level) { + cap->sc_cur = off - cur; + } else if (cap->sc_storfd != -1) { + if (lseek(cap->sc_storfd, off, SEEK_SET) == -1) return -1; - fp->fc_level = 0; - fp->fc_cur = 0; + cap->sc_level = 0; + cap->sc_cur = 0; } else { errno = EINVAL; return -1; diff --git a/t/Makefile b/t/Makefile index 7e62a8b..c24d98e 100644 --- a/t/Makefile +++ b/t/Makefile @@ -20,10 +20,12 @@ check: genout rt CPPFLAGS=-I.. CFLAGS = -ggdb -Wall -VPATH=.. +.c.o: + $(CC) -c -o$@ $(CPPFLAGS) $(CFLAGS) $< -rt: rt.o libruncap.a - cc $(CFLAGS) -o$@ $^ +RT_DEPS=rt.o ../libruncap.a +rt: $(RT_DEPS) + cc $(CFLAGS) -ort $(RT_DEPS) genout: genout.o diff --git a/t/rt.c b/t/rt.c index 6496cfd..2edc12b 100644 --- a/t/rt.c +++ b/t/rt.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "runcap.h" static char *progname; @@ -92,7 +93,8 @@ nl(struct runcap *rc, int stream) size_t size = 0; ssize_t res; char const *what = stream == RUNCAP_STDOUT ? "stdout" : "stderr"; - for (n = rc->rc_cap[stream].fc_nlines, width = 1; n > 0; n /= 10) + + for (n = rc->rc_cap[stream].sc_nlines, width = 1; n > 0; n /= 10) width++; printf("%s listing:\n", what); @@ -100,7 +102,7 @@ nl(struct runcap *rc, int stream) n = 1; while ((res = runcap_getline(rc, stream, &buf, &size)) > 0) { buf[res-1] = 0; - printf("% *zd: %s\n", width, n, buf); + printf("%*zu: %s\n", width, n, buf); n++; } if (res) @@ -150,8 +152,8 @@ main(int argc, char **argv) exit(1); } - rc.rc_cap[RUNCAP_STDIN].fc_size = size; - rc.rc_cap[RUNCAP_STDIN].fc_base = buffer; + rc.rc_cap[RUNCAP_STDIN].sc_size = size; + rc.rc_cap[RUNCAP_STDIN].sc_base = buffer; while (size) { ssize_t n = read(fd, buffer, size); if (n < 0) { @@ -168,10 +170,10 @@ main(int argc, char **argv) buffer += n; } close(fd); - rc.rc_cap[RUNCAP_STDIN].fc_fd = -1; + rc.rc_cap[RUNCAP_STDIN].sc_fd = -1; } else { - rc.rc_cap[RUNCAP_STDIN].fc_fd = fd; - rc.rc_cap[RUNCAP_STDIN].fc_size = 0; + rc.rc_cap[RUNCAP_STDIN].sc_fd = fd; + rc.rc_cap[RUNCAP_STDIN].sc_size = 0; } rcf |= RCF_STDIN; break; @@ -186,13 +188,13 @@ main(int argc, char **argv) break; case 'm': if (what & WA_STDOUT) { - rc.rc_cap[RUNCAP_STDOUT].fc_linemon = linemon; - rc.rc_cap[RUNCAP_STDOUT].fc_monarg = "stdout"; + rc.rc_cap[RUNCAP_STDOUT].sc_linemon = linemon; + rc.rc_cap[RUNCAP_STDOUT].sc_monarg = "stdout"; rcf |= RCF_STDOUT_LINEMON; } if (what & WA_STDERR) { - rc.rc_cap[RUNCAP_STDERR].fc_linemon = linemon; - rc.rc_cap[RUNCAP_STDERR].fc_monarg = "stderr"; + rc.rc_cap[RUNCAP_STDERR].sc_linemon = linemon; + rc.rc_cap[RUNCAP_STDERR].sc_monarg = "stderr"; rcf |= RCF_STDERR_LINEMON; } break; @@ -203,11 +205,11 @@ main(int argc, char **argv) case 's': size = strtoul(optarg, NULL, 10); if (what & WA_STDOUT) { - rc.rc_cap[RUNCAP_STDOUT].fc_size = size; + rc.rc_cap[RUNCAP_STDOUT].sc_size = size; rcf |= RCF_STDOUT_SIZE; } if (what & WA_STDERR) { - rc.rc_cap[RUNCAP_STDERR].fc_size = size; + rc.rc_cap[RUNCAP_STDERR].sc_size = size; rcf |= RCF_STDERR_SIZE; } break; @@ -248,12 +250,12 @@ main(int argc, char **argv) } else printf("unrecognized status: %d\n", rc.rc_status); - printf("stdout: %zu lines, %zu bytes\n", - rc.rc_cap[RUNCAP_STDOUT].fc_nlines, - rc.rc_cap[RUNCAP_STDOUT].fc_leng); - printf("stderr: %zu lines, %zu bytes\n", - rc.rc_cap[RUNCAP_STDERR].fc_nlines, - rc.rc_cap[RUNCAP_STDERR].fc_leng); + printf("stdout: %zu lines, %jd bytes\n", + rc.rc_cap[RUNCAP_STDOUT].sc_nlines, + (intmax_t)rc.rc_cap[RUNCAP_STDOUT].sc_leng); + printf("stderr: %zu lines, %jd bytes\n", + rc.rc_cap[RUNCAP_STDERR].sc_nlines, + (intmax_t)rc.rc_cap[RUNCAP_STDERR].sc_leng); if (numlines & WA_STDOUT) nl(&rc, RUNCAP_STDOUT); diff --git a/tell.c b/tell.c index 47729ed..3b7a048 100644 --- a/tell.c +++ b/tell.c @@ -20,22 +20,22 @@ #include "runcap.h" off_t -runcap_tell(struct runcap *rc, int stream) +runcap_tell(struct runcap *rc, int sd) { - struct filecapture *fp; + struct stream_capture *cap; off_t off; - fp = runcap_filecapture(rc, stream); - if (!fp) + cap = runcap_stream_capture(rc, sd); + if (!cap) return -1; - if (fp->fc_tmpfd != -1) { - off = lseek(fp->fc_tmpfd, 0, SEEK_CUR); + if (cap->sc_storfd != -1) { + off = lseek(cap->sc_storfd, 0, SEEK_CUR); if (off == -1) return -1; } else off = 0; - return off + fp->fc_cur; + return off + cap->sc_cur; } -- cgit v1.2.1