1
0
mirror of https://github.com/moparisthebest/curl synced 2024-08-13 17:03:50 -04:00

curl: use .curlrc (with a dot) on Windows as well

Fall-back to _curlrc if the dot-version is missing.

Co-Authored-By: Steve Holme

Closes #4230
This commit is contained in:
Daniel Stenberg 2019-08-16 11:31:29 +02:00
parent f9c7ba9096
commit 862393243d
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
2 changed files with 67 additions and 52 deletions

View File

@ -40,7 +40,7 @@ Unix-like systems (which returns the home dir given the current user in your
system). On Windows, it then checks for the APPDATA variable, or as a last system). On Windows, it then checks for the APPDATA variable, or as a last
resort the '%USERPROFILE%\\Application Data'. resort the '%USERPROFILE%\\Application Data'.
2) On windows, if there is no _curlrc file in the home dir, it checks for one 2) On windows, if there is no .curlrc file in the home dir, it checks for one
in the same dir the curl executable is placed. On Unix-like systems, it will in the same dir the curl executable is placed. On Unix-like systems, it will
simply try to load .curlrc from the determined home dir. simply try to load .curlrc from the determined home dir.

View File

@ -34,8 +34,6 @@
#include "memdebug.h" /* keep this as LAST include */ #include "memdebug.h" /* keep this as LAST include */
#define CURLRC DOT_CHAR "curlrc"
/* only acknowledge colon or equals as separators if the option was not /* only acknowledge colon or equals as separators if the option was not
specified with an initial dash! */ specified with an initial dash! */
#define ISSEP(x,dash) (!dash && (((x) == '=') || ((x) == ':'))) #define ISSEP(x,dash) (!dash && (((x) == '=') || ((x) == ':')))
@ -43,41 +41,15 @@
static const char *unslashquote(const char *line, char *param); static const char *unslashquote(const char *line, char *param);
static char *my_get_line(FILE *fp); static char *my_get_line(FILE *fp);
/* return 0 on everything-is-fine, and non-zero otherwise */
int parseconfig(const char *filename, struct GlobalConfig *global)
{
FILE *file = NULL;
char filebuffer[512];
bool usedarg = FALSE;
int rc = 0;
struct OperationConfig *operation = global->first;
if(!filename || !*filename) {
/* NULL or no file name attempts to load .curlrc from the homedir! */
#ifndef __AMIGA__
char *home = homedir(); /* portable homedir finder */
filename = CURLRC; /* sensible default */
if(home) {
if(strlen(home) < (sizeof(filebuffer) - strlen(CURLRC))) {
msnprintf(filebuffer, sizeof(filebuffer),
"%s%s%s", home, DIR_CHAR, CURLRC);
#ifdef WIN32 #ifdef WIN32
/* Check if the file exists - if not, try CURLRC in the same static FILE *execpath(const char *filename)
* directory as our executable {
char filebuffer[512];
/* Get the filename of our executable. GetModuleFileName is already declared
* via inclusions done in setup header file. We assume that we are using
* the ASCII version here.
*/ */
file = fopen(filebuffer, FOPEN_READTEXT); unsigned long len = GetModuleFileNameA(0, filebuffer, sizeof(filebuffer));
if(file != NULL) {
filename = filebuffer;
}
else {
/* Get the filename of our executable. GetModuleFileName is
* already declared via inclusions done in setup header file.
* We assume that we are using the ASCII version here.
*/
unsigned long len = GetModuleFileNameA(0, filebuffer,
sizeof(filebuffer));
if(len > 0 && len < sizeof(filebuffer)) { if(len > 0 && len < sizeof(filebuffer)) {
/* We got a valid filename - get the directory part */ /* We got a valid filename - get the directory part */
char *lastdirchar = strrchr(filebuffer, '\\'); char *lastdirchar = strrchr(filebuffer, '\\');
@ -86,31 +58,73 @@ int parseconfig(const char *filename, struct GlobalConfig *global)
*lastdirchar = 0; *lastdirchar = 0;
/* If we have enough space, build the RC filename */ /* If we have enough space, build the RC filename */
remaining = sizeof(filebuffer) - strlen(filebuffer); remaining = sizeof(filebuffer) - strlen(filebuffer);
if(strlen(CURLRC) < remaining - 1) { if(strlen(filename) < remaining - 1) {
msnprintf(lastdirchar, remaining, msnprintf(lastdirchar, remaining, "%s%s", DIR_CHAR, filename);
"%s%s", DIR_CHAR, CURLRC); return fopen(filebuffer, FOPEN_READTEXT);
/* Don't bother checking if it exists - we do that later */
filename = filebuffer;
} }
} }
} }
return NULL;
}
#endif
/* return 0 on everything-is-fine, and non-zero otherwise */
int parseconfig(const char *filename, struct GlobalConfig *global)
{
FILE *file = NULL;
bool usedarg = FALSE;
int rc = 0;
struct OperationConfig *operation = global->first;
char *pathalloc = NULL;
if(!filename || !*filename) {
/* NULL or no file name attempts to load .curlrc from the homedir! */
char *home = homedir(); /* portable homedir finder */
#ifndef WIN32
if(home) {
pathalloc = curl_maprintf("%s%s.curlrc", home, DIR_CHAR);
if(!pathalloc) {
free(home);
return 1; /* out of memory */
} }
#else /* WIN32 */ filename = pathalloc;
filename = filebuffer;
#endif /* WIN32 */
} }
#else /* Windows */
if(home) {
int i = 0;
char prefix = '.';
do {
/* check for .curlrc then _curlrc in the home dir */
pathalloc = curl_maprintf("%s%s%ccurlrc", home, DIR_CHAR, prefix);
if(!pathalloc) {
free(home);
return 1; /* out of memory */
}
/* Check if the file exists - if not, try _curlrc */
file = fopen(pathalloc, FOPEN_READTEXT);
if(file) {
filename = pathalloc;
break;
}
prefix = '_';
} while(++i < 2);
}
if(!filename) {
/* check for .curlrc then _curlrc in the dir of the executable */
file = execpath(".curlrc");
if(!file)
file = execpath("_curlrc");
}
#endif
Curl_safefree(home); /* we've used it, now free it */ Curl_safefree(home); /* we've used it, now free it */
} }
# else /* __AMIGA__ */ if(!file && filename) { /* no need to fopen() again */
/* On AmigaOS all the config files are into env:
*/
filename = "ENV:" CURLRC;
#endif
}
if(!file) { /* WIN32: no need to fopen() again */
if(strcmp(filename, "-")) if(strcmp(filename, "-"))
file = fopen(filename, FOPEN_READTEXT); file = fopen(filename, FOPEN_READTEXT);
else else
@ -271,6 +285,7 @@ int parseconfig(const char *filename, struct GlobalConfig *global)
else else
rc = 1; /* couldn't open the file */ rc = 1; /* couldn't open the file */
free(pathalloc);
return rc; return rc;
} }