Mercurial > hg > mlmmj
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 |
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> |
771
5bab557020a6
More readable interface to mygetuntil() when reading to EOF
Ben Schmidt
parents:
754
diff
changeset
|
29 #include <stdio.h> |
245 | 30 |
6 | 31 #include "mygetline.h" |
245 | 32 #include "memory.h" |
6 | 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 | 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 | 41 while(1) { |
42 res = read(fd, &ch, 1); | |
43 if(res < 0) { | |
44 if(errno == EINTR) | |
45 continue; | |
46 else { | |
245 | 47 myfree(buf); |
153 | 48 return NULL; |
49 } | |
50 } | |
51 if(res == 0) { | |
52 if(buf[0]) { | |
53 buf[i] = '\0'; | |
54 return buf; | |
55 } else { | |
245 | 56 myfree(buf); |
153 | 57 return NULL; |
58 } | |
59 } | |
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 | 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 | 71 } |
754 | 72 |
73 char *mygetline(int fd) | |
74 { | |
75 return mygetuntil(fd, '\n'); | |
76 } | |
77 | |
78 char *mygetcontent(int fd) | |
79 { | |
771
5bab557020a6
More readable interface to mygetuntil() when reading to EOF
Ben Schmidt
parents:
754
diff
changeset
|
80 return mygetuntil(fd, EOF); |
754 | 81 } |