diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-07-19 11:29:27 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-07-19 11:29:27 +0300 |
commit | 651babfefa21536112cbcd799932b3595aeb2da4 (patch) | |
tree | 6922b7ea70642f4ce7a8cb97f01e62115e97d940 | |
parent | 3e59cfb01443a14962547ed75bbbaaefa25a7633 (diff) | |
download | runcap-651babfefa21536112cbcd799932b3595aeb2da4.tar.gz runcap-651babfefa21536112cbcd799932b3595aeb2da4.tar.bz2 |
Rename struct filecapture to stream_capture; portability fixes
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | getc.c | 22 | ||||
-rw-r--r-- | getl.c | 9 | ||||
-rw-r--r-- | runcap.c | 232 | ||||
-rw-r--r-- | runcap.h | 39 | ||||
-rw-r--r-- | seek.c | 26 | ||||
-rw-r--r-- | t/Makefile | 8 | ||||
-rw-r--r-- | t/rt.c | 40 | ||||
-rw-r--r-- | tell.c | 14 |
9 files changed, 198 insertions, 195 deletions
@@ -36,12 +36,15 @@ OBJECTS = $(SOURCES:.c=.o) HEADERS = runcap.h CFLAGS = $(O) LDFLAGS = ARFLAGS = cru +.c.o: + $(CC) -c -o$@ $(CPPFLAGS) $(CFLAGS) $< + all: libruncap.a $(OBJECTS): $(HEADERS) libruncap.a: $(OBJECTS) ar $(ARFLAGS) libruncap.a $(OBJECTS) @@ -18,37 +18,37 @@ #include <unistd.h> #include <errno.h> #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; } @@ -18,15 +18,14 @@ #include <unistd.h> #include <errno.h> #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; char c; int res; @@ -35,28 +34,24 @@ runcap_getline(struct runcap *rc, int stream, char **pstr, size_t *psize) return -1; } str = *pstr; size = *psize; - fp = runcap_filecapture(rc, stream); - if (!fp) - return -1; - if (!str || size == 0) { /* Initial allocation */ size = 16; str = malloc(size); if (!str) return -1; *pstr = str; *psize = size; } 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; if (size >= (size_t) -1 / 3 * 2) { errno = ENOMEM; @@ -24,72 +24,72 @@ #include <errno.h> #include <signal.h> #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 full_write(int fd, char *buf, size_t size) { while (size) { @@ -104,91 +104,91 @@ full_write(int fd, char *buf, size_t size) size -= n; } return 0; } 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) { *feof = 1; 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 timeval_after(struct timeval const *a, struct timeval const *b) { if (a->tv_sec == b->tv_sec) @@ -216,25 +216,25 @@ static int runcap_start(struct runcap *rc) { int p[RUNCAP_NBUF][2] = { { -1, -1}, { -1, -1 }, { -1, -1 } }; 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; } } switch (rc->rc_pid = fork()) { case 0: /* Child */ 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) { dup2(p[RUNCAP_STDOUT][1], RUNCAP_STDOUT); close(p[RUNCAP_STDOUT][0]); } @@ -260,21 +260,21 @@ runcap_start(struct runcap *rc) case -1: break; 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; } err: rc->rc_errno = errno; for (i = 0; i < RUNCAP_NBUF; i++) { @@ -299,26 +299,26 @@ runcap_loop(struct runcap *rc) int eof; nfd = -1; 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++; if (rc->rc_pid != (pid_t) -1) { int flags = WNOHANG; pid_t pid; @@ -366,45 +366,45 @@ runcap_loop(struct runcap *rc) rc->rc_errno = errno; break; } 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; } } } if (rc->rc_pid != (pid_t)-1) { kill(rc->rc_pid, SIGKILL); @@ -420,47 +420,47 @@ runcap_init(struct runcap *rc, int flags) if (!(flags & RCF_PROGRAM)) rc->rc_program = NULL; 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; rc->rc_status = 0; rc->rc_errno = 0; @@ -513,19 +513,19 @@ runcap(struct runcap *rc, int flags) set_signals(sighan, NUMSIGNALS, sig, oldact); if (runcap_start(rc) == 0) 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; } @@ -14,28 +14,29 @@ You should have received a copy of the GNU General Public License along with Runcap. If not, see <http://www.gnu.org/licenses/>. */ #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, RUNCAP_STDOUT, RUNCAP_STDERR, RUNCAP_NBUF @@ -43,13 +44,13 @@ enum { 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 */ int rc_errno; /* [OUT] - Value of errno, if terminated on error */ }; @@ -58,31 +59,31 @@ struct runcap #define RCF_STDIN 0x0004 /* rc_cap[RUNCAP_STDIN] is set */ #define RCF_STDOUT_SIZE 0x0008 #define RCF_STDOUT_LINEMON 0x0010 #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; return NULL; } 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; } return fp; } @@ -17,29 +17,29 @@ #include <sys/types.h> #include <unistd.h> #include <errno.h> #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: break; default: @@ -49,21 +49,21 @@ runcap_seek(struct runcap *rc, int stream, off_t off, int whence) if (off < 0) { errno = EINVAL; 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; } return 0; } @@ -17,16 +17,18 @@ check: genout rt @./testsuite 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 TESTSUITE =\ testsuite\ 00simple.t\ @@ -21,12 +21,13 @@ #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/wait.h> #include <errno.h> +#include <inttypes.h> #include "runcap.h" static char *progname; void error(char const *fmt, ...) @@ -89,21 +90,22 @@ nl(struct runcap *rc, int stream) int width; size_t n; char *buf = NULL; 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); runcap_rewind(rc, 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) error("error getting lines: %s", strerror(errno)); printf("%s listing ends\n", what); } @@ -147,14 +149,14 @@ main(int argc, char **argv) buffer = malloc(size + 1); if (!buffer) { error("not enough memory"); 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) { error("error reading from \"%s\": %s", optarg, strerror(errno)); exit(1); @@ -165,16 +167,16 @@ main(int argc, char **argv) exit(1); } size -= n; 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; case 'i': inopt = 1; break; @@ -183,34 +185,34 @@ main(int argc, char **argv) break; case 'n': numlines = whatarg(optarg); 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; case 'p': rc.rc_program = optarg; rcf |= RCF_PROGRAM; break; 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; case 't': rc.rc_timeout = strtoul(optarg, NULL, 10); rcf |= RCF_TIMEOUT; @@ -245,18 +247,18 @@ main(int argc, char **argv) printf("got signal: %d\n", WTERMSIG(rc.rc_status)); } else if (WIFSTOPPED(rc.rc_status)) { printf("stopped by signal %d\n", WSTOPSIG(rc.rc_status)); } 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); if (numlines & WA_STDERR) nl(&rc, RUNCAP_STDERR); @@ -17,25 +17,25 @@ #include <sys/types.h> #include <unistd.h> #include <errno.h> #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; } |