Line 0
Link Here
|
|
|
1 |
--- ./src/console_output.c.orig 2008-06-12 04:48:13.000000000 +0400 |
2 |
+++ ./src/console_output.c 2008-06-15 18:04:23.000000000 +0400 |
3 |
@@ -44,6 +44,153 @@ |
4 |
#else |
5 |
/* defines & functions for gxine */ |
6 |
|
7 |
+/* from src/contrib/cvs/lib/getline.h */ |
8 |
+#if defined (__GNUC__) || (defined (__STDC__) && __STDC__) |
9 |
+#define __PROTO(args) args |
10 |
+#else |
11 |
+#define __PROTO(args) () |
12 |
+#endif /* GCC. */ |
13 |
+ |
14 |
+#define GETLINE_NO_LIMIT -1 |
15 |
+ |
16 |
+int |
17 |
+ getline __PROTO ((char **_lineptr, size_t *_n, FILE *_stream)); |
18 |
+int |
19 |
+ getline_safe __PROTO ((char **_lineptr, size_t *_n, FILE *_stream, |
20 |
+ int limit)); |
21 |
+int |
22 |
+ getstr __PROTO ((char **_lineptr, size_t *_n, FILE *_stream, |
23 |
+ int _terminator, int _offset, int limit)); |
24 |
+ |
25 |
+/* getline.h */ |
26 |
+/* from src/contrib/cvs/lib/getline.c */ |
27 |
+ |
28 |
+#include <sys/types.h> |
29 |
+#include <assert.h> |
30 |
+#include <errno.h> |
31 |
+ |
32 |
+#define MIN_CHUNK 64 |
33 |
+ |
34 |
+int |
35 |
+getstr (lineptr, n, stream, terminator, offset, limit) |
36 |
+ char **lineptr; |
37 |
+ size_t *n; |
38 |
+ FILE *stream; |
39 |
+ int terminator; |
40 |
+ int offset; |
41 |
+ int limit; |
42 |
+{ |
43 |
+ int nchars_avail; /* Allocated but unused chars in *LINEPTR. */ |
44 |
+ char *read_pos; /* Where we're reading into *LINEPTR. */ |
45 |
+ int ret; |
46 |
+ |
47 |
+ if (!lineptr || !n || !stream) |
48 |
+ { |
49 |
+ errno = EINVAL; |
50 |
+ return -1; |
51 |
+ } |
52 |
+ |
53 |
+ if (!*lineptr) |
54 |
+ { |
55 |
+ *n = MIN_CHUNK; |
56 |
+ *lineptr = malloc (*n); |
57 |
+ if (!*lineptr) |
58 |
+ { |
59 |
+ errno = ENOMEM; |
60 |
+ return -1; |
61 |
+ } |
62 |
+ *lineptr[0] = '\0'; |
63 |
+ } |
64 |
+ |
65 |
+ nchars_avail = *n - offset; |
66 |
+ read_pos = *lineptr + offset; |
67 |
+ |
68 |
+ for (;;) |
69 |
+ { |
70 |
+ int save_errno; |
71 |
+ register int c; |
72 |
+ |
73 |
+ if (limit == 0) |
74 |
+ break; |
75 |
+ else |
76 |
+ { |
77 |
+ c = getc (stream); |
78 |
+ |
79 |
+ /* If limit is negative, then we shouldn't pay attention to |
80 |
+ it, so decrement only if positive. */ |
81 |
+ if (limit > 0) |
82 |
+ limit--; |
83 |
+ } |
84 |
+ |
85 |
+ save_errno = errno; |
86 |
+ |
87 |
+ /* We always want at least one char left in the buffer, since we |
88 |
+ always (unless we get an error while reading the first char) |
89 |
+ NUL-terminate the line buffer. */ |
90 |
+ |
91 |
+ assert((*lineptr + *n) == (read_pos + nchars_avail)); |
92 |
+ if (nchars_avail < 2) |
93 |
+ { |
94 |
+ if (*n > MIN_CHUNK) |
95 |
+ *n *= 2; |
96 |
+ else |
97 |
+ *n += MIN_CHUNK; |
98 |
+ |
99 |
+ nchars_avail = *n + *lineptr - read_pos; |
100 |
+ *lineptr = realloc (*lineptr, *n); |
101 |
+ if (!*lineptr) |
102 |
+ { |
103 |
+ errno = ENOMEM; |
104 |
+ return -1; |
105 |
+ } |
106 |
+ read_pos = *n - nchars_avail + *lineptr; |
107 |
+ assert((*lineptr + *n) == (read_pos + nchars_avail)); |
108 |
+ } |
109 |
+ |
110 |
+ if (ferror (stream)) |
111 |
+ { |
112 |
+ /* Might like to return partial line, but there is no |
113 |
+ place for us to store errno. And we don't want to just |
114 |
+ lose errno. */ |
115 |
+ errno = save_errno; |
116 |
+ return -1; |
117 |
+ } |
118 |
+ |
119 |
+ if (c == EOF) |
120 |
+ { |
121 |
+ /* Return partial line, if any. */ |
122 |
+ if (read_pos == *lineptr) |
123 |
+ return -1; |
124 |
+ else |
125 |
+ break; |
126 |
+ } |
127 |
+ |
128 |
+ *read_pos++ = c; |
129 |
+ nchars_avail--; |
130 |
+ |
131 |
+ if (c == terminator) |
132 |
+ /* Return the line. */ |
133 |
+ break; |
134 |
+ } |
135 |
+ |
136 |
+ /* Done - NUL terminate and return the number of chars read. */ |
137 |
+ *read_pos = '\0'; |
138 |
+ |
139 |
+ ret = read_pos - (*lineptr + offset); |
140 |
+ return ret; |
141 |
+} |
142 |
+ |
143 |
+int |
144 |
+getline (lineptr, n, stream) |
145 |
+ char **lineptr; |
146 |
+ size_t *n; |
147 |
+ FILE *stream; |
148 |
+{ |
149 |
+ return getstr (lineptr, n, stream, '\n', 0, GETLINE_NO_LIMIT); |
150 |
+} |
151 |
+ |
152 |
+/* getline.c */ |
153 |
+ |
154 |
# include <pthread.h> |
155 |
# include <string.h> |
156 |
# include "log_window.h" |