/***************************************************************************1* _ _ ____ _2* Project ___| | | | _ \| |3* / __| | | | |_) | |4* | (__| |_| | _ <| |___5* \___|\___/|_| \_\_____|6*7* Copyright (C) Daniel Stenberg, <[email protected]>, et al.8*9* This software is licensed as described in the file COPYING, which10* you should have received as part of this distribution. The terms11* are also available at https://curl.se/docs/copyright.html.12*13* You may opt to use, copy, modify, merge, publish, distribute and/or sell14* copies of the Software, and permit persons to whom the Software is15* furnished to do so, under the terms of the COPYING file.16*17* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY18* KIND, either express or implied.19*20* SPDX-License-Identifier: curl21*22***************************************************************************/23#include "tool_setup.h"2425#ifdef HAVE_PWD_H26#ifdef __AMIGA__27#undef __NO_NET_API /* required for AmigaOS to declare getpwuid() */28#endif29#include <pwd.h>30#ifdef __AMIGA__31#define __NO_NET_API32#endif33#endif3435#include "tool_findfile.h"36#include "tool_cfgable.h"3738#include "memdebug.h" /* keep this as LAST include */3940struct finder {41const char *env;42const char *append;43bool withoutdot;44};4546/* The order of the variables below is important, as the index number is used47in the findfile() function */48static const struct finder conf_list[] = {49{ "CURL_HOME", NULL, FALSE },50{ "XDG_CONFIG_HOME", NULL, TRUE },51{ "HOME", NULL, FALSE },52#ifdef _WIN3253{ "USERPROFILE", NULL, FALSE },54{ "APPDATA", NULL, FALSE },55{ "USERPROFILE", "\\Application Data", FALSE},56#endif57/* these are for .curlrc if XDG_CONFIG_HOME is not defined */58{ "CURL_HOME", "/.config", TRUE },59{ "HOME", "/.config", TRUE },6061{ NULL, NULL, FALSE }62};6364static char *checkhome(const char *home, const char *fname, bool dotscore)65{66const char pref[2] = { '.', '_' };67int i;68for(i = 0; i < (dotscore ? 2 : 1); i++) {69char *c;70if(dotscore)71c = curl_maprintf("%s" DIR_CHAR "%c%s", home, pref[i], &fname[1]);72else73c = curl_maprintf("%s" DIR_CHAR "%s", home, fname);74if(c) {75int fd = curlx_open(c, O_RDONLY);76if(fd >= 0) {77char *path = strdup(c);78close(fd);79curl_free(c);80return path;81}82curl_free(c);83}84}85return NULL;86}8788/*89* findfile() - return the full path name of the file.90*91* If 'dotscore' is TRUE, then check for the file first with a leading dot92* and then with a leading underscore.93*94* 1. Iterate over the environment variables in order, and if set, check for95* the given file to be accessed there, then it is a match.96* 2. Non-Windows: try getpwuid97*/98char *findfile(const char *fname, int dotscore)99{100int i;101DEBUGASSERT(fname && fname[0]);102DEBUGASSERT((dotscore != 1) || (fname[0] == '.'));103104if(!fname[0])105return NULL;106107for(i = 0; conf_list[i].env; i++) {108char *home = curl_getenv(conf_list[i].env);109if(home) {110char *path;111const char *filename = fname;112if(!home[0]) {113curl_free(home);114continue;115}116if(conf_list[i].append) {117char *c = curl_maprintf("%s%s", home, conf_list[i].append);118curl_free(home);119if(!c)120return NULL;121home = c;122}123if(conf_list[i].withoutdot) {124if(!dotscore) {125/* this is not looking for .curlrc, or the XDG_CONFIG_HOME was126defined so we skip the extended check */127curl_free(home);128continue;129}130filename++; /* move past the leading dot */131dotscore = 0; /* disable it for this check */132}133path = checkhome(home, filename, dotscore ? dotscore - 1 : 0);134curl_free(home);135if(path)136return path;137}138}139#if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)140{141struct passwd *pw = getpwuid(geteuid());142if(pw) {143char *home = pw->pw_dir;144if(home && home[0])145return checkhome(home, fname, FALSE);146}147}148#endif /* PWD-stuff */149return NULL;150}151152153