1 /* Network utilities for the Midnight Commander Virtual File System.
3 Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2005, 2007
4 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License
8 as published by the Free Software Foundation; either version 2 of
9 the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 * \brief Source: Virtual File System: Network utilities
29 #include <sys/types.h>
34 #include "lib/global.h"
51 static char _initialized
= 0;
57 sa
.sa_handler
= sig_pipe
;
59 sigemptyset (&sa
.sa_mask
);
60 sigaction (SIGPIPE
, &sa
, NULL
);
65 /** Extract the hostname and username from the path
67 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
69 * ftp://sunsite.unc.edu/pub/linux
70 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
71 * ftp://tsx-11.mit.edu:8192/
72 * ftp://joe@foo.edu:11321/private
73 * ftp://joe:password@foo.se
75 * @param path is an input string to be parsed
76 * @param host is an outptun g_malloc()ed hostname
77 * @param user is an outptut g_malloc()ed username
78 * (NULL if not specified)
79 * @param port is an outptut integer port number
80 * @param pass is an outptut g_malloc()ed password
81 * @param default_port is an input default port
82 * @param flags are parsing modifier flags (@see VFS_URL_FLAGS)
84 * @return g_malloc()ed host, user and pass if they are present.
85 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
86 * is not set, then the current login name is supplied.
87 * Return value is a g_malloc()ed string with the pathname relative to the
91 vfs_split_url (const char *path
, char **host
, char **user
, int *port
,
92 char **pass
, int default_port
, enum VFS_URL_FLAGS flags
)
94 char *dir
, *colon
, *inner_colon
, *at
, *rest
;
96 char * const pcopy
= g_strdup (path
);
97 const char *pend
= pcopy
+ strlen (pcopy
);
101 *port
= default_port
;
106 if (!(flags
& URL_NOSLASH
)) {
107 /* locate path component */
108 while (*dir
!= PATH_SEP
&& *dir
)
111 retval
= g_strdup (dir
);
114 retval
= g_strdup (PATH_SEP_STR
);
117 /* search for any possible user */
118 at
= strrchr (pcopy
, '@');
120 /* We have a username */
123 inner_colon
= strchr (pcopy
, ':');
128 *pass
= g_strdup (inner_colon
);
131 *user
= g_strdup (pcopy
);
140 if (!*user
&& !(flags
& URL_USE_ANONYMOUS
))
141 *user
= vfs_get_local_username();
143 /* Check if the host comes with a port spec, if so, chop it */
145 colon
= strchr (++rest
, ']');
158 colon
= strchr (rest
, ':');
162 if (sscanf (colon
+ 1, "%d", port
) == 1) {
163 if (*port
<= 0 || *port
>= 65536)
164 *port
= default_port
;
179 *host
= g_strdup (rest
);