/* Similar to displayAll(), but only prints out folders and the size of those folders Also recursively enters sub-directories */ void displayFolders(DIR *directory, char *path, long *total, int depth, int human) { char *temp = malloc(sizeof(char) * MAX_LEN); char *curPath = malloc(sizeof(char) * MAX_LEN); struct dirent *tempdir = malloc(sizeof(struct dirent)); struct stat *stats = malloc(sizeof(struct stat)); if (directory) { while ((tempdir = readdir(directory))) { if ((path == NULL) && checkDir(tempdir->d_name) && strcmp(tempdir->d_name, "..") != 0 && strcmp(tempdir->d_name, ".") != 0) sprintf(curPath, "./%s", tempdir->d_name); else if (checkDir(tempdir->d_name) && strcmp(tempdir->d_name, "..") != 0 && strcmp(tempdir->d_name, ".") != 0) sprintf(curPath, "%s/%s", path, tempdir->d_name); if (checkDir(tempdir->d_name) && strcmp(tempdir->d_name, "..") != 0 && strcmp(tempdir->d_name, ".") != 0 && depth > 0) { sprintf(temp, "./%s", tempdir->d_name); chdir(tempdir->d_name); displayFolders(opendir("."), curPath, total, depth - 1, human); chdir(".."); lstat(tempdir->d_name, stats); *total += roundUp((stats->st_size + (BLOCKSIZE - 1)) / BLOCKSIZE); if (!human) printf("%d\t%s\n", roundUp((int)(stats->st_size + (BLOCKSIZE - 1)) / BLOCKSIZE), curPath); if (human) printHuman(stats, -1, curPath); } } } }
/* Main */ int main() { int n; while(n != 4){ printf("\nWelcome to the ECE158A FTP Client! What would you like to do?\n\n"); printf("1. Get a remote directory\n2. Send a file\n3. Receive a file\n4. Exit"); printf("\n\nPlease enter the number for the action you want: "); scanf("%d", &n); if(n == 4){ printf("Program will now exit."); break; } else if(n == 1){ displayFolders(); } else if(n == 2){ upload(); } else if(n == 3){ download(); } else { printf("Invalid entry, please enter a number 1-4\n"); } } return 0; }
/* flags: -a: Displays an entry for each file in a file hierarchy -dn: Goes n directories deep during recursion -c: Displays a grand total -h: Displays the output in a human readable way using prefixes like Byte, KB, MB... -f<%s>: Tallies up only files that contain the substring s -v: Displays a visual tree of the current directory -un: Goes up n directories and then does what it needs to do */ int main(int argc, char *argv[]) { int all = 0, depth = 5, total = 0, human = 0, count = 1, visual = 0, look = 0, none = 0, up = 0, tdepth = 0; long tot = 0; char *find = malloc(sizeof(char) * MAX_LEN), *temp = malloc(sizeof(char) * 10), *path = malloc(sizeof(char) * MAX_LEN); char *rtn = malloc(sizeof(char) * MAX_LEN); DIR *directory = opendir("."); if (argc > 1) { do { switch(argv[count][1]) { case 'a': all = 1; break; case 'd': depth = atoi(&argv[count][2]); tdepth = depth; break; case 'h': human = 1; break; case 'f': strcpy(find, &argv[count][2]); look = 1; break; case 'c': total = 1; break; case 'v': visual = 1; break; case 'u': up = atoi(&argv[count][2]); break; default: none = 1; break; } } while (argv[++count] != NULL); } if (up) { if (up && look) { if (up > 2) up = 2; } goUp(up, rtn); directory = opendir("."); } if (none || (!all && !look && !visual)) { displayFolders(directory, NULL, &tot, depth, human); depth = tdepth; } if (all) { if (look) { printf("Look through all files on this user for substring '%s'?\ny/n: ", find); scanf("%s", temp); if (*temp == 'y' || *temp == 'Y') { realpath(".", path); chdir(getenv("HOME")); if (visual) system("tree"); directory = opendir("."); findSub(directory, NULL, find); chdir(path); } } else { if (visual) system("tree"); if (!human) { displayAll(directory, NULL, &tot, 0, depth, human); depth = tdepth; } } } if (visual && !all && !up) { system("tree"); } if (look) { if (all) { directory = opendir(getenv("HOME")); } findSub(directory, NULL, find); } if (up) chdir(rtn); if (total && !up) { tot = 0; directory = opendir("."); displayAll(directory, NULL, &tot, 1, depth, human); depth = tdepth; if (!human) printf("%ld\ttotal\n", tot); else printHuman(NULL, tot, "!"); } else { tot = 0; directory = opendir("."); displayAll(directory, NULL, &tot, 1, depth, human); } if (up && visual) { goUp(up, rtn); directory = opendir("."); printf("%ld\t%s\n", tot, "."); system("tree"); } else if (visual) printf("%ld\t%s\n", tot, "."); if (!human && !visual) { printf("%ld\t%s\n", tot, "."); } if (human) { if (up) { if (up && look) { if (up > 2) up = 2; } goUp(up, rtn); directory = opendir("."); depth = tdepth; displayAll(directory, NULL, &tot, 1, depth, human); } printHuman(NULL, tot, "."); } return 0; }