]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/logindefs.c
2 * Copyright (C) 2003, 2004, 2005 Thorsten Kukuk
3 * Author: Thorsten Kukuk <kukuk@suse.de>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain any existing copyright
10 * notice, and this entire permission notice in its entirety,
11 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce all prior and current
14 * copyright notices, this list of conditions, and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * 3. The name of any author may not be used to endorse or promote
19 * products derived from this software without their specific prior
29 #include <sys/syslog.h>
32 #include "logindefs.h"
34 #include "pathnames.h"
38 char *name
; /* name of the option. */
39 char *value
; /* value of the option. */
40 char *path
; /* name of config file for this option. */
42 struct item
*next
; /* pointer to next option. */
45 static struct item
*list
= NULL
;
47 void free_getlogindefs_data(void)
53 struct item
*tmp
= ptr
->next
;
65 static void store(const char *name
, const char *value
, const char *path
)
67 struct item
*new = xmalloc(sizeof(struct item
));
72 new->name
= xstrdup(name
);
73 new->value
= value
&& *value
? xstrdup(value
) : NULL
;
74 new->path
= xstrdup(path
);
79 static void load_defaults(const char *filename
)
84 f
= fopen(filename
, "r");
88 while (fgets(buf
, sizeof(buf
), f
)) {
90 char *p
, *name
, *data
= NULL
;
92 if (*buf
== '#' || *buf
== '\n')
93 continue; /* only comment or empty line */
99 size_t n
= strlen(buf
);
100 if (n
&& *(buf
+ n
- 1) == '\n')
101 *(buf
+ n
- 1) = '\0';
105 continue; /* empty line */
107 /* ignore space at begin of the line */
109 while (*name
&& isspace((unsigned)*name
))
112 /* go to the end of the name */
114 while (*data
&& !(isspace((unsigned)*data
) || *data
== '='))
116 if (data
> name
&& *data
)
119 if (!*name
|| data
== name
)
122 /* go to the begin of the value */
124 && (isspace((unsigned)*data
) || *data
== '='
128 /* remove space at the end of the value */
129 p
= data
+ strlen(data
);
132 while (p
> data
&& (isspace((unsigned)*p
) || *p
== '"'))
135 store(name
, data
, filename
);
141 static struct item
*search(const char *name
)
146 load_defaults(_PATH_LOGINDEFS
);
149 while (ptr
!= NULL
) {
150 if (strcasecmp(name
, ptr
->name
) == 0)
158 static const char *search_config(const char *name
)
163 while (ptr
!= NULL
) {
164 if (strcasecmp(name
, ptr
->name
) == 0)
172 int getlogindefs_bool(const char *name
, int dflt
)
174 struct item
*ptr
= search(name
);
175 return ptr
&& ptr
->value
? (strcasecmp(ptr
->value
, "yes") == 0) : dflt
;
178 long getlogindefs_num(const char *name
, long dflt
)
180 struct item
*ptr
= search(name
);
184 if (!ptr
|| !ptr
->value
)
188 retval
= strtol(ptr
->value
, &end
, 0);
189 if (end
&& *end
== '\0' && !errno
)
192 syslog(LOG_NOTICE
, _("%s: %s contains invalid numerical value: %s"),
193 search_config(name
), name
, ptr
->value
);
199 * @dflt if @name not found
200 * "" (empty string) if found, but value not defined
203 const char *getlogindefs_str(const char *name
, const char *dflt
)
205 struct item
*ptr
= search(name
);
215 int main(int argc
, char *argv
[])
220 errx(EXIT_FAILURE
, "usage: %s <filename> "
221 "[<str|num|bool> <valname>]", argv
[0]);
223 load_defaults(argv
[1]);
225 if (argc
!= 4) { /* list all */
228 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
229 printf("%s: $%s: '%s'\n", ptr
->path
, ptr
->name
,
238 if (strcmp(type
, "str") == 0)
239 printf("$%s: '%s'\n", name
, getlogindefs_str(name
, "DEFAULT"));
240 else if (strcmp(type
, "num") == 0)
241 printf("$%s: '%ld'\n", name
, getlogindefs_num(name
, 0));
242 else if (strcmp(type
, "bool") == 0)
243 printf("$%s: '%s'\n", name
,
244 getlogindefs_bool(name
, 0) ? "Y" : "N");