__int64 LogReader::find(__int64 a_entry, __int64 a_close, char a_char) { if (a_entry < m_entry || a_entry >= m_close) { const __int64 l_position = findSub(a_entry, a_close, a_char); return l_position; } if (a_close <= m_close) { const __int64 l_position = findInBuffer(a_entry, a_close, a_char); return l_position; } __int64 l_position = findInBuffer(a_entry, m_close, a_char); if (l_position >= 0) return l_position; l_position = findSub(m_close, a_close, a_char); return l_position; }
/* Traverses the directory tree and only ouputs files and directories that contain the substring 'sub' */ void findSub(DIR *directory, char *path, char *sub) { 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) sprintf(curPath, "./%s", tempdir->d_name); else sprintf(curPath, "%s/%s", path, tempdir->d_name); if (checkDir(tempdir->d_name) && strcmp(tempdir->d_name, "..") != 0 && strcmp(tempdir->d_name, ".") != 0) { sprintf(temp, "./%s", tempdir->d_name); chdir(tempdir->d_name); findSub(opendir("."), curPath, sub); chdir(".."); } if (strstr(tempdir->d_name, sub)) { lstat(tempdir->d_name, stats); printf("%d\t%s\n", (int)(stats->st_size + BLOCKSIZE - 1) / BLOCKSIZE, curPath); } } } }
/* 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; }