Beispiel #1
0
int my_dirent_remove(const char *path) 
{
	my_dirent root, *i;
	int res = 0;
	int ft = is_dir_or_file(path);
	memset(&root, 0, sizeof(my_dirent));
	if (ft == 2) {
		if (filesearch(path, &root, 0)) {
			res = -1;
		}
		else {
			i = root.next;
			while(i) {
				if (my_dirent_remove(i->path)) {
					res = -1;
					break;
				}
				i = i->next;
			}
		}
		my_dirent_destroy(root.next);
		if (!i) {
			res = remove(path);
		}
	}
	else {
		res = remove(path);
	}
	return res;

}
Beispiel #2
0
int filesearch(const char *path, my_dirent *root, int recursion)
{
    struct dirent* ent = NULL;
    DIR* pDir;
	my_dirent *cusor, *p;

	pDir = opendir(path);
	if (pDir == NULL)
		return -1;
	cusor = root;
    while((ent = readdir(pDir)) != NULL) {
        if (ent->d_type == 4) {
			if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
				continue;
			p = create_dirent(path, ent->d_name, 1, 0);
            if (!ent)
				return -1;
			cusor->next = p;
			cusor = p;
			if (recursion) {
				if (filesearch(p->path, cusor, recursion))
					return -1;
			}
        }
        else if (ent->d_type == 8){
			p = create_dirent(path, ent->d_name, 0, 0);
            if (!p)
				return -1;
			cusor->next = p;
			cusor = p;
        }
    }    
    closedir(pDir);
	return 0;
}
Beispiel #3
0
//............................................................................
void filesearch(char const *dirname) {
    static char buffer[1024];

    DIR *dir = opendir(dirname);                 // open the current directory
    if (dir == (DIR *)0) {            // if open fails, the directory is empty
        return;
    }

    struct dirent *entry;
    while((entry = readdir(dir)) != (struct dirent *)0) {
        if ((strncmp(entry->d_name, "..", 2) != 0)
                && (strncmp(entry->d_name, ".",  1) != 0))
        {
            strncpy(buffer, dirname,       sizeof(buffer));
            strncat(buffer, "/",           sizeof(buffer));
            strncat(buffer, entry->d_name, sizeof(buffer));

            if (entry->d_type == DT_DIR) {               // is it a directory?
                filesearch(buffer);
            }
            else {
                int flags = isMatching(entry->d_name,
                                       access(buffer, W_OK) != 0);
                if (flags != 0) {
                    onMatchFound(buffer, flags);
                }
            }
        }
    }
    closedir(dir);
}
Beispiel #4
0
my_dirent *list_dir(const char *path, int recursion)
{
	my_dirent root;
	memset(&root, 0, sizeof(my_dirent));
	if (filesearch(path, &root, recursion)) {
		my_dirent_destroy(root.next);
	}
	return root.next;
}
Beispiel #5
0
// look for default ttf file in given directory
static char *searchFont(const char *fontsearchDir)
{
	static char tempsw[256] = "";
	filestatus_t fstemp;

	strcpy(tempsw, FONTFILE);
	fstemp = filesearch(tempsw, fontsearchDir, NULL, true, 20);
	if (fstemp == FS_FOUND)
	{
		return tempsw;
	}
	return NULL;
}
Beispiel #6
0
//............................................................................
int main(int argc, char *argv[]) {
    char const *fileName = "fsdata.h";

    printf("qfsgen 5.6.5 (c) Quantum Leaps, www.state-machine.com\n"
           "Usage: qfsgen [root-dir [output-file]] [-h]\n"
           "      -h suppresses generation of the HTTP headers\n\n");

                                                  // parse the command line...
    if (argc > 1) {
        if (strcmp(argv[1], "-h") == 0) {
            l_noHtml = true;
        }
        else {
            l_rootDir = argv[1];
        }
        if (argc > 2) {
            if (strcmp(argv[2], "-h") == 0) {
                l_noHtml = true;
            }
            else {
                fileName = argv[2];
            }
            if (argc > 3) {
                if (strcmp(argv[3], "-h") == 0) {
                    l_noHtml = true;
                }
            }
        }
    }

    l_file = fopen(fileName, "w");
    if (l_file == 0) {
        printf("The file %s could not be opened for writing.", fileName);
        return -1;
    }

    fprintf(l_file, "/* This file has been generated"
                    "with the qfsgen utility. */\n\n");
    l_nFiles = 0;
    strcpy(l_prevFile, "(struct fsdata_file *)0");
    filesearch(l_rootDir);    // search through the file-system directory tree
    fprintf(l_file, "#define FS_ROOT %s\n\n", l_prevFile);
    fprintf(l_file, "#define FS_NUMFILES %d\n", l_nFiles);
    fclose(l_file);

    printf("\nFiles processed: %d\n", l_nFiles);
    return 0;
}
Beispiel #7
0
void filesearch(char const *dname) {
    struct dirent *dent;
    DIR *dir;
    struct stat st;
    char fn[FILENAME_MAX];
    int len = strlen(dname);
    if (len >= FILENAME_MAX - 1) {
        return;
    }

    strcpy(fn, dname);
    fn[len++] = '/';
    if ((dir = opendir(dname)) == NULL) {
        return;
    }

    while ((dent = readdir(dir)) != NULL) {
        if ((dent->d_name[0] != '.')
             && (strcmp(dent->d_name, ".") != 0)
             && (strcmp(dent->d_name, "..") != 0))
        {
            strncpy(fn + len, dent->d_name, FILENAME_MAX - len);
            if (lstat(fn, &st) != -1) {
                /* don't follow symlink unless told so */
                if (S_ISLNK(st.st_mode)) {
                }
                else if (S_ISDIR(st.st_mode)) {    // false for symlinked dirs
                    filesearch(fn);                 // recursively follow dirs
                }
                else {
                    int flags = isMatching(dent->d_name,
                                           access(fn, W_OK) != 0);
                    if (flags != 0) {
                        onMatchFound(fn, flags);
                    }
                }
            }
        }
    }

    closedir(dir);
}
Beispiel #8
0
int filesearch(const char *path, my_dirent *root, int recursion)
{
	struct _finddata_t filefind;
	char *curr;
	int done = 0, handle;
	my_dirent *cusor, *ent;

	curr = (char *)alloca(strlen(path) + 5);
	if (!curr)
		return -1;
	strcpy(curr, path);
	strcat(curr, "\\*.*");
	if((handle = _findfirst(curr, &filefind)) == -1)
		return -1;
	cusor = root;
	while(!(done = _findnext(handle, &filefind))) {
		if(!strcmp(filefind.name, ".."))
			continue;
		if ((_A_SUBDIR == filefind.attrib)) {
			ent = create_dirent(path, filefind.name, 1, filefind.time_write);
			if (!ent)
				return -1;
			cusor->next = ent;
			cusor = ent;
			if (recursion) {
				if (filesearch(ent->path, cusor, recursion))
					return -1;
			}
		}
		else {
			ent = create_dirent(path, filefind.name, 0, filefind.time_write);
			if (!ent)
				return -1;
			cusor->next = ent;
			cusor = ent;
		}
	}    
	_findclose(handle);
	return 0;
}
Beispiel #9
0
/* the main method where the program runs from */ 
int main(int argc, char* argv[]){
	
	/* error checking */ 
	if(argc==2 && strcmp(argv[1], "-h")==0){
		printf("You have opened the help menu.\nTo run the program, call the program name with the following parameters: the file to write the results to, and the file or directory to read from.\nIf these are not given, or an invalid directory or file is given, the program will exit.\nThank You\nBye Bye\n"); 
		return 0; 
	}
	else if (argc!=3){
		printf("ERROR: Improper number of parameters. Input the proper number of parameters when you run it again\nBye Bye\n"); 
		return 0; 
	}

	/* declaring a few basic variables and initializing them */ 
	SortedListPtr ptr; 
	ptr = NULL; 
	char input; 
	FILE *file = NULL;

	file = fopen(argv[1], "r"); 

	/* file was not found so a new file with that name is created 
	 * else the file was found and the user options are given to either write it over or quit the program*/
	if (file ==NULL){
		file = fopen(argv[1], "w");
	}
	else {
		printf("\nThe file to write to already exists.\nWhat do you want to do?\n 1 Clear content and write over\n 2 Quit and start all over\nType the number next to the choice that you want:\n"); 
		scanf("%c", &input); 
		if (input =='2'){
			SLDestroy(ptr); 
			fclose(file); 
			return 0; 
		}	
		else if (input =='1'){
			fclose(file); 
			file = fopen(argv[1], "w"); 
		}
		else {
			printf("\nWrong Option. Bye bye\n");
			SLDestroy(ptr); 
			fclose(file); 
			return 0; 
		}
	}

	/* the second paramter is assumed as a directory and the dirtraverse function is called to search */
	/* if NULL is returned then it was not a directory and it assumes it was a file and calls filesearch function */
	ptr = dirtraverse(".", ptr, argv[2], 0, 0); 
	if (ptr==NULL){	
		ptr = filesearch(".", ptr, argv[2], 0);
	}
	
	/* if ptr is still NULL. the 2nd paramter is neither an object nor a file so an error is printed and the program quits */ 
	if (ptr==NULL){
		printf("directory or file to read from not found\nBYE BYE\n");
		fclose(file); 
		return 0; 
	}

	/* the function insertofile is called and given the linked so it can write it to the given file*/ 
	InsertToFile(file,ptr); 

	/* program is done running*/
	printf("GREAT SUCCESS!!!! The program ran successfully! Very Good.\nBYE BYE!\n"); 
	SLDestroy(ptr); 
	fclose(file); 
	return 0; 
}