The io_read_eol() function reads bytes until the end of the line, that is until one of the characters \r or \n is found, and stores them in the string pointed to by line.
The io_read_eof() function reads bytes until the end of the file is reached and stores them in the string pointed to by file.
The io_read_len() function reads an exact number of bytes from the file and stores them in the string pointed to by str.
Defines | |
#define | CHUNKSIZE 128 |
Functions | |
int | io_read_eol (int fd, char **line) |
read a line of input | |
int | io_read_eof (int fd, char **file) |
read until end of file | |
int | io_read_len (int fd, char **str, size_t len) |
read exact number of bytes |
#define CHUNKSIZE 128 |
bytes read at a time
Definition at line 40 of file io.h.
Referenced by io_read_eof(), and io_read_eol().
int io_read_eol | ( | int | fd, | |
char ** | line | |||
) |
read a line of input
[in] | fd | file descriptor to read from |
[out] | line | pointer to a string |
free(3)
read(2)
Definition at line 24 of file io_read_eol.c.
References CHUNKSIZE, and str_dupn().
00025 { 00026 size_t chunks = 1, len = 0; 00027 char *buf = malloc(chunks * CHUNKSIZE + 1); 00028 char c; 00029 00030 for (;;) { 00031 switch(read(fd, &c, 1)) { 00032 case -1: 00033 return -1; 00034 00035 case 0: 00036 goto out; 00037 00038 default: 00039 if (c == '\n' || c == '\r') 00040 goto out; 00041 00042 if (len >= chunks * CHUNKSIZE) { 00043 chunks++; 00044 buf = realloc(buf, chunks * CHUNKSIZE + 1); 00045 } 00046 00047 buf[len++] = c; 00048 break; 00049 } 00050 } 00051 00052 out: 00053 if (len > 0) 00054 *line = str_dupn(buf, len); 00055 00056 free(buf); 00057 return len; 00058 }
int io_read_eof | ( | int | fd, | |
char ** | file | |||
) |
read until end of file
[in] | fd | file descriptor to read from |
[out] | file | pointer to a string |
free(3)
read(2)
Definition at line 24 of file io_read_eof.c.
References CHUNKSIZE, and str_dupn().
Referenced by exec_fork_pipe().
00025 { 00026 int rc = -1; 00027 size_t chunks = 1, len = 0; 00028 char *buf = malloc(chunks * CHUNKSIZE + 1); 00029 00030 for (;;) { 00031 int bytes_read = read(fd, buf+len, CHUNKSIZE); 00032 00033 if (bytes_read == -1) 00034 goto err; 00035 00036 len += bytes_read; 00037 buf[len] = '\0'; 00038 00039 if (bytes_read == 0) 00040 goto out; 00041 00042 if (bytes_read == CHUNKSIZE) { 00043 chunks++; 00044 buf = realloc(buf, chunks * CHUNKSIZE + 1); 00045 } 00046 } 00047 00048 out: 00049 if (len > 0) 00050 *file = str_dupn(buf, len); 00051 00052 rc = len; 00053 00054 err: 00055 free(buf); 00056 return rc; 00057 }
int io_read_len | ( | int | fd, | |
char ** | str, | |||
size_t | len | |||
) |
read exact number of bytes
[in] | fd | file descriptor to read from |
[out] | str | pointer to a string |
[in] | len | bytes to be read |
free(3)
read(2)
Definition at line 24 of file io_read_len.c.
References str_dupn().
00025 { 00026 char *buf = calloc(len + 1, sizeof(char)); 00027 00028 ssize_t buflen = read(fd, buf, len); 00029 00030 if (buflen == -1) 00031 return -1; 00032 00033 if (buflen > 0) 00034 *str = str_dupn(buf, buflen); 00035 00036 free(buf); 00037 return buflen; 00038 }