ls.c 5.7 KB

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