1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <glob.h> |
---|
4 | |
---|
5 | #include <glib.h> |
---|
6 | #include <glib/gprintf.h> |
---|
7 | |
---|
8 | #define PATH_SEP "/" |
---|
9 | |
---|
10 | /** |
---|
11 | * Return glob_t with names of files found by glob. Use globfree() |
---|
12 | * to free struct. |
---|
13 | * |
---|
14 | * Example: |
---|
15 | * |
---|
16 | * glob_t *globbuf; |
---|
17 | * |
---|
18 | * globbuf = dot_dir_list (NULL, "/etc/mc/extfs", "extfs", "ini"); |
---|
19 | * globbuf = dot_dir_list (globbuf, "~/.mc/extfs", "extfs", "ini"); |
---|
20 | * |
---|
21 | * for (i = 0; i < globbuf->gl_pathc; i++) { |
---|
22 | * g_printf ("%s\n", globbuf->gl_pathv[i]); |
---|
23 | * } |
---|
24 | * |
---|
25 | * globfree (globbuf); |
---|
26 | */ |
---|
27 | glob_t * |
---|
28 | dot_dir_list (glob_t * const previous_globbuf, const gchar * const dir_path, |
---|
29 | const gchar * const file_name, const gchar * const file_ext) |
---|
30 | { |
---|
31 | |
---|
32 | glob_t *const globbuf = |
---|
33 | (previous_globbuf) ? previous_globbuf : (glob_t * |
---|
34 | const) |
---|
35 | g_try_malloc0 (sizeof (glob_t)); |
---|
36 | /* "DIR/FILE.EXT" */ |
---|
37 | const gchar *const file_path = |
---|
38 | g_strdup_printf ("%s" PATH_SEP "%s.%s", dir_path, file_name, file_ext); |
---|
39 | /* "DIR/FILE.d/ *.EXT" */ |
---|
40 | const gchar *const dot_dir_glob = |
---|
41 | g_strdup_printf ("%s" PATH_SEP "%s.d" PATH_SEP "*.%s", dir_path, |
---|
42 | file_name, file_ext); |
---|
43 | |
---|
44 | if (!globbuf || !file_path || !dot_dir_glob) |
---|
45 | { |
---|
46 | if (globbuf) |
---|
47 | globfree (globbuf); |
---|
48 | if (file_path) |
---|
49 | g_free ((gpointer) file_path); |
---|
50 | if (dot_dir_glob) |
---|
51 | g_free ((gpointer) dot_dir_glob); |
---|
52 | return NULL; |
---|
53 | } |
---|
54 | |
---|
55 | /* Look for file "dir/file.ext" */ |
---|
56 | glob (file_path, GLOB_TILDE_CHECK | GLOB_APPEND, NULL, globbuf); |
---|
57 | |
---|
58 | /* Look for files in .d dir "dir/file.d/ *.ext" */ |
---|
59 | glob (dot_dir_glob, GLOB_TILDE_CHECK | GLOB_APPEND, NULL, globbuf); |
---|
60 | |
---|
61 | g_free ((gpointer) file_path); |
---|
62 | g_free ((gpointer) dot_dir_glob); |
---|
63 | |
---|
64 | return globbuf; |
---|
65 | } |
---|
66 | |
---|
67 | int |
---|
68 | main (const int argc, const char *const *const argv) |
---|
69 | { |
---|
70 | glob_t *globbuf; |
---|
71 | int i; |
---|
72 | |
---|
73 | if (argc < 4) |
---|
74 | { |
---|
75 | globbuf = dot_dir_list (NULL, "/etc/mc/extfs", "extfs", "ini"); |
---|
76 | globbuf = dot_dir_list (globbuf, "~/.mc/extfs", "extfs", "ini"); |
---|
77 | } |
---|
78 | else |
---|
79 | { |
---|
80 | globbuf = dot_dir_list (NULL, argv[1], argv[2], argv[3]); |
---|
81 | if (argc >= 7) |
---|
82 | { |
---|
83 | globbuf = dot_dir_list (globbuf, argv[4], argv[5], argv[6]); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | if (globbuf) |
---|
88 | { |
---|
89 | if (globbuf->gl_pathc == 0) |
---|
90 | { |
---|
91 | g_printf ("%s\n", "No matches!"); |
---|
92 | } |
---|
93 | else |
---|
94 | { |
---|
95 | |
---|
96 | for (i = 0; i < globbuf->gl_pathc; i++) |
---|
97 | { |
---|
98 | g_printf ("%s\n", globbuf->gl_pathv[i]); |
---|
99 | } |
---|
100 | |
---|
101 | } |
---|
102 | |
---|
103 | globfree (globbuf); |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | g_printf ("%s\n", "An error happen!"); |
---|
108 | } |
---|
109 | |
---|
110 | return 0; |
---|
111 | } |
---|