commit e893a2d6a8bcef600bf3ce321e0e773e3850ce64
parent 48f462db3ee15c77e50ce3872d85697b5fca23a6
Author: ukai <ukai>
Date: Fri, 17 Jan 2003 17:05:57 +0000
[w3m-dev 03647] expandName() and expandPath()
* etc.c (openSecretFile): use expandPath
(expandName): rewrite
(file_to_url): use expandPath
* file.c (_doFileCopy): use expandPath
(doFileSave): use expandPath
* indep.c (expandPath): rewrite
* linein.c (inputLineHistSearch): use expandPath
(next_dcompl): use expandPath
(doComplete): use expandPath
* local.c (set_cgi_environ): rewrite
* mailcap.c (loadMailcap): use expandPath
* main.c (svBuf): use expandPath
(addDownloadList): use expandPath
* rc.c (init_rc): use expandPath
(rcFile): rewrite
(auxbinFile): use expandPath
(libFile): use expandPath
(etcFile): use expandPath
(helpFile): use expandPath
* url.c (loadMimeTypes): use expandPath
(loadURIMethods): use expandPath
From: Hironori SAKAMOTO <hsaka@mth.biglobe.ne.jp>
Diffstat:
10 files changed, 105 insertions(+), 78 deletions(-)
diff --git a/ChangeLog b/ChangeLog
@@ -1,5 +1,30 @@
2003-01-18 Hironori SAKAMOTO <hsaka@mth.biglobe.ne.jp>
+ * [w3m-dev 03647] expandName() and expandPath()
+ * etc.c (openSecretFile): use expandPath
+ (expandName): rewrite
+ (file_to_url): use expandPath
+ * file.c (_doFileCopy): use expandPath
+ (doFileSave): use expandPath
+ * indep.c (expandPath): rewrite
+ * linein.c (inputLineHistSearch): use expandPath
+ (next_dcompl): use expandPath
+ (doComplete): use expandPath
+ * local.c (set_cgi_environ): rewrite
+ * mailcap.c (loadMailcap): use expandPath
+ * main.c (svBuf): use expandPath
+ (addDownloadList): use expandPath
+ * rc.c (init_rc): use expandPath
+ (rcFile): rewrite
+ (auxbinFile): use expandPath
+ (libFile): use expandPath
+ (etcFile): use expandPath
+ (helpFile): use expandPath
+ * url.c (loadMimeTypes): use expandPath
+ (loadURIMethods): use expandPath
+
+2003-01-18 Hironori SAKAMOTO <hsaka@mth.biglobe.ne.jp>
+
* [w3m-dev 03646] setup child process, local CGI
* etc.c (reset_signals): static
don't ignore SIGUSR1
diff --git a/etc.c b/etc.c
@@ -1046,10 +1046,13 @@ parsePasswd(FILE * fp, int netrc)
FILE *
openSecretFile(char *fname)
{
+ char *efname;
struct stat st;
+
if (fname == NULL)
return NULL;
- if (stat(expandName(fname), &st) < 0)
+ efname = expandPath(fname);
+ if (stat(efname, &st) < 0)
return NULL;
/* check permissions, if group or others readable or writable,
@@ -1076,7 +1079,7 @@ openSecretFile(char *fname)
return NULL;
}
- return fopen(expandName(fname), "r");
+ return fopen(efname, "r");
}
void
@@ -1429,35 +1432,44 @@ myEditor(char *cmd, char *file, int line)
char *
expandName(char *name)
{
- Str userName = NULL;
char *p;
struct passwd *passent, *getpwnam(const char *);
- Str extpath = Strnew();
+ Str extpath = NULL;
+ if (name == NULL)
+ return NULL;
p = name;
- if (*p == '/' && *(p + 1) == '~' && IS_ALPHA(*(p + 2))) {
- if (personal_document_root != NULL) {
- userName = Strnew();
+ if (*p == '/') {
+ if (*(p + 1) == '~' && IS_ALPHA(*(p + 2)) && personal_document_root) {
+ char *q;
p += 2;
- while (IS_ALNUM(*p) || *p == '_' || *p == '-')
- Strcat_char(userName, *(p++));
- passent = getpwnam(userName->ptr);
- if (passent == NULL) {
- p = name;
- goto rest;
+ q = strchr(p, '/');
+ if (q) { /* /~user/dir... */
+ passent = getpwnam(allocStr(p, q - p));
+ p = q;
+ }
+ else { /* /~user */
+ passent = getpwnam(p);
+ p = "";
}
- Strcat_charp(extpath, passent->pw_dir);
- Strcat_char(extpath, '/');
- Strcat_charp(extpath, personal_document_root);
- if (Strcmp_charp(extpath, "/") == 0 && *p == '/')
+ if (!passent)
+ goto rest;
+ extpath = Strnew_m_charp(passent->pw_dir, "/",
+ personal_document_root, NULL);
+ if (*personal_document_root == '\0' && *p == '/')
p++;
}
+ else
+ goto rest;
+ if (Strcmp_charp(extpath, "/") == 0 && *p == '/')
+ p++;
+ Strcat_charp(extpath, p);
+ return extpath->ptr;
}
else
- p = expandPath(p);
+ return expandPath(p);
rest:
- Strcat_charp(extpath, p);
- return extpath->ptr;
+ return name;
}
char *
@@ -1471,7 +1483,7 @@ file_to_url(char *file)
char *host = NULL;
#endif
- file = expandName(file);
+ file = expandPath(file);
#ifdef SUPPORT_NETBIOS_SHARE
if (file[0] == '/' && file[1] == '/') {
char *p;
diff --git a/file.c b/file.c
@@ -7454,7 +7454,7 @@ _doFileCopy(char *tmpf, char *defstr, int download)
p = unescape_spaces(Strnew_charp(q))->ptr;
p = conv_to_system(q);
}
- p = expandName(p);
+ p = expandPath(p);
if (checkOverWrite(p) < 0)
return -1;
}
@@ -7511,7 +7511,7 @@ _doFileCopy(char *tmpf, char *defstr, int download)
if (*p == '|' && PermitSaveToPipe)
is_pipe = TRUE;
else {
- p = expandName(p);
+ p = expandPath(p);
if (checkOverWrite(p) < 0)
return -1;
}
@@ -7606,7 +7606,7 @@ doFileSave(URLFile uf, char *defstr)
*(p + 1) = '\0';
if (*q == '\0')
return -1;
- p = expandName(q);
+ p = expandPath(q);
if (checkOverWrite(p) < 0)
return -1;
if (checkSaveFile(uf.stream, p) < 0) {
diff --git a/indep.c b/indep.c
@@ -177,10 +177,9 @@ cleanupName(char *name)
char *
expandPath(char *name)
{
- Str userName = NULL;
char *p;
struct passwd *passent, *getpwnam(const char *);
- Str extpath = Strnew();
+ Str extpath = NULL;
if (name == NULL)
return NULL;
@@ -188,25 +187,31 @@ expandPath(char *name)
if (*p == '~') {
p++;
if (IS_ALPHA(*p)) {
- userName = Strnew();
- while (IS_ALNUM(*p) || *p == '_' || *p == '-')
- Strcat_char(userName, *(p++));
- passent = getpwnam(userName->ptr);
- if (passent == NULL) {
- p = name;
- goto rest;
+ char *q = strchr(p, '/');
+ if (q) { /* ~user/dir... */
+ passent = getpwnam(allocStr(p, q - p));
+ p = q;
+ }
+ else { /* ~user */
+ passent = getpwnam(p);
+ p = "";
}
- Strcat_charp(extpath, passent->pw_dir);
+ if (!passent)
+ goto rest;
+ extpath = Strnew_charp(passent->pw_dir);
}
- else {
- Strcat_charp(extpath, getenv("HOME"));
+ else if (*p == '/' || *p == '\0') { /* ~/dir... or ~ */
+ extpath = Strnew_charp(getenv("HOME"));
}
+ else
+ goto rest;
if (Strcmp_charp(extpath, "/") == 0 && *p == '/')
p++;
+ Strcat_charp(extpath, p);
+ return extpath->ptr;
}
rest:
- Strcat_charp(extpath, p);
- return extpath->ptr;
+ return name;
}
#ifndef HAVE_STRCHR
diff --git a/linein.c b/linein.c
@@ -309,7 +309,7 @@ inputLineHistSearch(char *prompt, char *def_str, int flag, Hist *hist,
pushHist(hist, p);
}
if (flag & IN_FILENAME)
- return expandName(p);
+ return expandPath(p);
else
return allocStr(p, -1);
}
@@ -853,7 +853,7 @@ next_dcompl(int next)
f = Strdup(d);
Strcat_charp(f, CFileBuf[n]);
addstr(conv_from_system(CFileBuf[n]));
- if (stat(expandName(f->ptr), &st) != -1 && S_ISDIR(st.st_mode))
+ if (stat(expandPath(f->ptr), &st) != -1 && S_ISDIR(st.st_mode))
addstr("/");
}
y++;
@@ -957,7 +957,7 @@ doComplete(Str ifn, int *status, int next)
if (Strlastchar(CompleteBuf) == '/' && CompleteBuf->length > 1) {
Strshrink(CompleteBuf, 1);
}
- if ((d = opendir(expandName(CompleteBuf->ptr))) == NULL) {
+ if ((d = opendir(expandPath(CompleteBuf->ptr))) == NULL) {
CompleteBuf = Strdup(ifn);
*status = CPL_FAIL;
if (cm_mode & CPL_ON)
@@ -1031,7 +1031,7 @@ doComplete(Str ifn, int *status, int next)
else if (strncmp(p, "file:/", 6) == 0 && p[6] != '/')
p = &p[5];
}
- if (stat(expandName(p), &st) != -1 && S_ISDIR(st.st_mode))
+ if (stat(expandPath(p), &st) != -1 && S_ISDIR(st.st_mode))
Strcat_char(CompleteBuf, '/');
}
if (cm_mode & CPL_ON)
diff --git a/local.c b/local.c
@@ -296,19 +296,22 @@ set_cgi_environ(char *name, char *fn, char *req_uri)
static Str
checkPath(char *fn, char *path)
{
+ char *p;
Str tmp;
struct stat st;
while (*path) {
- tmp = Strnew();
- while (*path && *path != ':')
- Strcat_char(tmp, *path++);
- if (*path == ':')
- path++;
+ p = strchr(path, ':');
+ tmp = Strnew_charp(expandPath(p ? allocStr(path, p - path) : path));
if (Strlastchar(tmp) != '/')
Strcat_char(tmp, '/');
Strcat_charp(tmp, fn);
if (stat(tmp->ptr, &st) == 0)
return tmp;
+ if (!p)
+ break;
+ path = p + 1;
+ while (*path == ':')
+ path++;
}
return NULL;
}
diff --git a/mailcap.c b/mailcap.c
@@ -187,7 +187,7 @@ loadMailcap(char *filename)
Str tmp;
struct mailcap *mcap;
- f = fopen(expandName(filename), "r");
+ f = fopen(expandPath(filename), "r");
if (f == NULL)
return NULL;
i = 0;
diff --git a/main.c b/main.c
@@ -4241,7 +4241,7 @@ svBuf(void)
file = unescape_spaces(Strnew_charp(qfile))->ptr;
file = conv_to_system(file);
}
- file = expandName(file);
+ file = expandPath(file);
if (checkOverWrite(file) < 0) {
displayBuffer(Currentbuf, B_NORMAL);
return;
@@ -6080,7 +6080,7 @@ addDownloadList(pid_t pid, char *url, char *save, char *lock, clen_t size)
d->url = url;
if (save[0] != '/' && save[0] != '~')
save = Strnew_m_charp(CurrentDir, "/", save, NULL)->ptr;
- d->save = expandName(save);
+ d->save = expandPath(save);
d->lock = lock;
d->size = size;
d->time = time(0);
diff --git a/rc.c b/rc.c
@@ -1375,7 +1375,7 @@ init_rc(void)
if (config_file != NULL)
goto open_rc;
- rc_dir = expandName(RC_DIR);
+ rc_dir = expandPath(RC_DIR);
i = strlen(rc_dir);
if (i > 1 && rc_dir[i - 1] == '/')
rc_dir[i - 1] = '\0';
@@ -1580,53 +1580,35 @@ rcFile(char *base)
(base[0] == '.'
&& (base[1] == '/' || (base[1] == '.' && base[2] == '/')))
|| (base[0] == '~' && base[1] == '/')))
- return expandName(base);
- else {
- Str file = Strnew_charp(rc_dir);
-
- if (Strlastchar(file) != '/')
- Strcat_char(file, '/');
- Strcat_charp(file, base);
- return expandName(file->ptr);
- }
+ /* /file, ./file, ../file, ~/file */
+ return expandPath(base);
+ return expandPath(Strnew_m_charp(rc_dir, "/", base, NULL)->ptr);
}
char *
auxbinFile(char *base)
{
- Str file = Strnew_charp(w3m_auxbin_dir());
- Strcat_char(file, '/');
- Strcat_charp(file, base);
- return expandName(file->ptr);
+ return expandPath(Strnew_m_charp(w3m_auxbin_dir(), "/", base, NULL)->ptr);
}
#if 0 /* not used */
char *
libFile(char *base)
{
- Str file = Strnew_charp(w3m_lib_dir());
- Strcat_char(file, '/');
- Strcat_charp(file, base);
- return expandName(file->ptr);
+ return expandPath(Strnew_m_charp(w3m_lib_dir(), "/", base, NULL)->ptr);
}
#endif
char *
etcFile(char *base)
{
- Str file = Strnew_charp(w3m_etc_dir());
- Strcat_char(file, '/');
- Strcat_charp(file, base);
- return expandName(file->ptr);
+ return expandPath(Strnew_m_charp(w3m_etc_dir(), "/", base, NULL)->ptr);
}
#ifndef USE_HELP_CGI
char *
helpFile(char *base)
{
- Str file = Strnew_charp(w3m_help_dir());
- Strcat_char(file, '/');
- Strcat_charp(file, base);
- return expandName(file->ptr);
+ return expandPath(Strnew_m_charp(w3m_help_dir(), "/", base, NULL)->ptr);
}
#endif
diff --git a/url.c b/url.c
@@ -154,7 +154,7 @@ loadMimeTypes(char *filename)
Str tmp;
struct table2 *mtypes;
- f = fopen(expandName(filename), "r");
+ f = fopen(expandPath(filename), "r");
if (f == NULL)
return NULL;
n = 0;
@@ -2103,7 +2103,7 @@ loadURIMethods(char *filename)
struct table2 *um;
char *up, *p;
- f = fopen(expandName(filename), "r");
+ f = fopen(expandPath(filename), "r");
if (f == NULL)
return NULL;
i = 0;