aboutsummaryrefslogtreecommitdiffstats
path: root/local.c
diff options
context:
space:
mode:
Diffstat (limited to 'local.c')
-rw-r--r--local.c211
1 files changed, 108 insertions, 103 deletions
diff --git a/local.c b/local.c
index 0c2fbf5..7e39b77 100644
--- a/local.c
+++ b/local.c
@@ -1,4 +1,4 @@
-/* $Id: local.c,v 1.8 2001/11/21 18:51:48 ukai Exp $ */
+/* $Id: local.c,v 1.9 2001/11/24 02:01:26 ukai Exp $ */
#include "fm.h"
#include <string.h>
#include <stdio.h>
@@ -10,8 +10,8 @@
#include <unistd.h>
#endif /* HAVE_READLINK */
#ifdef __EMX__
-#include <limits.h> /* _MAX_PATH ? */
-#endif /* __EMX__ */
+#include <limits.h> /* _MAX_PATH ? */
+#endif /* __EMX__ */
#include "local.h"
#define CGIFN_NORMAL 0
@@ -27,9 +27,9 @@ setLocalCookie()
{
Str buf;
char hostname[256];
- gethostname(hostname,256);
+ gethostname(hostname, 256);
- buf = Sprintf("%d.%ld@%s",getpid(),lrand48(),hostname);
+ buf = Sprintf("%d.%ld@%s", getpid(), lrand48(), hostname);
Local_cookie = buf->ptr;
}
@@ -57,7 +57,10 @@ dirBuffer(char *dname)
return NULL;
dirname = Strnew_charp(dname);
qdir = html_quote(Str_conv_from_system(dirname)->ptr);
- tmp = Sprintf("<title>Directory list of %s</title><h1>Directory list of %s</h1>\n", qdir, qdir);
+ tmp =
+ Sprintf
+ ("<title>Directory list of %s</title><h1>Directory list of %s</h1>\n",
+ qdir, qdir);
flist = New_N(char *, nfile_max);
nfile = 0;
while ((dir = readdir(d)) != NULL) {
@@ -82,7 +85,7 @@ dirBuffer(char *dname)
n = 1;
Strcat_charp(tmp, "<TABLE CELLPADDING=0><TR VALIGN=TOP>\n");
}
- qsort((void *) flist, nfile, sizeof(char *), strCmp);
+ qsort((void *)flist, nfile, sizeof(char *), strCmp);
for (i = 0; i < nfile; i++) {
p = flist[i];
if (strcmp(p, ".") == 0)
@@ -91,7 +94,7 @@ dirBuffer(char *dname)
if (Strlastchar(fbuf) != '/')
Strcat_char(fbuf, '/');
Strcat_charp(fbuf, p);
-#ifdef HAVE_READLINK /* readlink == lstat() ? (ukai) */
+#ifdef HAVE_READLINK /* readlink == lstat() ? (ukai) */
if (lstat(fbuf->ptr, &lst) < 0)
continue;
#endif /* READLINK */
@@ -132,7 +135,8 @@ dirBuffer(char *dname)
if (S_ISLNK(lst.st_mode)) {
if ((l = readlink(fbuf->ptr, lbuf, sizeof(lbuf))) > 0) {
lbuf[l] = '\0';
- Strcat_m_charp(tmp, " -> ", html_quote(conv_from_system(lbuf)), NULL);
+ Strcat_m_charp(tmp, " -> ",
+ html_quote(conv_from_system(lbuf)), NULL);
if (S_ISDIR(st.st_mode))
Strcat_char(tmp, '/');
}
@@ -160,46 +164,43 @@ check_local_cgi(char *file, int status)
if (status & CGIFN_CONTAIN_SLASH) {
/* local CGI file must be just under /cgi-bin/
- or /$LIB/
- */
+ * or /$LIB/
+ */
return -1;
}
#ifdef __EMX__
if (CGIFN_MODE(status) != CGIFN_CGIBIN) {
char tmp[_MAX_PATH];
- int len;
+ int len;
- _abspath(tmp, w3m_lib_dir(), _MAX_PATH); /* Translate '\\' to '/'
- *
- */
- len = strlen(tmp);
- while (len > 1 && tmp[len-1] == '/')
- len--;
- if (strnicmp(file, tmp, len) || /* and ignore case */
- (file[len] != '/'))
+ _abspath(tmp, w3m_lib_dir(), _MAX_PATH); /* Translate '\\' to '/'
+ *
+ */
+ len = strlen(tmp);
+ while (len > 1 && tmp[len - 1] == '/')
+ len--;
+ if (strnicmp(file, tmp, len) || /* and ignore case */
+ (file[len] != '/'))
return -1;
}
#else /* not __EMX__ */
if (CGIFN_MODE(status) != CGIFN_CGIBIN) {
- char *tmp = Strnew_charp(w3m_lib_dir())->ptr;
- int len = strlen(tmp);
+ char *tmp = Strnew_charp(w3m_lib_dir())->ptr;
+ int len = strlen(tmp);
- while (len > 1 && tmp[len-1] == '/')
- len--;
- if (strncmp(file, tmp, len) ||
- (file[len] != '/'))
- /*
- * a local-CGI script should be located on either
- * /cgi-bin/ directory or LIB_DIR (typically /usr/local/lib/w3m).
- */
- return -1;
+ while (len > 1 && tmp[len - 1] == '/')
+ len--;
+ if (strncmp(file, tmp, len) || (file[len] != '/'))
+ /*
+ * a local-CGI script should be located on either
+ * /cgi-bin/ directory or LIB_DIR (typically /usr/local/lib/w3m).
+ */
+ return -1;
}
#endif /* not __EMX__ */
if (stat(file, &st) < 0)
return -1;
- if ((st.st_uid == geteuid() && (st.st_mode & S_IXUSR)) ||
- (st.st_gid == getegid() && (st.st_mode & S_IXGRP)) ||
- (st.st_mode & S_IXOTH)) { /* executable */
+ if ((st.st_uid == geteuid() && (st.st_mode & S_IXUSR)) || (st.st_gid == getegid() && (st.st_mode & S_IXGRP)) || (st.st_mode & S_IXOTH)) { /* executable */
return 0;
}
return -1;
@@ -209,8 +210,8 @@ void
set_environ(char *var, char *value)
{
#ifdef HAVE_SETENV
- if ( var != NULL && value != NULL )
- setenv(var, value, 1);
+ if (var != NULL && value != NULL)
+ setenv(var, value, 1);
#else /* not HAVE_SETENV */
#ifdef HAVE_PUTENV
Str tmp = Strnew_m_charp(var, "=", value, NULL);
@@ -241,10 +242,10 @@ set_environ(char *var, char *value)
}
}
}
- newenv = (char **) GC_malloc((i + 2) * sizeof(char *));
+ newenv = (char **)GC_malloc((i + 2) * sizeof(char *));
if (newenv == NULL)
return;
- for (e = environ, ne = newenv; *e != NULL; *(ne++) = *(e++));
+ for (e = environ, ne = newenv; *e != NULL; *(ne++) = *(e++)) ;
*(ne++) = p;
*ne = NULL;
environ = newenv;
@@ -266,7 +267,7 @@ set_cgi_environ(char *name, char *fn, char *req_uri)
set_environ("SCRIPT_NAME", name);
set_environ("SCRIPT_FILENAME", fn);
set_environ("REQUEST_URI", req_uri);
- set_environ("LOCAL_COOKIE",Local_cookie);
+ set_environ("LOCAL_COOKIE", Local_cookie);
}
static Str
@@ -296,7 +297,7 @@ cgi_filename(char *fn, int *status)
struct stat st;
if (cgi_bin != NULL && strncmp(fn, "/cgi-bin/", 9) == 0) {
*status = CGIFN_CGIBIN;
- if (strchr(fn+9,'/'))
+ if (strchr(fn + 9, '/'))
*status |= CGIFN_CONTAIN_SLASH;
tmp = checkPath(fn + 9, cgi_bin);
if (tmp == NULL)
@@ -307,7 +308,7 @@ cgi_filename(char *fn, int *status)
*status = CGIFN_NORMAL;
tmp = Strnew_charp(w3m_lib_dir());
fn += 5;
- if (strchr(fn+1,'/'))
+ if (strchr(fn + 1, '/'))
*status |= CGIFN_CONTAIN_SLASH;
if (Strlastchar(tmp) == '/')
fn++;
@@ -316,7 +317,7 @@ cgi_filename(char *fn, int *status)
}
if (*fn == '/' && document_root != NULL && stat(fn, &st) < 0) {
*status = CGIFN_DROOT;
- if (strchr(fn+1,'/'))
+ if (strchr(fn + 1, '/'))
*status |= CGIFN_CONTAIN_SLASH;
tmp = Strnew_charp(document_root);
if (Strlastchar(tmp) != '/')
@@ -329,55 +330,57 @@ cgi_filename(char *fn, int *status)
}
static pid_t
-localcgi_popen_r(FILE **p_fp)
+localcgi_popen_r(FILE ** p_fp)
{
- int fd[2];
- FILE *fp;
- pid_t pid;
- Str emsg;
-
- if (pipe(fd) < 0) {
- emsg = Sprintf("localcgi_popen_r: pipe: %s", strerror(errno));
- disp_err_message(emsg->ptr, FALSE);
- return (pid_t)-1;
- }
+ int fd[2];
+ FILE *fp;
+ pid_t pid;
+ Str emsg;
- flush_tty();
- if ((pid = fork()) < 0) {
- emsg = Sprintf("localcgi_popen_r: fork: %s", strerror(errno));
- disp_err_message(emsg->ptr, FALSE);
- close(fd[0]);
- close(fd[1]);
- return (pid_t)-1;
- }
- else if (!pid) {
- close_tty();
- dup2(fd[1], 1);
+ if (pipe(fd) < 0) {
+ emsg = Sprintf("localcgi_popen_r: pipe: %s", strerror(errno));
+ disp_err_message(emsg->ptr, FALSE);
+ return (pid_t) - 1;
+ }
- if (fd[1] > 1)
- close(fd[1]);
+ flush_tty();
+ if ((pid = fork()) < 0) {
+ emsg = Sprintf("localcgi_popen_r: fork: %s", strerror(errno));
+ disp_err_message(emsg->ptr, FALSE);
+ close(fd[0]);
+ close(fd[1]);
+ return (pid_t) - 1;
+ }
+ else if (!pid) {
+ close_tty();
+ dup2(fd[1], 1);
- close(fd[0]);
- }
- else {
- close(fd[1]);
+ if (fd[1] > 1)
+ close(fd[1]);
- if (!(fp = fdopen(fd[0], "r"))) {
- emsg = Sprintf("localcgi_popen_r: fdopen(%d, \"r\"): %s", fd[0], strerror(errno));
- disp_err_message(emsg->ptr, FALSE);
- kill(pid, SIGTERM);
- close(fd[0]);
- return (pid_t)-1;
+ close(fd[0]);
}
+ else {
+ close(fd[1]);
+
+ if (!(fp = fdopen(fd[0], "r"))) {
+ emsg =
+ Sprintf("localcgi_popen_r: fdopen(%d, \"r\"): %s", fd[0],
+ strerror(errno));
+ disp_err_message(emsg->ptr, FALSE);
+ kill(pid, SIGTERM);
+ close(fd[0]);
+ return (pid_t) - 1;
+ }
- *p_fp = fp;
- }
+ *p_fp = fp;
+ }
- return pid;
+ return pid;
}
FILE *
-localcgi_post(char *uri, char *qstr, FormList * request, char *referer)
+localcgi_post(char *uri, char *qstr, FormList *request, char *referer)
{
FILE *f, *f1;
Str tmp1;
@@ -391,27 +394,28 @@ localcgi_post(char *uri, char *qstr, FormList * request, char *referer)
tmp1 = tmpfname(TMPF_DFL, NULL);
f1 = fopen(tmp1->ptr, "w");
if (f1 == NULL)
- return NULL;
+ return NULL;
pushText(fileToDelete, tmp1->ptr);
if ((pid = localcgi_popen_r(&f))) {
- fclose(f1);
- return pid > 0 ? f : NULL;
+ fclose(f1);
+ return pid > 0 ? f : NULL;
}
if (qstr == NULL) {
- set_cgi_environ(uri, file, uri);
- } else {
- set_cgi_environ(uri, file,
- Strnew_m_charp(uri, "?", qstr, NULL)->ptr);
+ set_cgi_environ(uri, file, uri);
+ }
+ else {
+ set_cgi_environ(uri, file, Strnew_m_charp(uri, "?", qstr, NULL)->ptr);
}
set_environ("REQUEST_METHOD", "POST");
if (qstr)
- set_environ("QUERY_STRING", qstr);
+ set_environ("QUERY_STRING", qstr);
set_environ("CONTENT_LENGTH", Sprintf("%d", request->length)->ptr);
if (referer && referer != NO_REFERER)
- set_environ("HTTP_REFERER",referer);
+ set_environ("HTTP_REFERER", referer);
if (request->enctype == FORM_ENCTYPE_MULTIPART) {
set_environ("CONTENT_TYPE",
- Sprintf("multipart/form-data; boundary=%s", request->boundary)->ptr);
+ Sprintf("multipart/form-data; boundary=%s",
+ request->boundary)->ptr);
}
else {
set_environ("CONTENT_TYPE", "application/x-www-form-urlencoded");
@@ -430,13 +434,13 @@ localcgi_post(char *uri, char *qstr, FormList * request, char *referer)
fputs(request->body, f1);
}
fclose(f1);
- freopen( tmp1->ptr, "r", stdin);
-#ifdef HAVE_CHDIR /* ifndef __EMX__ ? */
+ freopen(tmp1->ptr, "r", stdin);
+#ifdef HAVE_CHDIR /* ifndef __EMX__ ? */
chdir(mydirname(file));
#endif
execl(file, mybasename(file), NULL);
fprintf(stderr, "execl(\"%s\", \"%s\", NULL): %s\n",
- file, mybasename(file), strerror(errno));
+ file, mybasename(file), strerror(errno));
exit(1);
return NULL;
}
@@ -448,31 +452,32 @@ localcgi_get(char *uri, char *request, char *referer)
int status;
pid_t pid;
char *file;
-
+
file = cgi_filename(uri, &status);
if (check_local_cgi(file, status) < 0)
return NULL;
if ((pid = localcgi_popen_r(&f)) < 0)
- return NULL;
+ return NULL;
else if (pid)
- return f;
+ return f;
if (!strcmp(request, "")) {
- set_cgi_environ(Strnew_charp(uri)->ptr, file, Strnew_charp(uri)->ptr);
- } else {
- set_cgi_environ(Strnew_charp(uri)->ptr, file,
- Strnew_m_charp(uri, "?", request, NULL)->ptr);
+ set_cgi_environ(Strnew_charp(uri)->ptr, file, Strnew_charp(uri)->ptr);
+ }
+ else {
+ set_cgi_environ(Strnew_charp(uri)->ptr, file,
+ Strnew_m_charp(uri, "?", request, NULL)->ptr);
}
if (referer && referer != NO_REFERER)
- set_environ("HTTP_REFERER",referer);
+ set_environ("HTTP_REFERER", referer);
set_environ("REQUEST_METHOD", "GET");
set_environ("QUERY_STRING", request);
freopen(DEV_NULL_PATH, "r", stdin);
-#ifdef HAVE_CHDIR /* ifndef __EMX__? */
+#ifdef HAVE_CHDIR /* ifndef __EMX__? */
chdir(mydirname(file));
#endif
execl(file, mybasename(file), NULL);
fprintf(stderr, "execl(\"%s\", \"%s\", NULL): %s\n",
- file, mybasename(file), strerror(errno));
+ file, mybasename(file), strerror(errno));
exit(1);
return NULL;
}