11#define _GNU_SOURCE
22
33#include <errno.h>
4+ #include <glob.h>
45#include <stdarg.h>
56#include <stdlib.h>
67#include <string.h>
7- #include <sys/stat.h>
88
9- int stat (const char * pathname , struct stat * statbuf ) {
10- const char * mock_mode = getenv ("MOCK_STAT_MODE" );
11- if (strstr (pathname , "renderD128" )) {
12- if (mock_mode && strcmp (mock_mode , "notfound" ) == 0 ) {
13- errno = ENOENT ;
14- return -1 ;
15- }
16- if (mock_mode && strcmp (mock_mode , "exists" ) == 0 ) {
17- memset (statbuf , 0 , sizeof (* statbuf ));
18- statbuf -> st_mode = S_IFCHR | 0666 ;
19- return 0 ;
9+ int glob (const char * pattern , int flags , int (* errfunc )(const char * , int ),
10+ glob_t * pglob ) {
11+ const char * mock_mode = getenv ("MOCK_GLOB_MODE" );
12+ if (mock_mode && strcmp (mock_mode , "exists" ) == 0 ) {
13+ // Simulate that /dev/dri/renderD* exists
14+ pglob -> gl_pathc = 2 ;
15+ pglob -> gl_pathv = malloc (2 * sizeof (char * ));
16+ pglob -> gl_pathv [0 ] = strdup ("/dev/dri/renderD128" );
17+ pglob -> gl_pathv [1 ] = strdup ("/dev/dri/renderD129" );
18+ return 0 ;
19+ }
20+ // Default behavior: no matches
21+ pglob -> gl_pathc = 0 ;
22+ pglob -> gl_pathv = NULL ;
23+ return 0 ;
24+ }
25+
26+ void globfree (glob_t * pglob ) {
27+ if (pglob -> gl_pathv ) {
28+ for (size_t i = 0 ; i < pglob -> gl_pathc ; ++ i ) {
29+ free (pglob -> gl_pathv [i ]);
2030 }
31+ free (pglob -> gl_pathv );
32+ pglob -> gl_pathv = NULL ;
33+ pglob -> gl_pathc = 0 ;
2134 }
22- // Default: file does not exist
23- errno = ENOENT ;
24- return -1 ;
2535}
2636
2737int open (const char * pathname , int flags , ...) {
2838 const char * mock_mode = getenv ("MOCK_OPEN_MODE" );
29- if (strstr (pathname , "renderD128 " )) {
39+ if (strstr (pathname , "renderD12 " )) {
3040 if (mock_mode && strcmp (mock_mode , "deny" ) == 0 ) {
3141 errno = EACCES ;
3242 return -1 ;
3343 }
44+
45+ if (mock_mode && strcmp (mock_mode , "deny_second" ) == 0 ) {
46+ // Simulate that the second file is not accessible
47+ if (strstr (pathname , "renderD129" )) {
48+ errno = EACCES ;
49+ return -1 ;
50+ } else {
51+ return 3 ;
52+ }
53+ }
54+
3455 if (mock_mode && strcmp (mock_mode , "allow" ) == 0 ) {
3556 return 3 ; // Dummy fd
3657 }
@@ -39,3 +60,5 @@ int open(const char *pathname, int flags, ...) {
3960 errno = EACCES ;
4061 return -1 ;
4162}
63+
64+ int close (int fd ) { return 0 ; }
0 commit comments