annotate src/ggets.c @ 44:f0073a47c31d

Fix various warnings.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 06 Aug 2013 00:04:37 +0300
parents 785057719d9b
children
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 */
44
f0073a47c31d Fix various warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
46 if ((buffer = (char *) malloc(INITSIZE)) == NULL) return NOMEM;
0
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;
44
f0073a47c31d Fix various warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
50 while ((ch = getc(f)) != EOF && ch != '\n') {
0
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;
44
f0073a47c31d Fix various warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
53 if ((temp = (char *) realloc(buffer, (size_t)cursize)) == NULL) {
0
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 }
44
f0073a47c31d Fix various warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
63
f0073a47c31d Fix various warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
64 if (ch == EOF && ix == 0) {
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 free(buffer);
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 return EOF;
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 buffer[ix] = '\0';
44
f0073a47c31d Fix various warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
70 if ((temp = (char *) realloc(buffer, (size_t)ix + 1)) == NULL) {
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 *ln = buffer; /* without reducing it */
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 else *ln = temp;
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 return OK;
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 } /* fggets */
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 /* End of ggets.c */