18 #include <sys/types.h>
30 #ifdef HAVE_SYS_IOCTL_H
31 #include <sys/ioctl.h>
45 static int cmp_names(
const void *aa,
const void *bb)
47 char *
const *a = (
char *
const *)aa;
48 char *
const *
b = (
char *
const *)bb;
50 return strcmp(*a, *
b);
69 st->ls_closure = closure;
74 st->ls_ex_filter = func;
75 st->ls_ex_closure = closure;
94 char **
G_ls2(
const char *dir,
int *num_files)
98 char **dir_listing =
NULL;
101 if ((dfd = opendir(dir)) ==
NULL)
104 while ((dp = readdir(dfd)) !=
NULL) {
105 if (dp->d_name[0] ==
'.')
107 if (
st->ls_filter && !(*
st->ls_filter)(dp->d_name,
st->ls_closure))
109 if (
st->ls_ex_filter &&
110 (*
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)
186 unsigned int max_len = 0;
188 for (i = 0; i < num_items; i++) {
190 if (strlen(
list[i]) > max_len)
191 max_len = strlen(
list[i]);
203 column_height = (num_items / perline) + ((num_items % perline) > 0);
206 const int max = num_items + column_height - (num_items % column_height);
209 for (i = 1, next =
list; i <= num_items; i++) {
212 next += column_height;
213 if (next >=
list + num_items) {
215 next -= (
max - 1 - (next <
list +
max ? column_height : 0));
216 fprintf(stream,
"%s\n", *cur);
219 fprintf(stream,
"%-*s", field_width, *cur);
void G_free(void *)
Free allocated memory.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
char * G_store(const char *)
Copy string to allocated memory.
void G_ls(const char *dir, FILE *stream)
Prints a directory listing to a stream, in prettified column format.
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_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_ls2(const char *dir, int *num_files)
Stores a sorted directory listing in an array.
void G_set_ls_exclude_filter(ls_filter_func *func, void *closure)