]> git.lizzy.rs Git - getline.git/commitdiff
Initial commit
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sun, 24 Apr 2022 16:03:50 +0000 (18:03 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sun, 24 Apr 2022 16:03:50 +0000 (18:03 +0200)
.gitignore [new file with mode: 0644]
README.md [new file with mode: 0644]
getline.c [new file with mode: 0644]
getline.h [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..c6127b3
--- /dev/null
@@ -0,0 +1,52 @@
+# Prerequisites
+*.d
+
+# Object files
+*.o
+*.ko
+*.obj
+*.elf
+
+# Linker output
+*.ilk
+*.map
+*.exp
+
+# Precompiled Headers
+*.gch
+*.pch
+
+# Libraries
+*.lib
+*.a
+*.la
+*.lo
+
+# Shared objects (inc. Windows DLLs)
+*.dll
+*.so
+*.so.*
+*.dylib
+
+# Executables
+*.exe
+*.out
+*.app
+*.i*86
+*.x86_64
+*.hex
+
+# Debug files
+*.dSYM/
+*.su
+*.idb
+*.pdb
+
+# Kernel Module Compile Results
+*.mod*
+*.cmd
+.tmp_versions/
+modules.order
+Module.symvers
+Mkfile.old
+dkms.conf
diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..5ef3779
--- /dev/null
+++ b/README.md
@@ -0,0 +1,3 @@
+# Getline
+Provide a portable implementation of the getline POSIX 2008 function.
+Code taken from [Stackoverflow](https://stackoverflow.com/questions/735126/are-there-alternate-implementations-of-gnu-getline-interface/735472#735472) with slight modifications.
diff --git a/getline.c b/getline.c
new file mode 100644 (file)
index 0000000..07dd94b
--- /dev/null
+++ b/getline.c
@@ -0,0 +1,59 @@
+/* The original code is public domain -- Will Hartung 4/9/09 */
+/* Modifications, public domain as well, by Antti Haapala, 11/10/17
+   - Switched to getc on 5/23/19 */
+/* Modifications, public domain, by Elias Fleckenstein 24/04/2022 */
+
+#include <stdlib.h>
+#include <errno.h>
+#include "getline.h"
+
+// if typedef doesn't exist (msvc, blah)
+typedef intptr_t ssize_t;
+
+ssize_t getline(char **lineptr, size_t *n, FILE *stream) {
+    size_t pos;
+    int c;
+
+    if (lineptr == NULL || stream == NULL || n == NULL) {
+        errno = EINVAL;
+        return -1;
+    }
+
+    c = getc(stream);
+    if (c == EOF) {
+        return -1;
+    }
+
+    if (*lineptr == NULL) {
+        *lineptr = malloc(128);
+        if (*lineptr == NULL) {
+            return -1;
+        }
+        *n = 128;
+    }
+
+    pos = 0;
+    while(c != EOF) {
+        if (pos + 1 >= *n) {
+            size_t new_size = *n + (*n >> 2);
+            if (new_size < 128) {
+                new_size = 128;
+            }
+            char *new_ptr = realloc(*lineptr, new_size);
+            if (new_ptr == NULL) {
+                return -1;
+            }
+            *n = new_size;
+            *lineptr = new_ptr;
+        }
+
+        ((unsigned char *)(*lineptr))[pos ++] = c;
+        if (c == '\n') {
+            break;
+        }
+        c = getc(stream);
+    }
+
+    (*lineptr)[pos] = '\0';
+    return pos;
+}
diff --git a/getline.h b/getline.h
new file mode 100644 (file)
index 0000000..de3e483
--- /dev/null
+++ b/getline.h
@@ -0,0 +1,7 @@
+#ifndef _GETLINE_H_
+#define _GETLINE_H_
+
+#include <stdio.h>
+#include <stdint.h>
+
+#endif // _GETLINE_H_