annotate src/mygetline.c @ 771:5bab557020a6

More readable interface to mygetuntil() when reading to EOF
author Ben Schmidt
date Mon, 01 Nov 2010 08:31:08 +1100
parents ecb991e41a4c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
b045203f558b Add GPL info in top of files where it was missing, and remove a file that
mmj
parents: 11
diff changeset
1 /* Copyright (C) 2004 Morten K. Poulsen <morten at afdelingp.dk>
27
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
2 * Copyright (C) 2004 Mads Martin Joergensen <mmj at mmj.dk>
17
b045203f558b Add GPL info in top of files where it was missing, and remove a file that
mmj
parents: 11
diff changeset
3 *
b045203f558b Add GPL info in top of files where it was missing, and remove a file that
mmj
parents: 11
diff changeset
4 * $Id$
b045203f558b Add GPL info in top of files where it was missing, and remove a file that
mmj
parents: 11
diff changeset
5 *
225
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
7 * of this software and associated documentation files (the "Software"), to
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
8 * deal in the Software without restriction, including without limitation the
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
10 * sell copies of the Software, and to permit persons to whom the Software is
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
11 * furnished to do so, subject to the following conditions:
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
12 *
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
13 * The above copyright notice and this permission notice shall be included in
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
14 * all copies or substantial portions of the Software.
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
15 *
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 187
diff changeset
22 * IN THE SOFTWARE.
17
b045203f558b Add GPL info in top of files where it was missing, and remove a file that
mmj
parents: 11
diff changeset
23 */
b045203f558b Add GPL info in top of files where it was missing, and remove a file that
mmj
parents: 11
diff changeset
24
6
9eea0c9ae113 Helper functions
mmj
parents:
diff changeset
25 #include <stdlib.h>
9eea0c9ae113 Helper functions
mmj
parents:
diff changeset
26 #include <string.h>
27
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
27 #include <unistd.h>
153
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
28 #include <errno.h>
771
5bab557020a6 More readable interface to mygetuntil() when reading to EOF
Ben Schmidt
parents: 754
diff changeset
29 #include <stdio.h>
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 241
diff changeset
30
6
9eea0c9ae113 Helper functions
mmj
parents:
diff changeset
31 #include "mygetline.h"
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 241
diff changeset
32 #include "memory.h"
6
9eea0c9ae113 Helper functions
mmj
parents:
diff changeset
33
771
5bab557020a6 More readable interface to mygetuntil() when reading to EOF
Ben Schmidt
parents: 754
diff changeset
34 static char *mygetuntil(int fd, int eof)
27
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
35 {
153
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
36 size_t i = 0, res, buf_size = BUFSIZE; /* initial buffer size */
379
136e5bc27f7a Change statctrl to only return a file is not there, if stat() explicitly said so
mmj
parents: 245
diff changeset
37 char *buf, ch;
27
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
38
379
136e5bc27f7a Change statctrl to only return a file is not there, if stat() explicitly said so
mmj
parents: 245
diff changeset
39 buf = mymalloc(buf_size);
27
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
40 buf[0] = '\0';
153
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
41 while(1) {
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
42 res = read(fd, &ch, 1);
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
43 if(res < 0) {
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
44 if(errno == EINTR)
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
45 continue;
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
46 else {
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 241
diff changeset
47 myfree(buf);
153
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
48 return NULL;
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
49 }
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
50 }
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
51 if(res == 0) {
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
52 if(buf[0]) {
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
53 buf[i] = '\0';
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
54 return buf;
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
55 } else {
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 241
diff changeset
56 myfree(buf);
153
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
57 return NULL;
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
58 }
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
59 }
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
60
27
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
61 if(i == buf_size - 1) {
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
62 buf_size *= 2;
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 241
diff changeset
63 buf = myrealloc(buf, buf_size);
27
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
64 }
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
65 buf[i++] = ch;
771
5bab557020a6 More readable interface to mygetuntil() when reading to EOF
Ben Schmidt
parents: 754
diff changeset
66 if(eof != EOF && ch == eof) {
27
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
67 buf[i] = '\0';
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
68 return buf;
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
69 }
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
70 }
153
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
71 }
754
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
72
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
73 char *mygetline(int fd)
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
74 {
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
75 return mygetuntil(fd, '\n');
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
76 }
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
77
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
78 char *mygetcontent(int fd)
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
79 {
771
5bab557020a6 More readable interface to mygetuntil() when reading to EOF
Ben Schmidt
parents: 754
diff changeset
80 return mygetuntil(fd, EOF);
754
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
81 }