├── .gitignore
├── .travis.yml
├── Makefile
├── README.md
├── UNLICENSE
├── getline.c
└── getline.h
/.gitignore:
--------------------------------------------------------------------------------
1 | # General files
2 | *~
3 | *.swp
4 | .DS_Store
5 |
6 | # Object files
7 | *.o
8 | *.ko
9 |
10 | # Libraries
11 | *.lib
12 | *.a
13 |
14 | # Shared objects (inc. Windows DLLs)
15 | *.dll
16 | *.so
17 | *.so.*
18 | *.dylib
19 |
20 | # Executables
21 | *.exe
22 | *.out
23 | *.app
24 |
25 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: c
2 |
3 | compiler:
4 | - gcc
5 | - clang
6 |
7 | script: make
8 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | CFLAGS = -Wall
2 |
3 | .PHONY: all clean
4 |
5 | all: getline.o libgetline.so.1 libgetline.a
6 |
7 | getline.o: getline.c getline.h
8 | $(CC) $(CFLAGS) -o getline.o -c -fPIC getline.c
9 |
10 | libgetline.so.1: getline.o
11 | $(CC) $(CFLAGS) -o libgetline.so.1 -shared getline.o
12 |
13 | libgetline.a: getline.o
14 | ar rcs libgetline.a getline.o
15 |
16 | clean:
17 | rm -f libgetline.* *.o
18 |
19 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # getline/getdelim
2 |
3 | [](https://travis-ci.org/ivanrad/getline)
4 |
5 | Read a delimited record from stream.
6 |
7 | Yet another (hopefully portable C) implementation of getline/getdelim.
8 | These are ersatz functions, a drop-in replacement, to be used on those occasions when your C library does not already support them.
9 |
10 | For more details, see [Open Group Base Specification for getdelim/getline][opengroup-spec].
11 |
12 | ## Building the project
13 |
14 | Just run `make`.
15 |
16 | ## License
17 |
18 | This code is unlicensed -- free and released into the public domain. See `UNLICENSE` file for more information.
19 |
20 | [opengroup-spec]: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html
21 |
--------------------------------------------------------------------------------
/UNLICENSE:
--------------------------------------------------------------------------------
1 | This is free and unencumbered software released into the public domain.
2 |
3 | Anyone is free to copy, modify, publish, use, compile, sell, or
4 | distribute this software, either in source code form or as a compiled
5 | binary, for any purpose, commercial or non-commercial, and by any
6 | means.
7 |
8 | In jurisdictions that recognize copyright laws, the author or authors
9 | of this software dedicate any and all copyright interest in the
10 | software to the public domain. We make this dedication for the benefit
11 | of the public at large and to the detriment of our heirs and
12 | successors. We intend this dedication to be an overt act of
13 | relinquishment in perpetuity of all present and future rights to this
14 | software under copyright law.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | OTHER DEALINGS IN THE SOFTWARE.
23 |
24 | For more information, please refer to
25 |
--------------------------------------------------------------------------------
/getline.c:
--------------------------------------------------------------------------------
1 | /* getline.c
2 | *
3 | * getdelim(), getline() - read a delimited record from stream, ersatz implementation
4 | *
5 | * For more details, see: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html
6 | *
7 | */
8 |
9 | #include "getline.h"
10 | #include
11 | #include
12 | #include
13 |
14 | ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream) {
15 | char *cur_pos, *new_lineptr;
16 | size_t new_lineptr_len;
17 | int c;
18 |
19 | if (lineptr == NULL || n == NULL || stream == NULL) {
20 | errno = EINVAL;
21 | return -1;
22 | }
23 |
24 | if (*lineptr == NULL) {
25 | *n = 128; /* init len */
26 | if ((*lineptr = (char *)malloc(*n)) == NULL) {
27 | errno = ENOMEM;
28 | return -1;
29 | }
30 | }
31 |
32 | cur_pos = *lineptr;
33 | for (;;) {
34 | c = getc(stream);
35 |
36 | if (ferror(stream) || (c == EOF && cur_pos == *lineptr))
37 | return -1;
38 |
39 | if (c == EOF)
40 | break;
41 |
42 | if ((*lineptr + *n - cur_pos) < 2) {
43 | if (SSIZE_MAX / 2 < *n) {
44 | #ifdef EOVERFLOW
45 | errno = EOVERFLOW;
46 | #else
47 | errno = ERANGE; /* no EOVERFLOW defined */
48 | #endif
49 | return -1;
50 | }
51 | new_lineptr_len = *n * 2;
52 |
53 | if ((new_lineptr = (char *)realloc(*lineptr, new_lineptr_len)) == NULL) {
54 | errno = ENOMEM;
55 | return -1;
56 | }
57 | cur_pos = new_lineptr + (cur_pos - *lineptr);
58 | *lineptr = new_lineptr;
59 | *n = new_lineptr_len;
60 | }
61 |
62 | *cur_pos++ = (char)c;
63 |
64 | if (c == delim)
65 | break;
66 | }
67 |
68 | *cur_pos = '\0';
69 | return (ssize_t)(cur_pos - *lineptr);
70 | }
71 |
72 | ssize_t getline(char **lineptr, size_t *n, FILE *stream) {
73 | return getdelim(lineptr, n, '\n', stream);
74 | }
75 |
76 |
--------------------------------------------------------------------------------
/getline.h:
--------------------------------------------------------------------------------
1 | #ifndef GETLINE_H
2 | #define GETLINE_H
3 |
4 | #include
5 |
6 | extern ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
7 |
8 | extern ssize_t getline(char **lineptr, size_t *n, FILE *stream);
9 |
10 | #endif /* GETLINE_H */
11 |
12 |
--------------------------------------------------------------------------------