Code:
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
//global to not reset count to zero
//in recursive function
int i = 0, b = 0;
int getcount = 1;
char **
randomize_file_list(char **names, unsigned long len)
{ printf("randomize file list\n");
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("leaving randomize file list\n");
return (names);
}
char **
sort_file_list(char **names, unsigned long len)
{ printf("sort file list 342\n%s \n%ld\n",*names,len);
unsigned long i;
unsigned char done = 0;
while (!done)
{
done = 1;
//set at 1 because zero is NULL
for (i = 1; i < len - 1; i++)
{ printf("in for loop 353\n");
printf("i=%ldstrcmp(names[%s], names[i + 1 %s]) > 0\n",i, names[i], names[i+1]);
if (strcmp(names[i], names[i + 1]) > 0)
{
char *tmp;
tmp = names[i];
names[i] = names[i + 1];
names[i + 1] = tmp;
done = 0;
}
}
}
printf("leaving sort_file_list\n");
return (names);
}
//get total files to know how much to malloc 2d char array
void getAmount(const char *name, int indent, unsigned long *sum)
{ printf("get amount\n");
DIR *dirp;
struct dirent *entry;
char path[1024];
if (!(dirp = opendir(name)))
{
printf("cannot open dir.\n");
exit(1);
}
while ((entry = readdir(dirp)) != NULL) {
if (entry->d_type == DT_DIR) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
getAmount(path, indent + 2, sum);
} else {
i++;
}
}
closedir(dirp);
*sum = i;
printf("leaving get amount\n");
}
char ** listdir(const char *name, char *names[], int indent)
{printf("listdir %s, %s, %d\n", name,*names,indent);
DIR *dir;
struct dirent *entry;
char path[1024];
if (!(dir = opendir(name)))
return ((char **)NULL);
printf("n %s c %d \n", names[b], b);
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
listdir(path, names, indent + 2);
} else {
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
printf("%s\n",path);
names[b] = strdup(entry->d_name);
b++;
}
}
closedir(dir);
printf("returning listdir\n");
return (names);
}
int main(int argc, char **argv) {
char *path;
char **gotNames;
char **names;
int g;
unsigned long sum;
if(argc < 2)
{
printf("no path.\n");
return EXIT_FAILURE;
}
if (!strcmp(argv[1], "-p"))
{
path = strdup(argv[2]);
getAmount(path,0, &sum);
printf("i=%d\n",i);
printf("sum=%ld\n",sum);
printf("before malloc %s\n", path);
//segfaults here
names = (char **)malloc(sum * sizeof(char *));
if (names == NULL)
{
printf("Names is NULL\n");
}
printf("after malloc %s %s\n", path,*names);
gotNames = listdir(path,names,0);
printf("after malloc\n");
}
else
{
printf("could not get files missing -p.\n");
return EXIT_FAILURE;
}
for(g = 0; g < i; g++) {
;
//printf("%s\n",gotNames[g]);
}
printf("howmany %d\n", i);
sort_file_list(gotNames, sum);
return 0;
}
messages