diff options
author | Marcel Holtmann <marcel@holtmann.org> | 2006-02-07 09:10:07 +0000 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2006-02-07 09:10:07 +0000 |
commit | 71dc4067220c5b994885f63127b4b7eb947002fd (patch) | |
tree | 993e1bcb31145dd26f525be0028ce142ce29f72a /common/textfile.c | |
parent | b7d130c68efdeea62d459e5c6c968c89412d9f46 (diff) |
Add support for deleting keys
Diffstat (limited to 'common/textfile.c')
-rw-r--r-- | common/textfile.c | 48 |
1 files changed, 31 insertions, 17 deletions
diff --git a/common/textfile.c b/common/textfile.c index ec445b1a..3aa74d9a 100644 --- a/common/textfile.c +++ b/common/textfile.c @@ -86,6 +86,17 @@ int create_file(char *filename, mode_t mode) return 0; } +static inline char *find_key(char *map, char *key, size_t len) +{ + char *off = strstr(map, key); + + while (off && ((off > map && *(off - 1) != '\r' && + *(off - 1) != '\n') || *(off + len) != ' ')) + off = strstr(off + len, key); + + return off; +} + static inline int write_key_value(int fd, char *key, char *value) { char *str; @@ -108,18 +119,7 @@ static inline int write_key_value(int fd, char *key, char *value) return err; } -static inline char *find_key(char *map, char *key, size_t len) -{ - char *off = strstr(map, key); - - while (off && ((off > map && *(off - 1) != '\r' && - *(off - 1) != '\n') || *(off + len) != ' ')) - off = strstr(off + len, key); - - return off; -} - -int textfile_put(char *pathname, char *key, char *value) +static int write_key(char *pathname, char *key, char *value) { struct stat st; char *map, *off, *end, *str; @@ -156,9 +156,11 @@ int textfile_put(char *pathname, char *key, char *value) off = find_key(map, key, strlen(key)); if (!off) { - munmap(map, size); - pos = lseek(fd, size, SEEK_SET); - err = write_key_value(fd, key, value); + if (value) { + munmap(map, size); + pos = lseek(fd, size, SEEK_SET); + err = write_key_value(fd, key, value); + } goto unlock; } @@ -178,7 +180,8 @@ int textfile_put(char *pathname, char *key, char *value) munmap(map, size); ftruncate(fd, base); pos = lseek(fd, base, SEEK_SET); - err = write_key_value(fd, key, value); + if (value) + err = write_key_value(fd, key, value); goto unlock; } @@ -198,7 +201,8 @@ int textfile_put(char *pathname, char *key, char *value) munmap(map, size); ftruncate(fd, base); pos = lseek(fd, base, SEEK_SET); - err = write_key_value(fd, key, value); + if (value) + err = write_key_value(fd, key, value); write(fd, str, len); @@ -219,6 +223,16 @@ close: return -err; } +int textfile_put(char *pathname, char *key, char *value) +{ + return write_key(pathname, key, value); +} + +int textfile_del(char *pathname, char *key) +{ + return write_key(pathname, key, NULL); +} + char *textfile_get(char *pathname, char *key) { struct stat st; |