Mercurial > hg > mlmmj
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 |
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 | 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. | |
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 | 25 #include <stdlib.h> |
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 | 28 #include <errno.h> |
245 | 29 |
6 | 30 #include "mygetline.h" |
245 | 31 #include "memory.h" |
6 | 32 |
754 | 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 | 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 | 40 while(1) { |
41 res = read(fd, &ch, 1); | |
42 if(res < 0) { | |
43 if(errno == EINTR) | |
44 continue; | |
45 else { | |
245 | 46 myfree(buf); |
153 | 47 return NULL; |
48 } | |
49 } | |
50 if(res == 0) { | |
51 if(buf[0]) { | |
52 buf[i] = '\0'; | |
53 return buf; | |
54 } else { | |
245 | 55 myfree(buf); |
153 | 56 return NULL; |
57 } | |
58 } | |
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 | 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 | 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 | 70 } |
754 | 71 |
72 char *mygetline(int fd) | |
73 { | |
74 return mygetuntil(fd, '\n'); | |
75 } | |
76 | |
77 char *mygetcontent(int fd) | |
78 { | |
79 return mygetuntil(fd, '\0'); | |
80 } |