chiark / gitweb /
ucgi/: Return useful status codes when things go wrong.
[userv-utils.git] / www-cgi / ucgitarget.c
index 2b6a22293916d624c2ee9c8a819cd1f61390991a..866a2d5abd385f62cbb2274ed30e4676697ac9dc 100644 (file)
@@ -109,19 +109,19 @@ int main(int argc, char **argv) {
   if (ev && *ev) debugmode= 1;
   
   D( if (debugmode) printf(";;; UCGITARGET\n"); )
-  if (argc > MAX_ARGS) error("too many arguments");
+  if (argc > MAX_ARGS) error("too many arguments", 500);
 
   for (;;) {
     i= getopt(argc, argv, "+e:"); if (i < 0) break;
     switch (i) {
     case 'e': filters= optarg; break;
-    default: error("bad command line"); break;
+    default: error("bad command line", 500); break;
     }
   }
   argc -= optind; argv += optind;
 
-  if (!*argv) error("no script directory argument");
-  ev= getenv("HOME"); if (!ev) error("no HOME env. var");
+  if (!*argv) error("no script directory argument", 500);
+  ev= getenv("HOME"); if (!ev) error("no HOME env. var", 500);
   l= strlen(*argv)+strlen(ev);
   newvar= xmalloc(l+2);
   sprintf(newvar,"%s/%s",ev,*argv);
@@ -141,7 +141,7 @@ int main(int argc, char **argv) {
 
   scriptpath= 0;
   pathi= getenv("PATH_INFO");
-  if (!pathi) error("PATH_INFO not found");
+  if (!pathi) error("PATH_INFO not found", 500);
   lastslash= pathi;
   D( if (debugmode) {
        printf(";; find script name...\n"
@@ -149,23 +149,25 @@ int main(int argc, char **argv) {
              pathi);
   } )
   for (;;) {
-    if (*lastslash != '/') error("PATH_INFO expected slash not found");
-    if (lastslash[1]=='.' || lastslash[1]=='#' || !lastslash[1]) error("bad char begin");
+    if (*lastslash != '/') error("PATH_INFO expected slash not found", 400);
+    if (lastslash[1]=='.' || lastslash[1]=='#' || !lastslash[1])
+      error("bad char begin", 400);
     nextslash= strchr(lastslash+1,'/');
     if (!nextslash) nextslash= lastslash+1+strlen(lastslash+1);
-    if (!nextslash) error("insufficient elements in PATH_INFO");
-    if (nextslash==lastslash+1) error("empty component in PATH_INFO");
-    if (nextslash-pathi > MAX_SCRIPTPATH_LEN) error("PATH_INFO script path too long");
+    if (!nextslash) error("insufficient elements in PATH_INFO", 400);
+    if (nextslash==lastslash+1) error("empty component in PATH_INFO", 400);
+    if (nextslash-pathi > MAX_SCRIPTPATH_LEN)
+      error("PATH_INFO script path too long", 400);
     scriptpathlen= scriptdirlen+(nextslash-pathi);
     scriptpath= xrealloc(scriptpath,scriptpathlen+1);
     strcpy(scriptpath,scriptdir);
     memcpy(scriptpath+scriptdirlen,pathi,nextslash-pathi);
     scriptpath[scriptpathlen]= 0;
-    if (scriptpath[scriptpathlen-1]=='~') error("bad char end");
+    if (scriptpath[scriptpathlen-1]=='~') error("bad char end", 400);
     D( if (debugmode) printf(";;   try `%s'\n", scriptpath); )
     r= stat(scriptpath,&stab); if (r) syserror("stat script");
     if (S_ISREG(stab.st_mode)) break;
-    if (!S_ISDIR(stab.st_mode)) error("script not directory or file");
+    if (!S_ISDIR(stab.st_mode)) error("script not directory or file", 500);
     lastslash= nextslash;
   }
   D( if (debugmode) printf(";;   found script: tail = `%s'\n", nextslash); )
@@ -180,7 +182,7 @@ int main(int argc, char **argv) {
 
   ev= getenv("SCRIPT_NAME");
   if (ev) {
-    ev2= getenv("USER"); if (!ev2) error("no USER variable");
+    ev2= getenv("USER"); if (!ev2) error("no USER variable", 500);
     newvar= xmalloc(strlen(ev)+2+strlen(ev2)+scriptpathlen-scriptdirlen+2);
     sprintf(newvar,"%s/~%s%s",ev,ev2,scriptpath+scriptdirlen);
     xsetenv("SCRIPT_NAME",newvar,1);