ls.c 5.8 KB

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