]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/install+setup/libsmooth/varval.c
1 /* SmoothWall libsmooth.
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
6 * (c) Lawrence Manning, 2001
7 * Contains functions for manipulation files full of VAR=VAL pairs.
9 * 2003-07-27 Robert Kerr - Added cooperative file locking to prevent any
10 * clashes between setuid programs reading configuration and cgi scripts
13 * $Id: varval.c,v 1.4 2003/12/11 11:25:53 riddles Exp $
17 #include "libsmooth.h"
19 /* Sets up the list. First entry is a dummy one to avoid having to special
20 * case empty lists. */
21 struct keyvalue
*initkeyvalues(void)
23 struct keyvalue
*head
= malloc(sizeof(struct keyvalue
));
25 strcpy(head
->key
, "KEY");
26 strcpy(head
->value
, "VALUE");
32 /* Splats all the entries in a list. */
33 void freekeyvalues(struct keyvalue
*head
)
35 struct keyvalue
*cur
= head
->next
;
36 struct keyvalue
*next
;
46 /* Reads from a file into a new list. Uses appendkeyvalue to add entries.
47 * Will bomb out on a error (eg bad format line). */
48 int readkeyvalues(struct keyvalue
*head
, char *filename
)
51 char buffer
[STRING_SIZE
];
55 if (!(file
= fopen(filename
, "r")))
58 if (flock(fileno(file
), LOCK_SH
))
64 while (fgets(buffer
, STRING_SIZE
, file
))
69 if (*temp
=='\n') *temp
= '\0';
74 if (!(temp
= strchr(buffer
, '=')))
76 flock(fileno(file
), LOCK_UN
);
81 key
= buffer
; value
= temp
+ 1;
82 /* See if string is quoted. If so, skip first quote, and
83 * nuke the one at the end. */
87 if ((temp
= strrchr(value
, '\'')))
91 flock(fileno(file
), LOCK_UN
);
97 appendkeyvalue(head
, key
, value
);
100 flock(fileno(file
), LOCK_UN
);
106 /* Writes out a list to a file. Easy. */
107 int writekeyvalues(struct keyvalue
*head
, char *filename
)
110 struct keyvalue
*cur
= head
->next
;
112 if (!(file
= fopen(filename
, "w")))
115 if (flock(fileno(file
), LOCK_EX
))
124 /* No space in value? If there is, we need to quote the value
125 * so the shell can read it. */
126 if (!strchr(cur
->value
, ' '))
127 fprintf(file
, "%s=%s\n", cur
->key
, cur
->value
);
129 fprintf(file
, "%s=\'%s\'\n", cur
->key
, cur
->value
);
132 flock(fileno(file
), LOCK_UN
);
138 /* Finds a key and copies the value back. value must be at least STRING_SIZE
139 * long. Would be nice to have a func that just returns a pointer to the value?
141 int findkey(struct keyvalue
*head
, char *key
, char *value
)
143 struct keyvalue
*cur
= head
->next
;
147 if (strcmp(key
, cur
->key
) == 0)
149 strncpy(value
, cur
->value
, STRING_SIZE
);
150 value
[STRING_SIZE
-1] = '\0';
159 /* Appends a entry. Not very efficent because it rescans the list looking
160 * for the end. Maybe fix this later. */
161 void appendkeyvalue(struct keyvalue
*head
, char *key
, char *value
)
163 struct keyvalue
*new = malloc(sizeof(struct keyvalue
));
164 struct keyvalue
*cur
= head
->next
;
165 struct keyvalue
*tail
= head
;
167 strncpy(new->key
, key
, STRING_SIZE
);
168 strncpy(new->value
, value
, STRING_SIZE
);
169 new->key
[STRING_SIZE
-1] = '\0';
170 new->value
[STRING_SIZE
-1] = '\0';
181 /* Otherwrites a key with a new value, or if it dosn't exist, appends it
183 void replacekeyvalue(struct keyvalue
*head
, char *key
, char *value
)
185 struct keyvalue
*cur
= head
->next
;
189 if (strcmp(cur
->key
, key
) == 0)
191 strncpy(cur
->value
, value
, STRING_SIZE
);
192 cur
->value
[STRING_SIZE
-1] = '\0';
198 appendkeyvalue(head
, key
, value
);