De-fuzzyed some msgs...
[midnight-commander.git] / vfs / utilvfs.c
blob875fa7cdc18666a3a5935b087379e8fabd32a653
1 /* Utilities for VFS modules.
3 Currently includes login and tcp open socket routines.
5 Copyright (C) 1995, 1996 Miguel de Icaza
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License
9 as published by the Free Software Foundation; either version 2 of
10 the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Namespace: exports vfs_split_url */
23 #ifndef test_get_host_and_username
24 #include <config.h>
25 #endif
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <pwd.h>
33 #include <sys/types.h>
34 #include <netdb.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #ifdef USE_TERMNET
39 #include <termnet.h>
40 #endif
41 #include <errno.h>
43 #include "utilvfs.h"
45 #include "vfs.h"
47 /* Extract the hostname and username from the path */
48 /* path is in the form: [user@]hostname:port/remote-dir, e.g.:
50 * ftp://sunsite.unc.edu/pub/linux
51 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
52 * ftp://tsx-11.mit.edu:8192/
53 * ftp://joe@foo.edu:11321/private
54 * ftp://joe:password@foo.se
56 * If the user is empty, e.g. ftp://@roxanne/private, then your login name
57 * is supplied.
59 * returns malloced host if host isn't null, user and pass if pass is not null.
60 * returns a malloced strings with the pathname relative to the host.
61 * */
63 char *vfs_split_url (char *path, char **host, char **user, int *port, char **pass,
64 int default_port, int flags)
66 struct passwd *passwd_info;
67 char *dir, *colon, *inner_colon, *at, *rest;
68 char *retval;
69 char *pcopy = g_strdup (path);
70 char *pend = pcopy + strlen (pcopy);
71 int default_is_anon = flags & URL_DEFAULTANON;
73 if (pass)
74 *pass = NULL;
75 *port = default_port;
76 *user = NULL;
77 retval = NULL;
79 dir = pcopy;
80 if (!(flags & URL_NOSLASH)) {
81 /* locate path component */
82 while (*dir != PATH_SEP && *dir)
83 dir++;
84 if (*dir){
85 retval = g_strdup (dir);
86 *dir = 0;
87 } else
88 retval = g_strdup (PATH_SEP_STR);
91 /* search for any possible user */
92 at = strchr (pcopy, '@');
94 /* We have a username */
95 if (at){
96 *at = 0;
97 inner_colon = strchr (pcopy, ':');
98 if (inner_colon){
99 *inner_colon = 0;
100 inner_colon++;
101 if (pass)
102 *pass = g_strdup (inner_colon);
104 if (*pcopy != 0)
105 *user = g_strdup (pcopy);
106 else
107 default_is_anon = 0;
109 if (pend == at+1)
110 rest = at;
111 else
112 rest = at + 1;
113 } else
114 rest = pcopy;
116 if (!*user){
117 if (default_is_anon)
118 *user = g_strdup ("anonymous");
119 else {
120 if ((passwd_info = getpwuid (geteuid ())) == NULL)
121 *user = g_strdup ("anonymous");
122 else {
123 *user = g_strdup (passwd_info->pw_name);
125 endpwent ();
128 /* Check if the host comes with a port spec, if so, chop it */
129 colon = strchr (rest, ':');
130 if (colon){
131 *colon = 0;
132 if (sscanf(colon+1, "%d", port)==1) {
133 if (*port <= 0 || *port >= 65536)
134 *port = default_port;
135 } else {
136 while(*(++colon)){
137 switch(*colon) {
138 case 'C': *port = 1;
139 break;
140 case 'r': *port = 2;
141 break;
146 if (host)
147 *host = g_strdup (rest);
149 g_free (pcopy);
150 return retval;
153 #ifdef test_get_host_and_username
154 struct tt {
155 char *url;
156 char *r_host;
157 char *r_user;
158 char *r_pass;
159 char *r_dir;
160 int r_port;
163 struct tt tests [] = {
164 { "host", "host", "anonymous", NULL, "/", 21 },
165 { "host/dir", "host", "anonymous", NULL, "/dir", 21 },
166 { "host:40/", "host", "anonymous", NULL, "/", 40 },
167 { "host:40/dir", "host", "anonymous", NULL, "/dir", 40 },
168 { "user@host", "host", "user", NULL, "/", 21 },
169 { "user@host/dir", "host", "user", NULL, "/dir", 21 },
170 { "user@host:40/", "host", "user", NULL, "/", 40 },
171 { "user@host:40/dir", "host", "user", NULL, "/dir", 40 },
172 { "user:pass@host", "host", "user", "pass", "/", 21 },
173 { "user:pass@host/dir", "host", "user", "pass", "/dir", 21 },
174 { "user:pass@host:40", "host", "user", "pass", "/", 40 },
175 { "user:pass@host:40/dir", "host", "user", "pass", "/dir", 40 },
176 { "host/a@b", "host", "anonymous", NULL, "/a@b", 21 },
177 { "host:40/a@b", "host", "anonymous", NULL, "/a@b", 40 },
178 { "user@host/a@b", "host", "user", NULL, "/a@b", 21 },
179 { "user@host:40/a@b", "host", "user", NULL, "/a@b", 40 },
180 { "user:pass@host/a@b", "host", "user", "pass", "/a@b", 21 },
181 { "user:pass@host:40/a@b", "host", "user", "pass", "/a@b", 40 },
182 { "host/a:b", "host", "anonymous", NULL, "/a:b", 21 },
183 { "host:40/a:b", "host", "anonymous", NULL, "/a:b", 40 },
184 { "user@host/a:b", "host", "user", NULL, "/a:b", 21 },
185 { "user@host:40/a:b", "host", "user", NULL, "/a:b", 40 },
186 { "user:pass@host/a:b", "host", "user", "pass", "/a:b", 21 },
187 { "user:pass@host:40/a:b", "host", "user", "pass", "/a:b", 40 },
188 { "host/a/b", "host", "anonymous", NULL, "/a/b", 21 },
189 { "host:40/a/b", "host", "anonymous", NULL, "/a/b", 40 },
190 { "user@host/a/b", "host", "user", NULL, "/a/b", 21 },
191 { "user@host:40/a/b", "host", "user", NULL, "/a/b", 40 },
192 { "user:pass@host/a/b", "host", "user", "pass", "/a/b", 21 },
193 { "user:pass@host:40/a/b", "host", "user", "pass", "/a/b", 40 },
194 { NULL }
197 /* tests with implicit login name */
198 struct tt tests2 [] = {
199 { "@host", "host", "user", NULL, "/", 21 },
200 { "@host/dir", "host", "user", NULL, "/dir", 21 },
201 { "@host:40/", "host", "user", NULL, "/", 40 },
202 { "@host:40/dir", "host", "user", NULL, "/dir", 40 },
203 { ":pass@host", "host", "user", "pass", "/", 21 },
204 { ":pass@host/dir", "host", "user", "pass", "/dir", 21 },
205 { ":pass@host:40", "host", "user", "pass", "/", 40 },
206 { ":pass@host:40/dir", "host", "user", "pass", "/dir", 40 },
207 { "@host/a@b", "host", "user", NULL, "/a@b", 21 },
208 { "@host:40/a@b", "host", "user", NULL, "/a@b", 40 },
209 { ":pass@host/a@b", "host", "user", "pass", "/a@b", 21 },
210 { ":pass@host:40/a@b", "host", "user", "pass", "/a@b", 40 },
211 { "@host/a:b", "host", "user", NULL, "/a:b", 21 },
212 { "@host:40/a:b", "host", "user", NULL, "/a:b", 40 },
213 { ":pass@host/a:b", "host", "user", "pass", "/a:b", 21 },
214 { ":pass@host:40/a:b", "host", "user", "pass", "/a:b", 40 },
215 { "@host/a/b", "host", "user", NULL, "/a/b", 21 },
216 { "@host:40/a/b", "host", "user", NULL, "/a/b", 40 },
217 { ":pass@host/a/b", "host", "user", "pass", "/a/b", 21 },
218 { ":pass@host:40/a/b", "host", "user", "pass", "/a/b", 40 },
219 { NULL }
222 main ()
224 int i, port, err;
225 char *dir, *host, *user, *pass;
226 struct passwd *passwd_info;
227 char *current;
229 if ((passwd_info = getpwuid (geteuid ())) == NULL)
230 current = g_strdup ("anonymous");
231 else {
232 current= g_strdup (passwd_info->pw_name);
234 endpwent ();
236 for (i = 0; tests [i].url; i++){
237 err = 0;
238 dir = get_host_and_username (tests [i].url, &host, &user, &port, 21, 1, &pass);
240 if (strcmp (dir, tests [i].r_dir))
241 err++, printf ("dir: test %d flunked\n", i);
243 if (!err && strcmp (host, tests [i].r_host))
244 err++, printf ("host: test %d flunked\n", i);
246 if (!err && strcmp (user, tests [i].r_user))
247 err++, printf ("user: test %d flunked\n", i);
249 if (!err && tests [i].r_pass)
250 if (strcmp (dir, tests [i].r_dir))
251 err++, printf ("pass: test %d flunked\n", i);
253 if (!err & tests [i].r_port != port)
254 err++, printf ("port: test %d flunked\n", i);
256 if (err){
257 printf ("host=[%s] user=[%s] pass=[%s] port=[%d]\n",
258 host, user, pass, port);
262 printf ("%d tests ok\nExtra tests:", i);
264 for (i = 0; i < 4; i++){
265 dir = get_host_and_username (tests [i].url, &host, &user, &port, 21, 0, &pass);
266 if (strcmp (user, current))
267 printf ("ntest: flunked %d\n", i);
270 printf ("%d tests ok\nTests with implicit login name\n", i);
272 for (i = 0; tests2 [i].url; i++){
273 err = 0;
274 dir = get_host_and_username (tests2 [i].url, &host, &user, &port, 21, 1, &pass);
276 if (strcmp (dir, tests2 [i].r_dir))
277 err++, printf ("dir: test %d flunked\n", i);
279 if (!err && strcmp (host, tests2 [i].r_host))
280 err++, printf ("host: test %d flunked\n", i);
282 if (!err && strcmp (user, current))
283 err++, printf ("user: test %d flunked\n", i);
285 if (!err && tests2 [i].r_pass)
286 if (strcmp (dir, tests2 [i].r_dir))
287 err++, printf ("pass: test %d flunked\n", i);
289 if (!err & tests2 [i].r_port != port)
290 err++, printf ("port: test %d flunked\n", i);
292 if (err){
293 printf ("host=[%s] user=[%s] pass=[%s] port=[%d]\n",
294 host, user, pass, port);
297 printf ("%d tests ok\n", i);
299 #endif