MagickCore 7.1.2-32
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
utility-private.h
1/*
2 Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization
3 dedicated to making software imaging solutions freely available.
4
5 You may not use this file except in compliance with the License. You may
6 obtain a copy of the License at
7
8 https://imagemagick.org/license/
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 MagickCore private utility methods.
17*/
18#ifndef MAGICKCORE_UTILITY_PRIVATE_H
19#define MAGICKCORE_UTILITY_PRIVATE_H
20
21#include "MagickCore/memory_.h"
22#include "MagickCore/nt-base.h"
23#include "MagickCore/nt-base-private.h"
24#if defined(MAGICKCORE_HAVE_UTIME_H)
25#include <utime.h>
26#endif
27#if defined(__MINGW32__)
28#include <share.h>
29#endif
30
31#if defined(__cplusplus) || defined(c_plusplus)
32extern "C" {
33#endif
34
35#define MagickPathTemplate "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" /* min 6 X's */
36
37extern MagickPrivate char
38 **GetPathComponents(const char *,size_t *),
39 **ListFiles(const char *,const char *,size_t *);
40
41extern MagickPrivate MagickBooleanType
42 GetExecutionPath(char *,const size_t),
43 ShredFile(const char *);
44
45extern MagickPrivate ssize_t
46 GetMagickPageSize(void);
47
48extern MagickPrivate void
49 ChopPathComponents(char *,const size_t),
50 ExpandFilename(char *);
51
52static inline int access_utf8(const char *path,int mode)
53{
54 if (path == (const char *) NULL)
55 return(-1);
56#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
57 return(access(path,mode));
58#else
59 return(NTAccessWide(path,mode));
60#endif
61}
62
63#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__)
64#define close_utf8 _close
65#else
66#define close_utf8 close
67#endif
68
69static inline FILE *fopen_utf8(const char *path,const char *mode)
70{
71#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
72 return(fopen(path,mode));
73#else
74 return(NTOpenFileWide(path,mode));
75#endif
76}
77
78static inline MagickBooleanType is_symlink_utf8(const char *path)
79{
80#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
81#if defined(MAGICKCORE_POSIX_SUPPORT)
82 struct stat
83 status;
84
85 if (lstat(path,&status) == -1)
86 return(MagickFalse);
87 return(S_ISLNK(status.st_mode) != 0 ? MagickTrue : MagickFalse);
88#else
89 return(MagickFalse);
90#endif
91#else
92 return(NTIsSymlinkWide(path));
93#endif
94}
95
96static inline ssize_t MagickRead(int fd,void *buffer,size_t extent)
97{
98 unsigned char *p = (unsigned char *) buffer;
99 size_t offset = 0;
100 while (offset < extent)
101 {
102 ssize_t count = read(fd,p+offset,extent-offset);
103 if (count < 0)
104 {
105 if (errno == EINTR)
106 continue;
107 return(-1);
108 }
109 if (count == 0)
110 break;
111 offset+=(size_t) count;
112 }
113 return((ssize_t) offset);
114}
115
116static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
117 struct dirent **result)
118{
119 (void) entry;
120 errno=0;
121 *result=readdir(directory);
122 return(errno);
123}
124
125static inline ssize_t MagickWrite(int fd,const void *buffer,size_t extent)
126{
127 const unsigned char *p = (const unsigned char *) buffer;
128 size_t offset = 0;
129 while (offset < extent)
130 {
131 ssize_t count = write(fd,p+offset,extent-offset);
132 if (count < 0) {
133 if (errno == EINTR)
134 continue;
135 return(-1);
136 }
137 if (count == 0)
138 break;
139 offset+=(size_t) count;
140 }
141 return((ssize_t) offset);
142}
143
144static inline int open_utf8(const char *path,int flags,mode_t mode)
145{
146#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
147 return(open(path,flags,mode));
148#else
149 return(NTOpenWide(path,flags,mode));
150#endif
151}
152
153static inline FILE *popen_utf8(const char *command,const char *type)
154{
155#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
156 return(popen(command,type));
157#else
158 return(NTOpenPipeWide(command,type));
159#endif
160}
161
162static inline char *realpath_utf8(const char *path)
163{
164#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
165#if defined(MAGICKCORE_HAVE_REALPATH)
166 /*
167 This does not work for non-existing files so we should fine another way
168 to do this in the future. This is only a possible issue when writing files.
169 */
170 return(realpath(path,(char *) NULL));
171#else
172 return(AcquireString(path));
173#endif
174#else
175 return(NTRealPathWide(path));
176#endif
177}
178
179static inline int remove_utf8(const char *path)
180{
181#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
182 return(unlink(path));
183#else
184 return(NTRemoveWide(path));
185#endif
186}
187
188static inline int rename_utf8(const char *source,const char *destination)
189{
190#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
191 return(rename(source,destination));
192#else
193 return(NTRenameWide(source,destination));
194#endif
195}
196
197static inline int set_file_timestamp(const char *path,struct stat *attributes)
198{
199 int
200 status;
201
202#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
203#if defined(MAGICKCORE_HAVE_UTIMENSAT)
204#if defined(__APPLE__) || defined(__NetBSD__)
205#define st_atim st_atimespec
206#define st_ctim st_ctimespec
207#define st_mtim st_mtimespec
208#endif
209
210 struct timespec
211 timestamp[2];
212
213 timestamp[0].tv_sec=attributes->st_atim.tv_sec;
214 timestamp[0].tv_nsec=attributes->st_atim.tv_nsec;
215 timestamp[1].tv_sec=attributes->st_mtim.tv_sec;
216 timestamp[1].tv_nsec=attributes->st_mtim.tv_nsec;
217 status=utimensat(AT_FDCWD,path,timestamp,0);
218#else
219 struct utimbuf
220 timestamp;
221
222 timestamp.actime=attributes->st_atime;
223 timestamp.modtime=attributes->st_mtime;
224 status=utime(path,&timestamp);
225#endif
226#else
227 status=NTSetFileTimestamp(path,attributes);
228#endif
229 return(status);
230}
231
232static inline int stat_utf8(const char *path,struct stat *attributes)
233{
234#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
235 return(stat(path,attributes));
236#else
237 return(NTStatWide(path,attributes));
238#endif
239}
240
241#if defined(__cplusplus) || defined(c_plusplus)
242}
243#endif
244
245#endif