Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //test
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <dirent.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <string.h>
- int parse(char *dirName) {
- DIR *dir;
- if ((dir = opendir(dirName)) == NULL) {
- printf("Error at opendir().");
- exit(1);
- }
- struct dirent *d;
- struct stat st;
- char path[50];
- int counter = 0;
- while ((d = readdir(dir))) {
- if ((strcmp(d->d_name, ".") == 0) || (strcmp(d->d_name, "..") == 0))
- continue;
- snprintf(path, 50, "%s/%s", dirName, d->d_name);
- printf("%s\n", path);
- if (lstat(path, &st) == -1) {
- printf("Error at lstat().");
- exit(2);
- }
- if (S_ISDIR(st.st_mode)) {
- counter += parse(path);
- }
- if (S_ISREG(st.st_mode)) {
- printf("yes\n");
- counter ++;
- }
- }
- closedir(dir);
- return counter;
- }
- int main (int argc, char *argv[]) {
- if (argc != 2) {
- printf("Usage: Program <%s> file directory", argv[0]);
- exit(0);
- }
- printf("Files: %d", parse(argv[1]));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement