42#include "MagickCore/studio.h"
43#include "MagickCore/property.h"
44#include "MagickCore/blob.h"
45#include "MagickCore/color.h"
46#include "MagickCore/exception.h"
47#include "MagickCore/exception-private.h"
48#include "MagickCore/geometry.h"
49#include "MagickCore/image-private.h"
50#include "MagickCore/list.h"
51#include "MagickCore/log.h"
52#include "MagickCore/magick-private.h"
53#include "MagickCore/memory_.h"
54#include "MagickCore/nt-base-private.h"
55#include "MagickCore/option.h"
56#include "MagickCore/policy.h"
57#include "MagickCore/random_.h"
58#include "MagickCore/registry.h"
59#include "MagickCore/resource_.h"
60#include "MagickCore/semaphore.h"
61#include "MagickCore/signature-private.h"
62#include "MagickCore/statistic.h"
63#include "MagickCore/string_.h"
64#include "MagickCore/string-private.h"
65#include "MagickCore/token.h"
66#include "MagickCore/token-private.h"
67#include "MagickCore/utility.h"
68#include "MagickCore/utility-private.h"
69#if defined(MAGICKCORE_HAVE_PROCESS_H)
72#if defined(MAGICKCORE_HAVE_MACH_O_DYLD_H)
73#include <mach-o/dyld.h>
80 Base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
86 IsPathDirectory(
const char *);
111MagickExport MagickBooleanType AcquireUniqueFilename(
char *path)
116 file=AcquireUniqueFileResource(path);
119 file=close_utf8(file)-1;
153MagickExport MagickBooleanType AcquireUniqueSymbolicLink(
const char *source,
176 assert(source != (
const char *) NULL);
177 assert(destination != (
char *) NULL);
178#if defined(MAGICKCORE_HAVE_SYMLINK)
183 passes=GetPolicyValue(
"system:shred");
184 if (passes != (
char *) NULL)
185 passes=DestroyString(passes);
188 (void) AcquireUniqueFilename(destination);
189 (void) RelinquishUniqueFileResource(destination);
190 if (*source == *DirectorySeparator)
192 if (symlink(source,destination) == 0)
198 path[MagickPathExtent];
201 if (getcwd(path,MagickPathExtent) == (
char *) NULL)
203 (void) ConcatenateMagickString(path,DirectorySeparator,
205 (void) ConcatenateMagickString(path,source,MagickPathExtent);
206 if (symlink(path,destination) == 0)
212 destination_file=AcquireUniqueFileResource(destination);
213 if (destination_file == -1)
215 source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
216 if (source_file == -1)
218 (void) close_utf8(destination_file);
219 (void) RelinquishUniqueFileResource(destination);
222 quantum=(size_t) MagickMaxBufferExtent;
223 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
224 quantum=(size_t) MagickMin(attributes.st_size,MagickMaxBufferExtent);
225 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
226 if (buffer == (
unsigned char *) NULL)
228 (void) close_utf8(source_file);
229 (void) close_utf8(destination_file);
230 (void) RelinquishUniqueFileResource(destination);
236 count=(ssize_t) read(source_file,buffer,quantum);
239 length=(size_t) count;
240 count=(ssize_t) write(destination_file,buffer,length);
241 if ((
size_t) count != length)
243 (void) RelinquishUniqueFileResource(destination);
248 (void) close_utf8(destination_file);
249 (void) close_utf8(source_file);
250 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
281MagickExport
void AppendImageFormat(
const char *format,
char *filename)
284 extension[MagickPathExtent],
285 root[MagickPathExtent];
287 assert(format != (
char *) NULL);
288 assert(filename != (
char *) NULL);
289 if (IsEventLogging() != MagickFalse)
290 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
291 if ((*format ==
'\0') || (*filename ==
'\0'))
293 if (LocaleCompare(filename,
"-") == 0)
296 message[MagickPathExtent];
298 (void) FormatLocaleString(message,MagickPathExtent,
"%s:%s",format,
300 (void) CopyMagickString(filename,message,MagickPathExtent);
303 GetPathComponent(filename,ExtensionPath,extension);
304 if ((LocaleCompare(extension,
"Z") == 0) ||
305 (LocaleCompare(extension,
"bz2") == 0) ||
306 (LocaleCompare(extension,
"gz") == 0) ||
307 (LocaleCompare(extension,
"wmz") == 0) ||
308 (LocaleCompare(extension,
"svgz") == 0))
310 GetPathComponent(filename,RootPath,root);
311 (void) CopyMagickString(filename,root,MagickPathExtent);
312 GetPathComponent(filename,RootPath,root);
313 (void) FormatLocaleString(filename,MagickPathExtent,
"%s.%s.%s",root,
317 GetPathComponent(filename,RootPath,root);
318 (void) FormatLocaleString(filename,MagickPathExtent,
"%s.%s",root,format);
347MagickExport
unsigned char *Base64Decode(
const char *source,
size_t *length)
362 assert(source != (
char *) NULL);
363 assert(length != (
size_t *) NULL);
364 if (IsEventLogging() != MagickFalse)
365 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
367 decode=(
unsigned char *) AcquireQuantumMemory((strlen(source)+3)/4,
369 if (decode == (
unsigned char *) NULL)
370 return((
unsigned char *) NULL);
373 for (p=source; *p !=
'\0'; p++)
375 if (isspace((
int) ((
unsigned char) *p)) != 0)
380 if (q == (
char *) NULL)
382 decode=(
unsigned char *) RelinquishMagickMemory(decode);
383 return((
unsigned char *) NULL);
389 decode[i]=(
unsigned char)((q-Base64) << 2);
395 decode[i++]|=(
unsigned char)((q-Base64) >> 4);
396 decode[i]=(
unsigned char)(((q-Base64) & 0x0f) << 4);
402 decode[i++]|=(
unsigned char)((q-Base64) >> 2);
403 decode[i]=(
unsigned char)(((q-Base64) & 0x03) << 6);
409 decode[i++]|=(
unsigned char)(q-Base64);
422 decode=(
unsigned char *) RelinquishMagickMemory(decode);
423 return((
unsigned char *) NULL);
437 decode=(
unsigned char *) RelinquishMagickMemory(decode);
438 return((
unsigned char *) NULL);
442 for ( ; *p !=
'\0'; p++)
443 if (isspace((
int) ((
unsigned char) *p)) == 0)
447 decode=(
unsigned char *) RelinquishMagickMemory(decode);
448 return((
unsigned char *) NULL);
454 for ( ; *p !=
'\0'; p++)
455 if (isspace((
int) ((
unsigned char) *p)) == 0)
457 decode=(
unsigned char *) RelinquishMagickMemory(decode);
458 return((
unsigned char *) NULL);
460 if ((
int) decode[i] != 0)
462 decode=(
unsigned char *) RelinquishMagickMemory(decode);
463 return((
unsigned char *) NULL);
503MagickExport
char *Base64Encode(
const unsigned char *blob,
504 const size_t blob_length,
size_t *encode_length)
518 assert(blob != (
const unsigned char *) NULL);
519 assert(blob_length != 0);
520 assert(encode_length != (
size_t *) NULL);
521 if (IsEventLogging() != MagickFalse)
522 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
524 encode=(
char *) AcquireQuantumMemory(blob_length/3+4,4*
sizeof(*encode));
525 if (encode == (
char *) NULL)
526 return((
char *) NULL);
528 for (p=blob; p < (blob+blob_length-2); p+=(ptrdiff_t) 3)
530 encode[i++]=Base64[(int) (*p >> 2)];
531 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
532 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
533 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
535 remainder=blob_length % 3;
547 for (j=0; j < (ssize_t) remainder; j++)
549 encode[i++]=Base64[(int) (code[0] >> 2)];
550 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
554 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
587MagickPrivate
void ChopPathComponents(
char *path,
const size_t components)
592 for (i=0; i < (ssize_t) components; i++)
593 GetPathComponent(path,HeadPath,path);
619MagickPrivate
void ExpandFilename(
char *path)
622 expand_path[MagickPathExtent];
624 if (path == (
char *) NULL)
628 (void) CopyMagickString(expand_path,path,MagickPathExtent);
629 if ((*(path+1) == *DirectorySeparator) || (*(path+1) ==
'\0'))
637 (void) CopyMagickString(expand_path,
".",MagickPathExtent);
638 (void) ConcatenateMagickString(expand_path,path+1,MagickPathExtent);
639 home=GetEnvironmentValue(
"HOME");
640 if (home == (
char *) NULL)
641 home=GetEnvironmentValue(
"USERPROFILE");
642 if (home != (
char *) NULL)
644 (void) CopyMagickString(expand_path,home,MagickPathExtent);
645 (void) ConcatenateMagickString(expand_path,path+1,MagickPathExtent);
646 home=DestroyString(home);
651#if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
653#if defined(MAGICKCORE_HAVE_GETPWNAM_R)
654 buffer[MagickPathExtent],
656 username[MagickPathExtent];
668 (void) CopyMagickString(username,path+1,MagickPathExtent);
669 p=strchr(username,
'/');
670 if (p != (
char *) NULL)
672#if !defined(MAGICKCORE_HAVE_GETPWNAM_R)
673 entry=getpwnam(username);
675 entry=(
struct passwd *) NULL;
676 if (getpwnam_r(username,&pwd,buffer,
sizeof(buffer),&entry) < 0)
679 if (entry == (
struct passwd *) NULL)
681 (void) CopyMagickString(expand_path,entry->pw_dir,MagickPathExtent);
682 if (p != (
char *) NULL)
684 (void) ConcatenateMagickString(expand_path,
"/",MagickPathExtent);
685 (void) ConcatenateMagickString(expand_path,p+1,MagickPathExtent);
689 (void) CopyMagickString(path,expand_path,MagickPathExtent);
748static inline void getcwd_utf8(
char *path,
size_t extent)
750#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
754 directory=getcwd(path,extent);
758 wide_path[MagickPathExtent];
760 (void) _wgetcwd(wide_path,MagickPathExtent-1);
761 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
765MagickExport MagickBooleanType ExpandFilenames(
int *number_arguments,
769 home_directory[MagickPathExtent],
786 assert(number_arguments != (
int *) NULL);
787 assert(arguments != (
char ***) NULL);
788 if (IsEventLogging() != MagickFalse)
789 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
790 vector=(
char **) AcquireQuantumMemory((
size_t) (*number_arguments+1),
792 if (vector == (
char **) NULL)
793 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
797 *home_directory=
'\0';
799 for (i=0; i < (ssize_t) *number_arguments; i++)
803 filename[MagickPathExtent],
804 magick[MagickPathExtent],
806 path[MagickPathExtent],
807 subimage[MagickPathExtent];
812 option=(*arguments)[i];
818 vector[count++]=ConstantString(option);
820 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
826 for (j=0; j < parameters; j++)
829 if (i == (ssize_t) *number_arguments)
831 option=(*arguments)[i];
832 vector[count++]=ConstantString(option);
836 if ((*option ==
'"') || (*option ==
'\''))
838 GetPathComponent(option,TailPath,filename);
839 GetPathComponent(option,MagickPath,magick);
840 if ((LocaleCompare(magick,
"CAPTION") == 0) ||
841 (LocaleCompare(magick,
"LABEL") == 0) ||
842 (LocaleCompare(magick,
"PANGO") == 0) ||
843 (LocaleCompare(magick,
"VID") == 0))
845 if ((IsGlob(filename) == MagickFalse) && (*option !=
'@'))
847 if (IsPathAccessible(option) != MagickFalse)
854 GetPathComponent(option,HeadPath,path);
855 GetPathComponent(option,SubimagePath,subimage);
856 ExpandFilename(path);
857 if (*home_directory ==
'\0')
858 getcwd_utf8(home_directory,MagickPathExtent-1);
859 filelist=ListFiles(*path ==
'\0' ? home_directory : path,filename,
876 exception=AcquireExceptionInfo();
877 files=FileToString(option,~0UL,exception);
878 exception=DestroyExceptionInfo(exception);
879 if (files == (
char *) NULL)
881 filelist=StringToArgv(files,&length);
882 if (filelist == (
char **) NULL)
884 files=DestroyString(files);
885 filelist[0]=DestroyString(filelist[0]);
886 for (j=0; j < (ssize_t) (length-1); j++)
887 filelist[j]=filelist[j+1];
888 number_files=(size_t) length-1;
890 if (filelist == (
char **) NULL)
892 for (j=0; j < (ssize_t) number_files; j++)
893 if (IsPathDirectory(filelist[j]) <= 0)
895 if (j == (ssize_t) number_files)
897 for (j=0; j < (ssize_t) number_files; j++)
898 filelist[j]=DestroyString(filelist[j]);
899 filelist=(
char **) RelinquishMagickMemory(filelist);
905 vector=(
char **) ResizeQuantumMemory(vector,(
size_t) ((ssize_t)
906 *number_arguments+count+(ssize_t) number_files+1),
sizeof(*vector));
907 if (vector == (
char **) NULL)
909 for (j=0; j < (ssize_t) number_files; j++)
910 filelist[j]=DestroyString(filelist[j]);
911 filelist=(
char **) RelinquishMagickMemory(filelist);
914 for (j=0; j < (ssize_t) number_files; j++)
917 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
926 vector[count++]=ConstantString(option);
927 for (k=0; k < parameters; k++)
930 if (j == (ssize_t) number_files)
933 vector[count++]=ConstantString(option);
937 (void) CopyMagickString(filename,path,MagickPathExtent);
939 (void) ConcatenateMagickString(filename,DirectorySeparator,
941 if (filelist[j] != (
char *) NULL)
942 (void) ConcatenateMagickString(filename,filelist[j],MagickPathExtent);
943 filelist[j]=DestroyString(filelist[j]);
944 if (strlen(filename) >= (MagickPathExtent-1))
945 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
946 if (IsPathDirectory(filename) <= 0)
949 file_path[MagickPathExtent];
954 (void) ConcatenateMagickString(file_path,magick,
956 (void) ConcatenateMagickString(file_path,
":",MagickPathExtent);
958 (void) ConcatenateMagickString(file_path,filename,MagickPathExtent);
959 if (*subimage !=
'\0')
961 (void) ConcatenateMagickString(file_path,
"[",MagickPathExtent);
962 (void) ConcatenateMagickString(file_path,subimage,
964 (void) ConcatenateMagickString(file_path,
"]",MagickPathExtent);
966 if (strlen(file_path) >= (MagickPathExtent-1))
967 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
968 if (destroy != MagickFalse)
971 vector[count]=DestroyString(vector[count]);
974 vector[count++]=ConstantString(file_path);
977 filelist=(
char **) RelinquishMagickMemory(filelist);
979 vector[count]=(
char *) NULL;
980 if (IsEventLogging() != MagickFalse)
985 command_line=AcquireString(vector[0]);
986 for (i=1; i < count; i++)
988 (void) ConcatenateString(&command_line,
" {");
989 (void) ConcatenateString(&command_line,vector[i]);
990 (void) ConcatenateString(&command_line,
"}");
992 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
993 "Command line: %s",command_line);
994 command_line=DestroyString(command_line);
996 *number_arguments=(int) count;
1026MagickPrivate MagickBooleanType GetExecutionPath(
char *path,
const size_t extent)
1032 directory=getcwd(path,(
unsigned long) extent);
1034#if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
1037 execution_path[PATH_MAX+1],
1038 link_path[MagickPathExtent];
1043 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.20g/exe",
1045 count=readlink(link_path,execution_path,PATH_MAX);
1048 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.20g/file",
1050 count=readlink(link_path,execution_path,PATH_MAX);
1052 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
1054 execution_path[count]=
'\0';
1055 (void) CopyMagickString(path,execution_path,extent);
1059#if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
1062 executable_path[PATH_MAX << 1];
1067 length=
sizeof(executable_path);
1068 if (_NSGetExecutablePath(executable_path,&length) == 0)
1071 *real_path = realpath_utf8(executable_path);
1073 if (real_path != (
char *) NULL)
1075 (void) CopyMagickString(path,real_path,extent);
1076 real_path=DestroyString(real_path);
1081#if defined(MAGICKCORE_HAVE_GETEXECNAME)
1086 execution_path=(
const char *) getexecname();
1087 if (execution_path != (
const char *) NULL)
1089 if (*execution_path != *DirectorySeparator)
1090 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1091 (void) ConcatenateMagickString(path,execution_path,extent);
1095#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1096 NTGetExecutionPath(path,extent);
1107 program_name=program_invocation_name;
1108 if (*program_invocation_name !=
'/')
1113 extent=strlen(directory)+strlen(program_name)+2;
1114 program_name=AcquireQuantumMemory(extent,
sizeof(*program_name));
1115 if (program_name == (
char *) NULL)
1116 program_name=program_invocation_name;
1118 count=FormatLocaleString(program_name,extent,
"%s/%s",directory,
1119 program_invocation_name);
1124 *real_path = realpath_utf8(program_name);
1126 if (real_path != (
char *) NULL)
1128 (void) CopyMagickString(path,real_path,extent);
1129 real_path=DestroyString(real_path);
1132 if (program_name != program_invocation_name)
1133 program_name=(
char *) RelinquishMagickMemory(program_name);
1136#if defined(__OpenBSD__)
1141 (void) CopyMagickString(path,__progname,extent);
1144 return(IsPathAccessible(path));
1165MagickPrivate ssize_t GetMagickPageSize(
void)
1172#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1173 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1174#elif defined(MAGICKCORE_HAVE_GETPAGESIZE)
1175 page_size=(ssize_t) getpagesize();
1206MagickExport MagickBooleanType GetPathAttributes(
const char *path,
1212 if (path == (
const char *) NULL)
1215 return(MagickFalse);
1217 (void) memset(attributes,0,
sizeof(
struct stat));
1218 status=stat_utf8(path,(
struct stat *) attributes) == 0 ? MagickTrue :
1254MagickExport
void GetPathComponent(
const char *path,PathType type,
1268 assert(path != (
const char *) NULL);
1269 assert(component != (
char *) NULL);
1270 if (IsEventLogging() != MagickFalse)
1271 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",path);
1277 (void) CopyMagickString(component,path,MagickPathExtent);
1280 if (type != SubcanonicalPath)
1282 p=component+strlen(component)-1;
1283 if ((strlen(component) > 2) && (*p ==
']'))
1285 q=strrchr(component,
'[');
1286 if ((q != (
char *) NULL) && ((q == component) || (*(q-1) !=
']')) &&
1287 (IsPathAccessible(path) == MagickFalse))
1293 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1294 (IsGeometry(q+1) == MagickFalse))
1298 subimage_length=(size_t) (p-q);
1299 subimage_offset=(size_t) (q-component+1);
1309 for (p=component; *p !=
'\0'; p++)
1311 if ((*p ==
'%') && (*(p+1) ==
'['))
1316 for (p++; (*p !=
']') && (*p !=
'\0'); p++) ;
1320 if ((p != component) && (*p ==
':') && (IsPathDirectory(component) < 0) &&
1321 (IsPathAccessible(component) == MagickFalse))
1327 if (IsMagickConflict(component) != MagickFalse)
1331 magick_length=(size_t) (p-component+1);
1332 for (q=component; *(++p) !=
'\0'; q++)
1341 for (p=component+strlen(component)-1; p > component; p--)
1342 if (IsBasenameSeparator(*p) != MagickFalse)
1348 if (magick_length != 0)
1349 (void) CopyMagickString(component,path,magick_length);
1356 if (*component !=
'\0')
1358 for (p=component+(strlen(component)-1); p > component; p--)
1360 if (IsBasenameSeparator(*p) != MagickFalse)
1378 if (IsBasenameSeparator(*p) != MagickFalse)
1379 (void) CopyMagickString(component,p+1,MagickPathExtent);
1384 if (IsBasenameSeparator(*p) != MagickFalse)
1385 (void) CopyMagickString(component,p+1,MagickPathExtent);
1386 if (*component !=
'\0')
1387 for (p=component+(strlen(component)-1); p > component; p--)
1395 case BasePathSansCompressExtension:
1398 extension[MagickPathExtent];
1403 GetPathComponent(path,ExtensionPath,extension);
1404 if ((LocaleCompare(extension,
"bz2") == 0) ||
1405 (LocaleCompare(extension,
"gz") == 0) ||
1406 (LocaleCompare(extension,
"svgz") == 0) ||
1407 (LocaleCompare(extension,
"wmz") == 0) ||
1408 (LocaleCompare(extension,
"Z") == 0))
1409 GetPathComponent(path,BasePath,component);
1414 if (IsBasenameSeparator(*p) != MagickFalse)
1415 (void) CopyMagickString(component,p+1,MagickPathExtent);
1416 if (*component !=
'\0')
1417 for (p=component+strlen(component)-1; p > component; p--)
1422 (void) CopyMagickString(component,p+1,MagickPathExtent);
1428 if ((subimage_length != 0) && (magick_length < subimage_offset))
1429 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1432 case SubcanonicalPath:
1464MagickPrivate
char **GetPathComponents(
const char *path,
1465 size_t *number_components)
1477 if (path == (
char *) NULL)
1478 return((
char **) NULL);
1479 *number_components=1;
1480 for (p=path; *p !=
'\0'; p++)
1481 if (IsBasenameSeparator(*p))
1482 (*number_components)++;
1483 components=(
char **) AcquireQuantumMemory((
size_t) *number_components+1UL,
1484 sizeof(*components));
1485 if (components == (
char **) NULL)
1486 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1488 for (i=0; i < (ssize_t) *number_components; i++)
1490 for (q=p; *q !=
'\0'; q++)
1491 if (IsBasenameSeparator(*q))
1493 components[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MagickPathExtent,
1494 sizeof(**components));
1495 if (components[i] == (
char *) NULL)
1496 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1497 (void) CopyMagickString(components[i],p,(
size_t) (q-p+1));
1500 components[i]=(
char *) NULL;
1527MagickExport MagickBooleanType IsPathAccessible(
const char *path)
1535 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1536 return(MagickFalse);
1537 if (LocaleCompare(path,
"-") == 0)
1539 status=GetPathAttributes(path,&attributes);
1540 if (status == MagickFalse)
1542 if (S_ISREG(attributes.st_mode) == 0)
1543 return(MagickFalse);
1544 if (access_utf8(path,F_OK) != 0)
1545 return(MagickFalse);
1572static int IsPathDirectory(
const char *path)
1580 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1581 return(MagickFalse);
1582 status=GetPathAttributes(path,&attributes);
1583 if (status == MagickFalse)
1585 if (S_ISDIR(attributes.st_mode) == 0)
1625#if defined(__cplusplus) || defined(c_plusplus)
1629static int FileCompare(
const void *x,
const void *y)
1635 p=(
const char **) x;
1636 q=(
const char **) y;
1637 return(LocaleCompare(*p,*q));
1640#if defined(__cplusplus) || defined(c_plusplus)
1644MagickPrivate
char **ListFiles(
const char *directory,
const char *pattern,
1645 size_t *number_entries)
1663 assert(directory != (
const char *) NULL);
1664 assert(pattern != (
const char *) NULL);
1665 assert(number_entries != (
size_t *) NULL);
1666 if (IsEventLogging() != MagickFalse)
1667 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",directory);
1669 current_directory=opendir(directory);
1670 if (current_directory == (DIR *) NULL)
1671 return((
char **) NULL);
1676 filelist=(
char **) AcquireQuantumMemory((
size_t) max_entries,
1678 if (filelist == (
char **) NULL)
1680 (void) closedir(current_directory);
1681 return((
char **) NULL);
1686 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
1687 if (buffer == (
struct dirent *) NULL)
1688 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1689 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1690 (entry != (
struct dirent *) NULL))
1692 if ((LocaleCompare(entry->d_name,
".") == 0) ||
1693 (LocaleCompare(entry->d_name,
"..") == 0))
1695 if ((IsPathDirectory(entry->d_name) > 0) ||
1696#
if defined(MAGICKCORE_WINDOWS_SUPPORT)
1697 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1699 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1702 if (*number_entries >= max_entries)
1708 filelist=(
char **) ResizeQuantumMemory(filelist,(
size_t)
1709 max_entries,
sizeof(*filelist));
1710 if (filelist == (
char **) NULL)
1718 p=strchr(entry->d_name,
';');
1721 if (*number_entries > 0)
1722 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1726 filelist[*number_entries]=(
char *) AcquireString(entry->d_name);
1727 (*number_entries)++;
1730 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
1731 (void) closedir(current_directory);
1732 if (filelist == (
char **) NULL)
1733 return((
char **) NULL);
1737 qsort((
void *) filelist,(
size_t) *number_entries,
sizeof(*filelist),
1766MagickExport
void MagickDelay(
const MagickSizeType milliseconds)
1768 if (milliseconds == 0)
1770#if defined(MAGICKCORE_HAVE_NANOSLEEP)
1775 timer.tv_sec=(time_t) (milliseconds/1000);
1776 timer.tv_nsec=(time_t) ((milliseconds % 1000)*1000*1000);
1777 (void) nanosleep(&timer,(
struct timespec *) NULL);
1779#elif defined(MAGICKCORE_HAVE_USLEEP)
1780 usleep(1000*milliseconds);
1781#elif defined(MAGICKCORE_HAVE_SELECT)
1786 timer.tv_sec=(long) milliseconds/1000;
1787 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1788 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1790#elif defined(MAGICKCORE_HAVE_POLL)
1791 (void) poll((
struct pollfd *) NULL,0,(
int) milliseconds);
1792#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1793 Sleep((
long) milliseconds);
1799 timer=milliseconds/1000.0;
1802#elif defined(__BEOS__)
1803 snooze(1000*milliseconds);
1809 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1810 while (clock() < time_end)
1840MagickExport
size_t MultilineCensus(
const char *label)
1848 if (label == (
char *) NULL)
1850 for (number_lines=1; *label !=
'\0'; label++)
1853 return(number_lines);
1879MagickPrivate MagickBooleanType ShredFile(
const char *path)
1906 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1907 return(MagickFalse);
1914 property=GetEnvironmentValue(
"MAGICK_SHRED_PASSES");
1915 if (property != (
char *) NULL)
1917 passes=(ssize_t) StringToInteger(property);
1918 property=DestroyString(property);
1920 property=GetPolicyValue(
"system:shred");
1921 if (property != (
char *) NULL)
1923 passes=(ssize_t) StringToInteger(property);
1924 property=DestroyString(property);
1932 file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
1934 return(MagickFalse);
1935 quantum=(size_t) MagickMinBufferExtent;
1936 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1937 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
1938 length=(MagickSizeType) file_stats.st_size;
1939 random_info=AcquireRandomInfo();
1940 key=GetRandomKey(random_info,quantum);
1941 for (i=0; i < passes; i++)
1949 if (lseek(file,0,SEEK_SET) < 0)
1951 for (j=0; j < (MagickOffsetType) length; j+=count)
1954 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
1955 count=write(file,GetStringInfoDatum(key),(
size_t)
1956 MagickMin((MagickOffsetType) quantum,(MagickOffsetType) length-j));
1964 if (j < (MagickOffsetType) length)
1967 key=DestroyStringInfo(key);
1968 random_info=DestroyRandomInfo(random_info);
1969 status=close_utf8(file);
1970 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);