1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2016-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 // These functions may be provided by gnulib.  We don't include gnulib
27 // headers directly in Octave's C++ source files to avoid problems that
28 // may be caused by the way that gnulib overrides standard library
29 // functions.
30 
31 #if defined (HAVE_CONFIG_H)
32 #  include "config.h"
33 #endif
34 
35 #include <stdlib.h>
36 
37 #include <fnmatch.h>
38 #include <glob.h>
39 
40 #include "glob-wrappers.h"
41 
42 void *
octave_create_glob_info_struct(void)43 octave_create_glob_info_struct (void)
44 {
45   return malloc (sizeof (glob_t));
46 }
47 
48 // Does not call globfree.
49 void
octave_destroy_glob_info_struct(void * glob_info)50 octave_destroy_glob_info_struct (void *glob_info)
51 {
52   free (glob_info);
53 }
54 
55 int
octave_glob_wrapper(const char * pattern,int flags,void * glob_info)56 octave_glob_wrapper (const char *pattern, int flags, void *glob_info)
57 {
58   return glob (pattern, flags, 0, glob_info);
59 }
60 
61 int
octave_glob_num_matches(void * glob_info)62 octave_glob_num_matches (void *glob_info)
63 {
64   return glob_info ? ((glob_t *) glob_info)->gl_pathc : 0;
65 }
66 
67 char **
octave_glob_match_list(void * glob_info)68 octave_glob_match_list (void *glob_info)
69 {
70   return glob_info ? ((glob_t *) glob_info)->gl_pathv : 0;
71 }
72 
73 void
octave_globfree_wrapper(void * glob_info)74 octave_globfree_wrapper (void *glob_info)
75 {
76   globfree ((glob_t *) glob_info);
77 }
78 
79 int
octave_glob_nosort_wrapper(void)80 octave_glob_nosort_wrapper (void)
81 {
82   return GLOB_NOSORT;
83 }
84 
85 int
octave_fnmatch_wrapper(const char * pattern,const char * name,int flags)86 octave_fnmatch_wrapper (const char *pattern, const char *name, int flags)
87 {
88   return fnmatch (pattern, name, flags);
89 }
90 
91 int
octave_fnm_nomatch_wrapper(void)92 octave_fnm_nomatch_wrapper (void)
93 {
94   return FNM_NOMATCH;
95 }
96 
97 int
octave_fnm_pathname_wrapper(void)98 octave_fnm_pathname_wrapper (void)
99 {
100   return FNM_PATHNAME;
101 }
102 
103 int
octave_fnm_noescape_wrapper(void)104 octave_fnm_noescape_wrapper (void)
105 {
106   return FNM_NOESCAPE;
107 }
108 
109 int
octave_fnm_period_wrapper(void)110 octave_fnm_period_wrapper (void)
111 {
112   return FNM_PERIOD;
113 }
114