int readSampleClientes(MainTreePt *clientes, int (*comparaClientes[DIM])(void*,void*)){ FILE *cfile = fopen("utilizadores.txt", "r"); char fstr[2][63]; unsigned int nif; char *str[2]; *clientes = tree_new( comparaClientes ); while( fscanf(cfile, "%u:%[^:]:%[^\n]\n", &nif, fstr[0], fstr[1]) > 0){ str[0] = allocStr(fstr[0]); str[1] = allocStr(fstr[1]); tree_insert( *clientes, cliente_novo( nif, str[0], str[1], criaListaLigada( cliente_comparaServico ) )); } return 1; }
void extractMailcapEntry(char *mcap_entry, char **type, char **cmd) { int j; while (*mcap_entry && IS_SPACE(*mcap_entry)) mcap_entry++; for (j = 0; mcap_entry[j] && mcap_entry[j] != ';' && !IS_SPACE(mcap_entry[j]); j++) ; *type = allocStr(mcap_entry, j); if (mcap_entry[j] == ';') j++; while (mcap_entry[j] && IS_SPACE(mcap_entry[j])) j++; *cmd = allocStr(&mcap_entry[j], -1); }
StringRefM::StringRefM(int size) : begin(nullptr) , end(nullptr) { if (size > 0) { auto* p = allocStr(size + 1); // extra byte for null termination; begin = p; end = begin + size; p[size] = 0; } }
int readSampleLocalidades(TabelaHashPTR *table, int (*comparaLocalidades)(void*,void*), int(*hash_function)(void*,int)){ FILE *local = fopen("local.txt", "r"); FILE *liga = fopen("ligacoes.txt", "r"); char *str[2]; char fstr[2][106]; double custo, km; *table = criaTabelaHash(hash_function, 10000, comparaLocalidades); while( fscanf(local,"%[^\n]\n", fstr[0] ) > 0){ str[0] = allocStr(fstr[0]); inserelocalidade( *table, str[0] ); } while( fscanf(liga, "%[^:]:%[^:]:%lg:%lg\n", fstr[0], fstr[1], &km, &custo) > 0 ){ str[0] = allocStr(fstr[0]); str[1] = allocStr(fstr[1]); inserirligacao(*table, str[0], str[1], custo, km); } return 1; }
int readSampleCamioes(MainTreePt *camioes, int (*comparaCamioes[DIM])(void*,void*)){ int i=0; char matricula[10][20] = { "00-00-00", "AA-01-01", "BB-02-02", "CC-03-03", "DD-04-04", "EE-05-05", "FF-06-06", "GG-07-07", "HH-08-08", "II-09-09" }; char local[10][50] = { "Taipa", "Praia de Porto Chão", "Ponta do Fogo", "Póvoa da Pégada", "Alto do Vale de Sardão", "Vale de Mouros", "Sabacheira", "Costa do Valado", "Estreito", "Benavila" }; *camioes = tree_new( comparaCamioes ); for( i=0; i<10; i++){ tree_insert(*camioes, camiao_novo(i, allocStr(matricula[i]), i*0.1, i*10, allocStr(local[i]))); } return 1; }
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++; } }
static char * name_from_address(char *str, int n) { char *s, *p; int l; bool space = true; s = allocStr(str, -1); SKIP_BLANKS(s); if (*s == '<' && (p = strchr(s, '>'))) { *p++ = '\0'; SKIP_BLANKS(p); if (*p == '\0') /* <address> */ s++; else /* <address> name ? */ s = p; } else if ((p = strchr(s, '<'))) /* name <address> */ *p = '\0'; else if ((p = strchr(s, '('))) /* address (name) */ s = p; if (*s == '"' && (p = strchr(s + 1, '"'))) { /* "name" */ *p = '\0'; s++; } else if (*s == '(' && (p = strchr(s + 1, ')'))) { /* (name) */ *p = '\0'; s++; } for (p = s, l = 0; *p; p += get_mclen(p)) { if (IS_SPACE(*p)) { if (space) continue; space = true; } else space = false; l += get_mcwidth(p); if (l > n) break; } *p = '\0'; return s; }
char * acceptableMimeTypes() { static Str types = NULL; TextList *l; Hash_si *mhash; char *p; int i; if (types != NULL) return types->ptr; /* generate acceptable media types */ l = newTextList(); mhash = newHash_si(16); /* XXX */ /* pushText(l, "text"); */ putHash_si(mhash, "text", 1); pushText(l, "image"); putHash_si(mhash, "image", 1); for (i = 0; i < mailcap_list->nitem; i++) { struct mailcap *mp = UserMailcap[i]; char *mt; if (mp == NULL) continue; for (; mp->type; mp++) { p = strchr(mp->type, '/'); if (p == NULL) continue; mt = allocStr(mp->type, p - mp->type); if (getHash_si(mhash, mt, 0) == 0) { pushText(l, mt); putHash_si(mhash, mt, 1); } } } types = Strnew(); Strcat_charp(types, "text/html, text/*;q=0.5"); while ((p = popText(l)) != NULL) { Strcat_charp(types, ", "); Strcat_charp(types, p); Strcat_charp(types, "/*"); } return types->ptr; }
int lerStr(char **ptr){ char strBuffer[1024]; int i=0, excedeu=1; do{ strBuffer[i] = getchar(); i++; }while( strBuffer[i-1] != '\0' && strBuffer[i-1] != '\n' && i < 1023 ); strBuffer[i-1] = '\0'; if( i == 1023 ){ clearInputBuffer(); excedeu=-1; } if( (*ptr = allocStr(strBuffer)) == NULL ) return 0; else return excedeu; }
static int parse_ansi_color(char **str, Lineprop *effect, Linecolor *color) { char *p = *str, *q; Lineprop e = *effect; Linecolor c = *color; int i; if (*p != ESC_CODE || *(p + 1) != '[') return 0; p += 2; for (q = p; IS_DIGIT(*q) || *q == ';'; q++) ; if (*q != 'm') return 0; *str = q + 1; while (1) { if (*p == 'm') { e = PE_NORMAL; c = 0; break; } if (IS_DIGIT(*p)) { q = p; for (p++; IS_DIGIT(*p); p++) ; i = atoi(allocStr(q, p - q)); switch (i) { case 0: e = PE_NORMAL; c = 0; break; case 1: case 5: e = PE_BOLD; break; case 4: e = PE_UNDER; break; case 7: e = PE_STAND; break; case 100: /* for EWS4800 kterm */ c = 0; break; case 39: c &= 0xf0; break; case 49: c &= 0x0f; break; default: if (i >= 30 && i <= 37) c = (c & 0xf0) | (i - 30) | 0x08; else if (i >= 40 && i <= 47) c = (c & 0x0f) | ((i - 40) << 4) | 0x80; break; } if (*p == 'm') break; } else { e = PE_NORMAL; c = 0; break; } p++; /* *p == ';' */ } *effect = e; *color = c; return 1; }
Str loadNewsgroup0(ParsedURL *pu) #endif { volatile Str page; Str tmp; URLFile f; Buffer *buf; char *qgroup, *p, *q, *s, *t, *n; char *volatile scheme, *volatile group, *volatile list; int status, i, first, last; volatile int flag = 0, start = 0, end = 0; MySignalHandler(*volatile prevtrap) (SIGNAL_ARG) = NULL; #ifdef USE_M17N wc_ces doc_charset = DocumentCharset, mime_charset; *charset = WC_CES_US_ASCII; #endif if (current_news.host == NULL || !pu->file || *pu->file == '\0') return NULL; group = allocStr(pu->file, -1); if (pu->scheme == SCM_NNTP_GROUP) scheme = "/"; else scheme = "news:"; if ((list = strchr(group, '/'))) { /* <newsgroup>/<start-number>-<end-number> */ *list++ = '\0'; } if (fmInitialized) { message(Sprintf("Reading newsgroup %s...", group)->ptr, 0, 0); refresh(); } qgroup = html_quote(group); group = file_unquote(group); page = Strnew_m_charp("<html>\n<head>\n<base href=\"", parsedURL2Str(pu)->ptr, "\">\n<title>Newsgroup: ", qgroup, "</title>\n</head>\n<body>\n<h1>Newsgroup: ", qgroup, "</h1>\n<hr>\n", NULL); if (SETJMP(AbortLoading) != 0) { news_close(¤t_news); Strcat_charp(page, "</table>\n<p>Transfer Interrupted!\n"); goto news_end; } TRAP_ON; tmp = news_command(¤t_news, "GROUP", group, &status); if (status != 211) goto news_list; if (sscanf(tmp->ptr, "%d %d %d %d", &status, &i, &first, &last) != 4) goto news_list; if (list && *list) { if ((p = strchr(list, '-'))) { *p++ = '\0'; end = atoi(p); } start = atoi(list); if (start > 0) { if (start < first) start = first; if (end <= 0) end = start + MaxNewsMessage - 1; } } if (start <= 0) { start = first; end = last; if (end - start > MaxNewsMessage - 1) start = end - MaxNewsMessage + 1; } page = Sprintf("<html>\n<head>\n<base href=\"%s\">\n\ <title>Newsgroup: %s %d-%d</title>\n\ </head>\n<body>\n<h1>Newsgroup: %s %d-%d</h1>\n<hr>\n", parsedURL2Str(pu)->ptr, qgroup, start, end, qgroup, start, end); if (start > first) { i = start - MaxNewsMessage; if (i < first) i = first; Strcat(page, Sprintf("<a href=\"%s%s/%d-%d\">[%d-%d]</a>\n", scheme, qgroup, i, start - 1, i, start - 1)); } Strcat_charp(page, "<table>\n"); news_command(¤t_news, "XOVER", Sprintf("%d-%d", start, end)->ptr, &status); if (status == 224) { f.scheme = SCM_NEWS; while (true) { tmp = StrISgets(current_news.rf); if (NEWS_ENDLINE(tmp->ptr)) break; if (sscanf(tmp->ptr, "%d", &i) != 1) continue; if (!(s = strchr(tmp->ptr, '\t'))) continue; s++; if (!(n = strchr(s, '\t'))) continue; *n++ = '\0'; if (!(t = strchr(n, '\t'))) continue; *t++ = '\0'; if (!(p = strchr(t, '\t'))) continue; *p++ = '\0'; if (*p == '<') p++; if (!(q = strchr(p, '>')) && !(q = strchr(p, '\t'))) continue; *q = '\0'; tmp = decodeMIME(Strnew_charp(s), &mime_charset); s = convertLine(&f, tmp, HEADER_MODE, mime_charset ? &mime_charset : charset, mime_charset ? mime_charset : doc_charset)->ptr; tmp = decodeMIME(Strnew_charp(n), &mime_charset); n = convertLine(&f, tmp, HEADER_MODE, mime_charset ? &mime_charset : charset, mime_charset ? mime_charset : doc_charset)->ptr; add_news_message(page, i, t, n, s, p, scheme, pu->scheme == SCM_NNTP_GROUP ? qgroup : NULL); } } else { init_stream(&f, SCM_NEWS, current_news.rf); buf = newBuffer(INIT_BUFFER_WIDTH); for (i = start; i <= end && i <= last; i++) { news_command(¤t_news, "HEAD", Sprintf("%d", i)->ptr, &status); if (status != 221) continue; readHeader(&f, buf, FALSE, NULL); if (!(p = checkHeader(buf, "Message-ID:"))) continue; if (*p == '<') p++; if (!(q = strchr(p, '>')) && !(q = strchr(p, '\t'))) *q = '\0'; if (!(s = checkHeader(buf, "Subject:"))) continue; if (!(n = checkHeader(buf, "From:"))) continue; if (!(t = checkHeader(buf, "Date:"))) continue; add_news_message(page, i, t, n, s, p, scheme, pu->scheme == SCM_NNTP_GROUP ? qgroup : NULL); } } Strcat_charp(page, "</table>\n"); if (end < last) { i = end + MaxNewsMessage; if (i > last) i = last; Strcat(page, Sprintf("<a href=\"%s%s/%d-%d\">[%d-%d]</a>\n", scheme, qgroup, end + 1, i, end + 1, i)); } flag = 1; news_list: tmp = Sprintf("ACTIVE %s", group); if (!strchr(group, '*')) Strcat_charp(tmp, ".*"); news_command(¤t_news, "LIST", tmp->ptr, &status); if (status != 215) goto news_end; while (true) { tmp = StrISgets(current_news.rf); if (NEWS_ENDLINE(tmp->ptr)) break; if (flag < 2) { if (flag == 1) Strcat_charp(page, "<hr>\n"); Strcat_charp(page, "<table>\n"); flag = 2; } p = tmp->ptr; for (q = p; *q && !IS_SPACE(*q); q++) ; *(q++) = '\0'; if (sscanf(q, "%d %d", &last, &first) == 2 && last >= first) i = last - first + 1; else i = 0; Strcat(page, Sprintf ("<tr><td align=right>%d<td><a href=\"%s%s\">%s</a>\n", i, scheme, html_quote(file_quote(p)), html_quote(p))); } if (flag == 2) Strcat_charp(page, "</table>\n"); news_end: Strcat_charp(page, "</body>\n</html>\n"); TRAP_OFF; return page; }
InputStream openNewsStream(ParsedURL *pu) { char *host, *mode, *group, *p; Str tmp; int port, status; if (pu->file == NULL || *pu->file == '\0') return NULL; if (pu->scheme == SCM_NNTP || pu->scheme == SCM_NNTP_GROUP) host = pu->host; else host = NNTP_server; if (!host || *host == '\0') { if (current_news.host) news_quit(¤t_news); return NULL; } if (pu->scheme != SCM_NNTP && pu->scheme != SCM_NNTP_GROUP && (p = strchr(host, ':'))) { host = allocStr(host, p - host); port = atoi(p + 1); } else port = pu->port; if (NNTP_mode && *NNTP_mode) mode = NNTP_mode; else mode = NULL; if (current_news.host) { if (!strcmp(current_news.host, host) && current_news.port == port) { tmp = news_command(¤t_news, "MODE", mode ? mode : "READER", &status); if (status != 200 && status != 201) news_close(¤t_news); } else news_quit(¤t_news); } if (!current_news.host) { current_news.host = allocStr(host, -1); current_news.port = port; current_news.mode = mode ? allocStr(mode, -1) : NULL; if (!news_open(¤t_news)) return NULL; } if (pu->scheme == SCM_NNTP || pu->scheme == SCM_NEWS) { /* News article */ group = file_unquote(allocStr(pu->file, -1)); p = strchr(group, '/'); if (p == NULL) { /* <message-id> */ if (!strchr(group, '@')) return NULL; p = group; } else { /* <newsgroup>/<message-id or article-number> */ *p++ = '\0'; news_command(¤t_news, "GROUP", group, &status); if (status != 211) return NULL; } if (strchr(p, '@')) /* <message-id> */ news_command(¤t_news, "ARTICLE", Sprintf("<%s>", p)->ptr, &status); else /* <article-number> */ news_command(¤t_news, "ARTICLE", p, &status); if (status != 220) return NULL; return current_news.rf; } return NULL; }
static int extractMailcapEntry(char *mcap_entry, struct mailcap *mcap) { int j, k; char *p; bool quoted = false; Str tmp; memset(mcap, 0, sizeof(struct mailcap)); p = mcap_entry; SKIP_BLANKS(p); k = -1; for (j = 0; p[j] && p[j] != ';'; j++) { if (!IS_SPACE(p[j])) k = j; } mcap->type = allocStr(p, (k >= 0) ? k + 1 : j); if (!p[j]) return 0; p += j + 1; SKIP_BLANKS(p); k = -1; for (j = 0; p[j] && (quoted || p[j] != ';'); j++) { if (quoted || !IS_SPACE(p[j])) k = j; if (quoted) quoted = false; else if (p[j] == '\\') quoted = true; } mcap->viewer = allocStr(p, (k >= 0) ? k + 1 : j); p += j; while (*p == ';') { p++; SKIP_BLANKS(p); if (matchMailcapAttr(p, "needsterminal", 13, NULL)) { mcap->flags |= MAILCAP_NEEDSTERMINAL; } else if (matchMailcapAttr(p, "copiousoutput", 13, NULL)) { mcap->flags |= MAILCAP_COPIOUSOUTPUT; } else if (matchMailcapAttr(p, "x-htmloutput", 12, NULL) || matchMailcapAttr(p, "htmloutput", 10, NULL)) { mcap->flags |= MAILCAP_HTMLOUTPUT; } else if (matchMailcapAttr(p, "test", 4, &tmp)) { mcap->test = allocStr(tmp->ptr, tmp->length); } else if (matchMailcapAttr(p, "nametemplate", 12, &tmp)) { mcap->nametemplate = allocStr(tmp->ptr, tmp->length); } else if (matchMailcapAttr(p, "edit", 4, &tmp)) { mcap->edit = allocStr(tmp->ptr, tmp->length); } quoted = false; while (*p && (quoted || *p != ';')) { if (quoted) quoted = false; else if (*p == '\\') quoted = true; p++; } } return 1; }
InputStream openFTPStream(ParsedURL *pu, URLFile *uf) { Str tmp; int status; char *user = NULL; char *pass = NULL; Str uname = NULL; Str pwd = NULL; int add_auth_cookie_flag = FALSE; char *realpathname = NULL; if (!pu->host) return NULL; if (pu->user == NULL && pu->pass == NULL) { if (find_auth_user_passwd(pu, NULL, &uname, &pwd, 0)) { if (uname) user = uname->ptr; if (pwd) pass = pwd->ptr; } } if (user) /* do nothing */ ; else if (pu->user) user = pu->user; else user = "******"; if (current_ftp.host) { if (!strcmp(current_ftp.host, pu->host) && current_ftp.port == pu->port && !strcmp(current_ftp.user, user)) { ftp_command(¤t_ftp, "NOOP", NULL, &status); if (status != 200) ftp_close(¤t_ftp); else goto ftp_read; } else ftp_quit(¤t_ftp); } if (pass) /* do nothing */ ; else if (pu->pass) pass = pu->pass; else if (pu->user) { pwd = NULL; find_auth_user_passwd(pu, NULL, &uname, &pwd, 0); if (pwd == NULL) { if (fmInitialized) { term_raw(); pwd = Strnew_charp(inputLine("Password: "******"Password: "******"Password: "******"anonymous"); #else tmp = Strnew_charp("anonymous"); #endif /* __MINGW32_VERSION */ Strcat_char(tmp, '@'); pass = tmp->ptr; } if (!current_ftp.host) { current_ftp.host = allocStr(pu->host, -1); current_ftp.port = pu->port; current_ftp.user = allocStr(user, -1); current_ftp.pass = allocStr(pass, -1); if (!ftp_login(¤t_ftp)) return NULL; } if (add_auth_cookie_flag) add_auth_user_passwd(pu, NULL, uname, pwd, 0); ftp_read: ftp_command(¤t_ftp, "TYPE", "I", &status); if (ftp_pasv(¤t_ftp) < 0) { ftp_quit(¤t_ftp); return NULL; } if (pu->file == NULL || *pu->file == '\0' || pu->file[strlen(pu->file) - 1] == '/') goto ftp_dir; realpathname = file_unquote(pu->file); if (*realpathname == '/' && *(realpathname + 1) == '~') realpathname++; /* Get file */ uf->modtime = ftp_modtime(¤t_ftp, realpathname); ftp_command(¤t_ftp, "RETR", realpathname, &status); if (status == 125 || status == 150) return newFileStream(current_ftp.data, (void (*)())closeFTPdata); ftp_dir: pu->scheme = SCM_FTPDIR; return NULL; }