aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/lib.c21
-rw-r--r--lib/lib.h1
-rw-r--r--toys/posix/sed.c24
3 files changed, 22 insertions, 24 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 8f8b4a3b..a6cb0ec3 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -1207,3 +1207,24 @@ char *getgroupname(gid_t gid)
return gr ? gr->gr_name : gnum;
}
+// Iterate over lines in file, calling function. Function can write 0 to
+// the line pointer if they want to keep it, or 1 to terminate processing,
+// otherwise line is freed. Passed file descriptor is closed at the end.
+void do_lines(int fd, void (*call)(char **pline, long len))
+{
+ FILE *fp = fd ? xfdopen(fd, "r") : stdin;
+
+ for (;;) {
+ char *line = 0;
+ ssize_t len;
+
+ len = getline(&line, (void *)&len, fp);
+ if (len > 0) {
+ call(&line, len);
+ if (line == (void *)1) break;
+ free(line);
+ } else break;
+ }
+
+ if (fd) fclose(fp);
+}
diff --git a/lib/lib.h b/lib/lib.h
index 43d6b1ff..5cd991c9 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -225,6 +225,7 @@ int regexec0(regex_t *preg, char *string, long len, int nmatch,
regmatch_t pmatch[], int eflags);
char *getusername(uid_t uid);
char *getgroupname(gid_t gid);
+void do_lines(int fd, void (*call)(char **pline, long len));
#define HR_SPACE 1 // Space between number and units
#define HR_B 2 // Use "B" for single byte units
diff --git a/toys/posix/sed.c b/toys/posix/sed.c
index 71988248..c62d2d10 100644
--- a/toys/posix/sed.c
+++ b/toys/posix/sed.c
@@ -621,30 +621,6 @@ done:
free(line);
}
-// Genericish function, can probably get moved to lib.c
-
-// Iterate over lines in file, calling function. Function can write 0 to
-// the line pointer if they want to keep it, or 1 to terminate processing,
-// otherwise line is freed. Passed file descriptor is closed at the end.
-static void do_lines(int fd, void (*call)(char **pline, long len))
-{
- FILE *fp = fd ? xfdopen(fd, "r") : stdin;
-
- for (;;) {
- char *line = 0;
- ssize_t len;
-
- len = getline(&line, (void *)&len, fp);
- if (len > 0) {
- call(&line, len);
- if (line == (void *)1) break;
- free(line);
- } else break;
- }
-
- if (fd) fclose(fp);
-}
-
// Callback called on each input file
static void do_sed(int fd, char *name)
{