19 #include <sys/types.h> 31 #ifdef HAVE_SYS_IOCTL_H 32 # include <sys/ioctl.h> 46 static int cmp_names(
const void *aa,
const void *bb)
48 char *
const *a = (
char *
const *)aa;
49 char *
const *
b = (
char *
const *)bb;
51 return strcmp(*a, *b);
70 st->ls_closure = closure;
75 st->ls_ex_filter = func;
76 st->ls_ex_closure = closure;
95 char **
G_ls2(
const char *dir,
int *num_files)
99 char **dir_listing =
NULL;
106 if (dp->d_name[0] ==
'.')
108 if (st->ls_filter && !(*st->ls_filter)(dp->d_name, st->ls_closure))
110 if (st->ls_ex_filter && (*st->ls_ex_filter)(dp->d_name, st->ls_ex_closure))
112 dir_listing = (
char **)
G_realloc(dir_listing, (1 + n) *
sizeof(
char *));
113 dir_listing[n] =
G_store(dp->d_name);
119 qsort(dir_listing, n,
sizeof(
char *), cmp_names);
137 void G_ls(
const char *dir, FILE * stream)
140 char **dir_listing =
G_ls2(dir, &n);
144 for (i = 0; i < n; i++)
169 int field_width, column_height;
180 if (ioctl(fileno(stream), TIOCGWINSZ, (
char *)&size) == 0)
181 screen_width = size.ws_col;
188 for (i = 0; i < num_items; i++) {
190 if (strlen(list[i]) > max_len)
191 max_len = strlen(list[i]);
195 perline = screen_width / (max_len + 1);
201 field_width = screen_width / perline;
203 column_height = (num_items / perline) + ((num_items % perline) > 0);
207 = num_items + column_height - (num_items % column_height);
210 for (i = 1, next = list; i <= num_items; i++) {
213 next += column_height;
214 if (next >= list + num_items) {
216 next -= (max - 1 - (next < list + max ? column_height : 0));
217 fprintf(stream,
"%s\n", *cur);
220 fprintf(stream,
"%-*s", field_width, *cur);
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_ls_format(char **list, int num_items, int perline, FILE *stream)
Prints a listing of items to a stream, in prettified column format.
void G_free(void *)
Free allocated memory.
void G_ls(const char *dir, FILE *stream)
Prints a directory listing to a stream, in prettified column format.
char ** G_ls2(const char *dir, int *num_files)
Stores a sorted directory listing in an array.
void G_set_ls_filter(ls_filter_func *func, void *closure)
Sets a function and its complementary data for G_ls2 filtering.
int ls_filter_func(const char *, void *)
char * G_store(const char *)
Copy string to allocated memory.
void G_set_ls_exclude_filter(ls_filter_func *func, void *closure)