1 /* Test glob memory management.
2 for the filesystem access functions.
3 Copyright (C) 2001-2024 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
32 # define PRINTF(fmt, args...) \
35 int save_errno = errno; \
36 printf (fmt, ##args); \
40 # define PRINTF(fmt, args...)
44 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
45 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
46 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
47 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
48 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
49 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
50 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
51 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
52 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
53 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
63 { ".", 1, DT_DIR
, 0755 },
64 { "..", 1, DT_DIR
, 0755 },
65 { "dir", 1, DT_DIR
, 0755 },
66 { ".", 2, DT_DIR
, 0755 },
67 { "..", 2, DT_DIR
, 0755 },
68 { "readable", 2, DT_DIR
, 0755 },
69 { ".", 3, DT_DIR
, 0755 },
70 { "..", 3, DT_DIR
, 0755 },
71 { "a", 3, DT_REG
, 0644 },
72 { LONG_NAME
, 3, DT_REG
, 0644 },
73 { "unreadable", 2, DT_DIR
, 0111 },
74 { ".", 3, DT_DIR
, 0111 },
75 { "..", 3, DT_DIR
, 0755 },
76 { "a", 3, DT_REG
, 0644 },
77 { "zz-readable", 2, DT_DIR
, 0755 },
78 { ".", 3, DT_DIR
, 0755 },
79 { "..", 3, DT_DIR
, 0755 },
80 { "a", 3, DT_REG
, 0644 }
82 #define nfiles (sizeof (filesystem) / sizeof (filesystem[0]))
90 char room_for_dirent
[sizeof (LONG_NAME
)];
95 find_file (const char *s
)
100 if (strcmp (s
, ".") == 0)
103 if (s
[0] == '.' && s
[1] == '/')
108 char *endp
= strchrnul (s
, '/');
110 PRINTF ("looking for %.*s, level %d\n", (int) (endp
- s
), s
, level
);
112 while (idx
< nfiles
&& filesystem
[idx
].level
>= level
)
114 if (filesystem
[idx
].level
== level
115 && memcmp (s
, filesystem
[idx
].name
, endp
- s
) == 0
116 && filesystem
[idx
].name
[endp
- s
] == '\0')
121 if (idx
== nfiles
|| filesystem
[idx
].level
< level
)
130 if (filesystem
[idx
].type
!= DT_DIR
131 && (idx
+ 1 >= nfiles
132 || filesystem
[idx
].level
>= filesystem
[idx
+ 1].level
))
150 my_opendir (const char *s
)
152 long int idx
= find_file (s
);
157 PRINTF ("my_opendir(\"%s\") == NULL (%m)\n", s
);
161 if ((filesystem
[idx
].mode
& 0400) == 0)
164 PRINTF ("my_opendir(\"%s\") == NULL (%m)\n", s
);
168 dir
= (my_DIR
*) malloc (sizeof (my_DIR
));
171 printf ("cannot allocate directory handle: %m\n");
175 dir
->level
= filesystem
[idx
].level
;
178 PRINTF ("my_opendir(\"%s\") == { level: %d, idx: %ld }\n",
179 s
, filesystem
[idx
].level
, idx
);
185 static struct dirent
*
186 my_readdir (void *gdir
)
192 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
193 dir
->level
, (long int) dir
->idx
);
197 while (dir
->idx
< nfiles
&& filesystem
[dir
->idx
].level
> dir
->level
)
200 if (dir
->idx
== nfiles
|| filesystem
[dir
->idx
].level
< dir
->level
)
203 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
204 dir
->level
, (long int) dir
->idx
);
208 dir
->d
.d_ino
= 1; /* glob should not skip this entry. */
210 dir
->d
.d_type
= filesystem
[dir
->idx
].type
;
212 strcpy (dir
->d
.d_name
, filesystem
[dir
->idx
].name
);
214 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_type: %d, d_name: \"%s\" }\n",
215 dir
->level
, (long int) dir
->idx
, dir
->d
.d_ino
, dir
->d
.d_type
,
225 my_closedir (void *dir
)
227 PRINTF ("my_closedir ()\n");
232 /* We use this function for lstat as well since we don't have any. */
234 my_stat (const char *name
, struct stat
*st
)
236 long int idx
= find_file (name
);
240 PRINTF ("my_stat (\"%s\", ...) = -1 (%m)\n", name
);
244 memset (st
, '\0', sizeof (*st
));
246 if (filesystem
[idx
].type
== DT_UNKNOWN
)
247 st
->st_mode
= DTTOIF (idx
+ 1 < nfiles
248 && filesystem
[idx
].level
< filesystem
[idx
+ 1].level
249 ? DT_DIR
: DT_REG
) | filesystem
[idx
].mode
;
251 st
->st_mode
= DTTOIF (filesystem
[idx
].type
) | filesystem
[idx
].mode
;
253 PRINTF ("my_stat (\"%s\", { st_mode: %o }) = 0\n", name
, st
->st_mode
);
260 init_glob_altdirfuncs (glob_t
*pglob
)
262 pglob
->gl_closedir
= my_closedir
;
263 pglob
->gl_readdir
= my_readdir
;
264 pglob
->gl_opendir
= my_opendir
;
265 pglob
->gl_lstat
= my_stat
;
266 pglob
->gl_stat
= my_stat
;
276 memset (&gl
, 0, sizeof (gl
));
277 init_glob_altdirfuncs (&gl
);
279 if (glob ("dir/*able/*", GLOB_ERR
| GLOB_ALTDIRFUNC
, NULL
, &gl
)
282 puts ("glob did not fail with GLOB_ABORTED");
288 memset (&gl
, 0, sizeof (gl
));
289 init_glob_altdirfuncs (&gl
);
292 if (glob ("dir2/*", GLOB_DOOFFS
, NULL
, &gl
) != GLOB_NOMATCH
)
294 puts ("glob did not fail with GLOB_NOMATCH");
305 #define TEST_FUNCTION do_test ()
306 #include "../test-skeleton.c"