Link to home
Start Free TrialLog in
Avatar of wizkid2332
wizkid2332

asked on

Look through directiores to find a file

My code is acting really weird.  I can get the listing of the files before the while loop, then when i enter it something happens and it loses all the information.  It happens at the while k < 1 loop .  Before it enters the loop the locations are in the array.  Once it goes in, I get weird data in the array.

Any help would be appreciated.
#include <sys/types.h> 
#include <sys/stat.h> 
#include <dirent.h> 
#include <unistd.h> 
#include <time.h> 
#include <stdint.h> 
#include <locale.h> 
#include <langinfo.h> 
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
const int stringSize = 100;
int shouldFindAllInstances = 0;
 
void getDirectories(char [], char[]);
int isFindAllInstances(char []);
int isSearchAnotherDirectory(char []);
int isDirectory(char *);
 
void toPrint(char [][stringSize], int);
 
int main (int argc, char * argv[]) {
	char homeDirectory[stringSize];
	
	if (argc == 5) {
		if (isSearchAnotherDirectory(argv[3]) == 1) strcpy(homeDirectory, argv[4]);
		else strcpy(homeDirectory, "/Users/Rich/Desktop/Test");
	}
	else strcpy(homeDirectory, "/Users/Rich/Desktop/Test");
	
	if (argc == 3) {
		if (isFindAllInstances(argv[1]) == 1) {
			shouldFindAllInstances = 1;
			getDirectories(homeDirectory, argv[2]);
		}
		else getDirectories(homeDirectory, argv[1]);
	}
	
	return 0;
}
 
void getDirectories(char currentDirectory[], char fileToFind[]) {
	char directoryArray[20][stringSize];
	char fileArray[20][stringSize];
	char previousDirectory[stringSize];
	DIR *dirp;
	dirp = opendir(currentDirectory);
	struct dirent *dptr;
	
	int m = 0;
	while (m < 20) {
		memset(directoryArray[m],'\0', stringSize);
		memset(fileArray[m],'\0', stringSize);
		m++;
	}
	memset(previousDirectory,'\0', stringSize);
	
	strcpy(previousDirectory, currentDirectory);
	
	int i = 0, j = 0;
	while (dptr = readdir(dirp)) { 
		if ((isDirectory(dptr->d_name)) == 1) {
			strcpy(directoryArray[i++], dptr->d_name);
		}
//		if ((isDirectory(dptr->d_name)) == 0){
		else {
			strcpy(fileArray[j++], dptr->d_name);
		}
	}
	
	printf("%s\n", currentDirectory);
//	toPrint(directoryArray, i);
//	toPrint(fileArray, j);
	
	int k = 0;
	while (k < i) {
		if (canFileBeFound(fileArray, fileToFind, j) == 1) {
			printf("%s found in %s\n", fileToFind, currentDirectory);
		}
//		int l = 0;
//		while (l < j) { //Check each file
//			if (strcmp(fileArray[l], fileToFind) == 0) {
//				printf("%s found in %s\n", fileToFind, currentDirectory);
//				if (shouldFindAllInstances == 0) exit(0);
//				break;
//			}
//			printf("%s\n", fileArray[l++]);
//		}
		printf("\n");
		strcat(currentDirectory, "/");
		strcat(currentDirectory, directoryArray[k++]);
		getDirectories(currentDirectory, fileToFind);
		memset(currentDirectory,'\0', stringSize);
		strcpy(currentDirectory, previousDirectory);
	}
	closedir(dirp);
}
 
int isFindAllInstances(char isAll[]) {
	if (strncmp(isAll, "-a", 2) == 0) return 1;
	return 0;
}
 
int isSearchAnotherDirectory(char isDiectory[]) {
	if (strncmp(isDiectory, "-d", 2) == 0) return 1;
	return 0;
}
 
int isDirectory(char * nameToCheck) {
	struct stat statStruct;
	stat(nameToCheck, &statStruct);
	if ((statStruct.st_mode & S_IFMT) == S_IFDIR) {
		if (strpbrk(nameToCheck, ".") != NULL) return 0;
	}
	return 1;
}
 
int canFileBeFound(char dataArray[][100], char fileToFind[], int length) {
	int i = 0;
	while (i < length) {
		printf("%s\n", dataArray[i]);
		i++;
//		if (strcmp(dataArray[i], fileToFind) == 0) return 1;
	}
	return 0;
}
 
void toPrint(char dataArray[][100], int length){
	int i = 0;
	while (i < length) {
		printf("%s\n", dataArray[i]);
		i++;
	}
	return;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wizkid2332
wizkid2332

ASKER

Thanks