]>
git.ipfire.org Git - people/teissler/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
15 #include "libsmooth.h"
17 /* Sets up the list. First entry is a dummy one to avoid having to special
18 * case empty lists. */
19 struct keyvalue
*initkeyvalues(void)
21 struct keyvalue
*head
= malloc(sizeof(struct keyvalue
));
23 strcpy(head
->key
, "KEY");
24 strcpy(head
->value
, "VALUE");
30 /* Splats all the entries in a list. */
31 void freekeyvalues(struct keyvalue
*head
)
33 struct keyvalue
*cur
= head
->next
;
34 struct keyvalue
*next
;
44 /* Reads from a file into a new list. Uses appendkeyvalue to add entries.
45 * Will bomb out on a error (eg bad format line). */
46 int readkeyvalues(struct keyvalue
*head
, char *filename
)
49 char buffer
[STRING_SIZE
];
53 if (!(file
= fopen(filename
, "r")))
56 if (flock(fileno(file
), LOCK_SH
))
62 while (fgets(buffer
, STRING_SIZE
, file
))
67 if (*temp
=='\n') *temp
= '\0';
72 if (!(temp
= strchr(buffer
, '=')))
74 flock(fileno(file
), LOCK_UN
);
79 key
= buffer
; value
= temp
+ 1;
80 /* See if string is quoted. If so, skip first quote, and
81 * nuke the one at the end. */
85 if ((temp
= strrchr(value
, '\'')))
89 flock(fileno(file
), LOCK_UN
);
95 appendkeyvalue(head
, key
, value
);
98 flock(fileno(file
), LOCK_UN
);
104 /* Writes out a list to a file. Easy. */
105 int writekeyvalues(struct keyvalue
*head
, char *filename
)
108 struct keyvalue
*cur
= head
->next
;
110 if (!(file
= fopen(filename
, "w")))
113 if (flock(fileno(file
), LOCK_EX
))
122 /* No space in value? If there is, we need to quote the value
123 * so the shell can read it. */
124 if (!strchr(cur
->value
, ' '))
125 fprintf(file
, "%s=%s\n", cur
->key
, cur
->value
);
127 fprintf(file
, "%s=\'%s\'\n", cur
->key
, cur
->value
);
130 flock(fileno(file
), LOCK_UN
);
136 /* Finds a key and copies the value back. value must be at least STRING_SIZE
137 * long. Would be nice to have a func that just returns a pointer to the value?
139 int findkey(struct keyvalue
*head
, char *key
, char *value
)
141 struct keyvalue
*cur
= head
->next
;
145 if (strcmp(key
, cur
->key
) == 0)
147 strncpy(value
, cur
->value
, STRING_SIZE
);
148 value
[STRING_SIZE
-1] = '\0';
157 /* Appends a entry. Not very efficent because it rescans the list looking
158 * for the end. Maybe fix this later. */
159 void appendkeyvalue(struct keyvalue
*head
, char *key
, char *value
)
161 struct keyvalue
*new = malloc(sizeof(struct keyvalue
));
162 struct keyvalue
*cur
= head
->next
;
163 struct keyvalue
*tail
= head
;
165 strncpy(new->key
, key
, STRING_SIZE
);
166 strncpy(new->value
, value
, STRING_SIZE
);
167 new->key
[STRING_SIZE
-1] = '\0';
168 new->value
[STRING_SIZE
-1] = '\0';
179 /* Otherwrites a key with a new value, or if it dosn't exist, appends it
181 void replacekeyvalue(struct keyvalue
*head
, char *key
, char *value
)
183 struct keyvalue
*cur
= head
->next
;
187 if (strcmp(cur
->key
, key
) == 0)
189 strncpy(cur
->value
, value
, STRING_SIZE
);
190 cur
->value
[STRING_SIZE
-1] = '\0';
196 appendkeyvalue(head
, key
, value
);