Advertisement
ivask

Untitled

Mar 9th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 1.52 KB | None | 0 0
  1. Index: trunk/src/util/common.c
  2. ===================================================================
  3. --- trunk/src/util/common.c (revision 646)
  4. +++ trunk/src/util/common.c (working copy)
  5. @@ -22,11 +22,14 @@
  6.  #include <X11/Xutil.h>
  7.  #include <X11/Xatom.h>
  8.  #include <X11/extensions/Xrender.h>
  9. +#include <sys/types.h>
  10. +#include <sys/stat.h>
  11.  #include <stdio.h>
  12.  #include <stdlib.h>
  13.  #include <string.h>
  14.  #include <unistd.h>
  15.  #include <glib.h>
  16. +#include <fnmatch.h>
  17.  #include "common.h"
  18.  #include "../server.h"
  19.  
  20. @@ -381,3 +384,46 @@
  21.     XRenderFreePicture(server.dsp, pict_image);
  22.     XRenderFreePicture(server.dsp, pict_drawable);
  23.  }
  24. +
  25. +/**
  26. + * @brief
  27. + *  Scan given directory for files which are meet the given mask and sort them alphabetically
  28. + *
  29. + * @param path - directory path
  30. + * @param mask - filename mask
  31. + *
  32. + * @return files list
  33. + */
  34. +GList *dir_scan_alpha(const char *path, const char *mask)
  35. +{
  36. +   GError *err = NULL;
  37. +   GList *list = NULL;
  38. +   const char *n;
  39. +   gchar *fn;
  40. +   struct stat st;
  41. +
  42. +   GDir *dir = g_dir_open(path, 0, &err);
  43. +
  44. +   if (!dir) {
  45. +       fprintf(stderr, "%s\n", err->message);
  46. +       g_error_free(err);
  47. +   } else {
  48. +       // Enumerate files
  49. +           if (!fnmatch(mask, n, FNM_PATHNAME)) {
  50. +               fn = g_build_filename(path, n, NULL);
  51. +
  52. +               if (stat((char *)fn, &st) < 0)
  53. +                   continue;
  54. +
  55. +               // Only regular files
  56. +               if (S_ISREG(st.st_mode))
  57. +                   list = g_list_prepend(list, (gpointer)fn);
  58. +           }
  59. +       }
  60. +
  61. +       list = g_list_sort (list, (GCompareFunc) &strcmp);
  62. +   }
  63. +
  64. +   return list;
  65. +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement