1 /* Copyright (C) 2004 Mads Martin Joergensen <mmj at mmj.dk>
2 * Copyright (C) 2006 Morten K. Poulsen <morten at afdelingp.dk>
3 *
4 * $Id$
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include <stdlib.h>
26 #include <unistd.h>
27
28 #include "mygetline.h"
29 #include "gethdrline.h"
30 #include "strgen.h"
31 #include "memory.h"
32 #include "wrappers.h"
33 #include "log_error.h"
34 #include "chomp.h"
35
36
gethdrline(int fd,char ** unfolded)37 char *gethdrline(int fd, char **unfolded)
38 {
39 char *line = NULL, *retstr = NULL, *oldretstr = NULL, *oldunfolded = NULL;
40 char ch;
41 ssize_t n;
42
43 retstr = mygetline(fd);
44 if (!retstr) {
45 if (unfolded != NULL)
46 *unfolded = NULL;
47 return NULL;
48 }
49
50 if (unfolded != NULL)
51 *unfolded = mystrdup(retstr);
52
53 chomp(retstr);
54
55 /* end-of-headers */
56 if (*retstr == '\0') {
57 if (unfolded != NULL) {
58 myfree(*unfolded);
59 *unfolded = NULL;
60 }
61 myfree(retstr);
62 return NULL;
63 }
64
65 for(;;) {
66 /* look ahead one char to determine if we need to fold */
67 n = readn(fd, &ch, 1);
68 if (n == 0) { /* end of file, and therefore also headers */
69 return retstr;
70 } else if (n == -1) { /* error */
71 log_error(LOG_ARGS, "readn() failed in gethdrline()");
72 if (unfolded != NULL) {
73 myfree(*unfolded);
74 *unfolded = NULL;
75 }
76 myfree(retstr);
77 return NULL;
78 }
79
80 if (lseek(fd, -1, SEEK_CUR) == (off_t)-1) {
81 log_error(LOG_ARGS, "lseek() failed in gethdrline()");
82 if (unfolded != NULL) {
83 myfree(*unfolded);
84 *unfolded = NULL;
85 }
86 myfree(retstr);
87 return NULL;
88 }
89
90 if ((ch != '\t') && (ch != ' ')) /* no more folding */
91 return retstr;
92
93 line = mygetline(fd);
94 if (!line)
95 return retstr;
96
97 if (unfolded != NULL) {
98 oldunfolded = *unfolded;
99 *unfolded = concatstr(2, oldunfolded, line);
100 myfree(oldunfolded);
101 }
102
103 chomp(line);
104 oldretstr = retstr;
105 retstr = concatstr(2, oldretstr, line);
106 myfree(oldretstr);
107
108 myfree(line);
109 }
110 }
111