cdt/cdt_5_0_x/org.eclipse.cdt.core.aix/library/pfind.c
changeset 0 0e6d23e2b466
equal deleted inserted replaced
-1:000000000000 0:0e6d23e2b466
       
     1 /*******************************************************************************
       
     2  * Copyright (c) 2003, 2008 IBM Corporation and others.
       
     3  * All rights reserved. This program and the accompanying materials
       
     4  * are made available under the terms of the Eclipse Public License v1.0
       
     5  * which accompanies this distribution, and is available at
       
     6  * http://www.eclipse.org/legal/epl-v10.html
       
     7  *
       
     8  * Contributors:
       
     9  *     IBM Corporation - initial API and implementation
       
    10  *******************************************************************************/
       
    11 
       
    12 /*
       
    13  * pfind.c - Search for a binary in $PATH.
       
    14  */
       
    15 
       
    16 #include <stdio.h>
       
    17 #include <unistd.h>
       
    18 #include <stdlib.h>
       
    19 #include <string.h>
       
    20 #include <limits.h>
       
    21 
       
    22 #ifndef PATH_MAX
       
    23 #define PATH_MAX 1024
       
    24 #endif
       
    25 
       
    26 
       
    27 char * pfind(const char *name)
       
    28 {
       
    29 	char *tok;
       
    30 	char *sp;
       
    31 	char *path;
       
    32 	char fullpath[PATH_MAX+1];
       
    33 
       
    34 	/* Sanity check.  */
       
    35 	if (name == NULL) {
       
    36 		fprintf(stderr, "pfind(): Null argument.\n");
       
    37 		return NULL;
       
    38 	}
       
    39 
       
    40 	/* For absolute name or name with a path, check if it is an executable.  */
       
    41 	if (name[0] == '/' || name[0] == '.') {
       
    42 		if (access(name, X_OK | R_OK) == 0) {
       
    43 			return strdup(name);
       
    44 		}
       
    45 		return NULL;
       
    46 	}
       
    47 
       
    48 	/* Search in the PATH environment.  */
       
    49 	path = getenv("PATH" );
       
    50 
       
    51 	if (path == NULL || strlen(path) <= 0) {
       
    52 		fprintf(stderr, "Unable to get $PATH.\n");
       
    53 		return NULL;
       
    54 	}
       
    55 
       
    56 	/* The value return by getenv() is readonly */
       
    57 	path = strdup(path);
       
    58 
       
    59 	tok = strtok_r(path, ":", &sp);
       
    60 	while (tok != NULL) {
       
    61 		snprintf(fullpath, sizeof(fullpath) - 1, "%s/%s", tok, name);
       
    62 
       
    63 		if (access(fullpath, X_OK | R_OK) == 0) {
       
    64 			free(path);
       
    65 			return strdup(fullpath);
       
    66 		}
       
    67 
       
    68 		tok = strtok_r( NULL, ":", &sp );
       
    69 	}
       
    70 
       
    71 	free(path);
       
    72 	return NULL;
       
    73 }
       
    74 
       
    75 #ifdef BUILD_WITH_MAIN
       
    76 int main(int argc, char **argv)
       
    77 {
       
    78    int i;
       
    79    char *fullpath;
       
    80 
       
    81    for (i=1; i<argc; i++) {
       
    82       fullpath = pfind(argv[i]);
       
    83       if (fullpath == NULL)
       
    84         printf("Unable to find %s in $PATH.\n", argv[i]);
       
    85       else 
       
    86         printf("Found %s @ %s.\n", argv[i], fullpath);
       
    87    }
       
    88 }
       
    89 #endif