Advertisement
anechka_ne_plach

Untitled

Feb 21st, 2022
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.62 KB | None | 0 0
  1.    1   │ #include <stdio.h>
  2.    2   │ #include <dirent.h>
  3.    3   │ #include <sys/types.h>
  4.    4   │ #include <sys/stat.h>
  5.    5   │ #include <fcntl.h>
  6.    6   │ #include <stdlib.h>
  7.    7   │ #include <limits.h>
  8.    8   │ #include <ctype.h>
  9.    9   │ #include <unistd.h>
  10.   10   │ #include <string.h>
  11.   11   │
  12.   12   │ int cmp(const void* a, const void* b) {
  13.   13   │     const char* A = *(const char**)a;
  14.   14   │     const char* B = *(const char**)b;
  15.   15   │     return strcasecmp(A, B);
  16.   16   │ }
  17.   17   │
  18.   18   │ char gbuf[PATH_MAX];
  19.   19   │
  20.   20   │ void traverse(char* dir, char* myname, int size) {
  21.   21   │     DIR* d = opendir(dir);
  22.   22   │     if (d) {
  23.   23   │         char** names = NULL;
  24.   24   │         int a = 0, u = 0;
  25.   25   │
  26.   26   │         if (myname) {
  27.   27   │             printf("cd %s\n", myname);
  28.   28   │         }
  29.   29   │
  30.   30   │         struct dirent* dd;
  31.   31   │         while ((dd = readdir(d))) {
  32.   32   │             if (strcmp(dd->d_name, ".") && strcmp(dd->d_name, "..")) {
  33.   33   │                 char buf[PATH_MAX];
  34.   34   │                 if (snprintf(buf, sizeof(buf), "%s/%s", dir, dd->d_name) < sizeof(buf)) {
  35.   35   │                     struct stat stb;
  36.   36   │                     if (lstat(buf, &stb) >= 0 && S_ISDIR(stb.st_mode)) {
  37.   37   │                         if (u == a) {
  38.   38   │                             if (!(a *= 2)) a = 64;
  39.   39   │                             names = realloc(names, a * sizeof(names[0]));
  40.   40   │                             if (!names) abort();
  41.   41   │                         }
  42.   42   │                         names[u++] = strdup(dd->d_name);
  43.   43   │                     }
  44.   44   │                 }
  45.   45   │             }
  46.   46   │         }
  47.   47   │         closedir(d);
  48.   32   │             if (strcmp(dd->d_name, ".") && strcmp(dd->d_name, "..")) {
  49.   33   │                 char buf[PATH_MAX];
  50.   34   │                 if (snprintf(buf, sizeof(buf), "%s/%s", dir, dd->d_name) < sizeof(buf)) {
  51.   35   │                     struct stat stb;
  52.   36   │                     if (lstat(buf, &stb) >= 0 && S_ISDIR(stb.st_mode)) {
  53.   37   │                         if (u == a) {
  54.   38   │                             if (!(a *= 2)) a = 64;
  55.   39   │                             names = realloc(names, a * sizeof(names[0]));
  56.   40   │                             if (!names) abort();
  57.   41   │                         }
  58.   42   │                         names[u++] = strdup(dd->d_name);
  59.   43   │                     }
  60.   44   │                 }
  61.   45   │             }
  62.   46   │         }
  63.   47   │         closedir(d);
  64.   48   │
  65.   49   │         qsort(names, u, sizeof(names[0]), cmp);
  66.   50   │         for (int i = 0; i < u; ++i) {
  67.   51   │             int new_size = size + 1 + strlen(names[i]);
  68.   52   │             dir[size] = '/';
  69.   53   │             memcpy(dir + size + 1, names[i], strlen(names[i]) + 1);
  70.   54   │             traverse(dir, names[i], new_size);
  71.   55   │             dir[size] = '\0';
  72.   56   │         }
  73.   57   │
  74.   58   │         free(names);
  75.   59   │
  76.   60   │         if (myname) {
  77.   61   │             printf("cd ..\n");
  78.   62   │         }
  79.   63   │     }
  80.   64   │ }
  81.   65   │
  82.   66   │ int main(int argc, char* argv[]) {
  83.   67   │     memset(gbuf, 0, sizeof(gbuf));
  84.   68   │     if (strlen(argv[1]) < sizeof(gbuf)) {
  85.   69   │         memcpy(gbuf, argv[1], strlen(argv[1]));
  86.   70   │         traverse(gbuf, NULL, strlen(gbuf));
  87.   71   │     }
  88.   72   │ }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement