aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--VERSION2
-rw-r--r--l4array.h2
-rw-r--r--l4common.h2
-rw-r--r--l4file.c26
-rw-r--r--l4file.h15
-rw-r--r--test-file.c48
7 files changed, 95 insertions, 4 deletions
diff --git a/Makefile b/Makefile
index 23564d2..9804974 100644
--- a/Makefile
+++ b/Makefile
@@ -42,9 +42,9 @@ l4arg_o_DEPENDS= l4common.h l4array.o
test_array_o_DEPENDS= l4array.o
test_array2_o_DEPENDS= l4array2.o
-test_file_o_DEPENDS= l4file.o
+test_file_o_DEPENDS= l4file.o l4array.o
test_list_o_DEPENDS= l4list.o
-test_arg_o_DEPENDS= l4arg.o
+test_arg_o_DEPENDS= l4arg.o l4array.o
.POSIX:
.PHONY: all clean install install-HEADERS install-LIB \
diff --git a/VERSION b/VERSION
index 6979a6c..8d39474 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.91.0
+1.91.1
diff --git a/l4array.h b/l4array.h
index 145f6c9..d3e74ee 100644
--- a/l4array.h
+++ b/l4array.h
@@ -66,7 +66,7 @@ int lbs_array_set_max (LbsArray* array, size_t max);
((LBS_COMMON_CHECK_TYPE ((array), LbsArray*)->free_func) = (value))
#define lbs_array_append_var(array,var) \
- lbs_array_append_data ((array), (&(var)))
+ (lbs_array_append_data ((array), (&(var))))
int lbs_array_append_ptr (LbsArray* array, const void* ptr);
int lbs_array_append_data (LbsArray* array, const void* data);
int lbs_array_remove (LbsArray* array);
diff --git a/l4common.h b/l4common.h
index 34eef26..12d5697 100644
--- a/l4common.h
+++ b/l4common.h
@@ -20,4 +20,6 @@
# define LBS_COMMON_CHECK_TYPE(x,type) (x)
#endif /* __STDC_VERSION__ */
+#define LBS_COMMON_NULL_PTR ((void*)NULL)
+
#endif /* LBS_COMMON_H */
diff --git a/l4file.c b/l4file.c
new file mode 100644
index 0000000..b4f8a57
--- /dev/null
+++ b/l4file.c
@@ -0,0 +1,26 @@
+#include "l4file.h"
+
+#include <stdio.h>
+
+LbsArray* lbs_file_read_file_line_delim (FILE* infile, int delim) {
+ LbsArray* str = lbs_array_new (sizeof (char));
+ if (str == NULL){
+ return NULL;
+ }
+
+ int c;
+ char towrite;
+ while ((c = getc (infile)) != delim && !feof (infile)) {
+ towrite = c;
+ if (lbs_array_append_var (str, towrite) < 0){
+ lbs_array_unref (str);
+ return NULL;
+ }
+ }
+ towrite = '\0';
+ if (lbs_array_append_var (str, towrite) < 0) {
+ lbs_array_unref (str);
+ return NULL;
+ }
+ return str;
+}
diff --git a/l4file.h b/l4file.h
new file mode 100644
index 0000000..3e8e0f7
--- /dev/null
+++ b/l4file.h
@@ -0,0 +1,15 @@
+#ifndef LBS_FILE_H
+#define LBS_FILE_H
+
+#include <l4array.h>
+#include <stdio.h>
+
+#define lbs_file_read_stdin_line() \
+ (lbs_file_read_file_line_delim (stdin, '\n'))
+#define lbs_file_read_stdin_line_delim(delim) \
+ (lbs_file_read_file_line_delim (stdin, (delim)))
+#define lbs_file_read_file_line(infile) \
+ (lbs_file_read_file_line_delim ((infile), '\n'))
+LbsArray* lbs_file_read_file_line_delim (FILE* infile, int delim);
+
+#endif /* LBS_FILE_H */
diff --git a/test-file.c b/test-file.c
new file mode 100644
index 0000000..5db6ec6
--- /dev/null
+++ b/test-file.c
@@ -0,0 +1,48 @@
+#undef NDEBUG
+#define _POSIX_C_SOURCE 200809L
+#include <l4array.h>
+#include <l4file.h>
+
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+int main () {
+ int p[2];
+ assert (pipe (p) >= 0);
+
+ pid_t pid = fork ();
+ if (pid < 0) {
+ return 1;
+ } else if (pid > 0){
+ FILE* fp = fopen ("test-file.c", "rb");
+ assert (fp != NULL);
+ FILE* fw = fdopen (p[1], "wb");
+ assert (fw != NULL);
+
+ close (p[0]);
+
+ LbsArray* line;
+ while ((line = lbs_file_read_file_line (fp)) != NULL && !feof (fp)) {
+ fputs (lbs_array_get_data (line), fw);
+ putc ('\n', fw);
+ lbs_array_unref (line);
+ }
+
+ fclose (fp);
+ fclose (fw);
+ } else {
+ close (p[1]);
+ assert (dup2 (p[0], 0) >= 0);
+ execlp ("cmp", "cmp", "test-file.c", "-", LBS_COMMON_NULL_PTR);
+ assert (0);
+ }
+
+ int status;
+ wait (&status);
+ assert (WIFEXITED (status) && WEXITSTATUS (status) == 0);
+
+ return 0;
+}