root/libutil/usable.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. usable

   1 /*
   2  * Copyright (c) 1998, 1999, 2000, 2002
   3  *      Tama Communications Corporation
   4  *
   5  * This file is part of GNU GLOBAL.
   6  *
   7  * This program is free software: you can redistribute it and/or modify
   8  * it under the terms of the GNU General Public License as published by
   9  * the Free Software Foundation, either version 3 of the License, or
  10  * (at your option) any later version.
  11  * 
  12  * This program is distributed in the hope that it will be useful,
  13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  * GNU General Public License for more details.
  16  * 
  17  * You should have received a copy of the GNU General Public License
  18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19  */
  20 
  21 #ifdef HAVE_CONFIG_H
  22 #include <config.h>
  23 #endif
  24 #include <assert.h>
  25 #ifdef STDC_HEADERS
  26 #include <stdlib.h>
  27 #endif
  28 #ifdef HAVE_STRING_H
  29 #include <string.h>
  30 #else
  31 #include <strings.h>
  32 #endif
  33 
  34 #include "gparam.h"
  35 #include "locatestring.h"
  36 #include "makepath.h"
  37 #include "path.h"
  38 #include "test.h"
  39 #include "strbuf.h"
  40 #include "strlimcpy.h"
  41 #include "usable.h"
  42 
  43 #if defined(_WIN32) || defined(__DJGPP__)
  44 static const char *suffix[] = {".exe", ".com", ".bat",};
  45 #endif
  46 
  47 /**
  48  * usable: check if command is executable or not.
  49  *
  50  *      @param[in]      command
  51  *      @return         ==NULL: not found. <br>
  52  *                      !=NULL: absolute path of @a command.
  53  */
  54 char *
  55 usable(const char *command)
  56 {
  57         STRBUF *sb;
  58         char *p;
  59         const char *dir;
  60         static char path[MAXPATHLEN];
  61 
  62 #if defined(_WIN32) || defined(__DJGPP__)
  63         int i, lim = sizeof(suffix)/sizeof(char *);
  64 #endif
  65 
  66         if (isabspath(command) || locatestring(command, "./", MATCH_AT_FIRST)
  67                 || locatestring(command, "../", MATCH_AT_FIRST)) {
  68                 if (test("fx", command)) {
  69                         strlimcpy(path, command, sizeof(path));
  70                         return path;
  71                 }
  72                 return NULL;
  73         }
  74         /*
  75          * If found in BINDIR then use it.
  76          */
  77         if (test("fx", makepath(BINDIR, command, NULL))) {
  78                 strlimcpy(path, makepath(BINDIR, command, NULL), sizeof(path));
  79                 return path;
  80         }
  81         /*
  82          * Locate the command for each path in PATH.
  83          */
  84         *path = 0;
  85         /* Don't use fixed length buffer for environment variable
  86          * because it brings buffer overflow. */
  87         sb = strbuf_open(0);
  88         strbuf_puts(sb, getenv("PATH"));
  89         p = strbuf_value(sb);
  90         while (p) {
  91                 dir = p;
  92                 if ((p = locatestring(p, PATHSEP, MATCH_FIRST)) != NULL)
  93                         *p++ = 0;
  94                 if (test("fx", makepath(dir, command, NULL))) {
  95                         strlimcpy(path, makepath(dir, command, NULL), sizeof(path));
  96                         goto finish;
  97                 }
  98 #if defined(_WIN32) || defined(__DJGPP__)
  99                 for (i = 0; i < lim; i++)
 100                         if (test("f", makepath(dir, command, suffix[i]))) {
 101                                 strlimcpy(path, makepath(dir, command, suffix[i]), sizeof(path));
 102                                 goto finish;
 103                         }
 104 #endif
 105         }
 106 finish:
 107         strbuf_close(sb);
 108         return *path ? path : NULL;
 109 }

/* [previous][next][first][last][top][bottom][index][help] */