Can't get the reason for segfault - c

EDIT : Note that It's not that I can't access the memory allocated by storeContents() in main() if you think so. Program crashes during the execution of storeContents()
The program fails here :
strcpy(contents[count], dir->d_name);
printf("Stored %s(out hiddenVisible)\n", dir->d_name); // for testing
It's strcpy() not the printf(), I added it just for the reference.
The debugger(gdb) says :
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7f3cd72 in __strcpy_ssse3 () from /usr/lib/libc.so.6
I am making a program that involves the following function "storeContents"(It stores contents' names of a directory in a dynamic array). There are two issues with this function : (1) It says "Stored file_name" twice for the first file and (2) says "Segmentation fault". I can't figure out either of them. Thanks for your efforts!
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/types.h>
#include <limits.h>
static short hiddenVisible = 0;
/* Store directory's contents in **contents */
static char ** storeContents(struct dirent *dir, DIR *dirp, unsigned numOfContents);
/* Count files/directories in a directory */
static unsigned getNumOfContents(struct dirent *dir, DIR *dirp);
int main(int argc, char const *argv[])
{
char **contents;
DIR *dirp;
struct dirent *dir;
unsigned numOfContents;
dirp = opendir("/home/gaurav");
if((dir = readdir(dirp)) == NULL) {
perror("readdir");
exit(1);
}
/* Getting number of files/directories */
numOfContents = getNumOfContents(dir, dirp);
printf("There are %u files.\n", numOfContents);
/* To position again to the first entry */
rewinddir(dirp);
contents = storeContents(dir, dirp, numOfContents);
/* Print contents */
for(unsigned i = 0; i < numOfContents; ++i)
printf("%s\n", contents[i]);
closedir(dirp);
return 0;
}
char **
storeContents(struct dirent *dir, DIR *dirp, unsigned numOfContents) {
char **contents;
unsigned count = 0;
/* Allocating memory for entries */
contents = malloc(numOfContents * sizeof(*contents));
/* Allocating memory for each '*contents' */
for(unsigned i = 0; i < numOfContents; i++)
contents[i] = (char *)malloc(NAME_MAX); /* we know char is 1 byte, so no "sizeof" */
while(count < numOfContents) {
/* Ignore "." and ".." */
if(!(strcmp(dir->d_name, ".")) || !(strcmp(dir->d_name, ".."))) {
if((dir = readdir(dirp)) == NULL) {
perror("readdir");
exit(1);
}
continue;
}
if(hiddenVisible) {
strcpy(contents[count], dir->d_name);
if((dir = readdir(dirp)) == NULL) {
perror("readdir");
exit(1);
}
count++;
} else {
if(dir->d_name[0] == '.')
if((dir = readdir(dirp)) == NULL) {
perror("readdir");
exit(1);
}
else {
strcpy(contents[count], dir->d_name);
if((dir = readdir(dirp)) == NULL) {
perror("readdir");
exit(1);
}
count++;
}
}
}
return contents;
}
unsigned
getNumOfContents(struct dirent *dir, DIR *dirp) {
unsigned count = 0;
while(dir) {
if(hiddenVisible) {
/* Ignore "." and ".." */
if(!(strcmp(dir->d_name, ".")) || !(strcmp(dir->d_name, ".."))) {
if((dir = readdir(dirp)) == NULL) {
perror("readdir a");
exit(1);
}
continue;
}
count++;
if((dir = readdir(dirp)) == NULL) {
perror("readdir b");
exit(1);
}
} else {
if(dir->d_name[0] == '.') {
if((dir = readdir(dirp)) == NULL) {
perror("readdir c");
exit(1);
}
}
else {
count++;
if((dir = readdir(dirp)) == NULL) {
perror("readdir d");
exit(1);
}
}
}
}
return count;
}

contents in the function storeContents is a local copy of contents from main.
Changing it in the function does not change the variable in main.
You should return the array. Change
static void storeContents(struct dirent *dir, DIR *dirp, char **contents, unsigned numOfContents);
to
static char **storeContents(struct dirent *dir, DIR *dirp, unsigned numOfContents);
,return contents; in the function and call it like char **contents = storeContents(...);

Some bugs:
contents is a local parameter to the function, it will not get returned to main(). See Dynamic memory access only works inside function.
contents = (char **)malloc(numOfContents); is wrong, you need to allocate room for numOfContents pointers. Change this to contents = malloc(numOfContents * sizeof(*contents)).
You should check each call to readdir and make sure it doesn't return NULL.

Related

searching for a file name using only an extension

prev problem was not described well.
I need to retrieve a file name and assign that name to a variable. The only thing is that the file can have any name.
The thing that I know is the file extension and there can only be one file of this extension.
Is there any way to implement this in C?
Update #1
File extension is *.knb and target is Linux system.
#include <stdio.h>
#include <dirent.h>
#define LOG_() do{fprintf(stdout, " UNIT TESTING...\n");}while(0)
char* knbname;
void fName(void);
int main()
{
LOG_();
fName(void);
//do some more code
return 0;
}
void fName(void){
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d)
{
//find file with *.knb extension and place name into knbname
}
closedir(d);
}
I don't know how to find for a *.knb file in the fName() function and how to assign name to knbname. Any help will be appreciated
You are reinventing POSIX's glob() function. Here is an example of how to use it (see the flags in manpage for more options):
#include <glob.h>
int main(void)
{
glob_t glob_res = { 0 };
glob("*.knb", 0, NULL, &glob_res);
if (glob_res.gl_pathc == 0) puts("No match.");
else printf("First match is %s\n", glob_res.gl_pathv[0]);
globfree(&glob_res);
return 0;
}
After Andrew pointed out some major flaw in the previous design, i will make another attempt for a solution...
int hasExtension(char *filename, char *extension);
int main(void)
{
DIR *directory;
struct dirent *entry;
if(NULL == (directory = opendir(".")))
{
perror("Failed to open directory\n");
exit(1);
}
while( NULL != (entry = readdir(directory)) )
{
if(DT_REG == entry->d_type) { // Regualar files only
if(hasExtension(entry->d_name, ".knb"))
{
printf("Match - %s\n", entry->d_name);
} else {
printf("No match - %s\n", entry->d_name);
}
}
}
closedir(directory);
return 0;
}
int hasExtension(char *filename, char *extension)
{
size_t filename_len, extension_len;
if((NULL == filename) || (NULL ==extension))
return 0;
filename_len = strlen(filename);
extension_len = strlen(extension);
if(filename_len < extension_len)
return 0;
return !strcmp(filename + filename_len - extension_len, extension);
}

How to count only the number of directories from a path

I am trying to count only the directories from a path, but it doesn't work. So, i don't want to number both files and directories, i want only the directories. Could you help me, please?
The code:
int listdir(char *dir) {
struct dirent *dp;
struct stat s;
DIR *fd;
int count = 0;
if ((fd = opendir(dir)) == NULL) {
fprintf(stderr, "listdir: can't open %s\n", dir);
}
while ((dp = readdir(fd)) != NULL) {
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
continue;
stat(dp->d_name, &s);
if (S_ISDIR(s.st_mode))
count++;
}
closedir(fd);
return count;
}
your stat() call will fail, since you are not in the correct directory. This you can solve by either changing the current directory, or generate full paths and give to stat as argument.
Some Unixes, you can optimize away the stat call by looking at struct dirent, the d_type field
int listdir(char *dir) {
struct dirent *dp;
struct stat s;
DIR *fd;
int count = 0;
if ((fd = opendir(dir)) == NULL) {
fprintf(stderr, "listdir: can't open %s\n", dir);
}
chdir (dir); /* needed for stat to work */
while ((dp = readdir(fd)) != NULL) {
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
continue;
#ifdef _DIRENT_HAVE_D_TYPE
switch (dp->d_type)
{
case DT_UNKNOWN:
stat(dp->d_name, &s);
if (S_ISDIR(s.st_mode)) count++;
break;
case DT_DIR:
count++;
break;
}
#else
stat(dp->d_name, &s);
if (S_ISDIR(s.st_mode)) count++;
#endif
}
closedir(fd);
return count;
}
I think you want...
written in C
count the number of directoriesfrom a path.
the counting function will returns int value.
I have no idea about your environment, so it is just a example solution.
If you can use glob, it is so easy to count the number of directories. that is: main.c
#include <stdio.h>
#include <glob.h>
#include <string.h>
#define MAX_PATH 1023
int countDirectories(char* dir)
{
char path[MAX_PATH] = "";
strcat(path, dir);
strcat(path, "/*");
glob_t globbuf;
long i, count = 0;
if (glob(path, GLOB_NOSORT | GLOB_ONLYDIR, NULL, &globbuf) == 0)
{
count = globbuf.gl_pathc;
for (i = 0; i < globbuf.gl_pathc; i++)
{
count += countDirectories(globbuf.gl_pathv[i]);
}
}
globfree(&globbuf);
return count;
}
int main(int argc, char* argv[])
{
int count;
if (argc > 1)
{
count = countDirectories(argv[1]);
}
else
{
count = countDirectories(".");
}
printf("there are %d directories.\n", count);
return 0;
}
and you can try this like:
> gcc main.c -o testglob
> ./testglob /path/to/target/dir
then you'll receive output like this:
path = /path/to/target/dir/*, there are N directories
thanks.

Get (NOT display) the list of files and folder in C

I want to be able to get the list of folders and file (if possible with sub folders) and return it (array for example)
So i have this script:
DIR *dir;
struct dirent *ent;
if ((dir = opendir ("c:\\src\\")) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
printf ("%s\n", ent->d_name); // I dont want to print, I want to add it to a array
}
closedir (dir);
} else {
/* could not open directory */
perror ("");
return EXIT_FAILURE;
}
So i can do:
array = getFolders('.');
now if I loop thru the array i can see all folders and files.
eg:
array[0] = 'file1.txt';
array[1] = 'folder1/';
array[2] = 'folder1/file2.txt';
array[3] = 'folder1/file3.txt';
array[4] = 'folder2/';
array[5] = 'folder3/';
array[6] = 'folder3/filez.txt';
etc...
You can allocate a list of pointers and store each entry by allocating memory for the directory entry:
char **getFolder()
{
char **list = malloc(4096* sizeof *list);
size_t i = 0;
....
while ((ent = readdir (dir)) != NULL) {
list[i]=strdup(ent->d_name);
i++;
if( i >=4096 ) { /* time to realloc */}
}
return list;
}
and from the caller, you can use receive it as:
char **list=getFolder();
If you are going to have too many entries than you can use realloc() to increase the size of the list inside the loop. If strdup is not available, it can be easily implemented by using malloc() and strlen() functions.
I have excluded error checking for simplicity. Ideally, malloc() ans strdup should be checked for errors.
use DIR command in popen, and malloc and realloc
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#define INC 1024
char **getFolders(const char *path, /* out */ size_t *count){
char buff[1024];
snprintf(buff, sizeof buff, "dir /B /S /ON \"%s\"", path);
FILE *fp = popen(buff, "r");
size_t capacity = INC;
char **entry;
struct stat finfo;
if(NULL==(entry=malloc(capacity*sizeof(*entry)))){
perror("malloc");
exit(EXIT_FAILURE);
}
*count = 0;
while(fgets(buff, sizeof buff, fp)){
size_t len;
for(len = 0; buff[len]; ++len){
if(buff[len] == '\\')
buff[len] = '/';
}
buff[len-1] = '\0';
stat(buff, &finfo);
if(S_ISDIR(finfo.st_mode))
buff[len-1] = '/';
entry[*count] = strdup(buff);
if(++*count == capacity){
capacity += INC;
if(NULL==(entry=realloc(entry, capacity*sizeof(*entry)))){
perror("realloc");
exit(EXIT_FAILURE);
}
}
}
pclose(fp);
return entry;
}
int main(void){
size_t i, count = 0;
char **entry = getFolders("c:/src/", &count);
for(i = 0; i < count; ++i){
printf("%s\n", entry[i]);
free(entry[i]);
}
free(entry);
return 0;
}

Do I need to Allocate Memory for a Dirent Structure

Platform: Windows XP Service Pack 3
Compiler: Code::Blocks version 12.11
I am currently writing a program that will recursively delete a given directory using the POSIX directory functions. But I am having a problem with readdir() and its counterpart the dirent structure. I read in readdir's documentation that multiple calls to the function will overwrite the data held in the structure returned by the function. So I thought readdir() must allcoate memmory for the struct itself and then simply reassign the pointer address to the structure that captures it's return value. I tested this theory and I was correct readdir() allocated memmory for it's member d_name. The problem I am having is that readdir returns a NULL pointer when the directory stream is empty, so I use a while loop with the condition (dirent_ptr != NULL) to iterate the entire directory. But because readdir() will handle the memmory allocation of the structure I simply declare a dirent structure and let readdir() do its job. Unfourtatnly for some reason dirent structures are initialized to NULL(or it might be my complier) so my loop never starts because it's conditional statement is not initialy true. So I guess my question is what am I doing wrong here?
Here are important variable declarations and the included librarys. Please note that all of these variables are declared globaly.
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
int recursive_delete(const char *path);
int file_delete(const char *path, int file_size);
struct dirent *direntp;
struct stat *statp;
struct switches
{
int verbose;
int no_prompt;
int continue_if_error;
int files_only;
}; struct switches switches;
Rather than resolving relative paths I simply cd to the path given as an argument and then I use the . and .. wildcards to move threw the directorys so that relative paths(d_names) are valid. Also the switches structure simply contains command line switches and should be ignored and I am aware of errors in the following code but unfourtantly I cannot fix them because I cannot get past the above mentioned problem.
int recursive_delete(const char *path)
{
DIR *dirp;
int return_value = 0;
int recursive_return_value = 0;
if((chdir(path)) == -1)
{
perror("ERROR(3)");
return 1;
}
printf("CDED to \"%s\"\n", path);
dirp = opendir(".");
if(dirp == NULL)
{
perror("ERROR(4)");
return 1;
}
printf("OPENED \"%s\"\n", path);
while(direntp != NULL)
{
direntp = readdir(dirp);
if( (direntp == NULL) && (errno != 0) )
{
perror("ERROR(5)");
return 1;
}
printf("READ \"%s\" FROM \"%s\"\n", direntp->d_name, path);
if( (strcmp(direntp->d_name, ".")!=0) && (strcmp(direntp->d_name, "..")!=0) )
{
if((stat(direntp->d_name, statp)) == -1)
{
perror("ERROR(6)");
return 1;
}
printf("STATED \"%s\"\n", direntp->d_name);
if(S_ISREG(statp->st_mode))
{
printf("DELETING \"...\\%s\\%s\"\n", path, direntp->d_name);
return_value += file_delete(direntp->d_name, statp->st_size);
if( (!switches.continue_if_error) && (return_value != 0) )
{
break;
}
}
else if(S_ISDIR(statp->st_mode))
{
printf("\n\n\nCALLING RECURSIVE DELETE with \"%s\"\n", direntp->d_name);
recursive_return_value = recursive_delete(direntp->d_name);
return_value += recursive_return_value;
if( (!switches.continue_if_error) && (recursive_return_value != 0) )
{
break;
}
if( (!switches.files_only) && (recursive_return_value == 0) )
{
if((chdir("..")) == -1)
{
perror("ERROR(6)");
return 1;
}
printf("CDED BACK TO \"%s\" FROM \"%s\"\n", path, direntp->d_name);
if((rmdir(direntp->d_name)) == -1)
{
perror("ERROR(7)");
return 1;
}
if(switches.verbose)
{
printf("DELETED DIRECTORY \"...\\%s\\\"\n\n\n", direntp->d_name);
}
}
}
}
}
return return_value;
}
Your code structure should look something like thhis (with most error checks omitted for clarity):
int recursive_delete(const char *path)
{
DIR* dirp = NULL;
int return_value = 0;
char* initial_cur_dir = malloc(1000);
getcwd(initial_cur_dir, 1000);
chdir(path);
dirp = opendir(".");
while (dirp != NULL)
{
struct dirent* direntp;
struct stat stat;
direntp = readdir(dirp);
if (direntp == NULL)
break;
stat(direntp->d_name, &stat);
if (S_ISDIR(statp->st_mode))
{
if (strcmp(direntp->d_name, ".") && strcmp(direntp->d_name, ".."))
{
return_value += recursive_delete(direntp->d_name);
}
}
else if (S_ISREG(statp->st_mode))
{
unlink(direntp->d_name);
}
}
if (initial_cur_dir != NULL)
{
chdir(initial_cur_dir);
rmdir(path);
}
ErrorLabel: // you should goto here when an error is detected
if (dirp != NULL)
{
closedir(dirp);
}
if (initial_cur_dir != NULL)
{
chdir(initial_cur_dir);
free(initial_cur_dir);
}
return return_value;
}
From the code attached, it's not clear where direntp is being initialized (before the while loop). Possibly try something like:
direntp = readdir(dirp);
while(direntp != NULL)
{
// all your work here
direntp = readdir(dirp);
}
This pattern ensures that direntp is initialized and updated for your while loop. However, on second glance of your code I'm not exactly sure what the while loop is supposed to be doing in the first place. How is direntp or dirp changing in your loop?
It's possible that you can just get away with an if test (instead of the while) and just let the recursive call handle the "looping" effect...

Trying to recursively loop the folder contents

I'm trying to create a function which recursively returns the contents of a folder. While testing it, I encounter some problems. With some folders it works, With others it gives me an EXC_BAD_ACCESS, and sometimes it just stops.
I have been trying to debug it with GDB for a long time, but I just can't find a solution to my problem. The function is not quit short an goes as follows.
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
/* THIS IS AN EXTRACT FROM A LARGER FILE
THERE MAY BE TO MUCH INCLUDE STATEMENTS
*/
struct Directory {
DIR *handle;
const char *filename;
};
typedef struct Directory Directory;
int DirectoryCreate(const char *n, Directory *d) {
DIR *dh;
char *str;
dh = opendir(n);
if(dh == NULL) {
return -1;
}
d->handle = dh;
str = malloc(strlen(n) + 1);
if(str == NULL) {
errno = ENOMEM;
closedir(d->handle);
return -1;
}
strcpy(str, n);
d->filename = (const char *)str;
return 0;
}
void DirectoryFree(Directory *s) {
if(s->handle) {
closedir(s->handle);
}
if(s->filename) {
free((void *)s->filename);
}
}
void FreeDirectoryArray(Directory *array, size_t size) {
register size_t i;
for(i = 0; i < size; i++) {
DirectoryFree(&(array[i]));
}
free(array);
}
Directory *ReadFolders = NULL;
size_t ReadFoldersSize = 0;
const char *ReadFolderFilename = NULL;
const char *ReadNextRecursiveItemInFolder(const char *folder) {
struct dirent *entry;
struct stat fileStatus;
int status;
mode_t mode;
const char *newFilename;
char *fullName;
char *ptr;
size_t strLen;
if(folder == NULL && ReadFolders == NULL) {
errno = 0;
return NULL;
}
if(folder != NULL) {
/* free the previous directory list */
FreeDirectoryArray(ReadFolders, ReadFoldersSize);
ReadFolders = NULL;
ReadFoldersSize = 0;
/* open the new directory */
ReadFolders = (Directory *)realloc(ReadFolders, sizeof(Directory));
ReadFoldersSize++;
status = DirectoryCreate(folder, ReadFolders);
if(status != 0) {
FreeDirectoryArray(ReadFolders, ReadFoldersSize-1);
ReadFolders = NULL;
return NULL;
}
}
entry = readdir(ReadFolders[ReadFoldersSize - 1].handle);
/* If NULL, go to previous folder */
if(entry == NULL) {
DirectoryFree(&(ReadFolders[ReadFoldersSize - 1]));
--ReadFoldersSize;
/* if it's empty, we've reached the end */
if(ReadFoldersSize == 0) {
free(ReadFolders);
ReadFolders = NULL;
errno = 0;
return NULL;
}
newFilename = ReadNextRecursiveItemInFolder(NULL);
return newFilename;
}
/* Make sure the entry name is not . or .. */
if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
newFilename = ReadNextRecursiveItemInFolder(NULL);
return newFilename;
}
/* we've got an entry, now construct the full path */
strLen =
strlen(ReadFolders[ReadFoldersSize - 1].filename) +
1 +
strlen(entry->d_name);
fullName = malloc(strLen + 1);
ptr = fullName;
strcpy(ptr, ReadFolders[ReadFoldersSize - 1].filename);
ptr += strlen(ReadFolders[ReadFoldersSize - 1].filename);
strcpy(ptr, "/");
ptr++;
strcpy(ptr, entry->d_name);
newFilename = fullName;
/* no recurse on symbolic links */
status = lstat(newFilename, &fileStatus);
if(status != 0) {
FreeDirectoryArray(ReadFolders, ReadFoldersSize);
ReadFolders = NULL;
ReadFoldersSize = 0;
return NULL;
}
mode = fileStatus.st_mode;
/* if not readable for file or not searchable for folder, get next */
/* if folder and not link, recursively continue */
/* else return the new name */
if((((mode & S_IFDIR) == S_IFDIR) && (mode & S_IXUSR) != S_IXUSR) ||
(mode & S_IRUSR) != S_IRUSR) {
free((void *)newFilename);
newFilename = ReadNextRecursiveItemInFolder(NULL);
return newFilename;
} else if((mode & S_IFDIR) && (mode & S_IFLNK) != S_IFLNK) {
ReadFolders = realloc(ReadFolders, ReadFoldersSize + 1);
ReadFoldersSize++;
errno = 0;
status = DirectoryCreate(newFilename, &(ReadFolders[ReadFoldersSize - 1]));
if(status != 0) {
FreeDirectoryArray(ReadFolders, ReadFoldersSize - 1);
ReadFolders = NULL;
ReadFoldersSize = 0;
return NULL;
}
if(newFilename != ReadFolderFilename) {
free((void *)ReadFolderFilename);
ReadFolderFilename = newFilename;
}
} else {
if(newFilename != ReadFolderFilename) {
free((void *)ReadFolderFilename);
ReadFolderFilename = newFilename;
}
errno = 0;
}
return ReadFolderFilename;
}
int main() {
const char *filename = "/Users/";
const char *entry;
while(1) {
entry = ReadNextRecursiveItemInFolder(filename);
filename = NULL;
if(entry == NULL) {
if(errno == 0) {
printf("End reached\n");
} else {
printf("Error: %s\n", strerror(errno));
}
break;
}
printf("Entry: %s\n", entry);
}
return 0;
}
I'll give a brief explanation how the code works. To start looping a directory, you have to give the full directory path to the function. All subsequent calls have to pass NULL to get the next item in line, unless they want to process another directory.
The code counts every file and folder in a folder, recursively. It does not follow symbolic links, and it only counts readable files and executable directories. To keep track of its 'flow', the function uses 3 global variables:
ReadFolders: an array of Directory structures used to keep track of different levels of folders. The last one at the back.
ReadFoldersSize: the amount of Directory structures in ReadFolders.
ReadFolderFilename: the string that contains the last item processed.
I hope I can find some help around here,
ief2.
The realloc size is wrong : it not "n" but "n*size".
So the line 153 should be :
ReadFolders = realloc(ReadFolders, (ReadFoldersSize + 1)*sizeof(Directory));

Resources