File: bufio.c

package info (click to toggle)
diald 0.99.4-5
  • links: PTS
  • area: main
  • in suites: sarge, woody
  • size: 1,076 kB
  • ctags: 936
  • sloc: ansic: 7,109; tcl: 977; sh: 891; perl: 306; makefile: 110
file content (68 lines) | stat: -rw-r--r-- 1,785 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
65
66
67
68
/*
 * bufio.c - Buffered, selectable I/O on pipes.
 *
 * Copyright (c) 1994, 1995, 1996 Eric Schenk.
 * Copyright (c) 1999 Mike Jagdis.
 * All rights reserved. Please see the file LICENSE which should be
 * distributed with this software for terms of use.
 */

#include "diald.h"

void pipe_init(char *name, int access, int fd, PIPE *pipe, int flush)
{
    char buf[2];
    pipe->name = name;
    pipe->access = access;
    pipe->fd = fd;
    pipe->count = 0;
    fcntl(fd,F_SETFL,fcntl(fd,F_GETFL)|O_NONBLOCK);
    pipe->next = pipes;
    pipes = pipe;
    if (flush) {
        /* clear out any old garbage from the FIFO */
        while (read(fd,buf,1) > 0);
    }
}

/* Read from the file descriptor, and
 * the return the number of characters in the buffer.
 * This all assumes that there are some characters to read.
 */

int pipe_read(PIPE *pipe)
{
    int i;
    if (pipe->count == sizeof(pipe->buf)) {
	return pipe->count;	/* No room for more input */
    }
    i = read(pipe->fd, pipe->buf+pipe->count, sizeof(pipe->buf)-pipe->count);
    if (i > 0) {
	pipe->count += i;
	return pipe->count;
    } else if (i == 0 || errno == EAGAIN) {
	/* A close of a pipe is only significant if it is a control
	 * pipe, not if it is a capture pipe for a script.
	 */
	if ((pipe->access & ACCESS_MONITOR)) {
	    mon_syslog(LOG_NOTICE, "%s closed", pipe->name);
	}
	return -1;
    } else if (errno == EINTR) {
	return 0;
    } else {
	mon_syslog(LOG_ERR,"%s read error: %m", pipe->name);
	return -1;	/* error! shut down reader... */
    }
}

/* Drop count characters from the pipe's buffer */
void pipe_flush(PIPE *pipe,int count)
{
    if (count >= pipe->count) {
	pipe->count = 0;
    } else {
       pipe->count -= count;
       memmove(pipe->buf, pipe->buf+count, pipe->count);
    }
}