Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-12-2004, 02:22 PM   #1
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:


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

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

Complete Code follows:
#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])))

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

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

Rep: Reputation: 36
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, 02:41 PM   #3
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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:

char *ptr;

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

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 02:49 PM.
Old 04-12-2004, 03:52 PM   #4
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]


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 / openSUSE 2 12-02-2005 10:34 AM
problem in installing yahoo messenger segmentation fault lucky13luxs Linux - Software 2 10-15-2005 04:55 AM
problem with segmentation fault lucs Slackware 2 04-28-2005 10:14 AM
Segmentation fault on strcat() Ephracis Programming 8 12-17-2004 02:28 AM
Segmentation Fault Problem luvonmik Linux - Newbie 2 02-14-2004 08:44 PM > Forums > Non-*NIX Forums > Programming

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

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