diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-07-18 17:50:34 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-07-18 17:50:34 +0300 |
commit | 24b9e009c9c2eef1b6d1accb3ac7ccc53c44c0a8 (patch) | |
tree | fbd53229d5f87f9a99cbd64a89ac4fb1d06bd7ad /getc.c | |
parent | 3488a182f66e35b091f441b9201ad43ad6d0f1ac (diff) | |
download | runcap-24b9e009c9c2eef1b6d1accb3ac7ccc53c44c0a8.tar.gz runcap-24b9e009c9c2eef1b6d1accb3ac7ccc53c44c0a8.tar.bz2 |
Add missing API calls, provide testsuite
Diffstat (limited to 'getc.c')
-rw-r--r-- | getc.c | 54 |
1 files changed, 54 insertions, 0 deletions
@@ -0,0 +1,54 @@ | |||
1 | /* runcap - run program and capture its output | ||
2 | Copyright (C) 2017 Sergey Poznyakoff | ||
3 | |||
4 | Runcap is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU General Public License as published by the | ||
6 | Free Software Foundation; either version 3 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | Runcap is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License along | ||
15 | with Runcap. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #include <stdlib.h> | ||
18 | #include <unistd.h> | ||
19 | #include <errno.h> | ||
20 | |||
21 | #include "runcap.h" | ||
22 | |||
23 | int | ||
24 | runcap_getc(struct runcap *rc, int stream, char *cp) | ||
25 | { | ||
26 | struct filecapture *fp; | ||
27 | |||
28 | if (!cp) { | ||
29 | errno = EINVAL; | ||
30 | return -1; | ||
31 | } | ||
32 | |||
33 | fp = runcap_filecapture(rc, stream); | ||
34 | if (!fp) | ||
35 | return -1; | ||
36 | |||
37 | if (fp->fc_level == fp->fc_linestart) { | ||
38 | if (fp->fc_tmpfd != -1) { | ||
39 | ssize_t r = read(fp->fc_tmpfd, fp->fc_base, | ||
40 | fp->fc_size); | ||
41 | if (r < 0) | ||
42 | return -1; | ||
43 | else if (r == 0) | ||
44 | return 0; | ||
45 | fp->fc_level = r; | ||
46 | fp->fc_linestart = 0; | ||
47 | } else { | ||
48 | return 0; | ||
49 | } | ||
50 | } | ||
51 | *cp = fp->fc_base[fp->fc_linestart++]; | ||
52 | return 1; | ||
53 | } | ||
54 | |||