From b7849c483393cf510cbe4bca137ce4520b636685 Mon Sep 17 00:00:00 2001 From: epochqwert Date: Sat, 22 Aug 2015 03:36:15 -0500 Subject: initial commit. --- cuturl.c | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 cuturl.c (limited to 'cuturl.c') diff --git a/cuturl.c b/cuturl.c new file mode 100644 index 0000000..3f09020 --- /dev/null +++ b/cuturl.c @@ -0,0 +1,191 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "url.h" + +/* + schemes are case sensitive but cononicals are lower case. + domain is case insensitive. return it lowercased? + port is optional and in decimal + path + scheme://username:password@domain:port/path?query_string#fragment_id + mailto:username@domain + + optional stuff: + scheme, username, password, port, path, query_string, fragment_id +*/ + +#define AorB(a,b) ((a)?(a):(b)) + +#define F_SCHEME 1<<0 +#define F_USERNAME 1<<1 +#define F_PASSWORD 1<<2 +#define F_DOMAIN 1<<3 +#define F_PORT 1<<4 +#define F_PATH 1<<5 +#define F_QUERY_STRING 1<<6 +#define F_FRAGMENT_ID 1<<7 + +char *long_opts[]={"scheme","username","password","domain","port","path","query_string","fragment_id",0}; +char *short_opts[]={"s","u","k","d","P","p","q","f"}; + +int main(int argc,char *argv[]) { + char *name[2]; + char *line=0; + char sport[10]; + char args[256]; + struct servent *serv; + int i,j,c=0; + int size=1024; + int status; + char fixme=0; + char using_stdin=1; + char malloced=0; + struct url u; + if(argc > 1) { + if(!strcmp(argv[1],"--help") || !strcmp(argv[1],"-h")) { + printf("usage: echo urls | cuturl [options]\n"); + printf("usage: cuturl [options] url [options] [url]\n\n"); + printf("options: \n"); + for(i=0;long_opts[i];i++) { + printf(" -%s|--%s\n",short_opts[i],long_opts[i]); + } + printf("To set default values use environment variables like: CUTURL_[OPTION]\n"); + return 2; + } + } + argv++; + argc--; + while(1) { + u.scheme=0; + u.username=0; + u.password=0; + u.domain=0; + u.port=0; + u.path=0; + u.query_string=0; + u.fragment_id=0; + if(!using_stdin) c=0; + if(argc >= 1) { + for(;argc>0;argc--,argv++) { + for(i=0;long_opts[i];i++) { + if(!strncmp(*argv,"--",2)) { + if(!strcmp(*argv+2,long_opts[i])) { + args[c]=1< 80 + if(serv) snprintf(sport,sizeof(sport)-1,"%d",ntohs(serv->s_port)); + u.port=AorB(u.port,AorB(getenv("CUTURL_PORT"),(serv?sport:"DEFAULT"))); + u.path=AorB(u.path,AorB(getenv("CUTURL_PATH"),"DEFAULT")); + u.query_string=AorB(u.query_string,AorB(getenv("CUTURL_QUERY_STRING"),"DEFAULT")); + u.fragment_id=AorB(u.fragment_id,AorB(getenv("CUTURL_FRAGMENT_ID"),"DEFAULT")); + + if((name[0]=getenv("CUTURL__"))) { + setenv("CUTURL__SCHEME",u.scheme,1); + setenv("CUTURL__USERNAME",u.username,1); + setenv("CUTURL__PASSWORD",u.password,1); + setenv("CUTURL__DOMAIN",u.domain,1); + setenv("CUTURL__PORT",u.port,1); + setenv("CUTURL__PATH",u.path,1); + setenv("CUTURL__QUERY_STRING",u.query_string,1); + setenv("CUTURL__FRAGMENT_ID",u.fragment_id,1); + name[1]=0; + switch(fork()) { + case 0: + execv(name[0],name); + perror("execv"); + return errno; + case -1: + perror("fork"); + return errno; + default: + break; + } + wait(&status); + } else { + if(c) { + for(i=0;i