LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 04-12-2004, 01:22 PM   #1
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Rep: Reputation: 30
strcat and direntp->d_name segmentation fault [I'm guessing the problem, plz propose]


hello. well it's me again. You have realized already that C is not [and will never be] my favorite lang. But I'm trying :P

I can produce seg fault with this code:

strcat(getcwd(NULL,0),direntp->d_name);

the reason I believe is that d_name is [as gdb says]:

print direntp->d_name
$1 = "a_DIR\000\000\000\000\000\000\000\000Έ\003\000\000κΊ4\030\000\000aSecond_DIR\000\000KΆ\003\000\000 $X<\030\000\000a_file\000\000\000\000\000\000\000\213Χ\002\000\001$X<\030\000\000Anotherfile\000\000 \000\000a_file\000\000\000\000\000\000\000\213Χ\002\000\000\000\000\000\001$X<\000\000\000\000 \000\000Anotherfile", '\0' <repeats 124 times>

ptype direntp->d_name
type = char [256]

I just want a_DIR in the first time, then aSecond_DIR etc..

printf("%s", direntp->d_name);
will print the first time only a_DIR [what I want]
the point is that I want to strcat the CUR_PATH with a_DIR [and the second time with aSecond_DIR etc..]

So is there an easy way, to say for strcat to behave cleverly as printf does?

My knowledge in C is not the best :P. I could image maybe
fprintf(stderr,"%s", direntp->d_name);
and then somehow
strcat(stderr,a_DIR)

but I'm not even sure that there is a way for this.
If you know, please help.
Thank you

Complete Code follows:
Code:
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int scan_dir(char *path);
int isdirectory(char *path);
int do_symlink(char *fn); //fn is filename


int main(int argc, char **argv)
{
	if (argc != 2)
	{
		fprintf(stderr, "Usage: %s directory_name\n", argv[0]);
		return 1; 
	}

 	struct dirent *direntp;
  	DIR *dirp;
  	
        char *path;
        strcpy(path, argv[0]);
		
	if ((dirp = opendir(path)) == NULL)
  	{
  		fprintf(stderr,"Failed to open directory %s\n", path);
  		return 1;
  	}
	
	if ( chdir( path ) )
	{
 		fprintf(stderr,"%s is not a Directory\n", path);
 		return 1;
 	}
	
	
	while ((direntp = readdir(dirp)) != NULL)
	{
		// dot and dot dot check
		if ((direntp->d_name[0] == '.') &&	(!direntp->d_name[1] ||
				(direntp->d_name[1] == '.' && !direntp->d_name[2])))
		continue;

                 printf("%s\n", strcat(getcwd(NULL,0),direntp->d_name));
         }
	return 0;
}

Last edited by zeppelin; 04-12-2004 at 01:27 PM.
 
Old 04-12-2004, 01:39 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
getcwd() is a function; you cannot strcpy() or strcat() values onto it, becuase it lives in the CODE segment, not the TEXT segment of your program.

You will have to call chdir() to set another direc as the current working directory.
 
Old 04-12-2004, 01:41 PM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
You can get around the problem by calling getcwd() this way: getcwd(NULL, PATH_MAX);

The only problem is you'll have no pointer to the allocated buffer. A better way would be to do:

Code:
char *ptr;

printf("%s\n", strcat((ptr = getcwd(NULL, PATH_MAX)), direntp->d_name));
But what you REALLY want to do is:

Code:
printf("%s%s\n", getcwd(NULL, 0), direntp->d_name);
There's no need for the strcat() unless you want a pointer to the merged string at a later point.

Last edited by itsme86; 04-12-2004 at 01:49 PM.
 
Old 04-12-2004, 02:52 PM   #4
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
so the problem was in getcwd(NULL,0) that just needed getcwd(NULL,PATH_MAX)

that's weird! but thanks no seg fault now

that was really quick! thank you guys. Hope I won't need your help soon. [we'll see]
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika Suse/Novell 2 12-02-2005 09:34 AM
problem in installing yahoo messenger segmentation fault lucky13luxs Linux - Software 2 10-15-2005 03:55 AM
problem with segmentation fault lucs Slackware 2 04-28-2005 09:14 AM
Segmentation fault on strcat() Ephracis Programming 8 12-17-2004 01:28 AM
Segmentation Fault Problem luvonmik Linux - Newbie 2 02-14-2004 07:44 PM


All times are GMT -5. The time now is 09:37 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration