Beispiel #1
0
static char * omFindExec_link (const char *name, char* executable)
#endif
{
  char *search;
  char *p;
#ifdef WINNT
  char *extra = NULL;
#endif
  char tbuf[MAXPATHLEN];

  if (ABSOLUTE_FILENAME_P(name))
  {
      /* If we can execute the named file then return it. */
      if (! access (name, X_OK))
      {
        strcpy(executable, name);
#ifdef __CYGWIN__
        strcat(executable, ".exe");
#endif
        return executable;
      }
  }
  else
  {
    if (((name[0] == '.') && (name[1] == '/')) ||
        ((name[0] == '.') && (name[1] == '.') && (name[2] == '/')) ||
        strchr(name, '/') != NULL)
    {

#ifdef HAVE_GETCWD
      getcwd (tbuf, MAXPATHLEN);
#else
# ifdef HAVE_GETWD
      getwd (tbuf);
# endif
#endif
      strcat (tbuf, "/");
      strcat (tbuf, name);
      if (! access(tbuf, X_OK))
      {
        strcpy(executable, tbuf);
#ifdef __CYGWIN__
        strcat(executable, ".exe");
#endif
        return executable;
      }
    }


    search = getenv("PATH");
/* for winnt under msdos, cwd is implictly in the path */
#ifdef WINNT
    p = getenv("SHELL");
    if (p == NULL || strlen(p) < 2)
    {
      char *extra = NULL;
      /* we are under msdos display */
      extra = (char*) omAlloc((search != NULL ? strlen(search) : 0) + 3);
      strcpy(extra, ".:");
      if (search != NULL) strcat(extra, search);
      search = extra;
    }
#endif
    p = search;

    if (p != NULL)
    {
      while (1)
      {
        char *next;
        next = tbuf;

        /* Copy directory name into [tbuf]. */
        /* This is somewhat tricky: empty names mean cwd, w.r.t. some
           shell spec */
        while (*p && *p != ':')
          *next ++ = *p ++;
        *next = '\0';

        if ((tbuf[0] == '.' && tbuf[1] == '\0') || tbuf[0] == '\0') {
#ifdef HAVE_GETCWD
          getcwd (tbuf, MAXPATHLEN);
#else
# ifdef HAVE_GETWD
          getwd (tbuf);
# endif
#endif
        }

        if (tbuf[strlen(tbuf)-1] != '/') strcat(tbuf, "/");
        strcat (tbuf, name);

        /* If we can execute the named file, then return it. */
        if (! access (tbuf, X_OK))
        {
#ifdef WINNT
          if (extra != NULL)
            omFree(extra);
#endif
          strcpy(executable, tbuf);
#ifdef __CYGWIN__
          strcat(executable, ".exe");
#endif
          return executable;
        }

        if (*p != '\0')
        {
          p ++;
        }
        else
        {
          break;
        }
      }
    }
  }
  return NULL;
}
Beispiel #2
0
/* Return the absolute name of the program named NAME.  This function
   searches the directories in the PATH environment variable if PROG
   has no directory components. */
int find_executable(const char *name, char *buf, int size)
{
    char *search;
    char *p;
    char tbuf[MAXPATHLEN];

    if (ABSOLUTE_FILENAME_P(name)) 
    {
        /* If we can execute the named file, and it is normal, then return it. */
        if (!access (name, X_OK))
        {
	        struct stat stat_temp;

            if (stat (name, &stat_temp))
                return -1;

#ifndef STAT_MACROS_BROKEN
            if (!S_ISREG(stat_temp.st_mode))
                return -1;
#endif
            strncpy(buf, name, size);
            return 0; 
        }
    }
    else 
    {
        if ((name[0] == '.') && (name[1] == '/'))
        {
            strcpy(tbuf, ".");

#ifdef HAVE_GETCWD
            getcwd(tbuf, MAXPATHLEN);
#else
# ifdef HAVE_GETWD
            getwd(tbuf);
# endif
#endif
            strcat(tbuf, name + 1);
            strncpy(buf, tbuf, size);
            return 0;
        }

        if ((name[0] == '.') && (name[1] == '.') && (name[2] == '/'))
        {

#ifdef HAVE_GETCWD
            getcwd(tbuf, MAXPATHLEN);
            strcat(tbuf, "/");
#else
# ifdef HAVE_GETWD
            getwd(tbuf);
            strcat(tbuf, "/");
# endif
#endif
            strcat(tbuf, name);
            strncpy(buf, tbuf, size);
            return 0;
        }

        search = getenv("PATH");
        p = search;

        while (*p)
        {
            char *next;
            next = tbuf;

            /* Perform tilde-expansion. Stolen from GNU readline/tilde.c. */
            if (p[0] == '~') 
            {
                if (!p[1] || p[1] == '/') 
                {
                    /* Prepend $HOME to the rest of the string. */
                    char *temp_home = (char*)getenv("HOME");

                    /* If there is no HOME variable, look up the directory in
                       the password database. */
                    if (!temp_home) 
                    {
                        struct passwd *entry;

                        entry = getpwuid(getuid());
                        if (entry)
                             temp_home = entry->pw_dir;
                    }

                    strcpy(tbuf, temp_home);
                    next = tbuf + strlen(tbuf);
                    p++;
                }
                else
                {
                    char username[MAXPATHLEN];
                    struct passwd *user_entry;
                    int i;

                    p++; /* Skip the tilde. */
                    for (i = 0; *p && *p != '/' && *p != ':'; i++)
                        username[i] = *p ++;
                    username[i] = '\0';

                    user_entry = getpwnam(username);
                    if (user_entry) 
                    {
                        strcpy(tbuf, user_entry->pw_dir);
                        next = tbuf + strlen(tbuf);
                    }
                }

                endpwent();
            }

            /* Copy directory name into [tbuf]. */
            while (*p && *p != ':')
                *next ++ = *p ++;
            *next = '\0';
            if (*p != '\0')
            p++;

            if (tbuf[0] == '.' && tbuf[1] == '\0') 
            {
#ifdef HAVE_GETCWD
                getcwd (tbuf, MAXPATHLEN);
#else
# ifdef HAVE_GETWD
                getwd (tbuf);
# endif
#endif
            }

            strcat(tbuf, "/");
            strcat(tbuf, name);

            /* If we can execute the named file, and it is normal, then return it. */
            if (!access(tbuf, X_OK)) 
            {
                struct stat stat_temp;

                if (stat(tbuf, &stat_temp))
                    continue;

#ifndef STAT_MACROS_BROKEN
                if (!S_ISREG(stat_temp.st_mode))
                    continue;
#endif
                strncpy(buf, tbuf, size);
                return 0;
            }
        }
    }

    return -1;
}