Removed next_command.c.
[uftps.git] / path.c
blob60c864861ca7ae31ece0972305eb31625f209d88
1 /*
2 * User FTP Server, Share folders over FTP without being root.
3 * Copyright (C) 2008 Isaac Jurado
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
8 * version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "uftps.h"
22 * FUTURE IDEAS:
24 * - Check every component to see if it is a symbolic link in order to
25 * protect the shared hierarchy even more. This may come at a cost in
26 * performance though.
29 #include <string.h>
33 * Apply new path components to an existing working directory. The working
34 * directory is contained (full path) in wd, which is a buffer with a capacity
35 * of size bytes. This working directory path length is len bytes (counting the
36 * null ending).
38 * The function returns the length of the resulting path after walking the path
39 * argument from the working directory. In case the working directory would
40 * exceed size bytes (at any stage), -1 is returning.
42 static int apply_path (const char *path, char *wd, int len)
44 int i;
46 if (*path == '/')
48 /* Absolute path, truncate wd */
49 wd[0] = '.';
50 wd[1] = '/';
51 wd[2] = '\0';
52 len = 3;
55 do {
56 /* Combine delimiters */
57 while (*path == '/')
58 path++;
60 if (*path == '\0')
61 /* No more components to apply */
62 break;
64 /* Isolate next component */
65 i = 0;
66 while (path[i] != '/' && path[i] != '\0')
67 i++;
69 if (i == 2 && path[0] == '.' && path[1] == '.')
71 /* Return to parent directory, found ".." */
72 while (wd[len] != '/')
73 len--;
74 if (len == 1)
75 len++; /* Root reached, fix */
76 wd[len] = '\0';
77 len++;
79 else if (i != 1 || path[0] != '.')
81 if (len + i >= LINE_SIZE)
82 return -1;
84 /* Apply component, because it is different than "." */
85 if (len > 3)
86 wd[len - 1] = '/';
87 else
88 len--; /* Skip delimiter at root */
90 memcpy(wd + len, path, i);
91 len += i + 1;
92 wd[len - 1] = '\0';
95 /* Now skip to the next component */
96 path += i;
97 } while (1);
99 return len;
103 void change_dir (void)
105 int len;
107 if (SS.arg == NULL)
109 reply_c("501 Argument required.\r\n");
110 return;
113 len = apply_path(SS.arg, SS.cwd, SS.cwd_len);
114 if (len == -1)
116 reply_c("552 Path overflow.\r\n");
117 fatal("Path overflow in CWD");
120 SS.cwd_len = len;
121 reply_c("250 Directory changed.\r\n");
122 debug("Directory changed to '%s'", SS.cwd);
126 int expand_arg (void)
128 int len = SS.cwd_len;
130 strncpy(SS.aux, SS.cwd, len);
132 if (SS.arg != NULL)
134 len = apply_path(SS.arg, SS.aux, SS.cwd_len);
135 if (len == -1)
137 reply_c("552 Path overflow.\r\n");
138 fatal("Path overflow");
142 SS.arg = SS.aux;
143 debug("Argument expanded to '%s'", SS.arg);
144 return len;