89
|
1 /* Copyright (C) 2004 Mads Martin Joergensen <mmj at mmj.dk>
|
|
2 *
|
|
3 * $Id$
|
|
4 *
|
225
|
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6 * of this software and associated documentation files (the "Software"), to
|
|
7 * deal in the Software without restriction, including without limitation the
|
|
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
9 * sell copies of the Software, and to permit persons to whom the Software is
|
|
10 * furnished to do so, subject to the following conditions:
|
|
11 *
|
|
12 * The above copyright notice and this permission notice shall be included in
|
|
13 * all copies or substantial portions of the Software.
|
|
14 *
|
|
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
21 * IN THE SOFTWARE.
|
89
|
22 */
|
|
23
|
|
24 #include <sys/types.h>
|
|
25 #include <sys/stat.h>
|
|
26 #include <stdlib.h>
|
164
|
27 #include <fcntl.h>
|
|
28 #include <unistd.h>
|
89
|
29
|
|
30 #include "strgen.h"
|
|
31 #include "ctrlvalue.h"
|
|
32 #include "mygetline.h"
|
|
33 #include "chomp.h"
|
245
|
34 #include "memory.h"
|
89
|
35
|
754
|
36 static char *ctrlval(const char *listdir, const char *ctrlstr, int oneline)
|
89
|
37 {
|
291
|
38 char *filename, *value = NULL;
|
754
|
39 int ctrlfd, i;
|
89
|
40
|
291
|
41 if(listdir == NULL)
|
|
42 return NULL;
|
|
43
|
|
44 filename = concatstr(3, listdir, "/control/", ctrlstr);
|
164
|
45 ctrlfd = open(filename, O_RDONLY);
|
245
|
46 myfree(filename);
|
89
|
47
|
164
|
48 if(ctrlfd < 0)
|
|
49 return NULL;
|
754
|
50
|
|
51 if (oneline) {
|
|
52 value = mygetline(ctrlfd);
|
|
53 chomp(value);
|
|
54 } else {
|
|
55 value = mygetcontent(ctrlfd);
|
|
56 i = strlen(value) - 1;
|
|
57 if (i >= 0 && value[i] == '\n') {
|
|
58 value[i] = '\0';
|
|
59 i--;
|
|
60 }
|
|
61 if (i >= 0 && value[i] == '\r') {
|
|
62 value[i] = '\0';
|
|
63 i--;
|
|
64 }
|
|
65 }
|
164
|
66 close(ctrlfd);
|
89
|
67
|
|
68 return value;
|
|
69 }
|
|
70
|
754
|
71 char *ctrlvalue(const char *listdir, const char *ctrlstr)
|
|
72 {
|
|
73 return ctrlval(listdir, ctrlstr, 1);
|
|
74 }
|
|
75
|
|
76 char *ctrlcontent(const char *listdir, const char *ctrlstr)
|
|
77 {
|
|
78 return ctrlval(listdir, ctrlstr, 0);
|
|
79 }
|