From 95ddbb8aaf1631711299b0710820ef9b0545224c Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 16 Apr 2000 06:19:11 +0000 Subject: 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) --- source3/lib/util_file.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) 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); +} -- cgit