]> bicyclesonthemoon.info Git - ott/enhance/blob - bluenh-cgi.c
palette diff tool
[ott/enhance] / bluenh-cgi.c
1 //  //  //Download images and 
2    //  // ENHANCE them!
3   //  //  
4  //  //   Requires cgilib
5 //  //  //line 146 of cgi.h
6    //  // must be changed
7   //  //  from "extern }"
8  //  //   to "}"
9 //  //  //
10    //  // ~~bicyclesonthemoon
11
12 #include <cgi.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <sys/wait.h>
18 #include <sys/stat.h>
19
20 #define WGET_PATH  "/usr/bin/wget"
21 #define ENH_PATH   "./bluenh"
22 #define ENH_PATH2  "./bluenh2"
23 #define RM_PATH    "/bin/rm"
24 #define LOG_PATH   "/eizm/log/bluenh/bluenh.log"
25 #define USERAGENT  "Bluenhancement\x20(1190.bicyclesonthemoon.dnsd.info/bluenh/bluenh.htm)"
26 #define MSTD_PATH  "/pro/bluenh/nh.png"
27
28 int main (int argc, char *argv[]);
29 void mustard(int mustard);
30
31 FILE *pix;
32
33 char url[1024];
34 char path[256];
35 s_cgi *cgi;
36 int t;
37 pid_t sub;
38
39 int main (int argc, char *argv[])
40 {
41         unsigned char buf[1024];
42         struct stat st;
43         unsigned long p;
44         unsigned short q;
45         char **up;
46         bool enh2;
47         
48         cgi=cgiInit();
49         
50         up=cgiGetFiles(cgi);
51         if (up)
52         {
53                 sprintf(path,"%s",cgiGetFile(cgi,up[0])->tmpfile);
54                 sprintf(url,"%s",cgiGetFile(cgi,up[0])->filename);
55         }
56         else
57         {
58                 sprintf(url,"%s",(cgiGetValue(cgi,"inpix")!=NULL)?(cgiGetValue(cgi,"inpix")):"");
59                 sprintf(path,"/var/tmp/%lu",(unsigned long)getpid());
60                 
61                 //Why is  there no spawnl() in linux?
62                 sub=fork();
63                 if(sub==0)
64                 {
65                         t=execl(WGET_PATH,WGET_PATH,"-q","-t","2","-U",USERAGENT,"-O",path,url,(char *)0);
66                         exit(t);
67                 }
68                 waitpid(sub,&t,0);
69                 if(t)mustard(t);
70         }
71         
72         enh2=(cgiGetValue(cgi,"bluenh2")!=NULL);
73         sub=fork();
74         if(sub==0)
75         {
76                 t=execl(enh2?ENH_PATH2:ENH_PATH,enh2?ENH_PATH2:ENH_PATH,path,path,cgiGetValue(cgi,"a")!=NULL?cgiGetValue(cgi,"a"):"0",cgiGetValue(cgi,"b")!=NULL?cgiGetValue(cgi,"b"):"127",cgiGetValue(cgi,"c")!=NULL?cgiGetValue(cgi,"c"):"255",cgiGetValue(cgi,"d")!=NULL?cgiGetValue(cgi,"d"):"0",cgiGetValue(cgi,"e")!=NULL?cgiGetValue(cgi,"e"):"127",cgiGetValue(cgi,"f")!=NULL?cgiGetValue(cgi,"f"):"255","q",(char *)0);
77                 exit(t);
78         }
79         waitpid(sub,&t,0);
80         if(t)mustard(t);
81         
82         pix=fopen(path,"rb");
83         if(pix==NULL)
84                 mustard(123);
85         fstat(fileno(pix),&st);
86         p=st.st_size/1024;
87         q=st.st_size%1024;
88         
89         printf("Content-Length: %lu\n",(unsigned long)(st.st_size));
90         printf("Content-type: image/png\n\n");
91         for(unsigned long i=0;i<p;++i)
92         {
93                 fread(buf,1,1024,pix);
94                 fwrite(buf,1,1024,stdout);
95         }
96         if(q!=0)
97         {
98                 fread(buf,1,q,pix);
99                 fwrite(buf,1,q,stdout);
100         }
101         fclose(pix);
102         fflush(stdout);
103         mustard(0);//no mustard
104 }
105
106 void mustard(int mustard)
107 {
108         if(mustard)
109         {
110                 cgiRedirect(MSTD_PATH);
111         }
112         sub=fork();
113         if(sub==0)
114         {
115                 t=execl(RM_PATH,RM_PATH,"-f",path,(char *)0);
116                 exit(t);
117         }
118         waitpid(sub,&t,0);
119         pix=fopen(LOG_PATH,"at");
120         if(pix!=NULL)
121         {
122                 fprintf(pix,"%x %s\n",mustard,url);
123                 fclose(pix);
124         }
125         exit(mustard);
126 }