ls.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. \file ls.c
  3. \brief Functions to list the files in a directory.
  4. \author Paul Kelly
  5. (C) 2007, 2008 by the GRASS Development Team
  6. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/types.h>
  14. #include <dirent.h>
  15. #include <unistd.h>
  16. #include <grass/gis.h>
  17. #include <grass/config.h>
  18. #include <grass/glocale.h>
  19. #ifdef HAVE_TERMIOS_H
  20. # include <termios.h>
  21. #endif
  22. #ifdef HAVE_SYS_IOCTL_H
  23. # include <sys/ioctl.h>
  24. #endif
  25. typedef int ls_filter_func(const char * /*filename */ , void * /*closure */ );
  26. static struct state {
  27. ls_filter_func *ls_filter;
  28. void *ls_closure;
  29. ls_filter_func *ls_ex_filter;
  30. void *ls_ex_closure;
  31. } state;
  32. static struct state *st = &state;
  33. static int cmp_names(const void *aa, const void *bb)
  34. {
  35. char *const *a = (char *const *)aa;
  36. char *const *b = (char *const *)bb;
  37. return strcmp(*a, *b);
  38. }
  39. /**
  40. * \brief Sets a function and its complementary data for G__ls filtering.
  41. *
  42. * Defines a filter function and its rule data that allow G__ls to filter out
  43. * unwanted file names. Call this function before G__ls.
  44. *
  45. * \param func Filter callback function to compare a file name and closure
  46. * pattern (if NULL, no filter will be used).
  47. * func(filename, closure) should return 1 on success, 0 on
  48. * failure.
  49. * \param closure Data used to determine if a file name matches the rule.
  50. **/
  51. void G_set_ls_filter(ls_filter_func *func, void *closure)
  52. {
  53. st->ls_filter = func;
  54. st->ls_closure = closure;
  55. }
  56. void G_set_ls_exclude_filter(ls_filter_func *func, void *closure)
  57. {
  58. st->ls_ex_filter = func;
  59. st->ls_ex_closure = closure;
  60. }
  61. /**
  62. * \brief Stores a sorted directory listing in an array
  63. *
  64. * The filenames in the specified directory are stored in an array of
  65. * strings, then sorted alphabetically. Each filename has space allocated
  66. * using G_store(), which can be freed using G_free() if necessary. The
  67. * same goes for the array itself.
  68. *
  69. *
  70. * \param dir Directory to list
  71. * \param num_files Pointer to an integer in which the total number of
  72. * files listed will be stored
  73. *
  74. * \return Pointer to array of strings containing the listing
  75. **/
  76. char **G__ls(const char *dir, int *num_files)
  77. {
  78. struct dirent *dp;
  79. DIR *dfd;
  80. char **dir_listing = NULL;
  81. int n = 0;
  82. if ((dfd = opendir(dir)) == NULL)
  83. G_fatal_error(_("Unable to open directory %s"), dir);
  84. while ((dp = readdir(dfd)) != NULL) {
  85. if (dp->d_name[0] == '.') /* Don't list hidden files */
  86. continue;
  87. if (st->ls_filter && !(*st->ls_filter)(dp->d_name, st->ls_closure))
  88. continue;
  89. if (st->ls_ex_filter && (*st->ls_ex_filter)(dp->d_name, st->ls_ex_closure))
  90. continue;
  91. dir_listing = (char **)G_realloc(dir_listing, (1 + n) * sizeof(char *));
  92. dir_listing[n] = G_store(dp->d_name);
  93. n++;
  94. }
  95. /* Sort list of filenames alphabetically */
  96. qsort(dir_listing, n, sizeof(char *), cmp_names);
  97. *num_files = n;
  98. return dir_listing;
  99. }
  100. /**
  101. * \brief Prints a directory listing to a stream, in prettified column format
  102. *
  103. * A replacement for system("ls -C"). Lists the contents of the directory
  104. * specified to the given stream, e.g. stderr. Tries to determine an
  105. * appropriate column width to keep the number of lines used to a minimum
  106. * and look pretty on the screen.
  107. *
  108. * \param dir Directory to list
  109. * \param stream Stream to print listing to
  110. **/
  111. void G_ls(const char *dir, FILE * stream)
  112. {
  113. int i, n;
  114. char **dir_listing = G__ls(dir, &n);
  115. G_ls_format(dir_listing, n, 0, stream);
  116. for (i = 0; i < n; i++)
  117. G_free(dir_listing[i]);
  118. G_free(dir_listing);
  119. }
  120. /**
  121. * \brief Prints a listing of items to a stream, in prettified column format
  122. *
  123. * Lists the contents of the array passed to the given stream, e.g. stderr.
  124. * Prints the number of items specified by "perline" to each line, unless
  125. * perline is given as 0 in which case the function tries to determine an
  126. * appropriate column width to keep the number of lines used to a minimum
  127. * and look pretty on the screen.
  128. *
  129. * \param list Array of strings containing items to be printed
  130. * \param num_items Number of items in the array
  131. * \param perline Number of items to print per line, 0 for autodetect
  132. * \param stream Stream to print listing to
  133. **/
  134. void G_ls_format(char **list, int num_items, int perline, FILE * stream)
  135. {
  136. int i;
  137. int field_width, column_height;
  138. int screen_width = 80; /* Default width of 80 columns */
  139. if (num_items < 1)
  140. return; /* Nothing to print */
  141. #ifdef TIOCGWINSZ
  142. /* Determine screen_width if possible */
  143. {
  144. struct winsize size;
  145. if (ioctl(fileno(stream), TIOCGWINSZ, (char *)&size) == 0)
  146. screen_width = size.ws_col;
  147. }
  148. #endif
  149. if (perline == 0) {
  150. int max_len = 0;
  151. for (i = 0; i < num_items; i++) {
  152. /* Find maximum filename length */
  153. if (strlen(list[i]) > max_len)
  154. max_len = strlen(list[i]);
  155. }
  156. /* Auto-fit the number of items that will
  157. * fit per line (+1 because of space after item) */
  158. perline = screen_width / (max_len + 1);
  159. if (perline < 1)
  160. perline = 1;
  161. }
  162. /* Field width to accomodate longest filename */
  163. field_width = screen_width / perline;
  164. /* Longest column height (i.e. num_items <= perline * column_height) */
  165. column_height = (num_items / perline) + ((num_items % perline) > 0);
  166. {
  167. const int max
  168. = num_items + column_height - (num_items % column_height);
  169. char **next;
  170. for (i = 1, next = list; i <= num_items; i++) {
  171. char **cur = next;
  172. next += column_height;
  173. if (next >= list + num_items) {
  174. /* the next item has to be on the other line */
  175. next -= (max - 1 - (next < list + max ? column_height : 0));
  176. fprintf(stream, "%s\n", *cur);
  177. }
  178. else {
  179. fprintf(stream, "%-*s", field_width, *cur);
  180. }
  181. }
  182. }
  183. }