Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/src/tool_findfile.c
2065 views
1
/***************************************************************************
2
* _ _ ____ _
3
* Project ___| | | | _ \| |
4
* / __| | | | |_) | |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
7
*
8
* Copyright (C) Daniel Stenberg, <[email protected]>, et al.
9
*
10
* This software is licensed as described in the file COPYING, which
11
* you should have received as part of this distribution. The terms
12
* are also available at https://curl.se/docs/copyright.html.
13
*
14
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
* copies of the Software, and permit persons to whom the Software is
16
* furnished to do so, under the terms of the COPYING file.
17
*
18
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
* KIND, either express or implied.
20
*
21
* SPDX-License-Identifier: curl
22
*
23
***************************************************************************/
24
#include "tool_setup.h"
25
26
#ifdef HAVE_PWD_H
27
#undef __NO_NET_API /* required for AmigaOS to declare getpwuid() */
28
#include <pwd.h>
29
#define __NO_NET_API
30
#endif
31
32
#ifdef HAVE_SYS_STAT_H
33
#include <sys/stat.h>
34
#endif
35
#ifdef HAVE_FCNTL_H
36
#include <fcntl.h>
37
#endif
38
39
#include <curlx.h>
40
41
#include "tool_findfile.h"
42
#include "tool_cfgable.h"
43
44
#include <memdebug.h> /* keep this as LAST include */
45
46
struct finder {
47
const char *env;
48
const char *append;
49
bool withoutdot;
50
};
51
52
/* The order of the variables below is important, as the index number is used
53
in the findfile() function */
54
static const struct finder conf_list[] = {
55
{ "CURL_HOME", NULL, FALSE },
56
{ "XDG_CONFIG_HOME", NULL, TRUE },
57
{ "HOME", NULL, FALSE },
58
#ifdef _WIN32
59
{ "USERPROFILE", NULL, FALSE },
60
{ "APPDATA", NULL, FALSE },
61
{ "USERPROFILE", "\\Application Data", FALSE},
62
#endif
63
/* these are for .curlrc if XDG_CONFIG_HOME is not defined */
64
{ "CURL_HOME", "/.config", TRUE },
65
{ "HOME", "/.config", TRUE },
66
67
{ NULL, NULL, FALSE }
68
};
69
70
static char *checkhome(const char *home, const char *fname, bool dotscore)
71
{
72
const char pref[2] = { '.', '_' };
73
int i;
74
for(i = 0; i < (dotscore ? 2 : 1); i++) {
75
char *c;
76
if(dotscore)
77
c = aprintf("%s" DIR_CHAR "%c%s", home, pref[i], &fname[1]);
78
else
79
c = aprintf("%s" DIR_CHAR "%s", home, fname);
80
if(c) {
81
int fd = open(c, O_RDONLY);
82
if(fd >= 0) {
83
char *path = strdup(c);
84
close(fd);
85
curl_free(c);
86
return path;
87
}
88
curl_free(c);
89
}
90
}
91
return NULL;
92
}
93
94
/*
95
* findfile() - return the full path name of the file.
96
*
97
* If 'dotscore' is TRUE, then check for the file first with a leading dot
98
* and then with a leading underscore.
99
*
100
* 1. Iterate over the environment variables in order, and if set, check for
101
* the given file to be accessed there, then it is a match.
102
* 2. Non-Windows: try getpwuid
103
*/
104
char *findfile(const char *fname, int dotscore)
105
{
106
int i;
107
DEBUGASSERT(fname && fname[0]);
108
DEBUGASSERT((dotscore != 1) || (fname[0] == '.'));
109
110
if(!fname[0])
111
return NULL;
112
113
for(i = 0; conf_list[i].env; i++) {
114
char *home = curl_getenv(conf_list[i].env);
115
if(home) {
116
char *path;
117
const char *filename = fname;
118
if(!home[0]) {
119
curl_free(home);
120
continue;
121
}
122
if(conf_list[i].append) {
123
char *c = aprintf("%s%s", home, conf_list[i].append);
124
curl_free(home);
125
if(!c)
126
return NULL;
127
home = c;
128
}
129
if(conf_list[i].withoutdot) {
130
if(!dotscore) {
131
/* this is not looking for .curlrc, or the XDG_CONFIG_HOME was
132
defined so we skip the extended check */
133
curl_free(home);
134
continue;
135
}
136
filename++; /* move past the leading dot */
137
dotscore = 0; /* disable it for this check */
138
}
139
path = checkhome(home, filename, dotscore ? dotscore - 1 : 0);
140
curl_free(home);
141
if(path)
142
return path;
143
}
144
}
145
#if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
146
{
147
struct passwd *pw = getpwuid(geteuid());
148
if(pw) {
149
char *home = pw->pw_dir;
150
if(home && home[0])
151
return checkhome(home, fname, FALSE);
152
}
153
}
154
#endif /* PWD-stuff */
155
return NULL;
156
}
157
158