1 /* (C) 1998 Red Hat Software, Inc. -- Licensing details are in the COPYING
2 file accompanying popt source distributions, available from
3 ftp://ftp.redhat.com/pub/code/popt */
6 * \brief Source: a module for configuring popt
13 #include "poptalloca.h"
24 static void configLine(poptContext con
, char * line
) {
25 int nameLength
= strlen(con
->appName
);
27 struct poptAlias alias
;
29 char * longName
= NULL
;
30 char shortName
= '\0';
32 if (strncmp(line
, con
->appName
, nameLength
)) return;
34 if (!*line
|| !isspace((unsigned char) *line
)) return;
35 while (*line
&& isspace((unsigned char) *line
)) line
++;
38 while (!*line
|| !isspace((unsigned char) *line
)) line
++;
40 while (*line
&& isspace((unsigned char) *line
)) line
++;
44 while (!*line
|| !isspace((unsigned char) *line
)) line
++;
46 while (*line
&& isspace((unsigned char) *line
)) line
++;
49 if (opt
[0] == '-' && opt
[1] == '-')
51 else if (opt
[0] == '-' && !opt
[2])
54 if (!strcmp(entryType
, "alias")) {
55 if (poptParseArgvString(line
, &alias
.argc
, &alias
.argv
)) return;
56 alias
.longName
= longName
, alias
.shortName
= shortName
;
57 poptAddAlias(con
, alias
, 0);
58 } else if (!strcmp(entryType
, "exec")) {
59 con
->execs
= realloc(con
->execs
,
60 sizeof(*con
->execs
) * (con
->numExecs
+ 1));
62 con
->execs
[con
->numExecs
].longName
= strdup(longName
);
64 con
->execs
[con
->numExecs
].longName
= NULL
;
66 con
->execs
[con
->numExecs
].shortName
= shortName
;
67 con
->execs
[con
->numExecs
].script
= strdup(line
);
73 int poptReadConfigFile(poptContext con
, const char * fn
) {
74 char * file
, * chptr
, * end
;
79 fd
= open(fn
, O_RDONLY
);
84 return POPT_ERROR_ERRNO
;
87 fileLength
= lseek(fd
, 0, SEEK_END
);
90 file
= alloca(fileLength
+ 1);
91 if (read(fd
, file
, fileLength
) != fileLength
) {
95 return POPT_ERROR_ERRNO
;
99 dst
= buf
= alloca(fileLength
+ 1);
102 end
= (file
+ fileLength
);
103 while (chptr
< end
) {
108 while (*dst
&& isspace((unsigned char) *dst
)) dst
++;
109 if (*dst
&& *dst
!= '#') {
110 configLine(con
, dst
);
119 /* \ at the end of a line does not insert a \n */
132 int poptReadDefaultConfig(poptContext con
, int useEnv
) {
139 if (!con
->appName
) return 0;
141 rc
= poptReadConfigFile(con
, "/etc/popt");
143 if (getuid() != geteuid()) return 0;
145 if ((home
= getenv("HOME"))) {
146 fn
= alloca(strlen(home
) + 20);
148 strcat(fn
, "/.popt");
149 rc
= poptReadConfigFile(con
, fn
);