NetSurf
findfile.c
Go to the documentation of this file.
1/*
2 * Copyright 2008 Daniel Silverstone <dsilvers@netsurf-browser.org>
3 * Copyright 2009 Mark Benjamin <netsurf-browser.org.MarkBenjamin@dfgh.net>
4 *
5 * This file is part of NetSurf, http://www.netsurf-browser.org/
6 *
7 * NetSurf is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * NetSurf 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 General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <winsock2.h>
21#include <windows.h>
22
23#include <limits.h>
24#include <unistd.h>
25#include <stdbool.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include "utils/log.h"
31#include "utils/url.h"
32#include "utils/utils.h"
33#include "utils/filepath.h"
34
35#include "windows/findfile.h"
36
37/** Create an array of valid paths to search for resources.
38 *
39 * The idea is that all the complex path computation to find resources
40 * is performed here, once, rather than every time a resource is
41 * searched for.
42 */
43char **
44nsws_init_resource(const char *resource_path)
45{
46 char **pathv; /* resource path string vector */
47 char **respath; /* resource paths vector */
48 const char *lang = NULL;
49 char *winpath;
50 int pathi;
51 char *slsh;
52
53 pathv = filepath_path_to_strvec(resource_path);
54 if (pathv == NULL)
55 return NULL;
56
57 winpath = malloc(MAX_PATH);
58 GetModuleFileName(NULL, winpath, MAX_PATH);
59 slsh = strrchr(winpath, '\\');
60 if (slsh != NULL)
61 *slsh=0;
62 strncat(winpath, "\\windows\\res", MAX_PATH);
63
64 pathi = 0;
65 while (pathv[pathi] != NULL)
66 pathi++;
67 pathv[pathi] = winpath;
68
69 respath = filepath_generate(pathv, &lang);
70
72
73 return respath;
74}
75
76static char *realpath(const char *path, char *resolved_path)
77{
78 /* useless, but there we go */
79 return strncpy(resolved_path, path, PATH_MAX);
80}
81
82
83/**
84 * Locate a shared resource file by searching known places in order.
85 *
86 * Search order is: ~/.netsurf/, $NETSURFRES/ (where NETSURFRES is an
87 * environment variable), then the path specified in
88 * NETSURF_WINDOWS_RESPATH in the Makefile then .\\res\\ [windows paths]
89 *
90 * \param buf buffer to write to. must be at least PATH_MAX chars
91 * \param filename file to look for
92 * \param def default to return if file not found
93 * \return The passed in buffer
94 */
95
96char *nsws_find_resource(char *buf, const char *filename, const char *def)
97{
98 char *cdir = getenv("HOME");
99 char t[PATH_MAX];
100
101 if (cdir != NULL) {
102 NSLOG(netsurf, INFO, "Found Home %s", cdir);
103 strcpy(t, cdir);
104 strcat(t, "/.netsurf/");
105 strcat(t, filename);
106 if ((realpath(t, buf) != NULL) && (access(buf, R_OK) == 0))
107 return buf;
108 }
109
110 cdir = getenv("NETSURFRES");
111
112 if (cdir != NULL) {
113 if (realpath(cdir , buf) != NULL) {
114 strcat(buf, "/");
115 strcat(buf, filename);
116 if (access(buf, R_OK) == 0)
117 return buf;
118 }
119 }
120
121 strcpy(t, NETSURF_WINDOWS_RESPATH);
122 strcat(t, filename);
123 if ((realpath(t, buf) != NULL) && (access(buf, R_OK) == 0))
124 return buf;
125
126 getcwd(t, PATH_MAX - SLEN("\\res\\") - strlen(filename));
127 strcat(t, "\\res\\");
128 strcat(t, filename);
129 NSLOG(netsurf, INFO, "looking in %s", t);
130 if ((realpath(t, buf) != NULL) && (access(buf, R_OK) == 0))
131 return buf;
132
133 if (def[0] == '~') {
134 snprintf(t, PATH_MAX, "%s%s", getenv("HOME"), def + 1);
135 if (realpath(t, buf) == NULL) {
136 strcpy(buf, t);
137 }
138 } else {
139 if (realpath(def, buf) == NULL) {
140 strcpy(buf, def);
141 }
142 }
143
144 return buf;
145}
146
147
148/*
149 * Local Variables:
150 * c-basic-offset: 8
151 * End:
152 */
153
#define PATH_MAX
Definition: gui.h:31
char ** filepath_path_to_strvec(const char *path)
Convert a colon separated list of path elements into a string vector.
Definition: filepath.c:313
void filepath_free_strvec(char **pathv)
Free a string vector.
Definition: filepath.c:356
char ** filepath_generate(char *const *pathv, const char *const *langv)
Merge two string vectors into a resource search path vector.
Definition: filepath.c:184
Utility routines to obtain paths to file resources.
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
Interface to utility string handling.
Interface to URL parsing and joining operations.
Interface to a number of general purpose functionality.
#define SLEN(x)
Calculate length of constant C string.
Definition: utils.h:88
char * nsws_find_resource(char *buf, const char *filename, const char *def)
Locate a shared resource file by searching known places in order.
Definition: findfile.c:96
static char * realpath(const char *path, char *resolved_path)
Definition: findfile.c:76
char ** nsws_init_resource(const char *resource_path)
Create an array of valid paths to search for resources.
Definition: findfile.c:44
static nserror path(const struct redraw_context *ctx, const plot_style_t *pstyle, const float *p, unsigned int n, const float transform[6])
Plots a path.
Definition: plot.c:821