static struct mailcap * loadMailcap(char *filename) { FILE *f; int i, n; Str tmp; struct mailcap *mcap; f = fopen(expandPath(filename), "r"); if (f == NULL) return NULL; i = 0; while (tmp = Strfgets(f), tmp->length > 0) { if (tmp->ptr[0] != '#') i++; } fseek(f, 0, 0); n = i; mcap = New_N(struct mailcap, n + 1); i = 0; while (tmp = Strfgets(f), tmp->length > 0) { if (tmp->ptr[0] == '#') continue; redo: while (IS_SPACE(Strlastchar(tmp))) Strshrink(tmp, 1); if (Strlastchar(tmp) == '\\') { /* continuation */ Strshrink(tmp, 1); Strcat(tmp, Strfgets(f)); goto redo; } if (extractMailcapEntry(tmp->ptr, &mcap[i])) i++; } memset(&mcap[i], 0, sizeof(struct mailcap)); fclose(f); return mcap; }
Str StrmyISgets(InputStream stream) { BaseStream base; StreamBuffer sb; Str s = NULL; int i, len; if (stream == NULL) return '\0'; base = &stream->base; sb = &base->stream; while (!base->iseos) { if (MUST_BE_UPDATED(base)) { do_update(base); } else { if (s && Strlastchar(s) == '\r') { if (sb->buf[sb->cur] == '\n') Strcat_char(s, (char)sb->buf[sb->cur++]); return s; } for (i = sb->cur; i < sb->next && sb->buf[i] != '\n' && sb->buf[i] != '\r'; i++) ; if (i < sb->next) { len = i - sb->cur + 1; if (s == NULL) s = Strnew_size(len + MARGIN_STR_SIZE); Strcat_charp_n(s, (char *)&sb->buf[sb->cur], len); sb->cur = i + 1; if (sb->buf[i] == '\n') return s; } else { if (s == NULL) s = Strnew_size(sb->next - sb->cur + MARGIN_STR_SIZE); Strcat_charp_n(s, (char *)&sb->buf[sb->cur], sb->next - sb->cur); sb->cur = sb->next; } } } if (s == NULL) return Strnew(); return s; }
Str loadLocalDir(char *dname) { Str tmp; DIR *d; Directory *dir; struct stat st; char **flist; char *p, *qdir; Str fbuf = Strnew(); #ifdef HAVE_LSTAT struct stat lst; #ifdef HAVE_READLINK char lbuf[1024]; #endif /* HAVE_READLINK */ #endif /* HAVE_LSTAT */ int i, l, nrow = 0, n = 0, maxlen = 0; int nfile, nfile_max = 100; Str dirname; d = opendir(dname); if (d == NULL) return NULL; dirname = Strnew_charp(dname); if (Strlastchar(dirname) != '/') Strcat_char(dirname, '/'); qdir = html_quote(Str_conv_from_system(dirname)->ptr); /* FIXME: gettextize? */ tmp = Strnew_m_charp("<HTML>\n<HEAD>\n<BASE HREF=\"file://", html_quote(file_quote(dirname->ptr)), "\">\n<TITLE>Directory list of ", qdir, "</TITLE>\n</HEAD>\n<BODY>\n<H1>Directory list of ", qdir, "</H1>\n", NULL); flist = New_N(char *, nfile_max); nfile = 0; while ((dir = readdir(d)) != NULL) { flist[nfile++] = allocStr(dir->d_name, -1); if (nfile == nfile_max) { nfile_max *= 2; flist = New_Reuse(char *, flist, nfile_max); } if (multicolList) { l = strlen(dir->d_name); if (l > maxlen) maxlen = l; n++; } }
int add_cookie(ParsedURL *pu, Str name, Str value, time_t expires, Str domain, Str path, int flag, Str comment, int version, Str port, Str commentURL) { struct cookie *p; char *domainname = (version == 0) ? FQDN(pu->host) : pu->host; Str odomain = domain, opath = path; struct portlist *portlist = NULL; int use_security = !(flag & COO_OVERRIDE); #define COOKIE_ERROR(err) if(!((err) & COO_OVERRIDE_OK) || use_security) return (err) #ifdef DEBUG fprintf(stderr, "host: [%s, %s] %d\n", pu->host, pu->file, flag); fprintf(stderr, "cookie: [%s=%s]\n", name->ptr, value->ptr); fprintf(stderr, "expires: [%s]\n", asctime(gmtime(&expires))); if (domain) fprintf(stderr, "domain: [%s]\n", domain->ptr); if (path) fprintf(stderr, "path: [%s]\n", path->ptr); fprintf(stderr, "version: [%d]\n", version); if (port) fprintf(stderr, "port: [%s]\n", port->ptr); #endif /* DEBUG */ /* [RFC 2109] s. 4.3.2 case 2; but this (no request-host) shouldn't happen */ if (!domainname) return COO_ENODOT; if (domain) { char *dp; /* [DRAFT 12] s. 4.2.2 (does not apply in the case that * host name is the same as domain attribute for version 0 * cookie) * I think that this rule has almost the same effect as the * tail match of [NETSCAPE]. */ if (domain->ptr[0] != '.' && (version > 0 || strcasecmp(domainname, domain->ptr) != 0)) domain = Sprintf(".%s", domain->ptr); if (version == 0) { /* [NETSCAPE] rule */ int n = total_dot_number(domain->ptr, domain->ptr + domain->length, 3); if (n < 2) { if (! check_avoid_wrong_number_of_dots_domain(domain)) { COOKIE_ERROR(COO_ESPECIAL); } } else if (n == 2) { char **sdomain; int ok = 0; for (sdomain = special_domain; !ok && *sdomain; sdomain++) { int offset = domain->length - strlen(*sdomain); if (offset >= 0 && strcasecmp(*sdomain, &domain->ptr[offset]) == 0) ok = 1; } if (!ok && ! check_avoid_wrong_number_of_dots_domain(domain)) { COOKIE_ERROR(COO_ESPECIAL); } } } else { /* [DRAFT 12] s. 4.3.2 case 2 */ if (strcasecmp(domain->ptr, ".local") != 0 && contain_no_dots(&domain->ptr[1], &domain->ptr[domain->length])) COOKIE_ERROR(COO_ENODOT); } /* [RFC 2109] s. 4.3.2 case 3 */ if (!(dp = domain_match(domainname, domain->ptr))) COOKIE_ERROR(COO_EDOM); /* [RFC 2409] s. 4.3.2 case 4 */ /* Invariant: dp contains matched domain */ if (version > 0 && !contain_no_dots(domainname, dp)) COOKIE_ERROR(COO_EBADHOST); } if (path) { /* [RFC 2109] s. 4.3.2 case 1 */ if (version > 0 && strncmp(path->ptr, pu->file, path->length) != 0) COOKIE_ERROR(COO_EPATH); } if (port) { /* [DRAFT 12] s. 4.3.2 case 5 */ portlist = make_portlist(port); if (portlist && !port_match(portlist, pu->port)) COOKIE_ERROR(COO_EPORT); } if (!domain) domain = Strnew_charp(domainname); if (!path) { path = Strnew_charp(pu->file); while (path->length > 0 && Strlastchar(path) != '/') Strshrink(path, 1); if (Strlastchar(path) == '/') Strshrink(path, 1); } p = get_cookie_info(domain, path, name); if (!p) { p = New(struct cookie); p->flag = 0; if (default_use_cookie) p->flag |= COO_USE; p->next = First_cookie; First_cookie = p; }
Str loadFTPDir0(ParsedURL *pu) #endif { Str FTPDIRtmp; Str tmp; int status; volatile int sv_type; char *realpathname, *fn, *q; char **flist; int i, nfile, nfile_max; MySignalHandler(*volatile prevtrap) (SIGNAL_ARG) = NULL; #ifdef USE_M17N wc_ces doc_charset = DocumentCharset; *charset = WC_CES_US_ASCII; #endif if (current_ftp.data == NULL) return NULL; tmp = ftp_command(¤t_ftp, "SYST", NULL, &status); if (strstr(tmp->ptr, "UNIX") != NULL || !strncmp(tmp->ptr + 4, "Windows_NT", 10)) /* :-) */ sv_type = UNIXLIKE_SERVER; else sv_type = SERVER_NONE; if (pu->file == NULL || *pu->file == '\0') { if (sv_type == UNIXLIKE_SERVER) ftp_command(¤t_ftp, "LIST", NULL, &status); else ftp_command(¤t_ftp, "NLST", NULL, &status); pu->file = "/"; } else { realpathname = file_unquote(pu->file); if (*realpathname == '/' && *(realpathname + 1) == '~') realpathname++; if (sv_type == UNIXLIKE_SERVER) { ftp_command(¤t_ftp, "CWD", realpathname, &status); if (status == 250) ftp_command(¤t_ftp, "LIST", NULL, &status); } else ftp_command(¤t_ftp, "NLST", realpathname, &status); } if (status != 125 && status != 150) { fclose(current_ftp.data); current_ftp.data = NULL; return NULL; } tmp = parsedURL2Str(pu); if (Strlastchar(tmp) != '/') Strcat_char(tmp, '/'); fn = html_quote(tmp->ptr); tmp = convertLine(NULL, Strnew_charp(file_unquote(tmp->ptr)), RAW_MODE, charset, doc_charset); q = html_quote(tmp->ptr); FTPDIRtmp = Strnew_m_charp("<html>\n<head>\n<base href=\"", fn, "\">\n<title>", q, "</title>\n</head>\n<body>\n<h1>Index of ", q, "</h1>\n", NULL); if (SETJMP(AbortLoading) != 0) { if (sv_type == UNIXLIKE_SERVER) Strcat_charp(FTPDIRtmp, "</a></pre>\n"); else Strcat_charp(FTPDIRtmp, "</a></ul>\n"); Strcat_charp(FTPDIRtmp, "<p>Transfer Interrupted!\n"); goto ftp_end; } TRAP_ON; if (sv_type == UNIXLIKE_SERVER) Strcat_charp(FTPDIRtmp, "<pre>\n"); else Strcat_charp(FTPDIRtmp, "<ul>\n<li>"); Strcat_charp(FTPDIRtmp, "<a href=\"..\">[Upper Directory]</a>\n"); nfile_max = 100; flist = New_N(char *, nfile_max); nfile = 0; if (sv_type == UNIXLIKE_SERVER) { char *name, *link, *date, *size, *type_str; int ftype, max_len, len, j; max_len = 20; while (tmp = Strfgets(current_ftp.data), tmp->length > 0) { Strchop(tmp); if ((ftype = ex_ftpdir_name_size_date(tmp->ptr, &name, &link, &date, &size)) == FTPDIR_NONE) continue; if (!strcmp(".", name) || !strcmp("..", name)) continue; len = strlen(name); if (!len) continue; if (ftype == FTPDIR_DIR) { len++; type_str = "/"; } else if (ftype == FTPDIR_LINK) { len++; type_str = "@"; } else { type_str = " "; } if (max_len < len) max_len = len; flist[nfile++] = Sprintf("%s%s\n%s %5s%s", name, type_str, date, size, link)->ptr; if (nfile == nfile_max) { nfile_max *= 2; flist = New_Reuse(char *, flist, nfile_max); } }