Skip to content

Commit

Permalink
Set rc_dir based on W3M_DIR environment variable.
Browse files Browse the repository at this point in the history
By default, w3m puts all of its data in the `~/.w3m/` directory
(creating it as necessary). This was not configurable in any way.

This commit adds some quick reconfigurability -- when the "W3M_DIR"
environment variable is set, w3m will use that location instead. The
default location is unchanged.

Fixes tats#130.
  • Loading branch information
yashlala committed Dec 12, 2021
1 parent 2b59b9e commit c35796f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 28 deletions.
8 changes: 7 additions & 1 deletion doc/FAQ.html
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ <h2 id="other">
</dt>
<dd>
<p>
It is ~/.w3m/config.
By default, it is ~/.w3m/config.
</p>

<p>
Expand All @@ -734,6 +734,12 @@ <h2 id="other">
setting panel. Each line contains one option setting, consisting
of an option name and its value with a space as a separator.
</p>

<p>
If the W3M_DIR environment variable is set to the name of a
directory, w3m will store its files in that directory instead of
in ~/.w3m.
</p>

<p>Without a user-specific configuration file, w3m honours
the system wide configuration file /etc/w3m/config.
Expand Down
5 changes: 5 additions & 0 deletions doc/MANUAL.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ <h2 id="Options">
from standard input and display it. If it doesn't find a document
there either then normally w3m will terminate.
</p>
<p>
You can change how w3m behaves by providing it with options, either
via the command line or through the configuration file. The user
config file is located at $W3M_DIR/config (~/.w3m/config by default).
</p>
<p>
Options include:
</p>
Expand Down
11 changes: 9 additions & 2 deletions doc/w3m.1
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,16 @@ $ w3m \-v
.EE
.\".SH Errors
.SH ENVIRONMENT
\fIw3m\fP recognises the environment variable WWW_HOME as defining a
fallback target for use if it is invoked without one.
\fIw3m\fP recognises the environment variable \fBWWW_HOME\fP as
defining a fallback target for use if it is invoked without one.

If the \fBW3M_HOME\fP environment variable is set to a directory
name, \fIw3m\fP will store its user files there instead of
under the ~/.w3m directory.
.SH FILES
The default locations of some files are listed below. These
locations can be altered via the \fBW3M_HOME\fP environment
variable.
.TP
\f(CW~/.w3m/bookmark.html\fP
default bookmark file
Expand Down
81 changes: 56 additions & 25 deletions rc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,54 @@ do_mkdir(const char *dir, long mode)
#endif /* not __MINW32_VERSION */
#endif /* not __EMX__ */

static int
do_recursive_mkdir(const char *dir)
{
char *c, tmp, component[NAME_MAX];
struct stat st;

if (*dir == '\0')
return -1;

strncpy(component, dir, sizeof(component));
c = component + 1;
do {
while (!(*c == '/' || *c == '\0')) {
c++;
}

tmp = *c;
*c = '\0';

if (stat(component, &st) < 0) {
if (errno != ENOENT) { /* no directory */
goto err;
}
if (do_mkdir(component, 0700) < 0) {
/* fprintf(stderr, "Can't create directory (%s)!\n", component); */
goto err;
}
stat(component, &st);
}
if (!S_ISDIR(st.st_mode)) {
/* not a directory */
/* fprintf(stderr, "%s is not a directory!\n", component); */
goto err;
}
if (!(st.st_mode & S_IWUSR)) {
/* fprintf(stderr, "%s is not writable!\n", component); */
goto err;
}

*c = tmp;

} while (*c++ != '\0');

return 0;
err:
return -1;
}

static void loadSiteconf(void);

void
Expand Down Expand Up @@ -1303,8 +1351,12 @@ init_rc(void)

if (rc_dir != NULL)
goto open_rc;

if ((rc_dir = getenv("W3M_DIR")) == NULL || *rc_dir == '\0') {
rc_dir = RC_DIR;
}
rc_dir = expandPath(rc_dir);

rc_dir = expandPath(RC_DIR);
i = strlen(rc_dir);
if (i > 1 && rc_dir[i - 1] == '/')
rc_dir[i - 1] = '\0';
Expand All @@ -1315,30 +1367,9 @@ init_rc(void)
system_charset_str = display_charset_str;
#endif

if (stat(rc_dir, &st) < 0) {
if (errno == ENOENT) { /* no directory */
if (do_mkdir(rc_dir, 0700) < 0) {
/* fprintf(stderr, "Can't create config directory (%s)!\n", rc_dir); */
goto rc_dir_err;
}
else {
stat(rc_dir, &st);
}
}
else {
/* fprintf(stderr, "Can't open config directory (%s)!\n", rc_dir); */
goto rc_dir_err;
}
}
if (!S_ISDIR(st.st_mode)) {
/* not a directory */
/* fprintf(stderr, "%s is not a directory!\n", rc_dir); */
goto rc_dir_err;
}
if (!(st.st_mode & S_IWUSR)) {
/* fprintf(stderr, "%s is not writable!\n", rc_dir); */
goto rc_dir_err;
}
if (do_recursive_mkdir(rc_dir) == -1)
goto rc_dir_err;

no_rc_dir = FALSE;
tmp_dir = rc_dir;

Expand Down

0 comments on commit c35796f

Please sign in to comment.