annotate src/ggets.c @ 31:416346c6dc74

Add fp handle check.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 05 Aug 2013 19:18:40 +0300
parents 785057719d9b
children f0073a47c31d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /* File ggets.c - goodgets is a safe alternative to gets */
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 /* By C.B. Falconer. Public domain 2002-06-22 */
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 /* attribution appreciated. */
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 /* Revised 2002-06-26 New prototype.
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 2002-06-27 Incomplete final lines
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 2006-06-14 Simplified, using getc, not fgets
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 2006-06-15 Fixed memory leak at EOF
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 */
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 /* fggets and ggets [which is fggets(ln, stdin)] set *ln to
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 a buffer filled with the next complete line from the text
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 stream f. The storage has been allocated within fggets,
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 and is normally reduced to be an exact fit. The trailing
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 \n has been removed, so the resultant line is ready for
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 dumping with puts. The buffer will be as large as is
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 required to hold the complete line.
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 Note: this means a final file line without a \n terminator
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 has an effective \n appended, as EOF occurs within the read.
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 If no error occurs fggets returns 0. If an EOF occurs on
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 the input file, EOF is returned. For memory allocation
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 errors some positive value is returned. In this case *ln
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 may point to a partial line. For other errors memory is
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 freed and *ln is set to NULL.
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 Freeing of assigned storage is the callers responsibility
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 */
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 #include <stdio.h>
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 #include <stdlib.h>
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 #include "ggets.h"
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 #define INITSIZE 112 /* power of 2 minus 16, helps malloc */
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 #define DELTASIZE (INITSIZE + 16)
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 enum {OK = 0, NOMEM};
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 int fggets(char* *ln, FILE *f)
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 {
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 int cursize, ch, ix;
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 char *buffer, *temp;
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 *ln = NULL; /* default */
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 if (NULL == (buffer = malloc(INITSIZE))) return NOMEM;
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 cursize = INITSIZE;
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 ix = 0;
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 while ((EOF != (ch = getc(f))) && ('\n' != ch)) {
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 if (ix >= (cursize - 1)) { /* extend buffer */
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 cursize += DELTASIZE;
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 if (NULL == (temp = realloc(buffer, (size_t)cursize))) {
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 /* ran out of memory, return partial line */
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 buffer[ix] = '\0';
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 *ln = buffer;
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 return NOMEM;
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 buffer = temp;
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 buffer[ix++] = ch;
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 if ((EOF == ch) && (0 == ix)) {
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 free(buffer);
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 return EOF;
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 buffer[ix] = '\0';
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 if (NULL == (temp = realloc(buffer, (size_t)ix + 1))) {
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 *ln = buffer; /* without reducing it */
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 else *ln = temp;
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 return OK;
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 } /* fggets */
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 /* End of ggets.c */