]> git.cworth.org Git - notmuch/blob - compat/getdelim.c
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / compat / getdelim.c
1 /* getdelim.c --- Implementation of replacement getdelim function.
2  * Copyright (C) 1994, 1996, 1997, 1998, 2001, 2003, 2005, 2006, 2007,
3  * 2008, 2009 Free Software Foundation, Inc.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 3, or (at
8  * your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA.  */
19
20 /* Ported from glibc by Simon Josefsson. */
21
22 #include "compat.h"
23
24 #include <stdio.h>
25
26 #include <limits.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <errno.h>
30
31 #ifndef SSIZE_MAX
32 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
33 #endif
34
35 #if USE_UNLOCKED_IO
36 # include "unlocked-io.h"
37 # define getc_maybe_unlocked(fp)        getc (fp)
38 #elif ! HAVE_FLOCKFILE || ! HAVE_FUNLOCKFILE || ! HAVE_DECL_GETC_UNLOCKED
39 # undef flockfile
40 # undef funlockfile
41 # define flockfile(x) ((void) 0)
42 # define funlockfile(x) ((void) 0)
43 # define getc_maybe_unlocked(fp)        getc (fp)
44 #else
45 # define getc_maybe_unlocked(fp)        getc_unlocked (fp)
46 #endif
47
48 /* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
49  * NUL-terminate it).  *LINEPTR is a pointer returned from malloc (or
50  * NULL), pointing to *N characters of space.  It is realloc'ed as
51  * necessary.  Returns the number of characters read (not including
52  * the null terminator), or -1 on error or EOF.  */
53
54 ssize_t
55 getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
56 {
57     ssize_t result = -1;
58     size_t cur_len = 0;
59
60     if (lineptr == NULL || n == NULL || fp == NULL) {
61         errno = EINVAL;
62         return -1;
63     }
64
65     flockfile (fp);
66
67     if (*lineptr == NULL || *n == 0) {
68         char *new_lineptr;
69         *n = 120;
70         new_lineptr = (char *) realloc (*lineptr, *n);
71         if (new_lineptr == NULL) {
72             result = -1;
73             goto unlock_return;
74         }
75         *lineptr = new_lineptr;
76     }
77
78     for (;;) {
79         int i;
80
81         i = getc_maybe_unlocked (fp);
82         if (i == EOF) {
83             result = -1;
84             break;
85         }
86
87         /* Make enough space for len+1 (for final NUL) bytes.  */
88         if (cur_len + 1 >= *n) {
89             size_t needed_max =
90                 SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
91             size_t needed = 2 * *n + 1; /* Be generous. */
92             char *new_lineptr;
93
94             if (needed_max < needed)
95                 needed = needed_max;
96             if (cur_len + 1 >= needed) {
97                 result = -1;
98                 errno = EOVERFLOW;
99                 goto unlock_return;
100             }
101
102             new_lineptr = (char *) realloc (*lineptr, needed);
103             if (new_lineptr == NULL) {
104                 result = -1;
105                 goto unlock_return;
106             }
107
108             *lineptr = new_lineptr;
109             *n = needed;
110         }
111
112         (*lineptr)[cur_len] = i;
113         cur_len++;
114
115         if (i == delimiter)
116             break;
117     }
118     (*lineptr)[cur_len] = '\0';
119     result = cur_len ? (ssize_t) cur_len : result;
120
121   unlock_return:
122     funlockfile (fp); /* doesn't set errno */
123
124     return result;
125 }