Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/file.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <limits.h>
- #include <ctype.h>
- #include <dirent.h>
- #include <string.h>
- enum { buf_size = 100 };
- void dfs(char *path_to_dir, char *printing_name, int need_to_be_printed) {
- DIR *d;
- if (!(d = opendir(path_to_dir))) {
- return;
- }
- if (need_to_be_printed) {
- printf("cd %s\n", printing_name);
- }
- char name[buf_size][PATH_MAX], result_path[buf_size][PATH_MAX]; // плохо!
- struct dirent *entry;
- struct stat s;
- char *dv = "/";
- if (!strcmp(path_to_dir, "/")) {
- dv = "";
- }
- int cnt = 0;
- while ((entry = readdir(d))) {
- if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
- continue;
- }
- snprintf(name[cnt++], PATH_MAX, "%s", entry->d_name);
- snprintf(result_path[cnt - 1], PATH_MAX, "%s%s%s", path_to_dir, dv, entry->d_name);
- if (lstat(result_path[cnt - 1], &s) < 0) {
- cnt--;
- continue;
- }
- if (!(S_ISDIR(s.st_mode))) {
- cnt--;
- }
- }
- closedir(d);
- int i, j, tmp;
- int order[buf_size];
- for (i = 0; i < cnt; i++) {
- order[i] = i;
- }
- for (i = 0; i < cnt; i++)
- for (j = i + 1; j < cnt; j++)
- if (strcasecmp(name[order[i]], name[order[j]]) > 0) {
- tmp = order[i];
- order[i] = order[j];
- order[j] = tmp;
- }
- for (i = 0; i < cnt; i++) {
- snprintf(result_path[i], PATH_MAX, "%s%s%s", path_to_dir, dv, name[order[i]]);
- dfs(result_path[i], name[order[i]], 1);
- }
- if (need_to_be_printed) {
- printf("cd ..");
- }
- }
- int main(int argc, char *argv[])
- {
- char *start = "";
- dfs(argv[1], start, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement