/***************************************************************************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#undef __NO_NET_API /* required for AmigaOS to declare getpwuid() */27#include <pwd.h>28#define __NO_NET_API29#endif3031#ifdef HAVE_SYS_STAT_H32#include <sys/stat.h>33#endif34#ifdef HAVE_FCNTL_H35#include <fcntl.h>36#endif3738#include <curlx.h>3940#include "tool_findfile.h"41#include "tool_cfgable.h"4243#include <memdebug.h> /* keep this as LAST include */4445struct finder {46const char *env;47const char *append;48bool withoutdot;49};5051/* The order of the variables below is important, as the index number is used52in the findfile() function */53static const struct finder conf_list[] = {54{ "CURL_HOME", NULL, FALSE },55{ "XDG_CONFIG_HOME", NULL, TRUE },56{ "HOME", NULL, FALSE },57#ifdef _WIN3258{ "USERPROFILE", NULL, FALSE },59{ "APPDATA", NULL, FALSE },60{ "USERPROFILE", "\\Application Data", FALSE},61#endif62/* these are for .curlrc if XDG_CONFIG_HOME is not defined */63{ "CURL_HOME", "/.config", TRUE },64{ "HOME", "/.config", TRUE },6566{ NULL, NULL, FALSE }67};6869static char *checkhome(const char *home, const char *fname, bool dotscore)70{71const char pref[2] = { '.', '_' };72int i;73for(i = 0; i < (dotscore ? 2 : 1); i++) {74char *c;75if(dotscore)76c = aprintf("%s" DIR_CHAR "%c%s", home, pref[i], &fname[1]);77else78c = aprintf("%s" DIR_CHAR "%s", home, fname);79if(c) {80int fd = open(c, O_RDONLY);81if(fd >= 0) {82char *path = strdup(c);83close(fd);84curl_free(c);85return path;86}87curl_free(c);88}89}90return NULL;91}9293/*94* findfile() - return the full path name of the file.95*96* If 'dotscore' is TRUE, then check for the file first with a leading dot97* and then with a leading underscore.98*99* 1. Iterate over the environment variables in order, and if set, check for100* the given file to be accessed there, then it is a match.101* 2. Non-Windows: try getpwuid102*/103char *findfile(const char *fname, int dotscore)104{105int i;106DEBUGASSERT(fname && fname[0]);107DEBUGASSERT((dotscore != 1) || (fname[0] == '.'));108109if(!fname[0])110return NULL;111112for(i = 0; conf_list[i].env; i++) {113char *home = curl_getenv(conf_list[i].env);114if(home) {115char *path;116const char *filename = fname;117if(!home[0]) {118curl_free(home);119continue;120}121if(conf_list[i].append) {122char *c = aprintf("%s%s", home, conf_list[i].append);123curl_free(home);124if(!c)125return NULL;126home = c;127}128if(conf_list[i].withoutdot) {129if(!dotscore) {130/* this is not looking for .curlrc, or the XDG_CONFIG_HOME was131defined so we skip the extended check */132curl_free(home);133continue;134}135filename++; /* move past the leading dot */136dotscore = 0; /* disable it for this check */137}138path = checkhome(home, filename, dotscore ? dotscore - 1 : 0);139curl_free(home);140if(path)141return path;142}143}144#if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)145{146struct passwd *pw = getpwuid(geteuid());147if(pw) {148char *home = pw->pw_dir;149if(home && home[0])150return checkhome(home, fname, FALSE);151}152}153#endif /* PWD-stuff */154return NULL;155}156157158