Bug 1013: Don't assume errno is between 0 and 100000
[elinks.git] / src / protocol / file / file.c
blob833ed8bc56b179e2f721b10e403cce5d125c9e6f
1 /* Internal "file" protocol implementation */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h> /* OS/2 needs this after sys/types.h */
13 #ifdef HAVE_FCNTL_H
14 #include <fcntl.h> /* OS/2 needs this after sys/types.h */
15 #endif
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
20 #include "elinks.h"
22 #include "cache/cache.h"
23 #include "config/options.h"
24 #include "encoding/encoding.h"
25 #include "intl/gettext/libintl.h"
26 #include "main/module.h"
27 #include "network/connection.h"
28 #include "osdep/osdep.h"
29 #include "protocol/common.h"
30 #include "protocol/file/cgi.h"
31 #include "protocol/file/file.h"
32 #include "protocol/uri.h"
33 #include "util/conv.h"
34 #include "util/file.h"
35 #include "util/memory.h"
36 #include "util/string.h"
39 static struct option_info file_options[] = {
40 INIT_OPT_TREE("protocol", N_("Local files"),
41 "file", 0,
42 N_("Options specific to local browsing.")),
44 INIT_OPT_BOOL("protocol.file", N_("Allow reading special files"),
45 "allow_special_files", 0, 0,
46 N_("Whether to allow reading from non-regular files.\n"
47 "Note this can be dangerous; reading /dev/urandom or\n"
48 "/dev/zero can ruin your day!")),
50 INIT_OPT_BOOL("protocol.file", N_("Show hidden files in directory listing"),
51 "show_hidden_files", 0, 1,
52 N_("When set to false, files with name starting with a dot will be\n"
53 "hidden in local directory listings.")),
55 INIT_OPT_BOOL("protocol.file", N_("Try encoding extensions"),
56 "try_encoding_extensions", 0, 1,
57 N_("When set, if we can't open a file named 'filename', we'll try\n"
58 "to open 'filename' with some encoding extension appended\n"
59 "(ie. 'filename.gz'); it depends on the supported encodings.")),
61 NULL_OPTION_INFO,
64 struct module file_protocol_module = struct_module(
65 /* name: */ N_("File"),
66 /* options: */ file_options,
67 /* hooks: */ NULL,
68 /* submodules: */ NULL,
69 /* data: */ NULL,
70 /* init: */ NULL,
71 /* done: */ NULL
75 /* Directory listing */
77 /* Based on the @entry attributes and file-/dir-/linkname is added to the @data
78 * fragment. All the strings are in the system charset. */
79 static inline void
80 add_dir_entry(struct directory_entry *entry, struct string *page,
81 int pathlen, unsigned char *dircolor)
83 unsigned char *lnk = NULL;
84 struct string html_encoded_name;
85 struct string uri_encoded_name;
87 if (!init_string(&html_encoded_name)) return;
88 if (!init_string(&uri_encoded_name)) {
89 done_string(&html_encoded_name);
90 return;
93 encode_uri_string(&uri_encoded_name, entry->name + pathlen, -1, 1);
94 add_html_to_string(&html_encoded_name, entry->name + pathlen,
95 strlen(entry->name) - pathlen);
97 /* add_to_string(&fragment, &fragmentlen, " "); */
98 add_html_to_string(page, entry->attrib, strlen(entry->attrib));
99 add_to_string(page, "<a href=\"");
100 add_string_to_string(page, &uri_encoded_name);
102 if (entry->attrib[0] == 'd') {
103 add_char_to_string(page, '/');
105 #ifdef FS_UNIX_SOFTLINKS
106 } else if (entry->attrib[0] == 'l') {
107 struct stat st;
108 unsigned char buf[MAX_STR_LEN];
109 int readlen = readlink(entry->name, buf, MAX_STR_LEN);
111 if (readlen > 0 && readlen != MAX_STR_LEN) {
112 buf[readlen] = '\0';
113 lnk = straconcat(" -> ", buf, (unsigned char *) NULL);
116 if (!stat(entry->name, &st) && S_ISDIR(st.st_mode))
117 add_char_to_string(page, '/');
118 #endif
121 add_to_string(page, "\">");
123 if (entry->attrib[0] == 'd' && *dircolor) {
124 /* The <b> is for the case when use_document_colors is off. */
125 string_concat(page, "<font color=\"", dircolor, "\"><b>",
126 (unsigned char *) NULL);
129 add_string_to_string(page, &html_encoded_name);
130 done_string(&uri_encoded_name);
131 done_string(&html_encoded_name);
133 if (entry->attrib[0] == 'd' && *dircolor) {
134 add_to_string(page, "</b></font>");
137 add_to_string(page, "</a>");
138 if (lnk) {
139 add_html_to_string(page, lnk, strlen(lnk));
140 mem_free(lnk);
143 add_char_to_string(page, '\n');
146 /* First information such as permissions is gathered for each directory entry.
147 * Finally the sorted entries are added to the @data->fragment one by one. */
148 static inline void
149 add_dir_entries(struct directory_entry *entries, unsigned char *dirpath,
150 struct string *page)
152 unsigned char dircolor[8];
153 int dirpathlen = strlen(dirpath);
154 int i;
156 /* Setup @dircolor so it's easy to check if we should color dirs. */
157 if (get_opt_bool("document.browse.links.color_dirs")) {
158 color_to_string(get_opt_color("document.colors.dirs"),
159 (unsigned char *) &dircolor);
160 } else {
161 dircolor[0] = 0;
164 for (i = 0; entries[i].name; i++) {
165 add_dir_entry(&entries[i], page, dirpathlen, dircolor);
166 mem_free(entries[i].attrib);
167 mem_free(entries[i].name);
170 /* We may have allocated space for entries but added none. */
171 mem_free_if(entries);
174 /* Generates an HTML page listing the content of @directory with the path
175 * @dirpath. */
176 /* Returns a connection state. S_OK if all is well. */
177 static inline struct connection_state
178 list_directory(struct connection *conn, unsigned char *dirpath,
179 struct string *page)
181 int show_hidden_files = get_opt_bool("protocol.file.show_hidden_files");
182 struct directory_entry *entries;
183 struct connection_state state;
185 errno = 0;
186 entries = get_directory_entries(dirpath, show_hidden_files);
187 if (!entries) {
188 if (errno) return connection_state_for_errno(errno);
189 return connection_state(S_OUT_OF_MEM);
192 state = init_directory_listing(page, conn->uri);
193 if (!is_in_state(state, S_OK))
194 return connection_state(S_OUT_OF_MEM);
196 add_dir_entries(entries, dirpath, page);
198 if (!add_to_string(page, "</pre>\n<hr/>\n</body>\n</html>\n")) {
199 done_string(page);
200 return connection_state(S_OUT_OF_MEM);
203 return connection_state(S_OK);
207 /* To reduce redundant error handling code [calls to abort_connection()]
208 * most of the function is build around conditions that will assign the error
209 * code to @state if anything goes wrong. The rest of the function will then just
210 * do the necessary cleanups. If all works out we end up with @state being S_OK
211 * resulting in a cache entry being created with the fragment data generated by
212 * either reading the file content or listing a directory. */
213 void
214 file_protocol_handler(struct connection *connection)
216 unsigned char *redirect_location = NULL;
217 struct string page, name;
218 struct connection_state state;
219 int set_dir_content_type = 0;
221 if (get_cmd_opt_bool("anonymous")) {
222 if (strcmp(connection->uri->string, "file:///dev/stdin")
223 || isatty(STDIN_FILENO)) {
224 abort_connection(connection,
225 connection_state(S_FILE_ANONYMOUS));
226 return;
230 #ifdef CONFIG_CGI
231 if (!execute_cgi(connection)) return;
232 #endif /* CONFIG_CGI */
234 /* This function works on already simplified file-scheme URI pre-chewed
235 * by transform_file_url(). By now, the function contains no hostname
236 * part anymore, possibly relative path is converted to an absolute one
237 * and uri->data is just the final path to file/dir we should try to
238 * show. */
240 if (!init_string(&name)
241 || !add_uri_to_string(&name, connection->uri, URI_PATH)) {
242 done_string(&name);
243 abort_connection(connection, connection_state(S_OUT_OF_MEM));
244 return;
247 decode_uri_string(&name);
249 /* In Win32, file_is_dir seems to always return 0 if the name
250 * ends with a directory separator. */
251 if ((name.length > 0 && dir_sep(name.source[name.length - 1]))
252 || file_is_dir(name.source)) {
253 /* In order for global history and directory listing to
254 * function properly the directory url must end with a
255 * directory separator. */
256 if (name.source[0] && !dir_sep(name.source[name.length - 1])) {
257 redirect_location = STRING_DIR_SEP;
258 state = connection_state(S_OK);
259 } else {
260 state = list_directory(connection, name.source, &page);
261 set_dir_content_type = 1;
264 } else {
265 state = read_encoded_file(&name, &page);
266 /* FIXME: If state is now S_ENCODE_ERROR we should try loading
267 * the file undecoded. --jonas */
270 done_string(&name);
272 if (is_in_state(state, S_OK)) {
273 struct cache_entry *cached;
275 /* Try to add fragment data to the connection cache if either
276 * file reading or directory listing worked out ok. */
277 cached = connection->cached = get_cache_entry(connection->uri);
278 if (!connection->cached) {
279 if (!redirect_location) done_string(&page);
280 state = connection_state(S_OUT_OF_MEM);
282 } else if (redirect_location) {
283 if (!redirect_cache(cached, redirect_location, 1, 0))
284 state = connection_state(S_OUT_OF_MEM);
286 } else {
287 add_fragment(cached, 0, page.source, page.length);
288 connection->from += page.length;
290 if (!cached->head && set_dir_content_type) {
291 unsigned char *head;
293 /* If the system charset somehow
294 * changes after the directory listing
295 * has been generated, it should be
296 * parsed with the original charset. */
297 head = straconcat("\r\nContent-Type: text/html; charset=",
298 get_cp_mime_name(get_cp_index("System")),
299 "\r\n", (unsigned char *) NULL);
301 /* Not so gracefully handle failed memory
302 * allocation. */
303 if (!head)
304 state = connection_state(S_OUT_OF_MEM);
306 /* Setup directory listing for viewing. */
307 mem_free_set(&cached->head, head);
310 done_string(&page);
314 abort_connection(connection, state);