MagickCore  7.1.2-29
Convert, Edit, Or Compose Bitmap Images
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)
32 extern "C" {
33 #endif
34 
35 #define MagickPathTemplate "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" /* min 6 X's */
36 
37 extern MagickPrivate char
38  **GetPathComponents(const char *,size_t *),
39  **ListFiles(const char *,const char *,size_t *);
40 
41 extern MagickPrivate MagickBooleanType
42  GetExecutionPath(char *,const size_t),
43  ShredFile(const char *);
44 
45 extern MagickPrivate ssize_t
46  GetMagickPageSize(void);
47 
48 extern MagickPrivate void
49  ChopPathComponents(char *,const size_t),
50  ExpandFilename(char *);
51 
52 static 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 
69 static 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 
78 static 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 
96 static 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 
116 static 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 
125 static 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 
144 
145 static inline int open_utf8(const char *path,int flags,mode_t mode)
146 {
147 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
148  return(open(path,flags,mode));
149 #else
150  return(NTOpenWide(path,flags,mode));
151 #endif
152 }
153 
154 static inline FILE *popen_utf8(const char *command,const char *type)
155 {
156 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
157  return(popen(command,type));
158 #else
159  return(NTOpenPipeWide(command,type));
160 #endif
161 }
162 
163 static inline char *realpath_utf8(const char *path)
164 {
165 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
166 #if defined(MAGICKCORE_HAVE_REALPATH)
167  /*
168  This does not work for non-existing files so we should fine another way
169  to do this in the future. This is only a possible issue when writing files.
170  */
171  return(realpath(path,(char *) NULL));
172 #else
173  return(AcquireString(path));
174 #endif
175 #else
176  return(NTRealPathWide(path));
177 #endif
178 }
179 
180 static inline int remove_utf8(const char *path)
181 {
182 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
183  return(unlink(path));
184 #else
185  return(NTRemoveWide(path));
186 #endif
187 }
188 
189 static inline int rename_utf8(const char *source,const char *destination)
190 {
191 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
192  return(rename(source,destination));
193 #else
194  return(NTRenameWide(source,destination));
195 #endif
196 }
197 
198 static inline int set_file_timestamp(const char *path,struct stat *attributes)
199 {
200  int
201  status;
202 
203 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
204 #if defined(MAGICKCORE_HAVE_UTIMENSAT)
205 #if defined(__APPLE__) || defined(__NetBSD__)
206 #define st_atim st_atimespec
207 #define st_ctim st_ctimespec
208 #define st_mtim st_mtimespec
209 #endif
210 
211  struct timespec
212  timestamp[2];
213 
214  timestamp[0].tv_sec=attributes->st_atim.tv_sec;
215  timestamp[0].tv_nsec=attributes->st_atim.tv_nsec;
216  timestamp[1].tv_sec=attributes->st_mtim.tv_sec;
217  timestamp[1].tv_nsec=attributes->st_mtim.tv_nsec;
218  status=utimensat(AT_FDCWD,path,timestamp,0);
219 #else
220  struct utimbuf
221  timestamp;
222 
223  timestamp.actime=attributes->st_atime;
224  timestamp.modtime=attributes->st_mtime;
225  status=utime(path,&timestamp);
226 #endif
227 #else
228  status=NTSetFileTimestamp(path,attributes);
229 #endif
230  return(status);
231 }
232 
233 static inline int stat_utf8(const char *path,struct stat *attributes)
234 {
235 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
236  return(stat(path,attributes));
237 #else
238  return(NTStatWide(path,attributes));
239 #endif
240 }
241 
242 #if defined(__cplusplus) || defined(c_plusplus)
243 }
244 #endif
245 
246 #endif