diff options
Diffstat (limited to 'Source/CTest/Curl/netrc.c')
-rw-r--r-- | Source/CTest/Curl/netrc.c | 133 |
1 files changed, 64 insertions, 69 deletions
diff --git a/Source/CTest/Curl/netrc.c b/Source/CTest/Curl/netrc.c index da10a33..950beb0 100644 --- a/Source/CTest/Curl/netrc.c +++ b/Source/CTest/Curl/netrc.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2002, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -41,14 +41,17 @@ #endif #include <curl/curl.h> +#include "netrc.h" #include "strequal.h" #include "strtok.h" +#include "memory.h" + +#define _MPRINTF_REPLACE /* use our functions only */ +#include <curl/mprintf.h> /* The last #include file should be: */ -#ifdef MALLOCDEBUG #include "memdebug.h" -#endif /* Debug this single source file with: 'make netrc' then run './netrc'! @@ -71,56 +74,27 @@ enum { #define LOGINSIZE 64 #define PASSWORDSIZE 64 +/* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */ int Curl_parsenetrc(char *host, char *login, - char *password) + char *password, + char *netrcfile) { FILE *file; - char netrcbuffer[256]; int retcode=1; - int specific_login = (login[0] != 0); - char *home = NULL; + bool home_alloc = FALSE; + bool netrc_alloc = FALSE; int state=NOTHING; char state_login=0; /* Found a login keyword */ char state_password=0; /* Found a password keyword */ - char state_our_login=0; /* With specific_login, found *our* login name */ + int state_our_login=FALSE; /* With specific_login, found *our* login name */ #define NETRC DOT_CHAR "netrc" -#if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID) - struct passwd *pw; - pw= getpwuid(geteuid()); - if (pw) { -#ifdef VMS - /* VMS does not work because of warnings on icc */ - /* home = decc$translate_vms(pw->pw_dir); */ -#else - home = pw->pw_dir; -#endif - } -#else - void *pw=NULL; -#endif - - if(NULL == pw) { - home = curl_getenv("HOME"); /* portable environment reader */ - if(!home) { - return -1; - } - } - - if(strlen(home)>(sizeof(netrcbuffer)-strlen(NETRC))) { - if(NULL==pw) - free(home); - return -1; - } - - sprintf(netrcbuffer, "%s%s%s", home, DIR_CHAR, NETRC); - -#ifdef MALLOCDEBUG +#ifdef CURLDEBUG { /* This is a hack to allow testing. * If compiled with --enable-debug and CURL_DEBUG_NETRC is defined, @@ -128,32 +102,59 @@ int Curl_parsenetrc(char *host, char *override = curl_getenv("CURL_DEBUG_NETRC"); - if (override != NULL) { - printf("NETRC: overridden .netrc file: %s\n", home); + if (override) { + printf("NETRC: overridden " NETRC " file: %s\n", home); + netrcfile = override; + netrc_alloc = TRUE; + } + } +#endif /* CURLDEBUG */ + if(!netrcfile) { + home = curl_getenv("HOME"); /* portable environment reader */ + if(home) { + home_alloc = TRUE; +#if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID) + } + else { + struct passwd *pw; + pw= getpwuid(geteuid()); + if (pw) { +#ifdef VMS + home = decc$translate_vms(pw->pw_dir); +#else + home = pw->pw_dir; +#endif + } +#endif + } - if (strlen(override)+1 > sizeof(netrcbuffer)) { - free(override); - if(NULL==pw) - free(home); + if(!home) + return -1; - return -1; - } - strcpy(netrcbuffer, override); - free(override); + netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC); + if(!netrcfile) { + if(home_alloc) + free(home); + return -1; } + netrc_alloc = TRUE; } -#endif /* MALLOCDEBUG */ - file = fopen(netrcbuffer, "r"); + file = fopen(netrcfile, "r"); if(file) { char *tok; - char *tok_buf; - while(fgets(netrcbuffer, sizeof(netrcbuffer), file)) { + char *tok_buf; + bool done=FALSE; + char netrcbuffer[256]; + + while(!done && fgets(netrcbuffer, sizeof(netrcbuffer), file)) { tok=strtok_r(netrcbuffer, " \t\n", &tok_buf); - while(tok) { + while(!done && tok) { - if (login[0] && password[0]) - goto done; + if (login[0] && password[0]) { + done=TRUE; + break; + } switch(state) { case NOTHING: @@ -182,8 +183,9 @@ int Curl_parsenetrc(char *host, /* we are now parsing sub-keywords concerning "our" host */ if(state_login) { if (specific_login) { - state_our_login = (char)strequal(login, tok); - }else{ + state_our_login = strequal(login, tok); + } + else { strncpy(login, tok, LOGINSIZE-1); #ifdef _NETRC_DEBUG printf("LOGIN: %s\n", login); @@ -207,7 +209,7 @@ int Curl_parsenetrc(char *host, else if(strequal("machine", tok)) { /* ok, there's machine here go => */ state = HOSTFOUND; - state_our_login = 0; + state_our_login = FALSE; } break; } /* switch (state) */ @@ -216,12 +218,13 @@ int Curl_parsenetrc(char *host, } /* while (tok) */ } /* while fgets() */ -done: fclose(file); } - if(NULL==pw) + if(home_alloc) free(home); + if(netrc_alloc) + free(netrcfile); return retcode; } @@ -242,11 +245,3 @@ int main(int argc, char **argv) } #endif - -/* - * local variables: - * eval: (load-file "../curl-mode.el") - * end: - * vim600: fdm=marker - * vim: et sw=2 ts=2 sts=2 tw=78 - */ |