libinstaller: Use SOURCE_DATE_EPOCH for synthesized modification time stamps
[syslinux.git] / com32 / modules / ls.c
blob47eacdbc450f38481a7ee51daa322a6a12aa7a1d
1 /*
2 * Display directory contents
3 */
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <console.h>
7 #include <string.h>
8 #include <com32.h>
9 #include <dirent.h>
10 #include <minmax.h>
11 #include <unistd.h>
12 #include <getkey.h>
14 static int rows, cols; /* Screen parameters */
16 #define DIR_CHUNK 1024
18 static const char *type_str(int type)
20 switch (type) {
21 case DT_FIFO:
22 return "[fif]";
23 case DT_CHR:
24 return "[chr]";
25 case DT_DIR:
26 return "[dir]";
27 case DT_BLK:
28 return "[blk]";
29 case DT_UNKNOWN:
30 case DT_REG:
31 return "";
32 case DT_LNK:
33 return "[lnk]";
34 case DT_SOCK:
35 return "[sck]";
36 case DT_WHT:
37 return "[wht]";
38 default:
39 return "[???]";
43 static void free_dirents(struct dirent **dex, size_t n_de)
45 size_t i;
47 for (i = 0; i < n_de; i++)
48 free(dex[i]);
50 free(dex);
53 static int compare_dirent(const void *p_de1, const void *p_de2)
55 const struct dirent *de1 = *(const struct dirent **)p_de1;
56 const struct dirent *de2 = *(const struct dirent **)p_de2;
57 int ndir1, ndir2;
59 ndir1 = de1->d_type != DT_DIR;
60 ndir2 = de2->d_type != DT_DIR;
62 if (ndir1 != ndir2)
63 return ndir1 - ndir2;
65 return strcmp(de1->d_name, de2->d_name);
68 static int display_directory(const char *dirname)
70 DIR *dir;
71 struct dirent *de;
72 struct dirent **dex = NULL;
73 size_t n_dex = 0, n_de = 0;
74 size_t i, j, k;
75 size_t nrows, ncols, perpage;
76 size_t endpage;
77 int maxlen = 0;
78 int pos, tpos, colwidth;
80 dir = opendir(dirname);
81 if (!dir) {
82 printf("Unable to read directory: %s\n", dirname);
83 return -1;
86 while ((de = readdir(dir)) != NULL) {
87 struct dirent *nde;
89 if (n_de >= n_dex) {
90 struct dirent **ndex;
92 ndex = realloc(dex, (n_dex + DIR_CHUNK) * sizeof *dex);
93 if (!ndex)
94 goto nomem;
96 dex = ndex;
97 n_dex += DIR_CHUNK;
100 nde = malloc(de->d_reclen);
101 if (!nde)
102 goto nomem;
104 memcpy(nde, de, de->d_reclen);
105 dex[n_de++] = nde;
107 maxlen = max(maxlen, de->d_reclen);
110 closedir(dir);
112 qsort(dex, n_de, sizeof *dex, compare_dirent);
114 maxlen -= offsetof(struct dirent, d_name) + 1;
115 ncols = (cols + 2)/(maxlen + 8);
116 ncols = min(ncols, n_de);
117 ncols = max(ncols, 1U);
118 colwidth = (cols + 2)/ncols;
119 perpage = ncols * (rows - 1);
121 for (i = 0; i < n_de; i += perpage) {
122 /* Rows on this page */
123 endpage = min(i+perpage, n_de);
124 nrows = ((endpage-i) + ncols - 1)/ncols;
126 for (j = 0; j < nrows; j++) {
127 pos = tpos = 0;
128 for (k = i+j; k < endpage; k += nrows) {
129 pos += printf("%*s%-5s %s",
130 (tpos - pos), "",
131 type_str(dex[k]->d_type),
132 dex[k]->d_name);
133 tpos += colwidth;
135 printf("\n");
138 if (endpage >= n_de)
139 break;
141 get_key(stdin, 0);
144 free_dirents(dex, n_de);
145 return 0;
147 nomem:
148 closedir(dir);
149 printf("Out of memory error!\n");
150 free_dirents(dex, n_de);
151 return -1;
154 int main(int argc, char *argv[])
156 int rv;
158 if (getscreensize(1, &rows, &cols)) {
159 /* Unknown screen size? */
160 rows = 24;
161 cols = 80;
164 if (argc < 2)
165 rv = display_directory(".");
166 else if (argc == 2)
167 rv = display_directory(argv[1]);
168 else {
169 printf("Usage: %s directory\n", argv[0]);
170 rv = 1;
173 return rv ? 1 : 0;