Originally Posted by
Salem
> i get some warnings, but it runs thanks so much!!
Bad idea, if you've got warnings, you've still got problems. The fact that it runs is as much down to dumb luck as it is to your skill.
If you're confused about what a particular warning means, then feel free to post it and the relevant section of code it related to, then we should be able to tell you what it means.
Code:
/* This function is passed in a name of a executable file
* or is given an absolute file path name and then
* checks to see if it exists.
* Returns the path name if found, or null if not
* found(an error is printed too).
*/
char *lookupPath(char *name)
{
char *pName;
char *workingName[BUFSIZ];
int counter = 0;
while (dirs[counter] != NULL)
{
//printf("Working on %s trying to find %s\n"
// , dirs[counter], name);
if(name != NULL && name[0] == '/')
{
if( access(name, X_OK) == 0 )
return name;
}
else
{
//This way didn't work so well...
//strcat(workingName, "");
//strcat(workingName, dirs[counter]);
//strcat(workingName, "/");
//strcat(workingName, name);
sprintf(workingName, "%s/%s", dirs[counter], name);
if( access(workingName, X_OK) == 0 )
return workingName;
}
counter++;
}
printf("Error %s was not found.", name);
return NULL;
}
I get several warnings that i totally don't understand when i compile that. Here is the entire code so you may understand better....
Code:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#define MAXPATHS 64
char *dirs[MAXPATHS];
/* This function will store the $PATH's found in the
* global var *dirs[MAXPATHS]. Uses strtok, and is finished.
*/
int parsePath()
{
//char *strtok(char *str1, const char *str2); This is the prototype
char *path = getenv("PATH");
char *delims = ":"; //TODO why does char delims[] = ":" also work here?
char *result = NULL;
result = strtok(path, delims);
int counter = 0;
while(result != NULL)
{
//printf("result is \"%s\"\n", result);
dirs[counter] = result;
counter++;
/* Subisquent calls are made with
* NULL according to man page.*/
result = strtok(NULL, delims);
}
//www.elook.org/programming/c/strtok.html
return 1;
}
/* This function is passed in a name of a executable file
* or is given an absolute file path name and then
* checks to see if it exists.
* Returns the path name if found, or null if not
* found(an error is printed too).
*/
char *lookupPath(char *name)
{
char *pName;
char *workingName[BUFSIZ];
int counter = 0;
while (dirs[counter] != NULL)
{
//printf("Working on %s trying to find %s\n"
// , dirs[counter], name);
if(name != NULL && name[0] == '/')
{
if( access(name, X_OK) == 0 )
return name;
}
else
{
//This way didn't work so well...
//strcat(workingName, "");
//strcat(workingName, dirs[counter]);
//strcat(workingName, "/");
//strcat(workingName, name);
sprintf(workingName, "%s/%s", dirs[counter], name);
if( access(workingName, X_OK) == 0 )
return workingName;
}
counter++;
}
printf("Error %s was not found.", name);
return NULL;
}
main(int argc, char *argv[])
{
int argc_dup = argc;
parsePath();
while (argc--)
{
//printf("%i", argc_dup - argc);
if (NULL != argv[argc_dup - argc])
printf(lookupPath(argv[argc_dup - argc]));
printf("\n");
}
printf("I'm done\n");
exit(EXIT_SUCCESS);
}