[SOLVED] Hacking code: direct assignment 2d char ** array works only for one condition.
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.
Hacking code: direct assignment 2d char ** array works only for one condition.
I am trying to "fix" some code in an epplet, for e16, it only reads in one directory, not recursive, so I "fixed" that with a hack, but it only works when I am not reading a directory that has sub-directories.
the first 3 functions are off of the app E-Slides,
randomize_file_list
sort_file_list
dirscan
the modded, and hacked to get it to work using malloc for the 2d array, I globalized for 2 vars.
listdir
getAmount
printNames
main
are mine.
when I try to print out a dir and sub dir it seg faults in the print function. name copy is NULL only when reading in sub-directories, then direct assignment.
the size count matches on both single directory , and directory with sub-directories. using find [path type] | wc -l . I get the same amount of files that this says I have gotten.
I just do not understand why it works with one and not the other.
any questions let me know.
thanks.!
Code:
#include <errno.h>
#include <dirent.h>
#include <stdio.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#define randomize 1
unsigned long i = 0, g = 0;
static char ** randomize_file_list(char **names, unsigned long len)
{
printf("random len %ld\n", len);
int r;
unsigned long i;
char *tmp;
for (i = 0; i < len - 1; i++)
{
r = (int)((len - i - 1) * ((float)rand()) / (RAND_MAX + 1.0)) + i + 1;
tmp = names[i];
names[i] = names[r];
names[r] = tmp;
//printf("%s\n", names[r]);
}
printf("r = %d, i = %ld\n",r,i);
return (names);
}
static char ** sort_file_list(char **names, unsigned long len)
{printf("ordered\n");
unsigned long i;
unsigned char done = 0;
while (!done)
{
done = 1;
for (i = 0; i < len - 1; i++)
{
if (strcmp(names[i], names[i + 1]) > 0)
{
char *tmp;
tmp = names[i];
names[i] = names[i + 1];
names[i + 1] = tmp;
done = 0;
}
}
}
return (names);
}
//taken from epplets E-Slides
static char **
dirscan(char *dir, unsigned long *num)
{
unsigned long i, dirlen;
DIR *dirp;
char **names;
struct dirent *dp;
struct stat filestat;
char fullname[1024];
printf("dirscan(\"%s\", %8p) called.\n", dir, num);
if ((!dir) || (!*dir))
{
*num = 0;
return ((char **)NULL);
}
dirp = opendir(dir);
if (!dirp)
{
*num = 0;
return ((char **)NULL);
}
/* count # of entries in dir (worst case) */
for (dirlen = 0; (dp = readdir(dirp)); dirlen++);
printf(" -> Got %ld entries.\n", dirlen);
if (!dirlen)
{
closedir(dirp);
*num = 0;
return ((char **)NULL);
}
names = (char **)malloc(dirlen * sizeof(char *));
printf(" -> Storing names at %8p.\n", names);
if (!names)
{
*num = 0;
return ((char **)NULL);
}
rewinddir(dirp);
for (i = 0; (dp = readdir(dirp));)
{
if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, "..")))
{
snprintf(fullname, sizeof(fullname), "%s/%s", dir, dp->d_name);
// printf(" -> About to stat() %s\n", fullname);
if (stat(fullname, &filestat))
{
printf(" -> Couldn't stat() file %s -- %s\n", dp->d_name, strerror(errno));
}
else
{
if (S_ISREG(filestat.st_mode))
{
// printf(" -> Adding name \"%s\" at index %ld (%8p)\n", dp->d_name, i, names + i);
names[i] = strdup(dp->d_name);
i++;
}
else if (S_ISDIR(filestat.st_mode))
{
/* Recurse directories here at some point, maybe? */
}
}
}
}
if (i < dirlen)
{
dirlen = i;
}
if (!dirlen)
{
closedir(dirp);
*num = 0;
return ((char **)NULL);
}
closedir(dirp);
*num = dirlen;
names = (char **)realloc(names, dirlen * sizeof(char *));
printf(" -> Final directory length is %lu. List moved to %8p\n", *num, names);
if (randomize)
{
randomize_file_list(names, dirlen);
}
else
{
sort_file_list(names, dirlen);
}
return (names);
}
static char **
listdir(char *dir) //, int indent, unsigned long *num)
{
DIR *dirp;
struct dirent *entry;
char **names;
char fullname[1024];
names = (char **)malloc(i * sizeof(char *));
//printf(" -> Storing names at %8p.\n", names);
if (!(dirp = opendir(dir)))
return ((char **)NULL);
while ((entry = readdir(dirp)) != NULL) {
if (entry->d_type == DT_DIR) {
// char path[1024];
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(fullname, sizeof(fullname), "%s/%s", dir, entry->d_name);
//printf("%*s[%s]\n", indent, "", entry->d_name);
//listdir(path);
listdir(fullname);
} else {
// printf("%s - %s\n",path , entry->d_name);
names[g] = strdup(entry->d_name);
g++;
}
}
closedir(dirp);
if (randomize)
{
randomize_file_list(names, i);
}
else
{
sort_file_list(names, i);
}
return (names);
}
//get total files to know how much to malloc 2d char array
void getAmount(const char *name) //, unsigned long *num)
{
DIR *dirp;
struct dirent *entry;
unsigned long dirlen;
if (!(dirp = opendir(name)))
{
printf("cannot open dir.\n");
exit(1);
}
while ((entry = readdir(dirp)) != NULL) {
if (entry->d_type == DT_DIR) {
char path[1024];
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
// printf("%*s[%s]\n", indent, "", entry->d_name);
getAmount(path); //, num );
// i++;
} else {
i++;
}
}
closedir(dirp);
}
void printNames(char *s[], unsigned long size)
{
printf("Print names %s : %ld\n", *s,size);
for (int a = 0; a < size ;a++)
printf("%s\n", s[a]);
}
int main (int argc, char **argv)
{
char *path;
unsigned long size;
char **ImageNames;
char **nameofImages;
if(argc < 2)
{
printf("no path.\n");
return EXIT_FAILURE;
}
if (!strcmp(argv[1], "-p"))
{
path = strdup(argv[2]);
//ImageNames = dirscan(path, &size);
getAmount(path);
//direct assignment only
//works on dir without
//sub-directories.
nameofImages = listdir(path);
}
else
{
printf("could not get files.\n");
return EXIT_FAILURE;
}
printf("i = %ld\n", i);
printf("g = %ld\n", g);
printf("size = %ld\n", size);
//printNames(ImageNames, size);
printf("\n\n\n\n\n\n");
printNames(nameofImages, i );
printf("i = %ld\n", i);
printf("size = %ld\n", size);
return 0;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.