*
* (c) Lawrence Manning, 2001
* Contains functions for manipulation files full of VAR=VAL pairs.
- *
+ *
* 2003-07-27 Robert Kerr - Added cooperative file locking to prevent any
* clashes between setuid programs reading configuration and cgi scripts
* trying to write it
- *
+ *
*/
-
+
#include "libsmooth.h"
/* Sets up the list. First entry is a dummy one to avoid having to special
struct keyvalue *initkeyvalues(void)
{
struct keyvalue *head = malloc(sizeof(struct keyvalue));
-
+
strcpy(head->key, "KEY");
strcpy(head->value, "VALUE");
head->next = NULL;
-
+
return head;
}
{
struct keyvalue *cur = head->next;
struct keyvalue *next;
-
+
while (cur)
{
next = cur->next;
char buffer[STRING_SIZE];
char *temp;
char *key, *value;
-
+
if (!(file = fopen(filename, "r")))
return 0;
-
+
if (flock(fileno(file), LOCK_SH))
{
fclose(file);
return 0;
}
-
+
while (fgets(buffer, STRING_SIZE, file))
{
temp = buffer;
if (strlen(key))
appendkeyvalue(head, key, value);
}
-
+
flock(fileno(file), LOCK_UN);
fclose(file);
{
FILE *file;
struct keyvalue *cur = head->next;
-
+
if (!(file = fopen(filename, "w")))
return 0;
-
+
if (flock(fileno(file), LOCK_EX))
{
fclose(file);
return 0;
}
-
-
+
+
while (cur)
{
/* No space in value? If there is, we need to quote the value
}
flock(fileno(file), LOCK_UN);
fclose(file);
-
+
return 1;
}