LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-02-2008, 05:55 PM   #1
km603
LQ Newbie
 
Registered: Jan 2008
Posts: 4

Rep: Reputation: 0
wired paths when using strcat


Hi,

the following are my codes:

Code:
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

Can you tell me where i did wrong?

thanks
 
Old 02-02-2008, 07:13 PM   #2
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Rep: Reputation: 33
you allocate how much memory you need for your string here:
Code:
char *path;
	path = getenv("PATH");

	char *temp2 = malloc(strlen(path) + 1);
then you do other stuff to it (like this)
Code:
strcpy ( temp2, path );  // Save a copy of substring
		strcat ( temp2, "/" );     // Form a path and file name
		strcat (temp2, command);
therefore it's probably longer than how much memory you've reserved for it.

Use a intermediary string and resize, or keep resizing as you go along...
 
Old 02-02-2008, 07:26 PM   #3
km603
LQ Newbie
 
Registered: Jan 2008
Posts: 4

Original Poster
Rep: Reputation: 0
thanks for your reply.

even i allocate the memory to very big, the issue still exists.

Code:
char *temp2 = (char *)calloc(10240, sizeof(char));
I think it is big enough to hold the data.
 
Old 02-02-2008, 10:56 PM   #4
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Rep: Reputation: 33
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!

Code:
char* getCommandPath(char* command)
{
	FILE *fpin;

	char *path;
	path = getenv("PATH");
	
	//char *temp2 = (char*)malloc(strlen(path) + 1);
	char *temp2 = (char*)malloc(PATH_MAX+strlen(command)+2);
	
	char *tok;
	tok = strtok(path, ":");
	
	while (tok = strtok(NULL, ":"))
	{
		strcpy(temp2, tok);
		strcat(temp2, "/");
		strcat(temp2, command);
		if ( ( fpin = fopen ( temp2, "r" ) ) != NULL )
		{
			break;
		}
	}
	
	if ( fpin == NULL )
	{
		return "NULL";
	}
	else 
	{
		close(fpin);
		return temp2;
	}
}
 
Old 02-03-2008, 01:46 AM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

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).

For example:
Code:
  char p[1024];
  char *pts = p;
  char *path = getenv("PATH");
  char *temp2 = malloc(strlen(path) + 1);
  strcpy ( p, path );
  path = strtok_r ( p, ":" , &pts);   // Find the first PATH
  ...
Here's the output I got (in my test, I used "sh" instead of "xeyes"):
Quote:
path is /usr/bin, temp2 is /usr/bin/sh
path is /bin, temp2 is /usr/bin/sh
path is /bin, temp2 is /bin/sh
'Hope that helps .. PSM

Last edited by paulsm4; 02-03-2008 at 01:50 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
strcat Core Dump monil Programming 4 03-08-2005 11:20 AM
Segmentation fault on strcat() Ephracis Programming 8 12-17-2004 01:28 AM
Wired to Wireless back to Wired The_Nerd Linux - Hardware 5 09-15-2004 09:20 PM
Automatically resolving WINDOWS paths to pre-configured Linux paths gazzy Linux - General 1 09-05-2003 10:15 PM
RH 8.0 strcat problem shibdas Programming 1 07-02-2003 01:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration