Re: notmuch and "mute" -- useful to anyone?
[notmuch-archives.git] / b0 / 6ff6391f1ef159f82fcadacff7e9dd1a17f640
1 Return-Path: <jeff@ocjtech.us>\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 CA03B431FC4\r
6         for <notmuch@notmuchmail.org>; Mon, 23 Nov 2009 05:44:08 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 Received: from olra.theworths.org ([127.0.0.1])\r
9         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
10         with ESMTP id Ob-gCvvJIig4 for <notmuch@notmuchmail.org>;\r
11         Mon, 23 Nov 2009 05:44:07 -0800 (PST)\r
12 Received: from mail-yw0-f200.google.com (mail-yw0-f200.google.com\r
13         [209.85.211.200])\r
14         by olra.theworths.org (Postfix) with ESMTP id BCC58431FBF\r
15         for <notmuch@notmuchmail.org>; Mon, 23 Nov 2009 05:44:07 -0800 (PST)\r
16 Received: by ywh38 with SMTP id 38so4839962ywh.6\r
17         for <notmuch@notmuchmail.org>; Mon, 23 Nov 2009 05:44:07 -0800 (PST)\r
18 Received: by 10.150.109.7 with SMTP id h7mr4222931ybc.349.1258983847264;\r
19         Mon, 23 Nov 2009 05:44:07 -0800 (PST)\r
20 Received: from lt26923.campus.dmacc.edu ([69.57.47.215])\r
21         by mx.google.com with ESMTPS id 16sm2067660gxk.3.2009.11.23.05.44.05\r
22         (version=TLSv1/SSLv3 cipher=RC4-MD5);\r
23         Mon, 23 Nov 2009 05:44:05 -0800 (PST)\r
24 Received: from localhost ([127.0.0.1] helo=localhost.localdomain)\r
25         by lt26923.campus.dmacc.edu with esmtps (TLSv1:AES256-SHA:256)\r
26         (Exim 4.69) (envelope-from <jeff@ocjtech.us>)\r
27         id 1NCZD1-0007Hp-Qr; Mon, 23 Nov 2009 07:44:03 -0600\r
28 From: "Jeffrey C. Ollie" <jeff@ocjtech.us>\r
29 To: Not Much Mail <notmuch@notmuchmail.org>\r
30 Date: Mon, 23 Nov 2009 07:43:30 -0600\r
31 Message-Id: <1258983810-27522-2-git-send-email-jeff@ocjtech.us>\r
32 X-Mailer: git-send-email 1.6.5.2\r
33 In-Reply-To: <1258983810-27522-1-git-send-email-jeff@ocjtech.us>\r
34 References: <87bpiughr3.fsf@yoom.home.cworth.org>\r
35         <1258983810-27522-1-git-send-email-jeff@ocjtech.us>\r
36 Subject: [notmuch] [PATCH] Add private implementations of strndup and\r
37         getline.\r
38 X-BeenThere: notmuch@notmuchmail.org\r
39 X-Mailman-Version: 2.1.12\r
40 Precedence: list\r
41 List-Id: "Use and development of the notmuch mail system."\r
42         <notmuch.notmuchmail.org>\r
43 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
44         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
45 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
46 List-Post: <mailto:notmuch@notmuchmail.org>\r
47 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
48 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
49         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
50 X-List-Received-Date: Mon, 23 Nov 2009 13:44:09 -0000\r
51 \r
52 Add private implementations of strndup and getline for those platforms\r
53 that don't have them (notably Mac OS X) no matter what preprocessor\r
54 symbols you define.\r
55 \r
56 Signed-off-by: Jeffrey C. Ollie <jeff@ocjtech.us>\r
57 ---\r
58  lib/xutil.c |   99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
59  lib/xutil.h |    6 +++\r
60  2 files changed, 105 insertions(+), 0 deletions(-)\r
61 \r
62 diff --git a/lib/xutil.c b/lib/xutil.c\r
63 index 6fa5eb0..61467f1 100644\r
64 --- a/lib/xutil.c\r
65 +++ b/lib/xutil.c\r
66 @@ -79,6 +79,105 @@ xstrdup (const char *s)\r
67      return ret;\r
68  }\r
69  \r
70 +/* Mac OS X don't have strndup even if _GNU_SOURCE is defined */\r
71 +char *\r
72 +_notmuch_strndup (const char *s, size_t n)\r
73 +{\r
74 +    size_t len = strlen (s);\r
75 +    char *ret;\r
76 +\r
77 +    if (len <= n)\r
78 +       return strdup (s);\r
79 +\r
80 +    ret = malloc(n + 1);\r
81 +    strncpy(ret, s, n);\r
82 +    ret[n] = '\0';\r
83 +    return ret;\r
84 +}\r
85 +\r
86 +/* getline implementation is copied from glibc. */\r
87 +\r
88 +#ifndef SIZE_MAX\r
89 +# define SIZE_MAX ((size_t) -1)\r
90 +#endif\r
91 +#ifndef SSIZE_MAX\r
92 +# define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))\r
93 +#endif\r
94 +\r
95 +ssize_t\r
96 +_notmuch_getline (char **lineptr, size_t *n, FILE *fp)\r
97 +{\r
98 +    ssize_t result;\r
99 +    size_t cur_len = 0;\r
100 +\r
101 +    if (lineptr == NULL || n == NULL || fp == NULL)\r
102 +    {\r
103 +       errno = EINVAL;\r
104 +       return -1;\r
105 +    }\r
106 +\r
107 +    if (*lineptr == NULL || *n == 0)\r
108 +    {\r
109 +       *n = 120;\r
110 +       *lineptr = (char *) malloc (*n);\r
111 +       if (*lineptr == NULL)\r
112 +       {\r
113 +           result = -1;\r
114 +           goto end;\r
115 +       }\r
116 +    }\r
117 +\r
118 +    for (;;)\r
119 +    {\r
120 +       int i;\r
121 +\r
122 +       i = getc (fp);\r
123 +       if (i == EOF)\r
124 +       {\r
125 +           result = -1;\r
126 +           break;\r
127 +       }\r
128 +\r
129 +       /* Make enough space for len+1 (for final NUL) bytes.  */\r
130 +       if (cur_len + 1 >= *n)\r
131 +       {\r
132 +           size_t needed_max =\r
133 +               SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;\r
134 +           size_t needed = 2 * *n + 1;   /* Be generous. */\r
135 +           char *new_lineptr;\r
136 +\r
137 +           if (needed_max < needed)\r
138 +               needed = needed_max;\r
139 +           if (cur_len + 1 >= needed)\r
140 +           {\r
141 +               result = -1;\r
142 +               goto end;\r
143 +           }\r
144 +\r
145 +           new_lineptr = (char *) realloc (*lineptr, needed);\r
146 +           if (new_lineptr == NULL)\r
147 +           {\r
148 +               result = -1;\r
149 +               goto end;\r
150 +           }\r
151 +\r
152 +           *lineptr = new_lineptr;\r
153 +           *n = needed;\r
154 +       }\r
155 +\r
156 +       (*lineptr)[cur_len] = i;\r
157 +       cur_len++;\r
158 +\r
159 +       if (i == '\n')\r
160 +           break;\r
161 +    }\r
162 +    (*lineptr)[cur_len] = '\0';\r
163 +    result = cur_len ? (ssize_t) cur_len : result;\r
164 +\r
165 +end:\r
166 +    return result;\r
167 +}\r
168 +\r
169  char *\r
170  xstrndup (const char *s, size_t n)\r
171  {\r
172 diff --git a/lib/xutil.h b/lib/xutil.h\r
173 index b973f7d..d13259a 100644\r
174 --- a/lib/xutil.h\r
175 +++ b/lib/xutil.h\r
176 @@ -39,6 +39,12 @@ char *\r
177  xstrdup (const char *s);\r
178  \r
179  char *\r
180 +_notmuch_strndup (const char *s, size_t n);\r
181 +\r
182 +ssize_t\r
183 +_notmuch_getline (char **lineptr, size_t *n, FILE *stream);\r
184 +\r
185 +char *\r
186  xstrndup (const char *s, size_t n);\r
187  \r
188  void\r
189 -- \r
190 1.6.5.2\r
191 \r