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 *);
111 MagickExport MagickBooleanType AcquireUniqueFilename(
char *path)
116 file=AcquireUniqueFileResource(path);
153 MagickExport MagickBooleanType AcquireUniqueSymbolicLink(
const char *source,
176 assert(source != (
const char *) NULL);
177 assert(destination != (
char *) NULL);
178 #if defined(MAGICKCORE_HAVE_SYMLINK)
183 (void) AcquireUniqueFilename(destination);
184 (void) RelinquishUniqueFileResource(destination);
185 passes=GetPolicyValue(
"system:shred");
186 if (passes != (
char *) NULL)
187 passes=DestroyString(passes);
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(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(source_file);
229 (void) close(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(destination_file);
249 (void) close(source_file);
250 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
281 MagickExport
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);
347 MagickExport
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]=(q-Base64) << 2;
395 decode[i++]|=(q-Base64) >> 4;
396 decode[i]=((q-Base64) & 0x0f) << 4;
402 decode[i++]|=(q-Base64) >> 2;
403 decode[i]=((q-Base64) & 0x03) << 6;
409 decode[i++]|=(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);
503 MagickExport
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+=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))];
587 MagickPrivate
void ChopPathComponents(
char *path,
const size_t components)
592 for (i=0; i < (ssize_t) components; i++)
593 GetPathComponent(path,HeadPath,path);
619 MagickPrivate
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);
748 MagickExport MagickBooleanType ExpandFilenames(
int *number_arguments,
752 home_directory[MagickPathExtent],
769 assert(number_arguments != (
int *) NULL);
770 assert(arguments != (
char ***) NULL);
771 if (IsEventLogging() != MagickFalse)
772 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
773 vector=(
char **) AcquireQuantumMemory((
size_t) (*number_arguments+1),
775 if (vector == (
char **) NULL)
776 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
780 *home_directory=
'\0';
782 for (i=0; i < (ssize_t) *number_arguments; i++)
786 filename[MagickPathExtent],
787 magick[MagickPathExtent],
789 path[MagickPathExtent],
790 subimage[MagickPathExtent];
795 option=(*arguments)[i];
801 vector[count++]=ConstantString(option);
803 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
809 for (j=0; j < parameters; j++)
812 if (i == (ssize_t) *number_arguments)
814 option=(*arguments)[i];
815 vector[count++]=ConstantString(option);
819 if ((*option ==
'"') || (*option ==
'\''))
821 GetPathComponent(option,TailPath,filename);
822 GetPathComponent(option,MagickPath,magick);
823 if ((LocaleCompare(magick,
"CAPTION") == 0) ||
824 (LocaleCompare(magick,
"LABEL") == 0) ||
825 (LocaleCompare(magick,
"PANGO") == 0) ||
826 (LocaleCompare(magick,
"VID") == 0))
828 if ((IsGlob(filename) == MagickFalse) && (*option !=
'@'))
830 if ((*option !=
'@') && (IsPathAccessible(option) == MagickFalse))
835 GetPathComponent(option,HeadPath,path);
836 GetPathComponent(option,SubimagePath,subimage);
837 ExpandFilename(path);
838 if (*home_directory ==
'\0')
839 getcwd_utf8(home_directory,MagickPathExtent-1);
840 filelist=ListFiles(*path ==
'\0' ? home_directory : path,filename,
857 exception=AcquireExceptionInfo();
858 files=FileToString(option+1,~0UL,exception);
859 exception=DestroyExceptionInfo(exception);
860 if (files == (
char *) NULL)
862 filelist=StringToArgv(files,&length);
863 if (filelist == (
char **) NULL)
865 files=DestroyString(files);
866 filelist[0]=DestroyString(filelist[0]);
867 for (j=0; j < (ssize_t) (length-1); j++)
868 filelist[j]=filelist[j+1];
869 number_files=(size_t) length-1;
871 if (filelist == (
char **) NULL)
873 for (j=0; j < (ssize_t) number_files; j++)
874 if (IsPathDirectory(filelist[j]) <= 0)
876 if (j == (ssize_t) number_files)
878 for (j=0; j < (ssize_t) number_files; j++)
879 filelist[j]=DestroyString(filelist[j]);
880 filelist=(
char **) RelinquishMagickMemory(filelist);
886 vector=(
char **) ResizeQuantumMemory(vector,(
size_t) *number_arguments+
887 count+number_files+1,
sizeof(*vector));
888 if (vector == (
char **) NULL)
890 for (j=0; j < (ssize_t) number_files; j++)
891 filelist[j]=DestroyString(filelist[j]);
892 filelist=(
char **) RelinquishMagickMemory(filelist);
895 for (j=0; j < (ssize_t) number_files; j++)
898 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
907 vector[count++]=ConstantString(option);
908 for (k=0; k < parameters; k++)
911 if (j == (ssize_t) number_files)
914 vector[count++]=ConstantString(option);
918 (void) CopyMagickString(filename,path,MagickPathExtent);
920 (void) ConcatenateMagickString(filename,DirectorySeparator,
922 if (filelist[j] != (
char *) NULL)
923 (
void) ConcatenateMagickString(filename,filelist[j],MagickPathExtent);
924 filelist[j]=DestroyString(filelist[j]);
925 if (strlen(filename) >= (MagickPathExtent-1))
926 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
927 if (IsPathDirectory(filename) <= 0)
930 file_path[MagickPathExtent];
935 (void) ConcatenateMagickString(file_path,magick,
937 (void) ConcatenateMagickString(file_path,
":",MagickPathExtent);
939 (void) ConcatenateMagickString(file_path,filename,MagickPathExtent);
940 if (*subimage !=
'\0')
942 (void) ConcatenateMagickString(file_path,
"[",MagickPathExtent);
943 (void) ConcatenateMagickString(file_path,subimage,
945 (void) ConcatenateMagickString(file_path,
"]",MagickPathExtent);
947 if (strlen(file_path) >= (MagickPathExtent-1))
948 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
949 if (destroy != MagickFalse)
952 vector[count]=DestroyString(vector[count]);
955 vector[count++]=ConstantString(file_path);
958 filelist=(
char **) RelinquishMagickMemory(filelist);
960 vector[count]=(
char *) NULL;
961 if (IsEventLogging() != MagickFalse)
966 command_line=AcquireString(vector[0]);
967 for (i=1; i < count; i++)
969 (void) ConcatenateString(&command_line,
" {");
970 (void) ConcatenateString(&command_line,vector[i]);
971 (void) ConcatenateString(&command_line,
"}");
973 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
974 "Command line: %s",command_line);
975 command_line=DestroyString(command_line);
977 *number_arguments=(int) count;
1007 MagickPrivate MagickBooleanType GetExecutionPath(
char *path,
const size_t extent)
1013 directory=getcwd(path,(
unsigned long) extent);
1015 #if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
1018 execution_path[PATH_MAX+1],
1019 link_path[MagickPathExtent];
1024 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.20g/exe",
1026 count=readlink(link_path,execution_path,PATH_MAX);
1029 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.20g/file",
1031 count=readlink(link_path,execution_path,PATH_MAX);
1033 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
1035 execution_path[count]=
'\0';
1036 (void) CopyMagickString(path,execution_path,extent);
1040 #if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
1043 executable_path[PATH_MAX << 1],
1044 execution_path[PATH_MAX+1];
1049 length=
sizeof(executable_path);
1050 if ((_NSGetExecutablePath(executable_path,&length) == 0) &&
1051 (realpath(executable_path,execution_path) != (
char *) NULL))
1052 (
void) CopyMagickString(path,execution_path,extent);
1055 #if defined(MAGICKCORE_HAVE_GETEXECNAME)
1060 execution_path=(
const char *) getexecname();
1061 if (execution_path != (
const char *) NULL)
1063 if (*execution_path != *DirectorySeparator)
1064 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1065 (void) ConcatenateMagickString(path,execution_path,extent);
1069 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1070 NTGetExecutionPath(path,extent);
1072 #if defined(__GNU__)
1081 program_name=program_invocation_name;
1082 if (*program_invocation_name !=
'/')
1087 extent=strlen(directory)+strlen(program_name)+2;
1088 program_name=AcquireQuantumMemory(extent,
sizeof(*program_name));
1089 if (program_name == (
char *) NULL)
1090 program_name=program_invocation_name;
1092 count=FormatLocaleString(program_name,extent,
"%s/%s",directory,
1093 program_invocation_name);
1098 execution_path[PATH_MAX+1];
1100 if (realpath(program_name,execution_path) != (
char *) NULL)
1101 (void) CopyMagickString(path,execution_path,extent);
1103 if (program_name != program_invocation_name)
1104 program_name=(
char *) RelinquishMagickMemory(program_name);
1107 #if defined(__OpenBSD__)
1112 (void) CopyMagickString(path,__progname,extent);
1115 return(IsPathAccessible(path));
1136 MagickPrivate ssize_t GetMagickPageSize(
void)
1143 #if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1144 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1145 #elif defined(MAGICKCORE_HAVE_GETPAGESIZE)
1146 page_size=(ssize_t) getpagesize();
1177 MagickExport MagickBooleanType GetPathAttributes(
const char *path,
1183 if (path == (
const char *) NULL)
1186 return(MagickFalse);
1188 (void) memset(attributes,0,
sizeof(
struct stat));
1189 status=stat_utf8(path,(
struct stat *) attributes) == 0 ? MagickTrue :
1225 MagickExport
void GetPathComponent(
const char *path,PathType type,
1239 assert(path != (
const char *) NULL);
1240 assert(component != (
char *) NULL);
1241 if (IsEventLogging() != MagickFalse)
1242 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",path);
1248 (void) CopyMagickString(component,path,MagickPathExtent);
1251 if (type != SubcanonicalPath)
1253 p=component+strlen(component)-1;
1254 if ((strlen(component) > 2) && (*p ==
']'))
1256 q=strrchr(component,
'[');
1257 if ((q != (
char *) NULL) && ((q == component) || (*(q-1) !=
']')) &&
1258 (IsPathAccessible(path) == MagickFalse))
1264 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1265 (IsGeometry(q+1) == MagickFalse))
1269 subimage_length=(size_t) (p-q);
1270 subimage_offset=(size_t) (q-component+1);
1277 #if defined(__OS2__)
1280 for (p=component; *p !=
'\0'; p++)
1282 if ((*p ==
'%') && (*(p+1) ==
'['))
1287 for (p++; (*p !=
']') && (*p !=
'\0'); p++) ;
1291 if ((p != component) && (*p ==
':') && (IsPathDirectory(component) < 0) &&
1292 (IsPathAccessible(component) == MagickFalse))
1298 if (IsMagickConflict(component) != MagickFalse)
1302 magick_length=(size_t) (p-component+1);
1303 for (q=component; *(++p) !=
'\0'; q++)
1312 for (p=component+strlen(component)-1; p > component; p--)
1313 if (IsBasenameSeparator(*p) != MagickFalse)
1319 if (magick_length != 0)
1320 (void) CopyMagickString(component,path,magick_length);
1327 if (*component !=
'\0')
1329 for (p=component+(strlen(component)-1); p > component; p--)
1331 if (IsBasenameSeparator(*p) != MagickFalse)
1348 if (IsBasenameSeparator(*p) != MagickFalse)
1349 (
void) CopyMagickString(component,p+1,MagickPathExtent);
1354 if (IsBasenameSeparator(*p) != MagickFalse)
1355 (void) CopyMagickString(component,p+1,MagickPathExtent);
1356 if (*component !=
'\0')
1357 for (p=component+(strlen(component)-1); p > component; p--)
1365 case BasePathSansCompressExtension:
1368 extension[MagickPathExtent];
1373 GetPathComponent(path,ExtensionPath,extension);
1374 if ((LocaleCompare(extension,
"bz2") == 0) ||
1375 (LocaleCompare(extension,
"gz") == 0) ||
1376 (LocaleCompare(extension,
"svgz") == 0) ||
1377 (LocaleCompare(extension,
"wmz") == 0) ||
1378 (LocaleCompare(extension,
"Z") == 0))
1379 GetPathComponent(path,BasePath,component);
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--)
1392 (void) CopyMagickString(component,p+1,MagickPathExtent);
1398 if ((subimage_length != 0) && (magick_length < subimage_offset))
1399 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1402 case SubcanonicalPath:
1434 MagickPrivate
char **GetPathComponents(
const char *path,
1435 size_t *number_components)
1447 if (path == (
char *) NULL)
1448 return((
char **) NULL);
1449 *number_components=1;
1450 for (p=path; *p !=
'\0'; p++)
1451 if (IsBasenameSeparator(*p))
1452 (*number_components)++;
1453 components=(
char **) AcquireQuantumMemory((
size_t) *number_components+1UL,
1454 sizeof(*components));
1455 if (components == (
char **) NULL)
1456 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1458 for (i=0; i < (ssize_t) *number_components; i++)
1460 for (q=p; *q !=
'\0'; q++)
1461 if (IsBasenameSeparator(*q))
1463 components[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MagickPathExtent,
1464 sizeof(**components));
1465 if (components[i] == (
char *) NULL)
1466 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1467 (void) CopyMagickString(components[i],p,(
size_t) (q-p+1));
1470 components[i]=(
char *) NULL;
1497 MagickExport MagickBooleanType IsPathAccessible(
const char *path)
1505 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1506 return(MagickFalse);
1507 if (LocaleCompare(path,
"-") == 0)
1509 status=GetPathAttributes(path,&attributes);
1510 if (status == MagickFalse)
1512 if (S_ISREG(attributes.st_mode) == 0)
1513 return(MagickFalse);
1514 if (access_utf8(path,F_OK) != 0)
1515 return(MagickFalse);
1542 static int IsPathDirectory(
const char *path)
1550 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1551 return(MagickFalse);
1552 status=GetPathAttributes(path,&attributes);
1553 if (status == MagickFalse)
1555 if (S_ISDIR(attributes.st_mode) == 0)
1595 #if defined(__cplusplus) || defined(c_plusplus)
1599 static int FileCompare(
const void *x,
const void *y)
1605 p=(
const char **) x;
1606 q=(
const char **) y;
1607 return(LocaleCompare(*p,*q));
1610 #if defined(__cplusplus) || defined(c_plusplus)
1614 MagickPrivate
char **ListFiles(
const char *directory,
const char *pattern,
1615 size_t *number_entries)
1633 assert(directory != (
const char *) NULL);
1634 assert(pattern != (
const char *) NULL);
1635 assert(number_entries != (
size_t *) NULL);
1636 if (IsEventLogging() != MagickFalse)
1637 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",directory);
1639 current_directory=opendir(directory);
1640 if (current_directory == (
DIR *) NULL)
1641 return((
char **) NULL);
1646 filelist=(
char **) AcquireQuantumMemory((
size_t) max_entries,
1648 if (filelist == (
char **) NULL)
1650 (void) closedir(current_directory);
1651 return((
char **) NULL);
1656 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
1657 if (buffer == (
struct dirent *) NULL)
1658 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1659 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1660 (entry != (
struct dirent *) NULL))
1662 if ((LocaleCompare(entry->d_name,
".") == 0) ||
1663 (LocaleCompare(entry->d_name,
"..") == 0))
1665 if ((IsPathDirectory(entry->d_name) > 0) ||
1666 #
if defined(MAGICKCORE_WINDOWS_SUPPORT)
1667 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1669 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1672 if (*number_entries >= max_entries)
1678 filelist=(
char **) ResizeQuantumMemory(filelist,(
size_t)
1679 max_entries,
sizeof(*filelist));
1680 if (filelist == (
char **) NULL)
1688 p=strchr(entry->d_name,
';');
1691 if (*number_entries > 0)
1692 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1696 filelist[*number_entries]=(
char *) AcquireString(entry->d_name);
1697 (*number_entries)++;
1700 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
1701 (void) closedir(current_directory);
1702 if (filelist == (
char **) NULL)
1703 return((
char **) NULL);
1707 qsort((
void *) filelist,(
size_t) *number_entries,
sizeof(*filelist),
1736 MagickExport
void MagickDelay(
const MagickSizeType milliseconds)
1738 if (milliseconds == 0)
1740 #if defined(MAGICKCORE_HAVE_NANOSLEEP)
1745 timer.tv_sec=(time_t) (milliseconds/1000);
1746 timer.tv_nsec=(milliseconds % 1000)*1000*1000;
1747 (void) nanosleep(&timer,(
struct timespec *) NULL);
1749 #elif defined(MAGICKCORE_HAVE_USLEEP)
1750 usleep(1000*milliseconds);
1751 #elif defined(MAGICKCORE_HAVE_SELECT)
1756 timer.tv_sec=(long) milliseconds/1000;
1757 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1758 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1760 #elif defined(MAGICKCORE_HAVE_POLL)
1761 (void) poll((
struct pollfd *) NULL,0,(int) milliseconds);
1762 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1763 Sleep((
long) milliseconds);
1769 timer=milliseconds/1000.0;
1772 #elif defined(__BEOS__)
1773 snooze(1000*milliseconds);
1779 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1780 while (clock() < time_end)
1810 MagickExport
size_t MultilineCensus(
const char *label)
1818 if (label == (
char *) NULL)
1820 for (number_lines=1; *label !=
'\0'; label++)
1823 return(number_lines);
1849 MagickPrivate MagickBooleanType ShredFile(
const char *path)
1876 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1877 return(MagickFalse);
1884 property=GetEnvironmentValue(
"MAGICK_SHRED_PASSES");
1885 if (property != (
char *) NULL)
1887 passes=(ssize_t) StringToInteger(property);
1888 property=DestroyString(property);
1890 property=GetPolicyValue(
"system:shred");
1891 if (property != (
char *) NULL)
1893 passes=(ssize_t) StringToInteger(property);
1894 property=DestroyString(property);
1902 file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
1904 return(MagickFalse);
1905 quantum=(size_t) MagickMinBufferExtent;
1906 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1907 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
1908 length=(MagickSizeType) file_stats.st_size;
1909 random_info=AcquireRandomInfo();
1910 key=GetRandomKey(random_info,quantum);
1911 for (i=0; i < passes; i++)
1919 if (lseek(file,0,SEEK_SET) < 0)
1921 for (j=0; j < (MagickOffsetType) length; j+=count)
1924 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
1925 count=write(file,GetStringInfoDatum(key),(
size_t)
1926 MagickMin((MagickSizeType) quantum,length-j));
1934 if (j < (MagickOffsetType) length)
1937 key=DestroyStringInfo(key);
1938 random_info=DestroyRandomInfo(random_info);
1940 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);