Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <dirent.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdint.h>
- #include <inttypes.h>
- #include <string.h>
- enum
- {
- DIR_SIZE = 20
- };
- int
- cmp(const void *a, const void *b)
- {
- return strcasecmp(*(const char **)a, *(const char **)b);
- }
- char *
- name(char *path) {
- int len = strlen(path);
- int ind;
- for (ind = len - 1; ind >= 0; --ind) {
- if (path[ind] == '/') {
- return path + ind + 1;
- }
- }
- return path;
- }
- void
- check_dir(char *dir_path)
- {
- DIR *dir = opendir(dir_path);
- if (dir == NULL) {
- return;
- }
- printf("cd %s\n", name(dir_path));
- char files[DIR_SIZE][PATH_MAX];
- int size = 0;
- struct dirent *dirent;
- while ((dirent = readdir(dir)) != NULL) {
- struct stat st;
- if (!strcmp(dirent->d_name, "..") ||
- !strcmp(dirent->d_name, ".")) {
- continue;
- }
- char name[PATH_MAX];
- snprintf(name, sizeof(name), "%s/%s", dir_path, dirent->d_name);
- stat(name, &st);
- if (S_ISDIR(st.st_mode)) {
- strcpy(files[size], name);
- ++size;
- }
- }
- for (int i = 0; i < size; ++i)
- printf("%s\n", files[i]);
- qsort(files, size, sizeof(char[PATH_MAX]), cmp);
- for (int i = 0; i < size; ++i) {
- check_dir(files[i]);
- }
- printf("cd ..\n");
- }
- int
- main(int argc, char *argv[])
- {
- check_dir(argv[1]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement