int dirtree(char *dir,int *depth ) { struct dirent **pDirEntList; struct stat st; int total = 0; int index = 0; int count = 0; char *filename; char *format; /* change dir*/ total = scandir(dir,&pDirEntList,selectdir,alphasort); if(!total) { return 0; } if(-1 == chdir(dir)) { printf("error: chdir %s\n",dir); exit(-1); } while(index < total) { /* make format string*/ format = make_format(*depth); filename = pDirEntList[index]->d_name; printf("%s%s\n",format,filename); free(format); if(-1 == lstat(filename, &st)) { perror("lstat"); exit(-1); } if( S_IFDIR == (st.st_mode & S_IFMT)) { (*depth)++; count += dirtree(filename,depth); (*depth)--; } index++; count ++; } /* return to top dir*/ if(-1 == chdir("..")) { perror("chdir"); exit(-1); } return count; }
int main(int argc, char **argv) { int count = 0; int depth = 0; char filetype[10] = {0}; if(argc != 2) { printf("usage: %s dirname\n", argv[0]); exit(-1); } count = dirtree(argv[1], &depth); printf("total %d files\n", count); return 0; }
int main(int argc, char *argv[]) { int files; /* number of files in each dir */ int totfiles = 0; int dirs; /* directories in each dir */ int totdirs = 0; int levels; /* levels deep */ char *fname; char *dname; struct timeval time; char *opts; struct testparam *param; struct btest *b; char *config_file; char *test_dir; char *log_file; FILE *log; setbuf(stdout, NULL); Myname = *argv++; argc--; while(argc && **argv == '-') { for(opts = &argv[0][1]; *opts; opts++) { switch (*opts) { case 'h': /* help */ usage(); exit(1); break; case 's': /* silent */ Sflag++; break; case 't': /* time */ Tflag++; break; case 'f': /* funtionality */ Fflag++; break; case 'n': /* No Test Directory create */ Nflag++; break; default: error("unknown option '%c'", *opts); usage(); exit(1); } } argc--; argv++; } if(argc) { config_file = *argv; argc--; argv++; } else { fprintf(stderr, "Missing config_file"); exit(1); } if(argc != 0) { fprintf(stderr, "too many parameters"); usage(); exit(1); } param = readin_config(config_file); if(param == NULL) { fprintf(stderr, "Nothing built\n"); exit(1); } b = get_btest_args(param, ONE); if(b == NULL) { fprintf(stderr, "Missing basic test number 1 in the config file '%s'\n", config_file); free_testparam(param); exit(1); } if(b->levels == -1) { fprintf(stderr, "Missing 'levels' parameter in the config file '%s' for the basic test number 1\n", config_file); free_testparam(param); exit(1); } if(b->files == -1) { fprintf(stderr, "Missing 'files' parameter in the config file '%s' for the basic test number 1\n", config_file); free_testparam(param); exit(1); } if(b->dirs == -1) { fprintf(stderr, "Missing 'dirs' parameter in the config file '%s' for the basic test number 1\n", config_file); free_testparam(param); exit(1); } levels = b->levels; files = b->files; dirs = b->dirs; fname = b->fname; dname = b->dname; test_dir = get_test_directory(param); log_file = get_log_file(param); free_testparam(param); if(!Fflag) { Tflag = 0; levels = 2; files = 2; dirs = 2; } if(!Sflag) { fprintf(stdout, "%s: File and directory creation test\n", Myname); } if(!Nflag) testdir(test_dir); else mtestdir(test_dir); starttime(); dirtree(levels, files, dirs, fname, dname, &totfiles, &totdirs); endtime(&time); if(!Sflag) { fprintf(stdout, "\tcreated %d files %d directories %d levels deep", totfiles, totdirs, levels); } if(Tflag && !Sflag) { fprintf(stdout, " in %ld.%02ld seconds", (long)time.tv_sec, (long)time.tv_usec / 10000); } if(!Sflag) { fprintf(stdout, "\n"); } if((log = fopen(log_file, "a")) == NULL) { printf("Enable to open the file '%s'\n", log_file); complete(); } fprintf(log, "b1\t%d\t%d\t%d\t%ld.%02ld\n", totfiles, totdirs, levels, (long)time.tv_sec, (long)time.tv_usec / 10000); fclose(log); complete(); }
int main(int argc, char *argv[]) { #ifdef use_directs struct direct *dp; #else struct dirent *dp; #endif char *fname = FNAME; int files = 200; /* number of files in each dir */ int fi; int count = 200; /* times to read dir */ int ct; int entries = 0; int totfiles = 0; int totdirs = 0; DIR *dir; struct timeval time; char *p, str[MAXPATHLEN]; char *opts; int err, i, dot, dotdot; int nmoffset; umask(0); setbuf(stdout, NULL); Myname = *argv++; argc--; while (argc && **argv == '-') { for (opts = &argv[0][1]; *opts; opts++) { switch (*opts) { case 'h': /* help */ usage(); exit(1); break; case 't': /* time */ Tflag++; break; case 'f': /* funtionality */ Fflag++; break; case 'n': /* No Test Directory create */ Nflag++; break; case 'i': /* ignore spurious files */ Iflag++; break; default: error("unknown option '%c'", *opts); usage(); exit(1); } } argc--; argv++; } if (argc) { files = getparm(*argv, 1, "files"); argv++; argc--; } if (argc) { count = getparm(*argv, 1, "count"); argv++; argc--; } if (argc) { fname = *argv; argv++; argc--; } if (argc) { usage(); exit(1); } nmoffset = strlen(fname); if (Fflag) { Tflag = 0; count = 1; } if (count > files) { error("count (%d) can't be greater than files (%d)", count, files); exit(1); } if (files > MAXFILES) { error("too many files requested (max is %d)", MAXFILES); exit(1); } fprintf(stdout, "%s: readdir\n", Myname); if (!Nflag) testdir(NULL); else mtestdir(NULL); dirtree(1, files, 0, fname, DNAME, &totfiles, &totdirs); if (Tflag) { starttime(); } if ((dir = opendir(".")) == NULL) { error("can't opendir %s", "."); exit(1); } for (ct = 0; ct < count; ct++) { rewinddir(dir); dot = 0; dotdot = 0; err = 0; for (i = 0; i < sizeof(bitmap); i++) bitmap[i] = 0; while ((dp = readdir(dir)) != NULL) { entries++; if (strcmp(".", dp->d_name) == 0) { if (dot) { /* already read dot */ error("'.' dir entry read twice"); exit(1); } dot++; continue; } else if (strcmp("..", dp->d_name) == 0) { if (dotdot) { /* already read dotdot */ error("'..' dir entry read twice"); exit(1); } dotdot++; continue; } /* * at this point, should have entry of the form * fname%d */ /* If we don't have our own directory, ignore such errors (if Iflag set). */ if (strncmp(dp->d_name, fname, nmoffset)) { if (Iflag) continue; else { error("unexpected dir entry '%s'", dp->d_name); exit(1); } } /* get ptr to numeric part of name */ p = dp->d_name + nmoffset; fi = atoi(p); if (fi < 0 || fi >= MAXFILES) { error("unexpected dir entry '%s'", dp->d_name); exit(1); } if (BIT(fi)) { error("duplicate '%s' dir entry read", dp->d_name); err++; } else SETBIT(fi); } /* end readdir loop */ if (!dot) { error("didn't read '.' dir entry, pass %d", ct); err++; } if (!dotdot) { error("didn't read '..' dir entry, pass %d", ct); err++; } for (fi = 0; fi < ct; fi++) { if (BIT(fi)) { sprintf(str, "%s%d", fname, fi); error("unlinked '%s' dir entry read pass %d", str, ct); err++; } } for (fi = ct; fi < files; fi++) { if (!BIT(fi)) { sprintf(str, "%s%d", fname, fi); error("\ didn't read expected '%s' dir entry, pass %d", str, ct); err++; } } if (err) { error("Test failed with %d errors", err); exit(1); } sprintf(str, "%s%d", fname, ct); if (unlink(str) < 0) { error("can't unlink %s", str); exit(1); } }
int main(int argc, char *argv[]) { int files = 10; /* number of files in each dir */ int fi; int count = 50; /* times to do each file */ int ct; int totfiles = 0; int totdirs = 0; char *fname = FNAME; struct timeval time; char str[MAXPATHLEN]; struct stat statb; char *opts; umask(0); setbuf(stdout, NULL); Myname = *argv++; argc--; while (argc && **argv == '-') { for (opts = &argv[0][1]; *opts; opts++) { switch (*opts) { case 'h': /* help */ usage(); exit(1); break; case 't': /* time */ Tflag++; break; case 'f': /* funtionality */ Fflag++; break; case 'n': /* suppress initial directory */ Nflag++; break; default: error("unknown option '%c'", *opts); usage(); exit(1); } } argc--; argv++; } if (argc) { files = getparm(*argv, 1, "files"); argv++; argc--; } if (argc) { count = getparm(*argv, 1, "count"); argv++; argc--; } if (argc) { fname = *argv; argc--; argv++; } if (argc) { usage(); exit(1); } if (Fflag) { Tflag = 0; count = 1; } if (!Nflag) testdir(NULL); else mtestdir(NULL); dirtree(1, files, 0, fname, DNAME, &totfiles, &totdirs); fprintf(stdout, "%s: getattr and lookup\n", Myname); if (Tflag) { starttime(); } for (ct = 0; ct < count; ct++) { for (fi = 0; fi < files; fi++) { sprintf(str, "%s%d", fname, fi); if (stat(str, &statb) < 0) { error("can't stat %s", str); exit(1); } } } if (Tflag) { endtime(&time); } fprintf(stdout, "\t%d stats on %d files", files * count * 2, files); if (Tflag) { fprintf(stdout, " in %ld.%-2ld seconds", (long)time.tv_sec, (long)time.tv_usec / 10000); } fprintf(stdout, "\n"); /* XXX REMOVE DIRECTORY TREE? */ complete(); return 0; }