home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tkisrc04.zip / pycmd / pycmd.c < prev    next >
C/C++ Source or Header  |  1999-05-14  |  2KB  |  90 lines

  1. /* Minimal main program -- everything is loaded from the library */
  2. /*
  3.  * This is a hacked version of the minimal python program that allows us to
  4.  * use the "extproc" hack in OS/2 in cases where the python scripts resides
  5.  * somewhere other than the current directory.
  6.  */
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <sys/stat.h>
  12.  
  13. extern int Py_Main();
  14.  
  15. int 
  16. endsIn(name, suffix)
  17.     char *name, *suffix;
  18. {
  19.     int len = strlen(name), sfxLen = strlen(suffix);
  20.     if (len < sfxLen)
  21.         return 0;
  22.     
  23.     return !stricmp(&name[len - sfxLen], suffix);
  24. }
  25.  
  26. char *
  27. findFile(file)
  28.     char *file;
  29. {
  30.     struct stat st;
  31.     char *cur, *path, *orgPath;
  32.  
  33.     /* append a ".cmd" to the file if there isn't one already */
  34.     if (!endsIn(file, ".cmd")) {
  35.         file = strcpy(malloc(strlen(file) + 5), file);
  36.         strcat(file, ".cmd");
  37.     }
  38.     
  39.     /* first we check the current directory */
  40.     if (!stat(file, &st))
  41.         return file;
  42.     
  43.     /* make a copy of the PATH environment variable that we can
  44.      * carve up with strtok.
  45.      */
  46.     path = getenv("PATH");
  47.     orgPath = path = strcpy(malloc(strlen(path) + 1), path);
  48.     
  49.     while (cur = strtok(path, ";")) {
  50.         char *temp = malloc(strlen(cur) + strlen(file) + 2);
  51.         
  52.         sprintf(temp, "%s\\%s", cur, file);
  53.         if (!stat(temp, &st)) {
  54.             free(orgPath);
  55.             return temp;
  56.         }
  57.         free(temp);
  58.  
  59.         /* path must be null the after first time through */
  60.         if (path)
  61.             path = NULL;
  62.     }
  63.     free(orgPath);
  64.     return file;
  65. }
  66.  
  67. int
  68. main(argc, argv)
  69.     int argc;
  70.     char **argv;
  71. {
  72.     int rc;
  73.     /* copy the arguments into a local array, allocating enough space
  74.      * for an additional "-x" argument
  75.      */
  76.     char **localArgv = (char**)malloc(sizeof(char*) * (argc + 1));
  77.     int i;
  78.     localArgv[0] = argv[0];
  79.     localArgv[1] = "-x";
  80.     for (i = 1; i < argc; ++i)
  81.         localArgv[i + 1] = argv[i];
  82.     
  83.     /* find the exact location of the first parameter (the program file) */
  84.     if (argc >= 2)
  85.         localArgv[2] = findFile(localArgv[2]);
  86.     
  87.     rc = Py_Main(argc + 1, localArgv);
  88.     return rc;
  89. }
  90.