From 4fa8a93f62ab6d3e6b778f1b4791fe89e515d530 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Mon, 16 Jan 1995 21:32:30 +0000 Subject: [PATCH] find_path() now takes 2 copyout parameters (one for the qualified pathname and one for the unqualified pathname). The third parameter may be NULL. --- find_path.c | 58 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/find_path.c b/find_path.c index 392733911..4c79b4442 100644 --- a/find_path.c +++ b/find_path.c @@ -20,8 +20,9 @@ ******************************************************************* * * This module contains the find_path() function that returns - * a pointer to a static area with the absolute path of the - * command or NULL if the command is not found in the path. + * TRUE if the command was found and FALSE if not. + * If find_path() returns TRUE, the copyin paramters command and + * ocommand contain the resolved and unresolved pathnames respectively. * NOTE: if "." or "" exists in PATH it will be searched last. * * Todd C. Miller (millert@colorado.edu) Sat Sep 4 12:22:04 MDT 1993 @@ -94,14 +95,15 @@ static char * realpath_exec __P((char *, char *, char *)); * this function finds the full pathname for a command */ -char *find_path(file) - char *file; +int find_path(file, command, ocommand) + char *file; /* file to find */ + char **command; /* copyout parameter */ + char **ocommand; /* copyout parameter */ { register char *n; /* for traversing path */ char *path = NULL; /* contents of PATH env var */ - char *oldpath; /* so we can free path later */ + char *origpath; /* so we can free path later */ char *result = NULL; /* result of path/file lookup */ - char *command; /* resolved pathname */ int checkdot = 0; /* check current dir? */ if (strlen(file) > MAXPATHLEN) { @@ -112,8 +114,8 @@ char *find_path(file) /* * allocate memory for command */ - command = (char *) malloc(MAXPATHLEN + 1); - if (command == NULL) { + *command = (char *) malloc(MAXPATHLEN + 1); + if (*command == NULL) { perror("malloc"); (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); exit(1); @@ -122,20 +124,30 @@ char *find_path(file) /* * do we need to search the path? */ - if (strchr(file, '/')) - return((char *) sudo_realpath(file, command)); + if (strchr(file, '/')) { + /* store the unresolved command in ocommand */ + if (ocommand) { + *ocommand = strdup(file); + if (*ocommand == NULL) { + perror("malloc"); + (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); + exit(1); + } + } + return((sudo_realpath(file, *command)) ? TRUE : FALSE); + } /* * grab PATH out of environment and make a local copy */ if ((path = getenv("PATH")) == NULL) - return (NULL); + return (FALSE); if ((path = strdup(path)) == NULL) { fprintf(stderr, "sudo: out of memory!\n"); exit(1); } - oldpath=path; + origpath=path; do { if ((n = strchr(path, ':'))) @@ -154,7 +166,7 @@ char *find_path(file) /* * resolve the path and exit the loop if found */ - if ((result = realpath_exec(path, file, command))) + if ((result = realpath_exec(path, file, *command))) break; path = n + 1; @@ -165,10 +177,24 @@ char *find_path(file) * check current dir if dot was in the PATH */ if (!result && checkdot) - result = realpath_exec(".", file, command); + result = realpath_exec(".", file, *command); + + /* + * save old (unresolved) command + */ + if (result && ocommand) { + *ocommand = (char *) malloc(strlen(path) + strlen(file) + 2); + if (*ocommand == NULL) { + perror("malloc"); + (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); + exit(1); + } + (void) sprintf(*ocommand, "%s/%s", path, file); + } + + (void) free(origpath); - (void) free(oldpath); - return (result); + return(result ? TRUE : FALSE); } -- 2.40.0