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.nu>
32 * Troy Engel <tengel@palladium.net>
33 * Ryan Nelson <ryan@inch.com>
34 * Bjorn Stenberg <Bjorn.Stenberg@haxx.nu>
35 * Angus Mackay <amackay@gus.ml.org>
37 * ------------------------------------------------------------
39 * - Daniel Stenberg <Daniel.Stenberg@haxx.nu>
43 * $Source: /cvsroot/curl/curl/lib/netrc.c,v $
44 * $Revision: 1.1.1.1 $
45 * $Date: 1999-12-29 14:21:35 $
50 * ------------------------------------------------------------
51 ****************************************************************************/
60 /* Debug this single source file with:
61 'make netrc' then run './netrc'!
63 Oh, make sure you have a .netrc file too ;-)
66 /* Get user and password from .netrc when given a machine name */
70 HOSTFOUND
, /* the 'machine' keyword was found */
71 HOSTCOMPLETE
, /* the machine name following the keyword was found too */
72 HOSTVALID
, /* this is "our" machine! */
74 HOSTEND
/* LAST enum */
77 /* make sure we have room for at least this size: */
79 #define PASSWORDSIZE 64
81 int ParseNetrc(char *host
,
86 char netrcbuffer
[256];
89 char *home
= GetEnv("HOME"); /* portable environment reader */
93 char state_password
=0;
95 #define NETRC DOT_CHAR "netrc"
97 if(!home
|| (strlen(home
)>(sizeof(netrcbuffer
)-strlen(NETRC
))))
100 sprintf(netrcbuffer
, "%s%s%s", home
, DIR_CHAR
, NETRC
);
102 file
= fopen(netrcbuffer
, "r");
105 while(fgets(netrcbuffer
, sizeof(netrcbuffer
), file
)) {
106 tok
=strtok(netrcbuffer
, " \t\n");
110 if(strequal("machine", tok
)) {
111 /* the next tok is the machine name, this is in itself the
112 delimiter that starts the stuff entered for this machine,
113 after this we need to search for 'login' and
119 if(strequal(host
, tok
)) {
120 /* and yes, this is our host! */
123 printf("HOST: %s\n", tok
);
125 retcode
=0; /* we did find our host */
132 /* we are now parsing sub-keywords concerning "our" host */
134 strncpy(login
, tok
, LOGINSIZE
-1);
136 printf("LOGIN: %s\n", login
);
140 else if(state_password
) {
141 strncpy(password
, tok
, PASSWORDSIZE
-1);
143 printf("PASSWORD: %s\n", password
);
147 else if(strequal("login", tok
))
149 else if(strequal("password", tok
))
151 else if(strequal("machine", tok
)) {
152 /* ok, there's machine here go => */
156 } /* switch (state) */
157 tok
= strtok(NULL
, " \t\n");
159 } /* while fgets() */
168 int main(int argc
, char **argv
)
171 char password
[64]="";
176 if(0 == ParseNetrc(argv
[1], login
, password
)) {
177 printf("HOST: %s LOGIN: %s PASSWORD: %s\n",
178 argv
[1], login
, password
);