19 #include <sys/types.h>
23 #include <grass/gis.h>
24 #include <grass/config.h>
25 #include <grass/glocale.h>
27 #ifdef HAVE_SYS_IOCTL_H
28 # include <sys/ioctl.h>
34 static void *ls_closure;
37 static void *ls_ex_closure;
39 static int cmp_names(
const void *aa,
const void *bb)
41 char *
const *a = (
char *
const *)aa;
42 char *
const *
b = (
char *
const *)bb;
44 return strcmp(*a, *b);
69 ls_ex_closure = closure;
88 char **
G__ls(
const char *dir,
int *num_files)
92 char **dir_listing =
NULL;
99 if (dp->d_name[0] ==
'.')
101 if (ls_filter && !(*ls_filter)(dp->d_name, ls_closure))
103 if (ls_ex_filter && (*ls_ex_filter)(dp->d_name, ls_ex_closure))
105 dir_listing = (
char **)G_realloc(dir_listing, (1 + n) *
sizeof(
char *));
106 dir_listing[
n] =
G_store(dp->d_name);
111 qsort(dir_listing, n,
sizeof(
char *), cmp_names);
129 void G_ls(
const char *dir, FILE * stream)
132 char **dir_listing =
G__ls(dir, &n);
136 for (i = 0; i <
n; i++)
159 void G_ls_format(
char **list,
int num_items,
int perline, FILE * stream)
163 int field_width, column_height;
164 int screen_width = 80;
174 if (ioctl(fileno(stream), TIOCGWINSZ, (
char *)&size) == 0)
175 screen_width = size.ws_col;
182 for (i = 0; i < num_items; i++) {
184 if (strlen(list[i]) > max_len)
185 max_len = strlen(list[i]);
189 perline = screen_width / (max_len + 1);
195 field_width = screen_width / perline;
197 column_height = (num_items / perline) + ((num_items % perline) > 0);
201 = num_items + column_height - (num_items % column_height);
204 for (i = 1, next = list; i <= num_items; i++) {
207 next += column_height;
208 if (next >= list + num_items) {
210 next -= (max - 1 - (next < list + max ? column_height : 0));
211 fprintf(stream,
"%s\n", *cur);
214 fprintf(stream,
"%-*s", field_width, *cur);
void G_free(void *buf)
Free allocated memory.
char * G_store(const char *s)
Copy string to allocated memory.
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.
char ** G__ls(const char *dir, int *num_files)
Stores a sorted directory listing in an array.
void G_ls(const char *dir, FILE *stream)
Prints a directory listing 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__ls filtering.
int ls_filter_func(const char *, void *)
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
void G_set_ls_exclude_filter(ls_filter_func *func, void *closure)