File: external.c

package info (click to toggle)
xwax 1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 576 kB
  • sloc: ansic: 6,687; sh: 170; makefile: 142
file content (64 lines) | stat: -rw-r--r-- 1,118 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/poll.h>
#include <sys/wait.h>

#include "external.h"

int main(int argc, char *argv[])
{
    int fd, status;
    unsigned cycles, strings;
    pid_t pid, p;
    struct pollfd pe;
    struct rb rb;

    pid = fork_pipe_nb(&fd, "/usr/bin/find", "find", NULL);
    if (pid == -1)
        return -1;

    pe.fd = fd;
    pe.revents = POLLIN;

    rb_reset(&rb);

    cycles = 0;
    strings = 0;

    for (;;) {
        char *s;
        ssize_t z;

        cycles++;

        if (poll(&pe, 1, -1) == -1) {
            perror("poll");
            return -1;
        }

        z = get_line(fd, &rb, &s);
        if (z == -1) {
            if (errno == EAGAIN)
                continue;
            break;
        }
        if (z == 0)
            break;

        strings++;
        fprintf(stderr, "(%u, %u) %s\n", cycles, strings, s);
        free(s);
    }

    fprintf(stderr, "%u cycles, %u strings\n", cycles, strings);

    if (close(fd) == -1)
        abort();

    p = wait(&status);
    assert(p == pid);

    return 0;
}