mc.sh & mc.csh creation fixed...
[midnight-commander.git] / vfs / utilvfs.c
blob1fea7854f9021d0bac30029c6696e1804b3939e2
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 #include <config.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <signal.h>
29 #include <string.h>
30 #include <pwd.h>
31 #include <sys/types.h>
32 #include <netdb.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #ifdef HAVE_ARPA_INET_H
36 #include <arpa/inet.h>
37 #endif
38 #include <errno.h>
40 #include "utilvfs.h"
42 #include "vfs.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
58 * host.
61 char *
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;
67 char *retval;
68 char *pcopy = g_strdup (path);
69 char *pend = pcopy + strlen (pcopy);
71 if (pass)
72 *pass = NULL;
73 *port = default_port;
74 *user = NULL;
75 retval = NULL;
77 dir = pcopy;
78 if (!(flags & URL_NOSLASH)) {
79 /* locate path component */
80 while (*dir != PATH_SEP && *dir)
81 dir++;
82 if (*dir) {
83 retval = g_strdup (dir);
84 *dir = 0;
85 } else
86 retval = g_strdup (PATH_SEP_STR);
89 /* search for any possible user */
90 at = strchr (pcopy, '@');
92 /* We have a username */
93 if (at) {
94 *at = 0;
95 inner_colon = strchr (pcopy, ':');
96 if (inner_colon) {
97 *inner_colon = 0;
98 inner_colon++;
99 if (pass)
100 *pass = g_strdup (inner_colon);
102 if (*pcopy != 0)
103 *user = g_strdup (pcopy);
105 if (pend == at + 1)
106 rest = at;
107 else
108 rest = at + 1;
109 } else
110 rest = 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);
116 else {
117 /* This is very unlikely to happen */
118 *user = g_strdup ("anonymous");
120 endpwent ();
123 /* Check if the host comes with a port spec, if so, chop it */
124 colon = strchr (rest, ':');
125 if (colon) {
126 *colon = 0;
127 if (sscanf (colon + 1, "%d", port) == 1) {
128 if (*port <= 0 || *port >= 65536)
129 *port = default_port;
130 } else {
131 while (*(++colon)) {
132 switch (*colon) {
133 case 'C':
134 *port = 1;
135 break;
136 case 'r':
137 *port = 2;
138 break;
143 if (host)
144 *host = g_strdup (rest);
146 g_free (pcopy);
147 return retval;