fixed man and help
[free-mc.git] / lib / vfs / mc-vfs / netutil.c
blob0054f6b18fa020b98b7620514d7c52a452e6ddfb
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. */
20 /**
21 * \file
22 * \brief Source: Virtual File System: Network utilities
25 #include <config.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <signal.h>
29 #include <sys/types.h>
30 #include <pwd.h>
31 #include <unistd.h>
32 #include <string.h>
34 #include "lib/global.h"
35 #include "utilvfs.h"
36 #include "netutil.h"
38 int got_sigpipe;
40 static void
41 sig_pipe (int unused)
43 (void) unused;
44 got_sigpipe = 1;
47 void
48 tcp_init (void)
50 struct sigaction sa;
51 static char _initialized = 0;
53 if (_initialized)
54 return;
56 got_sigpipe = 0;
57 sa.sa_handler = sig_pipe;
58 sa.sa_flags = 0;
59 sigemptyset (&sa.sa_mask);
60 sigaction (SIGPIPE, &sa, NULL);
62 _initialized = 1;
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
88 * host.
90 char *
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;
95 char *retval;
96 char * const pcopy = g_strdup (path);
97 const char *pend = pcopy + strlen (pcopy);
99 if (pass)
100 *pass = NULL;
101 *port = default_port;
102 *user = NULL;
103 retval = NULL;
105 dir = pcopy;
106 if (!(flags & URL_NOSLASH)) {
107 /* locate path component */
108 while (*dir != PATH_SEP && *dir)
109 dir++;
110 if (*dir) {
111 retval = g_strdup (dir);
112 *dir = 0;
113 } else
114 retval = g_strdup (PATH_SEP_STR);
117 /* search for any possible user */
118 at = strrchr (pcopy, '@');
120 /* We have a username */
121 if (at) {
122 *at = 0;
123 inner_colon = strchr (pcopy, ':');
124 if (inner_colon) {
125 *inner_colon = 0;
126 inner_colon++;
127 if (pass)
128 *pass = g_strdup (inner_colon);
130 if (*pcopy != 0)
131 *user = g_strdup (pcopy);
133 if (pend == at + 1)
134 rest = at;
135 else
136 rest = at + 1;
137 } else
138 rest = 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 */
144 if ('[' == *rest) {
145 colon = strchr (++rest, ']');
146 if (colon) {
147 colon[0] = '\0';
148 colon[1] = '\0';
149 colon++;
150 } else {
151 g_free (pcopy);
152 g_free (retval);
153 *host = NULL;
154 *port = 0;
155 return NULL;
157 } else
158 colon = strchr (rest, ':');
160 if (colon) {
161 *colon = 0;
162 if (sscanf (colon + 1, "%d", port) == 1) {
163 if (*port <= 0 || *port >= 65536)
164 *port = default_port;
165 } else {
166 while (*(++colon)) {
167 switch (*colon) {
168 case 'C':
169 *port = 1;
170 break;
171 case 'r':
172 *port = 2;
173 break;
178 if (host)
179 *host = g_strdup (rest);
181 g_free (pcopy);
182 return retval;