File: GB_file.c

package info (click to toggle)
suitesparse 1%3A7.11.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 258,172 kB
  • sloc: ansic: 1,153,566; cpp: 48,145; makefile: 4,997; fortran: 2,087; java: 1,826; sh: 1,113; ruby: 725; python: 676; asm: 371; sed: 166; awk: 44
file content (176 lines) | stat: -rw-r--r-- 4,751 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//------------------------------------------------------------------------------
// GB_file.c: portable file I/O
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//------------------------------------------------------------------------------

// These methods provide portable file I/O functions in support of the JIT.  If
// the JIT is disabled at compile time, these functions do nothing.

// Windows references:
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/mkdir-wmkdir

#include "GB.h"
#include "jitifyer/GB_file.h"

#ifndef NJIT

    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <errno.h>

    #if GB_WINDOWS

        // Windows
        #include <io.h>
        #include <direct.h>
        #include <windows.h>
        #define GB_MKDIR(path)  _mkdir (path)

    #else

        // POSIX
        #include <unistd.h>
        #include <dlfcn.h>
        #define GB_MKDIR(path)  mkdir (path, S_IRWXU)

    #endif

#endif

//------------------------------------------------------------------------------
// GB_file_mkdir: create a directory
//------------------------------------------------------------------------------

// Create a directory, including all parent directories if they do not exist.
// Returns true if the directory already exists or if it was successfully
// created.  Returns true if the JIT is disabled (the directory is not created
// but also not needed in that case).  Returns false on error.

bool GB_file_mkdir (char *path)
{
    if (path == NULL)
    { 
        // invalid input
        return (false) ;
    }

    #ifdef NJIT
    {
        // JIT disabled; do not create the directory but do not return an error
        return (true) ;
    }
    #else
    {
        // create all the leading directories
        int result = 0 ;
        bool first = true ;
        for (char *p = path ; *p ; p++)
        {
            // look for a file separator
            if (*p == '/' || *p == '\\')
            {
                // found a file separator
                if (!first)
                { 
                    // terminate the path at this file separator
                    char save = *p ;
                    *p = '\0' ;
                    // construct the directory at this path
                    result = GB_MKDIR (path) ;
                    // err = (result == -1) ? errno : 0 ;
                    // restore the path
                    *p = save ;
                }
                first = false ;
            }
        }

        // create the final directory
        result = GB_MKDIR (path) ;
        int err = (result == -1) ? errno : 0 ;
        return (err == 0 || err == EEXIST) ;
    }
    #endif
}

//------------------------------------------------------------------------------
// GB_file_dlopen: open a dynamic library
//------------------------------------------------------------------------------

void *GB_file_dlopen (char *library_name)
{ 
    #ifdef NJIT
    {
        // JIT disabled
        return (NULL) ;
    }
    #elif GB_WINDOWS
    {
        // open a Windows dll
        HINSTANCE hdll = LoadLibrary (library_name) ;
        return ((void *) hdll) ;
    }
    #else
    {
        // open a POSIX dynamic library
        return (dlopen (library_name, RTLD_LAZY)) ;
    }
    #endif
}

//------------------------------------------------------------------------------
// GB_file_dlsym: get a function pointer from a dynamic library
//------------------------------------------------------------------------------

void *GB_file_dlsym (void *dl_handle, char *symbol)
{ 
    #ifdef NJIT
    {
        // JIT disabled
        return (NULL) ;
    }
    #elif GB_WINDOWS
    {
        // lookup a symbol in a Windows dll
        void *p = (void *) GetProcAddress (dl_handle, symbol) ;
        return ((void *) p) ;
    }
    #else
    {
        // lookup a symbol in a POSIX dynamic library
        return (dlsym (dl_handle, symbol)) ;
    }
    #endif
}

//------------------------------------------------------------------------------
// GB_file_dlclose: close a dynamic library
//------------------------------------------------------------------------------

void GB_file_dlclose (void *dl_handle)
{ 
    if (dl_handle != NULL)
    {
        #ifdef NJIT
        {
            // JIT disabled: do nothing
        }
        #elif GB_WINDOWS
        {
            // close a Windows dll
            FreeLibrary (dl_handle) ;
        }
        #else
        {
            // close a POSIX dynamic library
            dlclose (dl_handle) ;
        }
        #endif
    }
}