summaryrefslogtreecommitdiff
path: root/source3/lib/util_file.c
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2000-04-16 06:19:11 +0000
committerAndrew Tridgell <tridge@samba.org>2000-04-16 06:19:11 +0000
commit95ddbb8aaf1631711299b0710820ef9b0545224c (patch)
tree53884aa7493c52577437a176810ab53d81ed9d68 /source3/lib/util_file.c
parent837887ffd6a82616a5388cb3536c9c1283464ee0 (diff)
downloadsamba-95ddbb8aaf1631711299b0710820ef9b0545224c.tar.gz
samba-95ddbb8aaf1631711299b0710820ef9b0545224c.tar.bz2
samba-95ddbb8aaf1631711299b0710820ef9b0545224c.zip
the new file_lines_load() and file_lines_free() routines. Very useful!
------------ The following series of commits are for the new tdb based printing backend. This completely replaces our old printing backend. Major changes include: - all print ops are now done in printing/*.c rather than scattered all over the place - system job ids are decoupled from SMB job ids - the lpq parsers don't need to be nearly so smart, they only need to parse the filename, the status and system job id - we can store lots more info about a job, including the full job name - the queue cache control is much better I also added a new utility routine file_lines_load() that loads a text file and parses it into lines. This is used in out lpq parsing and I also want to use it to replace all of our fgets() based code in other places. (This used to be commit be1e98b3f7a6007d2c9ac8e079724cb264e0dd3a)
Diffstat (limited to 'source3/lib/util_file.c')
-rw-r--r--source3/lib/util_file.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c
index 9eb38460d9..39c97d5c3f 100644
--- a/source3/lib/util_file.c
+++ b/source3/lib/util_file.c
@@ -327,3 +327,67 @@ char *fgets_slash(char *s2,int maxlen,FILE *f)
return(s);
}
+
+/****************************************************************************
+load a file into memory and return an array of pointers to lines in the file
+must be freed with file_lines_free()
+****************************************************************************/
+char **file_lines_load(char *fname, int *numlines)
+{
+ int fd, i;
+ SMB_STRUCT_STAT sbuf;
+ char *p, *s, **ret;
+ size_t size;
+
+ fd = open(fname,O_RDONLY);
+ if (fd == -1) return NULL;
+
+ if (sys_fstat(fd, &sbuf) != 0) return NULL;
+ size = sbuf.st_size;
+
+ if (size == 0) return NULL;
+
+ p = (char *)malloc(size+1);
+ if (!p) return NULL;
+
+ if (read(fd, p, size) != size) {
+ free(p);
+ return NULL;
+ }
+ p[size] = 0;
+
+ close(fd);
+
+ for (s = p, i=0; s < p+size; s++) {
+ if (s[0] == '\n') i++;
+ }
+
+ ret = (char **)malloc(sizeof(ret[0])*(i+1));
+ if (!ret) {
+ free(p);
+ return NULL;
+ }
+ *numlines = i;
+
+ ret[0] = p;
+ for (s = p, i=0; s < p+size; s++) {
+ if (s[0] == '\n') {
+ s[0] = 0;
+ i++;
+ ret[i] = s+1;
+ }
+ if (s[0] == '\r') s[0] = 0;
+ }
+
+ return ret;
+}
+
+/****************************************************************************
+free lines loaded with file_lines_load
+****************************************************************************/
+void file_lines_free(char **lines)
+{
+ if (!lines) return;
+ free(lines[0]);
+ free(lines);
+}