repo: shell-daemons action: blob revision: path_from: read_headers.c revision_from: refs/heads/master: path_to: revision_to:
blob of:
/ read_headers.c
refs/heads/master:/read_headers.c
#include#include //toupper #include //strcpy, cat, etc #include //getenv #include //execv // this program is to read in headers like the type in: // urn:ietf:rfc:3864 // from stdin and then store them into environment variables // so that the program specified by argv[1] can do something with them char convert(char in) { if(in >= 'a' && in <= 'z') return in-' '; if(in == '-') return '_'; return in; } int main(int argc,char *argv[]) { setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); int i; char line[8192];//lol. how long should this be? char scheme[512]="(undefined)"; char name[8192 + 512 + 1]; char *value; char *prev_value; char *combined_value; if(getenv("SERVER_PROTOCOL")) { strncpy(scheme,getenv("SERVER_PROTOCOL"),sizeof(scheme)-1); } if(strchr(scheme,'/')) *strchr(scheme,'/')=0; while(fgets(line,sizeof(line)-1,stdin)) { if(strchr(line,'\n')) *strchr(line,'\n')=0; if(strchr(line,'\r')) *strchr(line,'\r')=0; if(!strlen(line)) break;//got a blank line. abort. if(*line == ' ' || *line == '\t') { // continuation of the value of the previous header. value=line; // just, don't change the name, and append to env var "name" while(*value == ' ' || *value == '\t') { value++; } // skip any leading whitespace if(!(prev_value = getenv(name))) continue; combined_value = malloc(strlen(prev_value) + 1 + strlen(value)); strcpy(combined_value,prev_value); strcat(combined_value," "); strcat(combined_value,value); setenv(name,combined_value,1); continue; } if((value=strchr(line,':'))) { *value=0; for(value++;*value==' ';value++);//collapse leading whitespace in values } else { continue;//lines missing a : aren't key: value pairs. skip I guess. } strcpy(name,scheme); strcat(name,"_"); strcat(name,line); for(i=0;name[i];name[i]=convert(name[i]),i++); if(!(prev_value = getenv(name))) { setenv(name,value,1); continue; } // we need to append the values of later headers to the end after putting a , combined_value = malloc(strlen(prev_value) + 2 + strlen(value)); strcpy(combined_value,prev_value); strcat(combined_value,", "); strcat(combined_value,value); setenv(name,combined_value,1); free(combined_value); } if(argc > 1) { execv(argv[1],argv+1);//run argv perror("execv"); } printf("usage: read_headers /external/program arg1 arg2\n"); return 1;//if we got here it was an error of some kind. }