static void kill_one(struct entry *ents, int ind, int curents) { int ret; int i; ret = unlink(ents[ind].name); if (ret < 0) { if (errno == EISDIR || errno == EPERM) do_dir(ents[ind].name); else err(1, "unlink %s", ents[ind].name); } ents[ind].status = 0; for (i = 0; i <= ind; ++i) { struct stat sb; ret = lstat(ents[i].name, &sb); if (ret == 0 || errno != ENOENT) err(1, "%s still exists?", ents[i].name); } for (i = ind + 1; i < curents; ++i) { struct stat sb; ret = lstat(ents[i].name, &sb); if (ret < 0) err(1, "stat %s", ents[i].name); } }
static void repeat_dir(const char *dirname) { DIR *dir; struct dirent *dp; dir = opendir("."); if (dir == NULL) err(1, "opendir %s", dirname); while ((dp = readdir(dir)) != NULL) { struct stat sb; int ret; if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) continue; ret = lstat(dp->d_name, &sb); if (ret < 0) err(1, "lstat %s", dp->d_name); if (S_ISDIR(sb.st_mode)) do_dir(dp->d_name); else if (S_ISREG(sb.st_mode)) read_and_truncate(dp->d_name, &sb); } closedir(dir); }
int main(int argc, char **argv) { if (argc != 2) errx(1, "usage: %s directory", argv[0]); do_dir(argv[1]); return 0; }
int main(int argc, char **argv) { if (argc < 2) errx(1, "usage: %s directory [...]", argv[0]); while (argc >= 2) { do_dir(argv[1]); argc--; argv++; } return 0; }
static void kill_one(const char *filename) { int ret; ret = unlink(filename); if (ret < 0) { if (errno == EISDIR || errno == EPERM) do_dir(filename); else err(1, "unlink %s", filename); } }
int main(int argc, char *argv[]) { struct AffWriter_s *aff; struct AffNode_s *dir; const char *status; if (argc != 2) { fprintf(stderr, "usage: write-aff output-name\n"); return 1; } aff = aff_writer(argv[1]); dir = aff_writer_root(aff); do_dir(aff, dir, "top-a", 1); do_dir(aff, dir, "top-b", 2); do_dir(aff, dir, "top-c", 3); status = aff_writer_close(aff); if (status) printf("Mistakes were made %s: %s\n", argv[1], status); return 0; }
int main(int argc, char **argv) { verbose = fdopen(4, "w"); if (verbose == NULL) { verbose = fopen("/dev/null", "w"); if (verbose == NULL) err(1, "fopen /dev/null"); } if (argc != 2) errx(1, "usage: %s directory", argv[0]); do_dir(argv[1]); return 0; }
int main(int argc, char **argv) { //printf("MojoShader finderrors\n"); //printf("Compiled against changeset %s\n", MOJOSHADER_CHANGESET); //printf("Linked against changeset %s\n", MOJOSHADER_changeset()); //printf("\n"); if (argc <= 2) printf("\n\nUSAGE: %s <profile> [dir1] ... [dirN]\n\n", argv[0]); else { int total = 0; int i; const char *profile = argv[1]; #if FINDERRORS_COMPILE_SHADERS SDL_Init(SDL_INIT_VIDEO); SDL_GL_LoadLibrary(NULL); SDL_SetVideoMode(640, 480, 0, SDL_OPENGL); printf("Best profile is '%s'\n", MOJOSHADER_glBestProfile(lookup, 0)); MOJOSHADER_glContext *ctx; ctx = MOJOSHADER_glCreateContext(profile, lookup, 0, 0, 0, 0); if (ctx == NULL) { printf("MOJOSHADER_glCreateContext() fail: %s\n", MOJOSHADER_glGetError()); SDL_Quit(); return 1; } // if MOJOSHADER_glMakeContextCurrent(ctx); #endif for (i = 2; i < argc; i++) total += do_dir(argv[i], profile); printf("Saw %d files.\n", total); #if FINDERRORS_COMPILE_SHADERS MOJOSHADER_glDestroyContext(ctx); SDL_Quit(); #endif } // else return 0; } // main
int main(int argv, char* args[]){ disk = open("/dev/fd0",O_RDWR); if (argv > 1){ if (strcmp(format, args[1]) == 0) do_format(); else if (strcmp(mkdir, args[1]) == 0){ if (argv > 2) do_mkdir(args[2]); else printf("Usage: mkdir dir\n"); } else if (strcmp(chdir, args[1]) == 0){ if (argv > 2) do_chdir(args[2]); else printf("Usage: chdir dir\n"); } else if (strcmp(copy, args[1]) == 0) { do_copy(args[2], args[3], args[4]); } else if (strcmp(dir, args[1]) == 0) do_dir(args[2]); else if (strcmp("info", args[1]) == 0){ info(args[2]); } else if (strcmp("freeblock", args[1]) == 0) free_block(); else if (strcmp("checkblock", args[1]) == 0) check_block(args[2]); else printf("Unknown dtool command.\n"); } return 0; }
/** * @brief calls do_file and do_directory on locations from the params struct * * @param params the parsed parameters * * @returns EXIT_SUCCESS, EXIT_FAILURE */ int do_location(params_t *params) { struct stat attr; char *location; do { /* ### FB: bei "multi-location support" wären Kommentare gut gewesen */ location = params->location; if (!location) { location = "."; } /* * try reading the attributes of the location * to verify that it exists and to check if it is a directory */ if (lstat(location, &attr) == 0) { do_file(location, params, attr); /* if a directory, process its contents */ if (S_ISDIR(attr.st_mode)) { do_dir(location, params, attr); } } else { fprintf(stderr, "%s: lstat(%s): %s\n", program_name, location, strerror(errno)); return EXIT_FAILURE; } params = params->next; } while (params && params->location); /* GNU find returns 1 even if a single entry failed */ /* * ### FB: ich nehme an, dass hier die errno von tieferen Calls durchgereicht wird? * wäre vielleicht übersichtlicher gewesen, wenn die Funktionen error-codes liefern würden * sollte hier die errno von lstat gemeint sein, ist das Erreichen nicht möglich */ if (errno != 0) { return EXIT_FAILURE; } return EXIT_SUCCESS; }
main(int argc, char **argv) { DIR *d; int x; if (argc < 2) { printf("Usage is: %s [-v] <RCS commands...>\n", argv[0]); exit(1); } for (x = 1; x < argc; ++x) { strcat(cmd, argv[x]); strcat(cmd, " "); } d = opendir("."); strcpy(curdir, "."); if (!d) { perror("."); exit(1); } do_dir(d); closedir(d); return(0); }
/** * @brief calls do_file and do_directory on locations from the params struct * * @param params the parsed parameters * * @returns EXIT_SUCCESS, EXIT_FAILURE */ int do_location(params_t *params) { struct stat attr; char *location; do { location = params->location; if (!location) { location = "."; } /* * try reading the attributes of the location * to verify that it exists and to check if it is a directory */ if (lstat(location, &attr) == 0) { do_file(location, params, attr); /* if a directory, process its contents */ if (S_ISDIR(attr.st_mode)) { do_dir(location, params, attr); } } else { fprintf(stderr, "%s: lstat(%s): %s\n", program_name, location, strerror(errno)); return EXIT_FAILURE; } params = params->next; } while (params && params->location); /* GNU find returns 1 even if a single entry failed */ if (errno != 0) { return EXIT_FAILURE; } return EXIT_SUCCESS; }
/** * * \brief Iterates through directory. * * \param dir_name directory where to iterate through. * \param params is the program argument vector. * * \return void */ static int do_dir(const char* dir_name, const char* const* params) { DIR* dirhandle = NULL; struct dirent* dirp = NULL; /*open directory catch error*/ dirhandle = opendir(dir_name); if (NULL == dirhandle) { snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "`%s': %s", dir_name, strerror(errno)); print_error(get_print_buffer()); return EXIT_SUCCESS; } errno = 0; while ((dirp = readdir(dirhandle))) { /* fetch each file from directory, until pointer is NULL */ StatType file_info; if ((strcmp(dirp->d_name, ".") == 0) || (strcmp(dirp->d_name, "..") == 0)) { /* '.' and '..' are not interesting */ continue; } /* build complete path to file (DIR/FILE) */ snprintf(get_path_buffer(), get_max_path_length(), "%s/%s", dir_name, dirp->d_name); /* get information about the file and catch errors */ if (-1 == lstat(get_path_buffer(), &file_info)) { snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "`%s': %s", get_path_buffer(), strerror(errno)); print_error(get_print_buffer()); /* check next file */ continue; } if (S_ISDIR(file_info.st_mode)) { char* next_path = NULL; do_file(get_path_buffer(), &file_info, params); #if DEBUG_OUTPUT snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "Move into directory %s.\n", dirp->d_name); #endif /* DEBUG_OUTPUT */ debug_print(get_print_buffer()); /* recursion for each directory in current directory */ next_path = (char*) malloc(get_max_path_length() * sizeof(char)); if (NULL == next_path) { print_error("malloc() failed: Out of memory."); if (closedir(dirhandle) < 0) { snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "`%s': closedir() failed: %s.", dir_name, strerror(errno)); print_error(get_print_buffer()); } return EXIT_FAILURE; } strcpy(next_path, get_path_buffer()); if (EXIT_FAILURE == do_dir(next_path, params)) { if (closedir(dirhandle) < 0) { snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "`%s': closedir() failed: %s.", dir_name, strerror(errno)); print_error(get_print_buffer()); } free(next_path); return EXIT_FAILURE; } free(next_path); } else { do_file(get_path_buffer(), &file_info, params); } errno = 0; /* reset errno for next call to readdir() */ } if (0 != errno) { snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "`%s': readdir() failed: %s.", dir_name, strerror(errno)); print_error(get_print_buffer()); } if (closedir(dirhandle) < 0) { snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "`%s':closedir() failed: %s.", dir_name, strerror(errno)); print_error(get_print_buffer()); } return EXIT_SUCCESS; }
/** * * \brief main implements a a simple replacement for Linux find. * * This is the main entry point for any C program. * * \param argc the number of arguments. * \param argv the arguments itself (including the program name in argv[0]). * * \return EXIT_SUCCESS on success EXIT_FAILURE on error. * \retval EXIT_SUCCESS Program ended successfully. * \retval EXIT_FAILURE Program ended with failure. */ int main(int argc, const char* argv[]) { int result = EXIT_FAILURE; char* start_dir = NULL; char* found_dir = NULL; boolean path_given = FALSE; StatType stbuf; int current_argument = 1; /* the first argument is the program name anyway */ int test_char = '\0'; result = init(argv); if (EXIT_SUCCESS != result) { cleanup(TRUE); } if (argc <= 1) { print_usage(); return EXIT_SUCCESS; } test_char = *argv[1]; path_given = (test_char == '-') ? FALSE : TRUE; /* check the input arguments first */ while (current_argument < argc) { if (0 == strcmp(PARAM_STR_USER, argv[current_argument])) { /* found -user */ if ((current_argument + 1) < argc) { current_argument += 2; continue; } else { print_error("Missing argument to `-user'."); cleanup(TRUE); } } if (0 == strcmp(PARAM_STR_NOUSER, argv[current_argument])) { /* found -nouser */ current_argument += 1; continue; } if (0 == strcmp(PARAM_STR_LS, argv[current_argument])) { /* found -ls */ current_argument += 1; continue; } if (0 == strcmp(PARAM_STR_PRINT, argv[current_argument])) { /* found -print */ current_argument += 1; continue; } if (0 == strcmp(PARAM_STR_NAME, argv[current_argument])) { /* found -name */ if (argc > (current_argument + 1)) { current_argument += 2; continue; } else { print_error("Missing argument to `-name'."); cleanup(TRUE); } } if (0 == strcmp(PARAM_STR_PATH, argv[current_argument])) { /* found -path */ if (argc > (current_argument + 1)) { current_argument += 2; continue; } else { print_error("Missing argument to `-path'."); cleanup(TRUE); } } if (0 == strcmp(PARAM_STR_TYPE, argv[current_argument])) { /* found -type */ if (argc > (current_argument + 1)) { const char* next_argument = argv[current_argument + 1]; if (strlen(next_argument) > 1) { snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "Argument of -type must be one character of these `%s'.", PARAM_STR_TYPE_VALS); print_error(get_print_buffer()); return EXIT_FAILURE; } test_char = (int) (*next_argument); if (NULL == strchr(PARAM_STR_TYPE_VALS, test_char)) { snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "Argument -type unknown options of %s: %c.", PARAM_STR_TYPE, *next_argument); print_error(get_print_buffer()); return EXIT_FAILURE; } current_argument += 2; continue; } else { print_error("Missing argument to `-type'."); cleanup(TRUE); } } if (current_argument > 1) { /* we have an unknown option */ snprintf(get_print_buffer(), MAX_PRINT_BUFFER, "Invalid predicate `%s'.", argv[current_argument]); print_error(get_print_buffer()); cleanup(TRUE); } ++current_argument; } /* determine the directory for start */ get_path_buffer()[0] = '\0'; start_dir = (char*) malloc(get_max_path_length() * sizeof(char)); if (NULL == start_dir) { free(start_dir); start_dir = NULL; print_error("malloc() failed: Out of memory."); cleanup(TRUE); } /* build complete path to file (DIR/FILE) */ snprintf(get_path_buffer(), get_max_path_length(), "%s", argv[1]); parameter_directory_given = TRUE; /*get information about the file and catch errors*/ if (!path_given) { /* no search path defined - we set it to work directory and start */ parameter_directory_given = FALSE; result = do_dir(".", argv); } else if (-1 != lstat(get_path_buffer(), &stbuf)) { /*search path defined */ result = do_file(argv[1], &stbuf, argv); if (S_ISDIR(stbuf.st_mode)) { found_dir = get_path_buffer(); strcpy(start_dir, found_dir); /* this condition should not happen, but its better to be safe */ if (NULL == found_dir) { /* use current directory */ parameter_directory_given = FALSE; strcpy(start_dir, "."); } result = do_dir(start_dir, argv); } } /* cleanup */ free(start_dir); start_dir = NULL; cleanup(FALSE); return result; }
static duc_dir *do_dir(duc_dir *dir, int depth) { int top = 0; int cur = 0; for(;;) { int attr_size, attr_name, attr_class, attr_graph, attr_bar, attr_cursor; if(opt_color) { attr_size = COLOR_PAIR(PAIR_SIZE); attr_name = COLOR_PAIR(PAIR_NAME); attr_bar = COLOR_PAIR(PAIR_BAR); attr_cursor = COLOR_PAIR(PAIR_CURSOR); attr_graph = COLOR_PAIR(PAIR_GRAPH); attr_class = COLOR_PAIR(PAIR_CLASS); } else { attr_size = 0; attr_name = A_BOLD; attr_class = 0; attr_graph = 0; attr_bar = A_REVERSE; attr_cursor = A_REVERSE; use_default_colors(); } duc_size_type st = opt_apparent ? DUC_SIZE_TYPE_APPARENT : DUC_SIZE_TYPE_ACTUAL; /* Iterate all dirents to find largest size */ duc_dir_seek(dir, 0); off_t size_max = 1; struct duc_dirent *e; while( (e = duc_dir_read(dir, st)) != NULL) { off_t size = opt_apparent ? e->size.apparent : e->size.actual; if(size > size_max) size_max = size; } int count = duc_dir_get_count(dir); int pgsize = rows - 2; /* Check boundaries */ if(cur < 0) cur = 0; if(cur > count - 1) cur = count - 1; if(cur < top) top = cur; if(cur > top + pgsize - 1) top = cur - pgsize + 1; if(top < 0) top = 0; /* Draw header */ char *path = duc_dir_get_path(dir); attrset(attr_bar); mvhline(0, 0, ' ', cols); mvprintw(0, 1, " %s ", path); attrset(0); free(path); /* Draw footer */ struct duc_size size; duc_dir_get_size(dir, &size); char siz[32], cnt[32]; duc_human_size(&size, st, opt_bytes, siz, sizeof siz); duc_human_number(count, opt_bytes, cnt, sizeof cnt); attrset(attr_bar); mvhline(rows-1, 0, ' ', cols); mvprintw(rows-1, 0, " Total %sB in %s files/directories", siz, cnt); attrset(0); /* Draw dirents */ duc_dir_seek(dir, top); int i; int y = 1; for(i=top; i<top + pgsize; i++) { struct duc_dirent *e = duc_dir_read(dir, st); attrset(cur == i ? attr_cursor : 0); mvhline(y, 0, ' ', cols); if(e) { off_t size = opt_apparent ? e->size.apparent : e->size.actual; size_t max_size_len = opt_bytes ? 12 : 7; char class = duc_file_type_char(e->type); char siz[32]; duc_human_size(&e->size, st, opt_bytes, siz, sizeof siz); if(cur != i) attrset(attr_size); printw("%*s", max_size_len, siz); printw(" "); char *p = e->name; if(cur != i) attrset(attr_name); while(*p) { if(*(uint8_t *)p >= 32) { printw("%c", *p); } else { printw("^%c", *p+64); } p++; } if(cur != i) attrset(attr_class); printw("%c", class); int w = cols - 30; if(w > cols / 2) w = cols / 2; if(opt_graph && w > 2) { int j; off_t g = w * size / size_max; if(cur != i) attrset(attr_graph); mvprintw(y, cols - w - 4, " ["); for(j=0; j<w; j++) printw("%s", j < g ? "=" : " "); printw("] "); } } else { attrset(A_DIM); mvprintw(y, 0, "~"); } y++; } duc_dir *dir2 = NULL; /* Handle key */ int c = getch(); switch(c) { case 'k': case KEY_UP: cur--; break; case 'j': case KEY_DOWN: cur++; break; case 21: cur -= pgsize/2; break; case KEY_PPAGE: cur -= pgsize; break; case 4: cur += pgsize/2; break; case KEY_NPAGE: cur += pgsize; break; case KEY_RESIZE: getmaxyx(stdscr, rows, cols); break; case 'a': opt_apparent ^= 1; break; case 'b': opt_bytes ^= 1; break; case 'c': opt_color ^= 1; break; case 'g': opt_graph ^= 1; break; case 'h': help(); break; case 27: case 'q': exit(0); break; case KEY_BACKSPACE: case KEY_LEFT: if(depth > 0) { return NULL; } else { dir2 = duc_dir_openat(dir, ".."); if(dir2) { do_dir(dir2, 0); duc_dir_close(dir2); } } break; case KEY_RIGHT: case '\r': case '\n': duc_dir_seek(dir, cur); struct duc_dirent *e = duc_dir_read(dir, st); if(e->type == DUC_FILE_TYPE_DIR) { dir2 = duc_dir_openent(dir, e); if(dir2) { do_dir(dir2, depth + 1); duc_dir_close(dir2); } } break; default: break; } }
/** * @brief calls do_file on each directory entry recursively * * @param path the path to be processed * @param params the parsed parameters * @param attr the entry attributes from lstat * * @returns EXIT_SUCCESS, EXIT_FAILURE */ int do_dir(char *path, params_t *params, struct stat attr) { DIR *dir; struct dirent *entry; size_t length; char *slash = ""; char *full_path; dir = opendir(path); if (!dir) { fprintf(stderr, "%s: opendir(%s): %s\n", program_name, path, strerror(errno)); return EXIT_FAILURE; } while ((entry = readdir(dir))) { /* skip '.' and '..' */ if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } /* add a trailing slash if not present */ length = strlen(path); if (path[length - 1] != '/') { /* ### FB: sehr gute Idee! */ slash = "/"; } /* allocate memory for the full entry path */ length = strlen(path) + strlen(entry->d_name) + 2; full_path = malloc(sizeof(char) * length); if (!full_path) { fprintf(stderr, "%s: malloc(): %s\n", program_name, strerror(errno)); break; /* a return would require a closedir() */ } /* concat the path with the entry name */ if (snprintf(full_path, length, "%s%s%s", path, slash, entry->d_name) < 0) { fprintf(stderr, "%s: snprintf(): %s\n", program_name, strerror(errno)); free(full_path); break; } /* process the entry */ if (lstat(full_path, &attr) == 0) { /* * there are no returns for do_file and do_dir on purpose here; * it is normal for a single entry to fail, then we try the next one */ do_file(full_path, params, attr); /* if a directory, call the function recursively */ if (S_ISDIR(attr.st_mode)) { do_dir(full_path, params, attr); } } else { fprintf(stderr, "%s: lstat(%s): %s\n", program_name, full_path, strerror(errno)); free(full_path); continue; } free(full_path); } if (closedir(dir) != 0) { fprintf(stderr, "%s: closedir(%s): %s\n", program_name, path, strerror(errno)); return EXIT_FAILURE; } return EXIT_SUCCESS; }
static void do_dir(DIR *d) { long pos; int olen; struct dirent *de; struct stat sb; char *nm; olen = strlen(curdir); while (de = readdir(d)) { /* * Skip names starting in ".", and "rcs" and "RCS" */ nm = de->d_name; if (!strcmp(nm, "rcs") || !strcmp(nm, "RCS") || (de->d_name[0] == '.')) { continue; } /* * Get dope on entry */ sprintf(buf, "%s/%s", curdir, nm); if (stat(buf, &sb) < 0) { continue; } /* * Recurse for directories. Because of limitations * on number of open files, we save the position and * close the current dir during the recursion. */ if ((sb.st_mode & S_IFMT) == S_IFDIR) { pos = telldir(d); closedir(d); sprintf(curdir+olen, "/%s", nm); if (d = opendir(curdir)) { do_dir(d); } else { perror(nm); } closedir(d); curdir[olen] = '\0'; d = opendir(curdir); seekdir(d, pos); continue; } /* * Make sure it's an RCS file. Allow with or without * the ",v" suffix. */ sprintf(buf, "%s/RCS/%s,v", curdir, nm); if (access(buf, 0) < 0) { sprintf(buf, "%s/RCS/%s", curdir, nm); if (access(buf, 0) < 0) { continue; } } /* * Apply the command to this file */ sprintf(buf, "%s %s/%s", cmd, curdir, nm); system(buf); } }