cgi-servlet.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* A front-end to allow a servlet to be run as a CGI script.
  2. */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. extern char **environ;
  8. extern char* get_classpath(const char *);
  9. char *progname;
  10. #ifndef JAVA
  11. #define JAVA "java"
  12. #endif
  13. #ifndef COMMAND
  14. #ifdef GCJ_COMPILED
  15. #define COMMAND "kawa-bin"
  16. #else
  17. #define COMMAND JAVA
  18. #endif
  19. #endif
  20. int
  21. main(int argc, char** argv)
  22. {
  23. char* progname = argv[0];
  24. char *basename = progname;
  25. char *p;
  26. char**envp;
  27. int env_size = 0, env_count = 0;
  28. char** nargv, **nargp;
  29. for (p = progname; *p; p++)
  30. {
  31. if (*p == '/')
  32. basename = p + 1;
  33. }
  34. /*printf("basename: %s\n", basename);*/
  35. #ifndef GCJ_COMPILED
  36. p = getenv("CLASSPATH");
  37. if (p == NULL)
  38. {
  39. if (basename == progname)
  40. putenv("CLASSPATH=.");
  41. else
  42. {
  43. p = malloc (basename-progname + 50);
  44. sprintf(p, "CLASSPATH=%.*s", basename-progname-1, progname);
  45. putenv(p);
  46. }
  47. }
  48. putenv (get_classpath(argv[0]));
  49. #endif
  50. for (envp = environ; *envp != NULL; envp++)
  51. {
  52. env_count++;
  53. env_size += strlen (*envp);
  54. }
  55. nargv = (char**) malloc ((env_count + 5) * sizeof(char*));
  56. nargp = nargv;
  57. /**nargp++ = "/bin/echo";*/
  58. *nargp++ = COMMAND;
  59. for (envp = environ; *envp != NULL; envp++)
  60. {
  61. *nargp = (char*) malloc(strlen(*envp) + 3);
  62. sprintf(*nargp, "-D%s", *envp);
  63. nargp++;
  64. }
  65. *nargp++ = "gnu.kawa.servlet.CGIServletWrapper";
  66. *nargp++ = basename;
  67. *nargp++ = NULL;
  68. execvp(nargv[0], nargv);
  69. }