http programm
-
INERT schrieb:
ein code snippet, das es an google schickt und das response in einer datei speichert, würde mich sehr glücklich machen

struct tCookie { char *pcname; char *pcvalue; char *pcpath; char *pcdomain; }; struct tb { char *pc; int cnt; }; struct tE { char *pcpath; char *pchost; char *pcsend; char *pcpost; struct tb recv; char *pchtml; char *pclocation; struct tCookie *Cookie; int CookieAnzahl; }; static void MakeLoginCplusplusPost(struct tE *e, char *username, char *password) { e->pcpost = realloc(e->pcpost, SIZEOF0); e->pcpost[0] = 0; StringCat(&e->pcpost, "username=%s", username); StringCat(&e->pcpost, "&password=%s", password); StringCat(&e->pcpost, "&login=Login"); } static int cplusplus(struct tE *pe) { int ifehler; char *url = "http://www.c-plusplus.net/forum/login-var-.html"; a_UrlUrlToHostPath(url, pe); printf("%s%s\n", pe->pchost, pe->pcpath); MakeLoginCplusplusPost(pe, "<username>", "<password>"); MakeSendHeader(pe, pe->pcpost); if ((ifehler = DoDownLoad(pe)) == 0) ifehler = Loop302(pe); printf("ifehler %i\n", ifehler); return ifehler; } static int google(struct tE *pe) { char *url = "http://www.google.de"; int ifehler; a_UrlUrlToHostPath(url, pe); printf("%s%s\n", pe->pchost, pe->pcpath); MakeSendHeader(pe, HEADERTYPE_GET); if ((ifehler = DoDownLoad(pe)) == 0) ifehler = Loop302(pe); printf("ifehler %i\n", ifehler); return ifehler; } static int google2(struct tE *pe) { int ifehler; char *url = "http://www.google.de" "/search?hl=de" "&q=Schweine%20im%20Weltall" "&btnG=Google-Suche" "&meta="; a_UrlUrlToHostPath(url, pe); printf("%s%s\n", pe->pchost, pe->pcpath); MakeSendHeader(pe, HEADERTYPE_GET); if ((ifehler = DoDownLoad(pe)) == 0) ifehler = Loop302(pe); printf("ifehler %i\n", ifehler); return ifehler; } int main() { WSADATA ws; struct tE *pe; WSAStartup(0x101, &ws); pe = calloc(1, sizeof(struct tE)); cplusplus(pe); /*google2(pe);*/ /*google(pe);*/ /*freenet(pe);*/ FreeVars(pe); WSACleanup(); printf("fertig"); getch(); return 0; }
-
Vielen Dank euch allen.
Ich werde mir das mal anschauen

-
keksekekse schrieb:
INERT schrieb:
ein code snippet, das es an google schickt und das response in einer datei speichert, würde mich sehr glücklich machen

/* etwas Code */hi keksekekse.
wieso sind da soviele return values static?
überhaupt komm ich bei C89 bzw. C99 mit dem static immer durcheinander, wieso das z.B. wie hier für return values benutzt wird. mir ist ja klar, dass es, wenn es in einem functionbody steht, dass es heißt, dass der wert zwischen den aufrufen erhalten bleibt. aber hier erschließt sich mir nicht ganz der sinn und nutzen, wenn so viele variablen und vorallem die return values static sind. habe das komischerweiße auch in sehr vielen selfmade-httpds gesehen. kannst du das bitte näher erläutern? vielleicht hats ja auch was mit der netzwerkprogrammierung an sich zutun.
-
static int a; /* Auf das Modul beschränkte Variable = 0 */ static void b(void) /* Auf das Modul beschränkte Funktion */ { int c; /* Flüchtige Variable auf dem Stack */ static int d; /* Statische Variable im Datenbereich = 0 */ static int e = 8888; /* Statische Variable im Datenbereich = 8888 */ e++; /* Nach jedem Aufruf von b() ist e um 1 größer */ } /* <- Zeile 10 */ int main() { return 0; } /* <- Zeile 16 */Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland src\main.c: Warning W8004 src\main.c 10: 'd' is assigned a value that is never used in function b Warning W8080 src\main.c 10: 'c' is declared but never used in function b Warning W8080 src\main.c 16: 'a' is declared but never used Warning W8080 src\main.c 16: 'b()' is declared but never used
-
keksekekse schrieb:
INERT schrieb:
ein code snippet, das es an google schickt und das response in einer datei speichert, würde mich sehr glücklich machen

selfmade wgetfehlt da nicht die hälfte?
-
also kann ich durch static nameclashes verhindern und ich kann verhindern, dass jemand von außerhalb auf die funktion zugreift. hat es auch einen optimierungstechnischen grund bzw. wird es gerade wegen den aufgezählten gründen vom compiler optimiert?
-
Der Grund ist, daß der *Compiler* static-Variablennamen und static-Funkionsnamen in die *.obj-Datei nicht oder nicht als gültige Bezeichner einträgt, d.h. der *Linker* kommt dadurch nicht in die Verlegenheit, sich zwischen zwei gleichen Namen mit unterschiedlicher Bedeutung entscheiden zu müssen.
-
vista schrieb:
fehlt da nicht die hälfte?
mal sehen, wieviel Code das Forum pro Post verträgt...
main.c Teil 1:
#include <windows.h> #include <conio.h> #include <stdio.h> #include "globals.h" #include "cookie.h" #include "url.h" #define SENDNOSOCKET 1 #define SENDNOCONNECT 2 #define SENDNOSEND 3 #define SENDNOTCOMPLETE 4 #define RECVERRRECV 5 #define RECVCLOSED 6 #define NOHOST 7 #define OUTNOTWRITE 8 #define HTTPERR0 9 #define HTTPERR1 10 #define HTTPERR2 11 #define HTTPERR3 12 #define HTTPERR4 13 static int CopyRecvToHtml(struct tE *e) { char *pc; int inewblkcnt, ihtmllen; e->pchtml = realloc(e->pchtml, SIZEOF0); e->pchtml[0] = 0; pc = strstr(e->recv.pc, "\r\n\r\n"); if (pc == 0) return HTTPERR0; pc += 2; ihtmllen = 0; while(1) { if (strncmp(pc, "\r\n", 2) != 0) return HTTPERR1; pc += 2; inewblkcnt = strtoul(pc, 0, 0x10); pc = strchr(pc, '\r'); if (pc == 0) return HTTPERR2; if (inewblkcnt == 0 && strcmp(pc, "\r\n\r\n") == 0) break; /* last block ending ok "\r\n0\r\n\r\n" */ if (inewblkcnt == 0) return HTTPERR3; if (strncmp(pc, "\r\n", 2) != 0) return HTTPERR4; pc += 2; e->pchtml = realloc(e->pchtml, ihtmllen+inewblkcnt+SIZEOF0); memmove(e->pchtml+ihtmllen, pc, inewblkcnt); e->pchtml[ihtmllen+inewblkcnt] = 0; ihtmllen += inewblkcnt; pc += inewblkcnt; } return 0; } static int filecnt; static void WriteToFile(int ifnum, char *pcinouterr, char *pcext, char *s, int slen) { char fname[80]; FILE *fp; _snprintf(fname, sizeof(fname), "f%i%s.%s", ifnum, pcinouterr, pcext); if ((fp = fopen(fname, "wb")) == 0) return; fwrite(s, 1, slen, fp); fclose(fp); } static int DnLoad(long host, short port, struct tE *e) { SOCKET s; struct sockaddr_in A; int iretconn, iretsend, isendbytes; static char pctmpbuf[2000]; int itmpbufcnt; long content_length; long content_offset; char *tmpptr; s = socket(AF_INET, SOCK_STREAM, 0); if (s == INVALID_SOCKET) return SENDNOSOCKET; A.sin_family = AF_INET; A.sin_port = htons(port); A.sin_addr.s_addr = host; iretconn = connect(s, (struct sockaddr *)&A, sizeof(A)); if (iretconn == SOCKET_ERROR) { closesocket(s); return SENDNOCONNECT; } isendbytes = strlen(e->pcsend); iretsend = send(s, e->pcsend, isendbytes, 0); if (iretsend == SOCKET_ERROR) { closesocket(s); return SENDNOSEND; } if (iretsend < isendbytes) { closesocket(s); return SENDNOTCOMPLETE; } content_offset = 0; content_length = -1; e->recv.pc = realloc(e->recv.pc, SIZEOF0); e->recv.pc[0] = 0; e->recv.cnt = 0; while(1) { itmpbufcnt = recv(s, pctmpbuf, sizeof(pctmpbuf), 0); if (itmpbufcnt == SOCKET_ERROR) { closesocket(s); return RECVERRRECV; } if (itmpbufcnt == 0) { closesocket(s); return RECVCLOSED; } e->recv.pc = realloc(e->recv.pc, e->recv.cnt+itmpbufcnt+SIZEOF0); memmove(e->recv.pc+e->recv.cnt, pctmpbuf, itmpbufcnt); e->recv.pc[e->recv.cnt+itmpbufcnt] = 0; e->recv.cnt += itmpbufcnt; if (content_length == -1 && e->recv.cnt >= 17) if ((tmpptr = strstr(e->recv.pc, "Content-length: ")) != 0 || (tmpptr = strstr(e->recv.pc, "Content-Length: ")) != 0) content_length = strtoul(tmpptr+16, 0, 10); if (!content_offset && e->recv.cnt >= 4) if ((tmpptr = strstr(e->recv.pc, "\r\n\r\n")) != 0) content_offset = (tmpptr-e->recv.pc)+4; if (content_offset && content_length != -1) if (e->recv.cnt >= content_offset + content_length) break; if (content_length == -1 && e->recv.cnt >= 5 && strcmp(e->recv.pc+e->recv.cnt-5, "\r\n\r\n ") == 0) break; if (content_length == -1 && e->recv.cnt >= 7 && strcmp(e->recv.pc+e->recv.cnt-7, "\r\n0\r\n\r\n") == 0) { closesocket(s); return CopyRecvToHtml(e); } } closesocket(s); return 0; } static int DownLoad(struct tE *e) { HOSTENT *he; if ((he = gethostbyname(e->pchost)) == 0) return NOHOST; return DnLoad(*((long *)he->h_addr_list[0]), 80, e); } static int DoDownLoad(struct tE *e) { WriteToFile(filecnt++, "in", "txt", e->pcsend, strlen(e->pcsend)); return DownLoad(e); } static void FreeVars(struct tE *e) { int i; free(e->pcpath); free(e->pchost); free(e->pcsend); free(e->pcpost); free(e->recv.pc); free(e->pchtml); for (i = 0; i < e->CookieAnzahl; i++) { free(e->Cookie[i].pcname); free(e->Cookie[i].pcvalue); free(e->Cookie[i].pcpath); free(e->Cookie[i].pcdomain); } free(e->Cookie); memset(e, 0, sizeof(struct tE)); } static void StringCat(char **S, char *format, ...) { char o[1000]; _vsnprintf(o, sizeof(o), format, &format+1); *S = realloc(*S, strlen(*S)+strlen(o)+SIZEOF0); strcat(*S, o); } static void MakeSendHeader(struct tE *e, char *pcpost) { e->pcsend = realloc(e->pcsend, SIZEOF0); e->pcsend[0] = 0; if (pcpost) { StringCat(&e->pcsend, "POST %s HTTP/1.1\r\n", e->pcpath); StringCat(&e->pcsend, "Content-Type: application" "/x-www-form-urlencoded\r\n"); StringCat(&e->pcsend, "Host: %s\r\n", e->pchost); StringCat(&e->pcsend, "Content-Length: %u\r\n", strlen(pcpost)); a_CookieSetCookies(e); StringCat(&e->pcsend, "\r\n"); StringCat(&e->pcsend, "%s", pcpost); } else /* HEADERTYPE_GET */ { StringCat(&e->pcsend, "GET %s HTTP/1.1\r\n", e->pcpath); StringCat(&e->pcsend, "Accept: image/gif, image/x-xbitmap, " "image/jpeg, image/pjpeg, " "application/x-shockwave-flash, " "*/*\r\n"); StringCat(&e->pcsend, "Accept-Language: de\r\n"); StringCat(&e->pcsend, "User-Agent: Mozilla/4.0 (compatible; " "MSIE 6.0; Windows 98; .NET CLR 1.1.4322)\r\n"); StringCat(&e->pcsend, "Host: %s\r\n", e->pchost); StringCat(&e->pcsend, "Connection: Keep-Alive\r\n"); a_CookieSetCookies(e); StringCat(&e->pcsend, "\r\n"); } } #define HEADERTYPE_GET 0 static int Loop302(struct tE *e) { int ifehler; while(1) { a_CookieParseCookies(e); if (strnicmp(e->recv.pc, "HTTP/1.0 302 Found", 18) != 0 && strnicmp(e->recv.pc, "HTTP/1.1 302 Found", 18) != 0 && strnicmp(e->recv.pc, "HTTP/1.1 301 Moved Permanently", 30) != 0) { WriteToFile(filecnt++, "out", "htm", e->recv.pc, e->recv.cnt); return 0; } WriteToFile(filecnt++, "out", "txt", e->recv.pc, e->recv.cnt); a_UrlParseLocation(e); a_UrlUrlToHostPath(e->pclocation, e); printf("%s%s\n", e->pchost, e->pcpath); MakeSendHeader(e, HEADERTYPE_GET); if ((ifehler = DoDownLoad(e)) != 0) break; } WriteToFile(filecnt++, "err", "txt", e->recv.pc, e->recv.cnt); return ifehler; } static void MakeLoginFreenetPost(struct tE *e, char *callback, char *world, char *profile, char *password) { e->pcpost = realloc(e->pcpost, SIZEOF0); e->pcpost[0] = 0; StringCat(&e->pcpost, "callback=%s", callback); StringCat(&e->pcpost, "&world=%s", world); StringCat(&e->pcpost, "&username=%s", profile); StringCat(&e->pcpost, "&password=%s", password); } static int freenet(struct tE *pe) { char *url = "http://e-tools.freenet.de/login.php3"; int ifehler; a_UrlUrlToHostPath(url, pe); printf("%s%s\n", pe->pchost, pe->pcpath); MakeLoginFreenetPost(pe, "http%3A%2F%2Fcommunity.freenet.de" "%2Ffreunde-finden" "%2Findex.html", "frn_DE", "<profile>", "<password>"); MakeSendHeader(pe, pe->pcpost); if ((ifehler = DoDownLoad(pe)) == 0) ifehler = Loop302(pe); printf("ifehler %i\n", ifehler); if (ifehler) return ifehler; url = "http://logout.freenet.de"; a_UrlUrlToHostPath(url, pe); printf("%s%s\n", pe->pchost, pe->pcpath); MakeSendHeader(pe, HEADERTYPE_GET); if ((ifehler = DoDownLoad(pe)) == 0) ifehler = Loop302(pe); printf("ifehler %i\n", ifehler); return ifehler; }
-
cookie.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "globals.h" #include "cookie.h" static void DelCookie(struct tE *e, char *pcname) { int i; for (i = 0; i < e->CookieAnzahl; i++) { if (strcmp(e->Cookie[i].pcname, pcname) == 0) { free(e->Cookie[i].pcname); free(e->Cookie[i].pcvalue); free(e->Cookie[i].pcpath); free(e->Cookie[i].pcdomain); memmove(e->Cookie+i, e->Cookie+i+1, (e->CookieAnzahl-i-1)*sizeof(struct tCookie)); e->CookieAnzahl--; return; } } } static int IsCookie(struct tE *e, char *pcname) { int i; for (i = 0; i < e->CookieAnzahl; i++) if (strcmp(e->Cookie[i].pcname, pcname) == 0) return 1; return 0; } static void SetCookie(struct tE *e, char *pcname, char *pcvalue, char *pcpath, char *pcdomain) { if (stricmp(pcvalue, "deleted") == 0) { DelCookie(e, pcname); return; } if (IsCookie(e, pcname)) DelCookie(e, pcname); e->Cookie = realloc(e->Cookie, (e->CookieAnzahl+1)*sizeof(struct tCookie)); memset(e->Cookie+e->CookieAnzahl, 0, sizeof(struct tCookie)); if (pcname) e->Cookie[e->CookieAnzahl].pcname = strdup(pcname); if (pcvalue) e->Cookie[e->CookieAnzahl].pcvalue = strdup(pcvalue); if (pcpath) e->Cookie[e->CookieAnzahl].pcpath = strdup(pcpath); if (pcdomain) e->Cookie[e->CookieAnzahl].pcdomain = strdup(pcdomain); e->CookieAnzahl++; } struct tNameValue { char *pcname; char *pcvalue; }; struct tbNameValue { struct tNameValue *pnv; int nvcnt; }; static char *DupNameOrValue(char *s, char *e) { char *p; p = malloc(e-s+SIZEOF0); memmove(p, s, e-s); p[e-s] = 0; return p; } static void FreeNameValue(struct tbNameValue *pb) { int i; for (i = 0; i < pb->nvcnt; i++) { free(pb->pnv[i].pcname); free(pb->pnv[i].pcvalue); } free(pb->pnv); free(pb); } static struct tbNameValue *ParseNameValue(char *s) { char *n, *v; struct tbNameValue *pret; pret = calloc(1, sizeof(struct tbNameValue)); while(1) { while(1) { while(*s == ' ' || *s == '\t') /* Leerzeichen überlesen */ s++; if (*s == '\r' || *s == 0) /* Zeilen-/Textende */ return pret; /* ==> fertig */ /* Name parsen */ n = s; while(*s && *s != '=' && *s != ';' && *s != '\r') s++; /* neues Name/Wert-Paar anlegen und auf null setzen */ pret->nvcnt++; pret->pnv = realloc(pret->pnv, pret->nvcnt*sizeof(struct tNameValue)); memset(pret->pnv+pret->nvcnt-1, 0, sizeof(struct tNameValue)); /* Name speichern */ pret->pnv[pret->nvcnt-1].pcname = DupNameOrValue(n, s); if (*s == '=') /* Wert folgt */ break; if (*s == ';') /* nur Name */ s++; } /* while(1) */ /* Name=... */ s++; /* *s == '=' */ if (*s == '"') { s++; /* *s == '"' */ /* Wert-String parsen */ while(*s == ' ' || *s == '\t') /* Leerzeichen überlesen */ s++; v = s; while(*s && *s != '"' && *s != '\r') s++; /* Wert speichern */ pret->pnv[pret->nvcnt-1].pcvalue = DupNameOrValue(v, s); if (*s == '"') /* *s == '"' */ s++; while(*s == ' ' || *s == '\t') /* Leerzeichen überlesen */ s++; while(*s && *s != ';' && *s != '\r') /* nach ';' suchen */ s++; } else { /* Wert parsen */ v = s; while(*s && *s != ';' && *s != '\r') s++; /* Wert speichern */ pret->pnv[pret->nvcnt-1].pcvalue = DupNameOrValue(v, s); } if (*s == ';') s++; } /* while(1) */ } void a_CookieParseCookies(struct tE *e) { char *pcfound; char *pcname, *pcvalue, *pcpath, *pcdomain; char *s; struct tbNameValue *px; int i; if (e->recv.pc == 0) return; s = e->recv.pc; while((pcfound = strstr(s, "\r\nSet-Cookie: ")) != 0) { px = ParseNameValue(pcfound+14); pcname = px->pnv[0].pcname; pcvalue = px->pnv[0].pcvalue; pcpath = pcdomain = 0; for (i = 1; i < px->nvcnt; i++) { if (stricmp(px->pnv[i].pcname, "path") == 0) pcpath = px->pnv[i].pcvalue; else if (stricmp(px->pnv[i].pcname, "domain") == 0) pcdomain = px->pnv[i].pcvalue; } if (pcdomain == 0) pcdomain = e->pchost; SetCookie(e, pcname, pcvalue, pcpath, pcdomain); FreeNameValue(px); s = pcfound+14; } } static int IsRightCookie(char *pchost, char *pcdomain) { int idomainlen, ihostlen; idomainlen = strlen(pcdomain); ihostlen = strlen(pchost); if (ihostlen >= idomainlen) if (strncmp(pcdomain, pchost+(ihostlen-idomainlen), idomainlen) == 0) return 1; return 0; } static void StringCat(char **S, char *format, ...) { char o[1000]; _vsnprintf(o, sizeof(o), format, &format+1); *S = realloc(*S, strlen(*S)+strlen(o)+SIZEOF0); strcat(*S, o); } void a_CookieSetCookies(struct tE *e) { int i; int cookiesetflag; cookiesetflag = 0; for (i = 0; i < e->CookieAnzahl; i++) { if (IsRightCookie(e->pchost, e->Cookie[i].pcdomain)) { if (!cookiesetflag) StringCat(&e->pcsend, "Cookie: "); if (cookiesetflag) StringCat(&e->pcsend, "; "); cookiesetflag = 1; StringCat(&e->pcsend, "%s=%s", e->Cookie[i].pcname, e->Cookie[i].pcvalue); } } if (cookiesetflag) StringCat(&e->pcsend, "\r\n"); }
-
url.c
#include <string.h> #include <stdlib.h> #include "globals.h" #include "url.h" void a_UrlUrlToHostPath(char *url, struct tE *e) { char *pcprotoend, *host, *path; if ((pcprotoend = strstr(url, "://")) != 0) { host = pcprotoend+3; if ((path = strchr(host, '/')) == 0) path = strchr(host, 0); e->pchost = realloc(e->pchost, path-host+SIZEOF0); memmove(e->pchost, host, path-host); e->pchost[path-host] = 0; } else path = url; e->pcpath = realloc(e->pcpath, (path[0] != '/')+strlen(path)+SIZEOF0); e->pcpath[0] = 0; if (*path != '/') strcat(e->pcpath, "/"); strcat(e->pcpath, path); } void a_UrlParseLocation(struct tE *e) { char *pcs, *pce; if (e->recv.pc == 0) return; pcs = strstr(e->recv.pc, "\r\nLocation: "); if (pcs == 0) return; pcs += 2; pcs += 10; pce = strchr(pcs, '\r'); e->pclocation = realloc(e->pclocation, pce-pcs+SIZEOF0); memmove(e->pclocation, pcs, pce-pcs); e->pclocation[pce-pcs] = 0; }ur.h
#ifndef __URL_H__ #define __URL_H__ void a_UrlUrlToHostPath(char *url, struct tE *e); void a_UrlParseLocation(struct tE *e); #endif /* __URL_H__ */cookie.h
#ifndef __COOKIE_H__ #define __COOKIE_H__ void a_CookieParseCookies(struct tE *e); void a_CookieSetCookies(struct tE *e); #endif /* __COOKIE_H__ */globals.h
#ifndef __GLOBALS_H__ #define __GLOBALS_H__ struct tCookie { char *pcname; char *pcvalue; char *pcpath; char *pcdomain; }; struct tb { char *pc; int cnt; }; struct tE { char *pcpath; char *pchost; char *pcsend; char *pcpost; struct tb recv; char *pchtml; char *pclocation; struct tCookie *Cookie; int CookieAnzahl; }; #define SIZEOF0 1 #endif /* __GLOBALS_H__ */M.bat
@d:\borland\tcc55\bin\make -fm.makm.mak
COMPILER=d:\borland\tcc55\bin\bcc32 LINKER=d:\borland\tcc55\bin\ilink32 output\main.exe: output\main.obj \ output\cookie.obj \ output\url.obj @$(LINKER) /Gn/c/x/ap/Tpe -Ioutput -joutput c0x32 main cookie url, output\main,, import32 cw32 @copy output\main.exe output\main.obj: src\main.c src\globals.h src\cookie.h src\url.h @$(COMPILER) -c -w -noutput src\main.c output\cookie.obj: src\cookie.c src\cookie.h src\globals.h @$(COMPILER) -c -w -noutput src\cookie.c output\url.obj: src\url.c src\url.h src\globals.h @$(COMPILER) -c -w -noutput src\url.c