aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2004-02-11 15:27:36 +0000
committerSergey Poznyakoff <gray@gnu.org.ua>2004-02-11 15:27:36 +0000
commit549877951757607f076323a8a4a2b1fb5f00088c (patch)
tree4c2a4b457edcf797a26455cfc4eaf947022e68fd /lib
parent77d7bc31982fc071c1b82c0d7a182bb459786451 (diff)
downloadanubis-549877951757607f076323a8a4a2b1fb5f00088c.tar.gz
anubis-549877951757607f076323a8a4a2b1fb5f00088c.tar.bz2
Added getline.c, getline.h
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile.am2
-rw-r--r--lib/getline.c117
-rw-r--r--lib/getline.h35
3 files changed, 154 insertions, 0 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 07f5bb7..c7205bc 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -29,2 +29,4 @@ libanubis_a_SOURCES =
EXTRA_DIST = \
+ getline.h \
+ getopt.h \
getopt.h \
diff --git a/lib/getline.c b/lib/getline.c
new file mode 100644
index 0000000..30fc5e0
--- /dev/null
+++ b/lib/getline.c
@@ -0,0 +1,117 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+ Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+
+/* First implementation by Alain Magloire */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+ssize_t
+getline (char **lineptr, size_t *n, FILE *stream)
+{
+ return getdelim (lineptr, n, '\n', stream);
+}
+
+#ifndef HAVE_GETDELIM
+
+/* Default value for line length. */
+static const int line_size = 128;
+
+ssize_t
+getdelim (char **lineptr, size_t *n, int delim, FILE *stream)
+{
+ int indx = 0;
+ int c;
+
+ /* Sanity checks. */
+ if (lineptr == NULL || n == NULL || stream == NULL)
+ return -1;
+
+ /* Allocate the line the first time. */
+ if (*lineptr == NULL)
+ {
+ *lineptr = malloc (line_size);
+ if (*lineptr == NULL)
+ return -1;
+ *n = line_size;
+ }
+
+ while ((c = getc (stream)) != EOF)
+ {
+ /* Check if more memory is needed. */
+ if (indx >= *n)
+ {
+ *lineptr = realloc (*lineptr, *n + line_size);
+ if (*lineptr == NULL)
+ return -1;
+ *n += line_size;
+ }
+
+ /* Push the result in the line. */
+ (*lineptr)[indx++] = c;
+
+ /* Bail out. */
+ if (c == delim)
+ break;
+ }
+
+ /* Make room for the null character. */
+ if (indx >= *n)
+ {
+ *lineptr = realloc (*lineptr, *n + line_size);
+ if (*lineptr == NULL)
+ return -1;
+ *n += line_size;
+ }
+
+ /* Null terminate the buffer. */
+ (*lineptr)[indx++] = 0;
+
+ /* The last line may not have the delimiter, we have to
+ * return what we got and the error will be seen on the
+ * next iteration. */
+ return (c == EOF && (indx - 1) == 0) ? -1 : indx - 1;
+}
+
+#endif /* HAVE_GETDELIM */
+
+
+#ifdef STANDALONE
+int main(void)
+{
+ FILE * fp;
+ char * line = NULL;
+ size_t len = 0;
+ ssize_t read;
+ fp = fopen("/etc/passwd", "r");
+ if (fp == NULL)
+ exit(EXIT_FAILURE);
+ while ((read = getline(&line, &len, fp)) != -1) {
+ printf("Retrieved line of length %zu :\n", read);
+ printf("%s", line);
+ }
+ if (line)
+ free(line);
+ return EXIT_SUCCESS;
+}
+#endif
diff --git a/lib/getline.h b/lib/getline.h
new file mode 100644
index 0000000..77b234f
--- /dev/null
+++ b/lib/getline.h
@@ -0,0 +1,35 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+ Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+
+#ifndef _GETLINE_H_
+# define _GETLINE_H_ 1
+
+# include <stdio.h>
+
+# ifndef PARAMS
+# if defined (__GNUC__) || __STDC__
+# define PARAMS(args) args
+# else
+# define PARAMS(args) ()
+# endif
+# endif
+
+extern int getline PARAMS ((char **_lineptr, size_t *_n, FILE *_stream));
+
+extern int getdelim PARAMS ((char **_lineptr, size_t *_n, int _delimiter, FILE *_stream));
+
+#endif /* ! _GETLINE_H_ */

Return to:

Send suggestions and report system problems to the System administrator.