Re: [PATCH v4 01/16] add util/search-path.{c, h} to test for executables in $PATH
[notmuch-archives.git] / d1 / b4f3bc2fd557c2752e2b53aeddb4446d7420a1
1 Return-Path: <bremner@tethera.net>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 55219431E82\r
6         for <notmuch@notmuchmail.org>; Tue,  1 Apr 2014 18:16:51 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id 5NxuBKvmr506 for <notmuch@notmuchmail.org>;\r
16         Tue,  1 Apr 2014 18:16:48 -0700 (PDT)\r
17 Received: from yantan.tethera.net (yantan.tethera.net [199.188.72.155])\r
18         (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 5F827431FCF\r
21         for <notmuch@notmuchmail.org>; Tue,  1 Apr 2014 18:16:46 -0700 (PDT)\r
22 Received: from remotemail by yantan.tethera.net with local (Exim 4.80)\r
23         (envelope-from <bremner@tethera.net>)\r
24         id 1WV9nH-000734-TI; Tue, 01 Apr 2014 22:16:43 -0300\r
25 Received: (nullmailer pid 18503 invoked by uid 1000); Wed, 02 Apr 2014\r
26         01:16:27 -0000\r
27 From: David Bremner <david@tethera.net>\r
28 To: notmuch@notmuchmail.org\r
29 Subject: [Patch v5 3/6] util: add gz_readline\r
30 Date: Tue,  1 Apr 2014 22:16:18 -0300\r
31 Message-Id: <1396401381-18128-4-git-send-email-david@tethera.net>\r
32 X-Mailer: git-send-email 1.9.0\r
33 In-Reply-To: <1396401381-18128-1-git-send-email-david@tethera.net>\r
34 References: <1396401381-18128-1-git-send-email-david@tethera.net>\r
35 X-BeenThere: notmuch@notmuchmail.org\r
36 X-Mailman-Version: 2.1.13\r
37 Precedence: list\r
38 List-Id: "Use and development of the notmuch mail system."\r
39         <notmuch.notmuchmail.org>\r
40 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
41         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
42 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
43 List-Post: <mailto:notmuch@notmuchmail.org>\r
44 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
45 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
46         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
47 X-List-Received-Date: Wed, 02 Apr 2014 01:16:51 -0000\r
48 \r
49 The idea is to provide a more or less drop in replacement for readline\r
50 to read from zlib/gzip streams.  Take the opportunity to replace\r
51 malloc with talloc.\r
52 ---\r
53  util/Makefile.local |  2 +-\r
54  util/util.h         | 12 +++++++++\r
55  util/zlib-extra.c   | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++\r
56  util/zlib-extra.h   | 11 ++++++++\r
57  4 files changed, 100 insertions(+), 1 deletion(-)\r
58  create mode 100644 util/util.h\r
59  create mode 100644 util/zlib-extra.c\r
60  create mode 100644 util/zlib-extra.h\r
61 \r
62 diff --git a/util/Makefile.local b/util/Makefile.local\r
63 index 29c0ce6..e2a5b65 100644\r
64 --- a/util/Makefile.local\r
65 +++ b/util/Makefile.local\r
66 @@ -4,7 +4,7 @@ dir := util\r
67  extra_cflags += -I$(srcdir)/$(dir)\r
68  \r
69  libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \\r
70 -                 $(dir)/string-util.c $(dir)/talloc-extra.c\r
71 +                 $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c\r
72  \r
73  libutil_modules := $(libutil_c_srcs:.c=.o)\r
74  \r
75 diff --git a/util/util.h b/util/util.h\r
76 new file mode 100644\r
77 index 0000000..8663cfc\r
78 --- /dev/null\r
79 +++ b/util/util.h\r
80 @@ -0,0 +1,12 @@\r
81 +#ifndef _UTIL_H\r
82 +#define _UTIL_H\r
83 +\r
84 +typedef enum util_status {\r
85 +    UTIL_SUCCESS = 0,\r
86 +    UTIL_ERROR = 1,\r
87 +    UTIL_OUT_OF_MEMORY,\r
88 +    UTIL_EOF,\r
89 +    UTIL_FILE,\r
90 +} util_status_t;\r
91 +\r
92 +#endif\r
93 diff --git a/util/zlib-extra.c b/util/zlib-extra.c\r
94 new file mode 100644\r
95 index 0000000..cb1eba0\r
96 --- /dev/null\r
97 +++ b/util/zlib-extra.c\r
98 @@ -0,0 +1,76 @@\r
99 +/* zlib-extra.c -  Extra or enhanced routines for compressed I/O.\r
100 + *\r
101 + * Copyright (c) 2014 David Bremner\r
102 + *\r
103 + * This program is free software: you can redistribute it and/or modify\r
104 + * it under the terms of the GNU General Public License as published by\r
105 + * the Free Software Foundation, either version 3 of the License, or\r
106 + * (at your option) any later version.\r
107 + *\r
108 + * This program is distributed in the hope that it will be useful,\r
109 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
110 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
111 + * GNU General Public License for more details.\r
112 + *\r
113 + * You should have received a copy of the GNU General Public License\r
114 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
115 + *\r
116 + * Author: David Bremner <david@tethera.net>\r
117 + */\r
118 +\r
119 +#include "zlib-extra.h"\r
120 +#include <talloc.h>\r
121 +#include <stdio.h>\r
122 +#include <string.h>\r
123 +\r
124 +/* mimic POSIX/glibc getline, but on a zlib gzFile stream, and using talloc */\r
125 +util_status_t\r
126 +gz_getline (void *talloc_ctx, char **bufptr, size_t *bufsiz, ssize_t *bytes_read,\r
127 +           gzFile stream)\r
128 +{\r
129 +    size_t len = *bufsiz;\r
130 +    char *buf = *bufptr;\r
131 +    size_t offset = 0;\r
132 +\r
133 +    if (len == 0 || buf == NULL) {\r
134 +       /* same as getdelim from gnulib */\r
135 +       len = 120;\r
136 +       buf = talloc_size (talloc_ctx, len);\r
137 +       if (buf == NULL)\r
138 +           return UTIL_OUT_OF_MEMORY;\r
139 +    }\r
140 +\r
141 +    while (1) {\r
142 +       if (! gzgets (stream, buf + offset, len - offset)) {\r
143 +           int zlib_status = 0;\r
144 +           (void) gzerror (stream, &zlib_status);\r
145 +           switch (zlib_status) {\r
146 +           case Z_OK:\r
147 +               /* follow getline behaviour */\r
148 +               *bytes_read = -1;\r
149 +               return UTIL_EOF;\r
150 +               break;\r
151 +           case Z_ERRNO:\r
152 +               return UTIL_FILE;\r
153 +               break;\r
154 +           default:\r
155 +               return UTIL_ERROR;\r
156 +           }\r
157 +       }\r
158 +\r
159 +       offset += strlen (buf + offset);\r
160 +\r
161 +       if ( buf[offset - 1] == '\n' )\r
162 +           break;\r
163 +\r
164 +       len *= 2;\r
165 +       buf = talloc_realloc (talloc_ctx, buf, char, len);\r
166 +       if (buf == NULL)\r
167 +           return UTIL_OUT_OF_MEMORY;\r
168 +    }\r
169 +\r
170 +    *bufptr = buf;\r
171 +    *bufsiz = len;\r
172 +    *bytes_read = offset;\r
173 +    return UTIL_SUCCESS;\r
174 +}\r
175 diff --git a/util/zlib-extra.h b/util/zlib-extra.h\r
176 new file mode 100644\r
177 index 0000000..ed46ac1\r
178 --- /dev/null\r
179 +++ b/util/zlib-extra.h\r
180 @@ -0,0 +1,11 @@\r
181 +#ifndef _ZLIB_EXTRA_H\r
182 +#define _ZLIB_EXTRA_H\r
183 +\r
184 +#include <zlib.h>\r
185 +#include "util.h"\r
186 +/* Like getline, but read from a gzFile. Allocation is with talloc */\r
187 +util_status_t\r
188 +gz_getline (void *ctx, char **lineptr, size_t *line_size, ssize_t *bytes_read,\r
189 +           gzFile stream);\r
190 +\r
191 +#endif\r
192 -- \r
193 1.9.0\r
194 \r