~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to dttools/src/envtools.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-05-07 09:05:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110507090500-lqpmdtwndor6e7os
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2010- The University of Notre Dame
 
3
This software is distributed under the GNU General Public License.
 
4
See the file COPYING for details.
 
5
*/
 
6
 
 
7
#include "xmalloc.h"
 
8
 
 
9
#include <stdio.h>
 
10
#include <stdlib.h>
 
11
#include <string.h>
 
12
 
 
13
#include <sys/stat.h>
 
14
#include <sys/types.h>
 
15
#include <unistd.h>
 
16
 
 
17
int find_executable(const char *exe_name, const char *env_path_var, char *exe_path, int max_length)
 
18
{       
 
19
    char *env_paths;
 
20
    char *cur_path;
 
21
 
 
22
    if(access(exe_name,R_OK|X_OK)==0) {
 
23
        snprintf(exe_path,max_length,"%s",exe_name);
 
24
        return 1;
 
25
    }
 
26
 
 
27
    if (!getenv(env_path_var)) return 0;
 
28
 
 
29
    env_paths = xstrdup(getenv(env_path_var));
 
30
 
 
31
    for (cur_path = strtok(env_paths, ":"); cur_path; cur_path = strtok(NULL, ":")) {
 
32
        snprintf(exe_path, max_length, "%s/%s", cur_path, exe_name);
 
33
        if (access(exe_path, R_OK | X_OK) == 0) 
 
34
            goto fe_cleanup;
 
35
    }
 
36
 
 
37
fe_cleanup:
 
38
    free(env_paths);
 
39
 
 
40
    return cur_path != NULL;
 
41
}