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 */
31 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #ifdef HAVE_ARPA_INET_H
36 #include <arpa/inet.h>
44 /* Extract the hostname and username from the path */
45 /* path is in the form: [user@]hostname:port/remote-dir, e.g.:
47 * ftp://sunsite.unc.edu/pub/linux
48 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
49 * ftp://tsx-11.mit.edu:8192/
50 * ftp://joe@foo.edu:11321/private
51 * ftp://joe:password@foo.se
53 * Returns malloc()ed host, user and pass they are present.
54 * If the user is empty, e.g. ftp://@roxanne/private, and URL_ALLOW_ANON
55 * is not set, then the current login name is supplied.
57 * Return value is a malloc()ed string with the pathname relative to the
62 vfs_split_url (const char *path
, char **host
, char **user
, int *port
,
63 char **pass
, int default_port
, int flags
)
65 struct passwd
*passwd_info
;
66 char *dir
, *colon
, *inner_colon
, *at
, *rest
;
68 char *pcopy
= g_strdup (path
);
69 char *pend
= pcopy
+ strlen (pcopy
);
78 if (!(flags
& URL_NOSLASH
)) {
79 /* locate path component */
80 while (*dir
!= PATH_SEP
&& *dir
)
83 retval
= g_strdup (dir
);
86 retval
= g_strdup (PATH_SEP_STR
);
89 /* search for any possible user */
90 at
= strchr (pcopy
, '@');
92 /* We have a username */
95 inner_colon
= strchr (pcopy
, ':');
100 *pass
= g_strdup (inner_colon
);
103 *user
= g_strdup (pcopy
);
112 if (!*user
&& !(flags
& URL_ALLOW_ANON
)) {
113 passwd_info
= getpwuid (geteuid ());
114 if (passwd_info
&& passwd_info
->pw_name
)
115 *user
= g_strdup (passwd_info
->pw_name
);
117 /* This is very unlikely to happen */
118 *user
= g_strdup ("anonymous");
123 /* Check if the host comes with a port spec, if so, chop it */
124 colon
= strchr (rest
, ':');
127 if (sscanf (colon
+ 1, "%d", port
) == 1) {
128 if (*port
<= 0 || *port
>= 65536)
129 *port
= default_port
;
144 *host
= g_strdup (rest
);