1 | /* Copyright (C) MariaDB Corporation Ab */ |
2 | #include "my_global.h" |
3 | #include <stdlib.h> |
4 | #include <string.h> |
5 | #include <stdio.h> |
6 | #include "osutil.h" |
7 | |
8 | #ifdef __WIN__ |
9 | my_bool CloseFileHandle(HANDLE h) |
10 | { |
11 | return !CloseHandle(h); |
12 | } /* end of CloseFileHandle */ |
13 | |
14 | #else /* UNIX */ |
15 | /* code to handle Linux and Solaris */ |
16 | #include <unistd.h> |
17 | #include <sys/stat.h> |
18 | #include <ctype.h> |
19 | #include <fcntl.h> |
20 | #include <pwd.h> |
21 | |
22 | extern FILE *debug; |
23 | |
24 | /***********************************************************************/ |
25 | /* Define some functions not existing in the UNIX library. */ |
26 | /***********************************************************************/ |
27 | PSZ strupr(PSZ p) |
28 | { |
29 | register int i; |
30 | |
31 | for (i = 0; p[i]; i++) |
32 | p[i] = toupper(p[i]); |
33 | |
34 | return (p); |
35 | } /* end of strupr */ |
36 | |
37 | PSZ strlwr(PSZ p) |
38 | { |
39 | register int i; |
40 | |
41 | for (i = 0; p[i]; i++) |
42 | p[i] = tolower(p[i]); |
43 | |
44 | return (p); |
45 | } /* end of strlwr */ |
46 | |
47 | /***********************************************************************/ |
48 | /* Define the splitpath function not existing in the UNIX library. */ |
49 | /***********************************************************************/ |
50 | void _splitpath(LPCSTR name, LPSTR drive, LPSTR dir, LPSTR fn, LPSTR ft) |
51 | { |
52 | LPCSTR p2, p = name; |
53 | |
54 | #ifdef DEBTRACE |
55 | htrc("SplitPath: name=%s [%s (%d)]\n" , |
56 | XSTR(name), XSTR(__FILE__), __LINE__); |
57 | #endif |
58 | |
59 | if (drive) *drive = '\0'; |
60 | if (dir) *dir = '\0'; |
61 | if (fn) *fn = '\0'; |
62 | if (ft) *ft = '\0'; |
63 | |
64 | if ((p2 = strrchr(p, '/'))) { |
65 | p2++; |
66 | if (dir) strncat(dir, p, p2 - p); |
67 | p = p2; |
68 | } /* endif p2 */ |
69 | |
70 | if ((p2 = strrchr(p, '.'))) { |
71 | if (fn) strncat(fn, p, p2 - p); |
72 | if (ft) strcpy(ft, p2); |
73 | } else |
74 | if (fn) strcpy(fn, p); |
75 | |
76 | #ifdef DEBTRACE |
77 | htrc("SplitPath: name=%s drive=%s dir=%s filename=%s type=%s [%s(%d)]\n" , |
78 | XSTR(name), XSTR(drive), XSTR(dir), XSTR(fn), XSTR(ft), __FILE__, __LINE__); |
79 | #endif |
80 | } /* end of _splitpath */ |
81 | |
82 | /***********************************************************************/ |
83 | /* Define the makepath function not existing in the UNIX library. */ |
84 | /***********************************************************************/ |
85 | void _makepath(LPSTR name, LPCSTR drive __attribute__((unused)), LPCSTR dir, LPCSTR fn, LPCSTR ft) |
86 | { |
87 | int n; |
88 | |
89 | if (!name) |
90 | return; |
91 | else |
92 | *name = '\0'; |
93 | |
94 | if (dir && (n = strlen(dir)) > 0) { |
95 | strcpy(name, dir); |
96 | |
97 | if (name[n-1] != '/') |
98 | strcat(name, "/" ); |
99 | |
100 | } /* endif dir */ |
101 | |
102 | if (fn) |
103 | strcat(name, fn); |
104 | |
105 | if (ft && strlen(ft)) { |
106 | if (*ft != '.') |
107 | strcat(name, "." ); |
108 | |
109 | strcat(name, ft); |
110 | } /* endif ft */ |
111 | |
112 | } /* end of _makepath */ |
113 | |
114 | my_bool CloseFileHandle(HANDLE h) |
115 | { |
116 | return (close(h)) ? TRUE : FALSE; |
117 | } /* end of CloseFileHandle */ |
118 | |
119 | int GetLastError() |
120 | { |
121 | return errno; |
122 | } /* end of GetLastError */ |
123 | |
124 | unsigned long _filelength(int fd) |
125 | { |
126 | struct stat st; |
127 | |
128 | if (fd == -1) |
129 | return 0; |
130 | |
131 | if (fstat(fd, &st) != 0) |
132 | return 0; |
133 | |
134 | return st.st_size; |
135 | } /* end of _filelength */ |
136 | |
137 | char *_fullpath(char *absPath, const char *relPath, size_t maxLength) |
138 | { |
139 | // Fixme |
140 | char *p; |
141 | |
142 | if ( *relPath == '\\' || *relPath == '/' ) { |
143 | strncpy(absPath, relPath, maxLength); |
144 | } else if (*relPath == '~') { |
145 | // get the path to the home directory |
146 | struct passwd *pw = getpwuid(getuid()); |
147 | const char *homedir = pw->pw_dir; |
148 | |
149 | if (homedir) |
150 | strcat(strncpy(absPath, homedir, maxLength), relPath + 1); |
151 | else |
152 | strncpy(absPath, relPath, maxLength); |
153 | |
154 | } else { |
155 | char buff[2*_MAX_PATH]; |
156 | |
157 | p= getcwd(buff, _MAX_PATH); |
158 | assert(p); |
159 | strcat(buff,"/" ); |
160 | strcat(buff, relPath); |
161 | strncpy(absPath, buff, maxLength); |
162 | } /* endif's relPath */ |
163 | |
164 | p = absPath; |
165 | |
166 | for(; *p; p++) |
167 | if (*p == '\\') |
168 | *p = '/'; |
169 | |
170 | return absPath; |
171 | } /* end of _fullpath */ |
172 | |
173 | BOOL MessageBeep(uint i __attribute__((unused))) |
174 | { |
175 | // Fixme |
176 | return TRUE; |
177 | } /* end of MessageBeep */ |
178 | |
179 | #endif // UNIX |
180 | |