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 */
9 #include "poptalloca.h"
20 static void configLine(poptContext con
, unsigned char * line
) {
21 int nameLength
= strlen(con
->appName
);
23 struct poptAlias alias
;
25 char * longName
= NULL
;
26 char shortName
= '\0';
28 if (strncmp(line
, con
->appName
, nameLength
)) return;
30 if (!*line
|| !isspace(*line
)) return;
31 while (*line
&& isspace(*line
)) line
++;
34 while (!*line
|| !isspace(*line
)) line
++;
36 while (*line
&& isspace(*line
)) line
++;
40 while (!*line
|| !isspace(*line
)) line
++;
42 while (*line
&& isspace(*line
)) line
++;
45 if (opt
[0] == '-' && opt
[1] == '-')
47 else if (opt
[0] == '-' && !opt
[2])
50 if (!strcmp(entryType
, "alias")) {
51 if (poptParseArgvString(line
, &alias
.argc
, &alias
.argv
)) return;
52 alias
.longName
= longName
, alias
.shortName
= shortName
;
53 poptAddAlias(con
, alias
, 0);
54 } else if (!strcmp(entryType
, "exec")) {
55 con
->execs
= realloc(con
->execs
,
56 sizeof(*con
->execs
) * (con
->numExecs
+ 1));
58 con
->execs
[con
->numExecs
].longName
= strdup(longName
);
60 con
->execs
[con
->numExecs
].longName
= NULL
;
62 con
->execs
[con
->numExecs
].shortName
= shortName
;
63 con
->execs
[con
->numExecs
].script
= strdup(line
);
69 int poptReadConfigFile(poptContext con
, const char * fn
) {
70 char * file
, * chptr
, * end
;
75 fd
= open(fn
, O_RDONLY
);
80 return POPT_ERROR_ERRNO
;
83 fileLength
= lseek(fd
, 0, SEEK_END
);
86 file
= alloca(fileLength
+ 1);
87 if (read(fd
, file
, fileLength
) != fileLength
) {
91 return POPT_ERROR_ERRNO
;
95 dst
= buf
= alloca(fileLength
+ 1);
98 end
= (file
+ fileLength
);
104 while (*dst
&& isspace((unsigned)*dst
)) dst
++;
105 if (*dst
&& *dst
!= '#') {
106 configLine(con
, dst
);
115 /* \ at the end of a line does not insert a \n */
128 int poptReadDefaultConfig(poptContext con
, int useEnv
) {
133 if (!con
->appName
) return 0;
135 rc
= poptReadConfigFile(con
, "/etc/popt");
137 if (getuid() != geteuid()) return 0;
139 if ((home
= getenv("HOME"))) {
140 fn
= alloca(strlen(home
) + 20);
142 strcat(fn
, "/.popt");
143 rc
= poptReadConfigFile(con
, fn
);