1 /* Test glob memory management.
2 for the filesystem access functions.
3 Copyright (C) 2001, 2002, 2004 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
33 # define PRINTF(fmt, args...) \
36 int save_errno = errno; \
37 printf (fmt, ##args); \
41 # define PRINTF(fmt, args...)
53 { ".", 1, DT_DIR, 0755 },
54 { "..", 1, DT_DIR, 0755 },
55 { "dir", 1, DT_DIR, 0755 },
56 { ".", 2, DT_DIR, 0755 },
57 { "..", 2, DT_DIR, 0755 },
58 { "readable", 2, DT_DIR, 0755 },
59 { ".", 3, DT_DIR, 0755 },
60 { "..", 3, DT_DIR, 0755 },
61 { "a", 3, DT_REG, 0644 },
62 { "unreadable", 2, DT_DIR, 0111 },
63 { ".", 3, DT_DIR, 0111 },
64 { "..", 3, DT_DIR, 0755 },
65 { "a", 3, DT_REG, 0644 },
66 { "zz-readable", 2, DT_DIR, 0755 },
67 { ".", 3, DT_DIR, 0755 },
68 { "..", 3, DT_DIR, 0755 },
69 { "a", 3, DT_REG, 0644 }
71 #define nfiles (sizeof (filesystem) / sizeof (filesystem[0]))
79 char room_for_dirent[NAME_MAX];
84 find_file (const char *s)
89 if (strcmp (s, ".") == 0)
92 if (s[0] == '.' && s[1] == '/')
97 char *endp = strchrnul (s, '/');
99 PRINTF ("looking for %.*s, level %d\n", (int) (endp - s), s, level);
101 while (idx < nfiles && filesystem[idx].level >= level)
103 if (filesystem[idx].level == level
104 && memcmp (s, filesystem[idx].name, endp - s) == 0
105 && filesystem[idx].name[endp - s] == '\0')
110 if (idx == nfiles || filesystem[idx].level < level)
119 if (filesystem[idx].type != DT_DIR
120 && (idx + 1 >= nfiles
121 || filesystem[idx].level >= filesystem[idx + 1].level))
139 my_opendir (const char *s)
141 long int idx = find_file (s);
146 PRINTF ("my_opendir(\"%s\") == NULL (%m)\n", s);
150 if ((filesystem[idx].mode & 0400) == 0)
153 PRINTF ("my_opendir(\"%s\") == NULL (%m)\n", s);
157 dir = (my_DIR *) malloc (sizeof (my_DIR));
160 printf ("cannot allocate directory handle: %m\n");
164 dir->level = filesystem[idx].level;
167 PRINTF ("my_opendir(\"%s\") == { level: %d, idx: %ld }\n",
168 s, filesystem[idx].level, idx);
174 static struct dirent *
175 my_readdir (void *gdir)
181 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
182 dir->level, (long int) dir->idx);
186 while (dir->idx < nfiles && filesystem[dir->idx].level > dir->level)
189 if (dir->idx == nfiles || filesystem[dir->idx].level < dir->level)
192 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
193 dir->level, (long int) dir->idx);
197 dir->d.d_ino = dir->idx;
199 #ifdef _DIRENT_HAVE_D_TYPE
200 dir->d.d_type = filesystem[dir->idx].type;
203 strcpy (dir->d.d_name, filesystem[dir->idx].name);
205 #ifdef _DIRENT_HAVE_D_TYPE
206 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_type: %d, d_name: \"%s\" }\n",
207 dir->level, (long int) dir->idx, dir->d.d_ino, dir->d.d_type,
210 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_name: \"%s\" }\n",
211 dir->level, (long int) dir->idx, dir->d.d_ino,
222 my_closedir (void *dir)
224 PRINTF ("my_closedir ()\n");
229 /* We use this function for lstat as well since we don't have any. */
231 my_stat (const char *name, struct stat *st)
233 long int idx = find_file (name);
237 PRINTF ("my_stat (\"%s\", ...) = -1 (%m)\n", name);
241 memset (st, '\0', sizeof (*st));
243 if (filesystem[idx].type == DT_UNKNOWN)
244 st->st_mode = DTTOIF (idx + 1 < nfiles
245 && filesystem[idx].level < filesystem[idx + 1].level
246 ? DT_DIR : DT_REG) | filesystem[idx].mode;
248 st->st_mode = DTTOIF (filesystem[idx].type) | filesystem[idx].mode;
250 PRINTF ("my_stat (\"%s\", { st_mode: %o }) = 0\n", name, st->st_mode);
257 init_glob_altdirfuncs (glob_t *pglob)
259 pglob->gl_closedir = my_closedir;
260 pglob->gl_readdir = my_readdir;
261 pglob->gl_opendir = my_opendir;
262 pglob->gl_lstat = my_stat;
263 pglob->gl_stat = my_stat;
273 memset (&gl, 0, sizeof (gl));
274 init_glob_altdirfuncs (&gl);
276 if (glob ("dir/*able/*", GLOB_ERR | GLOB_ALTDIRFUNC, NULL, &gl)
279 puts ("glob did not fail with GLOB_ABORTED");
285 memset (&gl, 0, sizeof (gl));
286 init_glob_altdirfuncs (&gl);
289 if (glob ("dir2/*", GLOB_DOOFFS, NULL, &gl) != GLOB_NOMATCH)
291 puts ("glob did not fail with GLOB_NOMATCH");
302 #define TEST_FUNCTION do_test ()
303 #include "../test-skeleton.c"