|
Revision 1616, 1.8 kB
(checked in by ensc, 4 years ago)
|
initial checkin
|
- Property svn:eol-style set to
native
- Property svn:keywords set to
Author Date Id Revision
|
| Line | |
|---|
| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
#ifdef HAVE_CONFIG_H |
|---|
| 20 |
# include <config.h> |
|---|
| 21 |
#endif |
|---|
| 22 |
|
|---|
| 23 |
#include "filecfg.h" |
|---|
| 24 |
#include <string.h> |
|---|
| 25 |
#include <unistd.h> |
|---|
| 26 |
#include <fcntl.h> |
|---|
| 27 |
|
|---|
| 28 |
char * |
|---|
| 29 |
FileCfg_readEntryStr (PathInfo const *base, char const *file, |
|---|
| 30 |
bool allow_multiline, char const *dflt) |
|---|
| 31 |
{ |
|---|
| 32 |
PathInfo filepath = { .d = file, .l = strlen(file) }; |
|---|
| 33 |
PathInfo path = *base; |
|---|
| 34 |
char path_buf[ENSC_PI_APPSZ(path, filepath)]; |
|---|
| 35 |
int fd = -1; |
|---|
| 36 |
off_t sz; |
|---|
| 37 |
char * res = 0; |
|---|
| 38 |
|
|---|
| 39 |
PathInfo_append(&path, &filepath, path_buf); |
|---|
| 40 |
fd = open(path.d, O_RDONLY); |
|---|
| 41 |
if (fd==-1) goto err; |
|---|
| 42 |
|
|---|
| 43 |
sz = lseek(fd, 0, SEEK_END); |
|---|
| 44 |
if (sz==-1 || |
|---|
| 45 |
lseek(fd, 0, SEEK_SET)==-1) goto err; |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
if (sz>0 && sz<FILECFG_MAX_FILESIZE) { |
|---|
| 49 |
char buf[sz+1]; |
|---|
| 50 |
|
|---|
| 51 |
if (read(fd, buf, sz+1)!=sz) goto err; |
|---|
| 52 |
|
|---|
| 53 |
if (!allow_multiline) { |
|---|
| 54 |
char * pos; |
|---|
| 55 |
|
|---|
| 56 |
buf[sz] = '\0'; |
|---|
| 57 |
pos = strchr(buf, '\n'); |
|---|
| 58 |
if (pos) *pos = '\0'; |
|---|
| 59 |
} |
|---|
| 60 |
else { |
|---|
| 61 |
while (sz>0 && buf[sz-1]=='\n') --sz; |
|---|
| 62 |
buf[sz] = '\0'; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
res = strdup(buf); |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
err: |
|---|
| 69 |
if (res==0 && dflt) |
|---|
| 70 |
res = strdup(dflt); |
|---|
| 71 |
|
|---|
| 72 |
if (fd!=-1) close(fd); |
|---|
| 73 |
return res; |
|---|
| 74 |
} |
|---|