]> the.earth.li Git - onak.git/blob - charfuncs.c
Remove --with-systemd option to dh
[onak.git] / charfuncs.c
1 /*
2  * charfuncs.c - Routines for dealing with character streams.
3  *
4  * Copyright 2002 Jonathan McDowell <noodles@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include "charfuncs.h"
24
25 /*
26  * Fetches a char from a buffer.
27  *      @ctx: Our buffer context structure.
28  *      @count: The number of characters to get from the buffer.
29  *      @c: Where to put the characters retrieved.
30  */
31 int buffer_fetchchar(void *ctx, size_t count, void *c)
32 {
33         struct buffer_ctx *buf = NULL;
34         
35         buf = (struct buffer_ctx *) ctx;
36
37         if (buf->offset + count > buf->size) {
38                 return 1;
39         }
40         
41         memcpy(c, &buf->buffer[buf->offset], count);
42         buf->offset += count;
43
44         return 0;
45 }
46
47 /*
48  *      buffer_putchar - Puts a char to a buffer.
49  *      @ctx: Our buffer context structure.
50  *      @count: The number of characters to put into the buffer.
51  *      @c: The characters to add to the buffer.
52  *
53  *      Adds characters to the buffer references by the buffer context. If we
54  *      fill it then we double the size of the current buffer and then add the
55  *      rest.
56  */
57 int buffer_putchar(void *ctx, size_t count, void *c)
58 {
59         struct buffer_ctx *buf = NULL;
60         size_t newsize = 0;
61         
62         buf = (struct buffer_ctx *) ctx;
63
64         for (newsize = buf->size; newsize < (buf->offset + count);
65                         newsize *= 2) ;
66
67         if (newsize != buf->size) {
68                 buf->buffer = realloc(buf->buffer, newsize);
69                 buf->size = newsize;
70         }
71
72         memcpy(&buf->buffer[buf->offset], c, count);
73         buf->offset += count;
74         
75         return 1;
76 }
77
78 /*
79  * Fetches a char from a file.
80  */
81 int file_fetchchar(void *fd, size_t count, void *c)
82 {
83         return !(read( *(int *) fd, c, count));
84 }
85
86 /*
87  * Puts a char to a file.
88  */
89 int file_putchar(void *fd, size_t count, void *c)
90 {
91         return !(write( *(int *) fd, c, count));
92 }
93
94 /*
95  * Gets a char from stdin.
96  */
97 int stdin_getchar(void *ctx, size_t count, void *c)
98 {
99         return (fread(c, 1, count, stdin) != count);
100 }
101
102 /*
103  * Puts a char to stdout.
104  */
105 int stdout_putchar(void *ctx, size_t count, void *c)
106 {
107         return (fwrite(c, 1, count, stdout) != count);
108 }