ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
char* getCommandPath(char* command) {
FILE *fpin;
char p[1024];
char **pts;
// char *temp2 = (char *)calloc(1024, sizeof(char));
char *path;
path = getenv("PATH");
char *temp2 = malloc(strlen(path) + 1);
strcpy ( p, path );
path = strtok_r ( p, ":" , pts); // Find the first PATH
while ( path != NULL )
{
strcpy ( temp2, path ); // Save a copy of substring
strcat ( temp2, "/" ); // Form a path and file name
strcat (temp2, command);
printf(" \tpath is %s, temp2 is %s \n", path, temp2); //check outputs
if ( ( fpin = fopen ( temp2, "r" ) ) == NULL )
{
path = strtok_r ( NULL, ":", pts ); // Try next path
printf(" \tpath is %s, temp2 is %s \n", path, temp2); //check outputs again
}
else
{
break; // Must have a good fpin
}
}
if ( fpin == NULL )
return NULL;
else
return temp2;
}
when i made command to be "xeyes", the paths are so weird.
Code:
path is /usr/local/sbin, temp2 is /usr/local/sbin/TÒ�s
path is /usr/local/bin, temp2 is /usr/local/sbin/TÒ�s
path is /usr/local/bin, temp2 is /usr/local/bin/cÒ�s
path is /usr/sbin, temp2 is /usr/local/bin/cÒ�s
Not that i've covered this before, but from what I can tell, what is returned from getenv() is a pointer and I think your getting into trouble when your copying it and then trying to reuse the variable you've set as a pointer as a string (or something like that (don't you just love pointers?)).
Anyway, you were right, when setting the size of 'temp2' to the size of 'path' it should be generally be big enough to hold the new string, but imagine if your system only had the '/bin' path and then you added '/xeyes', it'd be too short. Therefore there's a variable called MAXPATH set in 'limits.h' which you can add with the length of the command, the path separator (+1)... anyway here's what i've got working... Hope it helps!
There are lots of problems with the code, including:
1. I wasn't even able to compile it as coded
2. You open the file...but never close it
3. You alloc memory ... but never free it
4. The same thing (you're basically just re-implementing the "whereis"
function) could be accomplished with a lot less code
But...
I think the biggest problem was your use of "pts" in "strtok_r()". The whole purpose of this argument is:
1. You allocate a buffer yourself (the "classic" strtok() uses an internal, static buffer - which makes it non-reentrant)
2. You declare a pointer to this buffer (e.g. "pts")
3. You pass a pointer to the pointer (so that when strtok_r updates the buffer, your function receives the updated pointer).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.