1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * The contents of this file are subject to the Mozilla Public License
9 * Version 1.0 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS IS"
14 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15 * License for the specific language governing rights and limitations
18 * The Original Code is Curl.
20 * The Initial Developer of the Original Code is Daniel Stenberg.
22 * Portions created by the Initial Developer are Copyright (C) 1998.
23 * All Rights Reserved.
26 * Rafael Sagula <sagula@inf.ufrgs.br>
27 * Sampo Kellomaki <sampo@iki.fi>
28 * Linas Vepstas <linas@linas.org>
29 * Bjorn Reese <breese@imada.ou.dk>
30 * Johan Anderson <johan@homemail.com>
31 * Kjell Ericson <Kjell.Ericson@haxx.se>
32 * Troy Engel <tengel@palladium.net>
33 * Ryan Nelson <ryan@inch.com>
34 * Bjorn Stenberg <Bjorn.Stenberg@haxx.se>
35 * Angus Mackay <amackay@gus.ml.org>
37 * ------------------------------------------------------------
39 * - Daniel Stenberg <daniel@haxx.se>
50 * ------------------------------------------------------------
51 ****************************************************************************/
62 /* Debug this single source file with:
63 'make netrc' then run './netrc'!
65 Oh, make sure you have a .netrc file too ;-)
68 /* Get user and password from .netrc when given a machine name */
72 HOSTFOUND, /* the 'machine' keyword was found */
73 HOSTCOMPLETE, /* the machine name following the keyword was found too */
74 HOSTVALID, /* this is "our" machine! */
76 HOSTEND /* LAST enum */
79 /* make sure we have room for at least this size: */
81 #define PASSWORDSIZE 64
83 int ParseNetrc(char *host,
88 char netrcbuffer[256];
91 char *home = GetEnv("HOME"); /* portable environment reader */
95 char state_password=0;
97 #define NETRC DOT_CHAR "netrc"
102 if(strlen(home)>(sizeof(netrcbuffer)-strlen(NETRC))) {
107 sprintf(netrcbuffer, "%s%s%s", home, DIR_CHAR, NETRC);
109 file = fopen(netrcbuffer, "r");
112 while(fgets(netrcbuffer, sizeof(netrcbuffer), file)) {
113 tok=strtok(netrcbuffer, " \t\n");
117 if(strequal("machine", tok)) {
118 /* the next tok is the machine name, this is in itself the
119 delimiter that starts the stuff entered for this machine,
120 after this we need to search for 'login' and
126 if(strequal(host, tok)) {
127 /* and yes, this is our host! */
130 printf("HOST: %s\n", tok);
132 retcode=0; /* we did find our host */
139 /* we are now parsing sub-keywords concerning "our" host */
141 strncpy(login, tok, LOGINSIZE-1);
143 printf("LOGIN: %s\n", login);
147 else if(state_password) {
148 strncpy(password, tok, PASSWORDSIZE-1);
150 printf("PASSWORD: %s\n", password);
154 else if(strequal("login", tok))
156 else if(strequal("password", tok))
158 else if(strequal("machine", tok)) {
159 /* ok, there's machine here go => */
163 } /* switch (state) */
164 tok = strtok(NULL, " \t\n");
166 } /* while fgets() */
177 int main(int argc, char **argv)
180 char password[64]="";
185 if(0 == ParseNetrc(argv[1], login, password)) {
186 printf("HOST: %s LOGIN: %s PASSWORD: %s\n",
187 argv[1], login, password);