annotate src/mygetline.c @ 754:ecb991e41a4c

Add $controlN$ substitution The interfaces to substitute() and substitute_one() have changed, as they now need to know the listdir to be able to find control files
author Ben Schmidt
date Wed, 06 Oct 2010 23:30:26 +1100
parents 136e5bc27f7a
children 5bab557020a6
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>
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 241
diff changeset
29
6
9eea0c9ae113 Helper functions
mmj
parents:
diff changeset
30 #include "mygetline.h"
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 241
diff changeset
31 #include "memory.h"
6
9eea0c9ae113 Helper functions
mmj
parents:
diff changeset
32
754
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
33 static char *mygetuntil(int fd, char eof)
27
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
34 {
153
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
35 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
36 char *buf, ch;
27
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
37
379
136e5bc27f7a Change statctrl to only return a file is not there, if stat() explicitly said so
mmj
parents: 245
diff changeset
38 buf = mymalloc(buf_size);
27
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
39 buf[0] = '\0';
153
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
40 while(1) {
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
41 res = read(fd, &ch, 1);
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
42 if(res < 0) {
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
43 if(errno == EINTR)
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
44 continue;
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
45 else {
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 241
diff changeset
46 myfree(buf);
153
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
47 return NULL;
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
48 }
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 if(res == 0) {
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
51 if(buf[0]) {
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
52 buf[i] = '\0';
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
53 return buf;
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
54 } else {
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 241
diff changeset
55 myfree(buf);
153
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
56 return NULL;
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
57 }
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
27
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
60 if(i == buf_size - 1) {
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
61 buf_size *= 2;
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 241
diff changeset
62 buf = myrealloc(buf, buf_size);
27
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
63 }
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
64 buf[i++] = ch;
754
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
65 if(ch == eof) {
27
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
66 buf[i] = '\0';
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
67 return buf;
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
68 }
91830455b49c mygetline is turned into myfgetline since it works on FILE *'s and
mmj
parents: 17
diff changeset
69 }
153
1c3083e7acfa mygetline EINTR handling and errno = 0
mmj
parents: 88
diff changeset
70 }
754
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
71
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
72 char *mygetline(int fd)
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
73 {
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
74 return mygetuntil(fd, '\n');
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
75 }
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
76
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
77 char *mygetcontent(int fd)
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
78 {
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
79 return mygetuntil(fd, '\0');
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 379
diff changeset
80 }