Differences

This shows you the differences between two versions of the page.

Link to this comparison view

codaz:c:pastebin_en_console_post_http [2010/01/12 13:29] (current)
Line 1: Line 1:
 +Bon c'est pas *uber* clean, mais ça fait ce qu'on lui demande.
  
 +Si donc ça vous amuse de checker les retours des *alloc, ou utiliser des haine printaiffe, ou même enlever ces BUFFER-1 qui n'ont pas trop de sens, ne vous gênez pas, c'est Public Domain ! :-)
 +
 +<code c>
 +#include <​stdio.h>​
 +#include <​unistd.h>​
 +#include <​string.h>​
 +#include <​stdlib.h>​
 +
 +#include <​sys/​types.h>​
 +#include <​sys/​socket.h>​
 +#include <​netdb.h>​
 +#include <​netinet/​in.h>​
 +#include <​arpa/​inet.h>​
 +
 +#define FORMAT_LEN 15
 +#define BUFLEN 1024
 +
 +
 +void usage();
 +char *send_post(char *paste, char *language);
 +char *urlencode(char *str_to_encode);​
 +
 +int main(int argc, char **argv) {
 +
 + char format[FORMAT_LEN] = "​text";​
 + char buf[BUFLEN+1] = {0};
 +
 + char *tmp;
 + char *file_name;
 + char *str_to_paste = (char *) malloc(sizeof(char));​
 + char *options_list = "​l:​f:​h";​
 +
 + char *formats = "bash c cpp html4strict java javascript lua perl php python ruby abap actionscript ada apache applescript asm asp autoit blitzbasic bnf c_mac caddcl csharp cfm css d delphi diff dos eiffel fortran freebasic genero gml groovy haskell idl ini inno latex matlab m68k mpasm mirc mysql nsis objc ocaml oobas oracle8 pascal plswl qbasic rails robots scheme smalltalk smarty tcl vb vbnet visualfoxpro xml z80";
 +
 + int option;
 + int numread = 0;
 + int data_on_stdin = 1;
 + int datalen = 0;
 +
 + FILE *file_to_paste;​
 +
 +
 + opterr = 0;
 +
 + if(argc < 3)
 + usage();
 +
 + while ((option = getopt(argc,​ argv, options_list)) != -1) {
 + switch (option) {
 + case '​l':​
 + if(strstr(formats,​ optarg))
 + snprintf(format,​ FORMAT_LEN, "​%s",​ optarg);
 + else
 + usage();​
 + break;
 + case '​f':​
 + file_name = optarg;
 + file_to_paste = fopen(file_name,​ "​r"​);​
 + while (!feof(file_to_paste)) {
 + numread += fread(buf, sizeof(char),​ BUFLEN, file_to_paste);​
 + tmp = (char *) realloc(str_to_paste,​ sizeof(char)*numread);​
 + str_to_paste = strcat(tmp, buf);
 + bzero(buf,​ BUFLEN);
 + }
 + fclose(file_to_paste);​
 + data_on_stdin = 0;
 + break;
 + case '​h':​
 + usage();​
 + break;
 + default:
 + usage();​
 + break;
 + }
 + }
 +
 + if (optind != argc)
 + usage();
 +
 + if (data_on_stdin) {
 + while ((numread = read(0, buf, BUFLEN-1))) { 
 + datalen += numread;
 + tmp = (char *) realloc(str_to_paste,​ sizeof(char)*(datalen+1));​
 + str_to_paste = strcat(tmp, buf);
 + bzero(buf,​ BUFLEN);
 + }
 + }
 +
 + printf("​%s\n",​ send_post(str_to_paste,​ format));
 +
 + free(str_to_paste);​
 +
 + return EXIT_SUCCESS;​
 +}
 +
 +
 +void usage() {
 + puts("​ --------------------------------------------------------------------"​);​
 + puts("​| Usage: ./cpastebin [-l language] [-f fichier] ​                     |");
 + puts("​| Example: cat mycode.c | cpastebin -l c                             ​|"​);​
 + puts("​| ​       : ./cpastebin -f mycode.c ​                                  ​|"​);​
 + puts("​| ​       : ./cpastebin -l bash -f myscrip.sh ​                        ​|"​);​
 + puts("​| Option -l :                                                        |");
 + puts("​| bash c cpp html4strict java javascript lua perl php python ruby    |");
 + puts("​| abap actionscript ada apache applescript asm asp autoit blitzbasic |");
 + puts("​| bnf c_mac caddcl csharp cfm css d delphi diff dos eiffel fortran ​  ​|"​);​
 + puts("​| freebasic genero gml groovy haskell idl ini inno latex matlab m68k |");
 + puts("​| mpasm mirc mysql nsis objc ocaml oobas oracle8 pascal plswl qbasic |");
 + puts("​| rails robots scheme smalltalk smarty tcl vb vbnet visualfoxpro xml |");
 + puts("​| z80                                                                |");
 + puts("​ --------------------------------------------------------------------"​);​
 +
 + exit(0);
 +}
 +
 +
 +char *send_post(char *paste, char*language) {
 +
 + char content_length[40];​
 + char header[300];​
 + char *hostname = "​pastebin.com";​
 + char *str_to_post = strdup(paste);​
 + char *tmp;
 + char *tmp2;
 +
 + struct sockaddr_in servaddr;
 + struct hostent *hptr;
 +
 + int sock;
 + int junk;
 +
 + str_to_post = urlencode(str_to_post);​
 +
 + hptr = gethostbyname(hostname);​
 +
 + sock = socket(AF_INET,​ SOCK_STREAM,​ 0);
 + bzero(&​servaddr,​ sizeof(struct sockaddr_in));​
 +
 + servaddr.sin_family = AF_INET;
 + servaddr.sin_port = htons(80);
 + servaddr.sin_addr = *((struct in_addr *) hptr->​h_addr);​
 +
 + connect(sock,​ (struct sockaddr *) &​servaddr,​ sizeof(struct sockaddr_in));​
 +
 + tmp2 = (char *) calloc(strlen(str_to_post)+100,​ sizeof(char));​
 +
 + sprintf(tmp2,​ "​code2=%s&​parent_pid=&​format=%s&​poster=&​paste=Send&​expiry=d&​email=",​str_to_post,​ language);
 + sprintf(content_length,​ "​Content-length:​ %d", strlen(tmp2));​
 + sprintf(header,​ "POST /​pastebin.php HTTP/​1.0\r\nContent-type:​ application/​x-www-form-urlencoded\r\n%s\r\nHost:​ pastebin.com\r\nUser-agent:​ jfgagent/​1.0\r\n\r\n",​ content_length);​
 +
 + tmp = (char *) calloc(strlen(tmp2)+strlen(header)+1,​ sizeof(char));​
 + sprintf(tmp,​ "​%s%s",​ header, tmp2);
 +
 + free(tmp2);​
 + str_to_post = tmp;
 +
 + junk = strlen(str_to_post);​
 +
 + send(sock, (const void *) str_to_post,​ junk, 0);
 + bzero(str_to_post,​ junk);
 + recv(sock, (void *) str_to_post,​ junk, 0);
 +
 + tmp = strstr(str_to_post,​ "​Location:"​);​
 + sscanf(tmp,​ "​Location:​ %s\r\n",​ tmp2);
 +
 + close(sock);​
 +
 + return tmp2;
 +}
 +
 +
 +char *urlencode(char *str_to_encode) {
 +
 + int lent = strlen(str_to_encode);​
 + char *tmp = calloc(lent*10,​ sizeof(char));​
 + int i = 0;
 + int x = 0;
 + char c = 0;
 + char hex[3];
 +
 +/* http://​www.codeguru.com/​cpp/​cpp/​cpp_mfc/​article.php/​c4029/#​more */
 +
 + for (; i<lent; i++) {
 + c = str_to_encode[i];​
 + if ((c > 47 && c < 58) || (c > 64 && c < 91) || (c > 96 && c < 123)) 
 + tmp[x++] = c;
 + else {
 + sprintf(hex,​ "​%02X",​ c);
 + tmp[x++] = '​%';​
 + tmp[x++] = hex[0];
 + tmp[x++] = hex[1];
 + }
 + }
 +
 + tmp = (char *) realloc(tmp,​ sizeof(char)*strlen(tmp));​
 + return tmp;
 +}
 +
 +</​code>​
 +
 +Hop Hop "gcc cpastebin.c -o cpastebin -Wall -pedantic && cp -p cpastebin /​usr/​bin/"​
 +
 +Spécial dédicasse à gaston la loutre qui a peur des serpents. 8]~~~
 +
 + --- //​[[jf.guchens@gmail.com|jfg]] 2007/09/18 00:18//
codaz/c/pastebin_en_console_post_http.txt · Last modified: 2010/01/12 13:29 (external edit)