mirror of
https://github.com/samba-team/samba.git
synced 2025-03-24 10:50:22 +03:00
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)
This commit is contained in:
parent
837887ffd6
commit
95ddbb8aaf
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user