From 71dc4067220c5b994885f63127b4b7eb947002fd Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 7 Feb 2006 09:10:07 +0000 Subject: Add support for deleting keys --- common/textfile.c | 48 +++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) (limited to 'common/textfile.c') 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; -- cgit