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)
186 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights |
187 WritePolicyRights,
"follow");
188 passes=GetPolicyValue(
"system:shred");
189 if (passes == (
char *) NULL)
190 passes=GetEnvironmentValue(
"MAGICK_SHRED_PASSES");
191 if ((passes != (
char *) NULL) || (status == MagickFalse))
192 passes=DestroyString(passes);
195 (void) AcquireUniqueFilename(destination);
196 (void) RelinquishUniqueFileResource(destination);
197 if (*source == *DirectorySeparator)
199 if (symlink(source,destination) == 0)
205 path[MagickPathExtent];
208 if (getcwd(path,MagickPathExtent) == (
char *) NULL)
210 (void) ConcatenateMagickString(path,DirectorySeparator,
212 (void) ConcatenateMagickString(path,source,MagickPathExtent);
213 if (symlink(path,destination) == 0)
222 destination_file=AcquireUniqueFileResource(destination);
223 if (destination_file == -1)
225 source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
226 if (source_file == -1)
228 (void) close_utf8(destination_file);
229 (void) RelinquishUniqueFileResource(destination);
232 quantum=(size_t) MagickMaxBufferExtent;
233 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
234 quantum=(
size_t) MagickMin(attributes.st_size,MagickMaxBufferExtent);
235 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
236 if (buffer == (
unsigned char *) NULL)
238 (void) close_utf8(source_file);
239 (void) close_utf8(destination_file);
240 (void) RelinquishUniqueFileResource(destination);
246 count=(ssize_t) read(source_file,buffer,quantum);
249 length=(size_t) count;
250 count=(ssize_t) write(destination_file,buffer,length);
251 if ((
size_t) count != length)
253 (void) RelinquishUniqueFileResource(destination);
258 (void) close_utf8(destination_file);
259 (void) close_utf8(source_file);
260 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
291MagickExport
void AppendImageFormat(
const char *format,
char *filename)
294 extension[MagickPathExtent],
295 root[MagickPathExtent];
297 assert(format != (
char *) NULL);
298 assert(filename != (
char *) NULL);
299 if (IsEventLogging() != MagickFalse)
300 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
301 if ((*format ==
'\0') || (*filename ==
'\0'))
303 if (LocaleCompare(filename,
"-") == 0)
306 message[MagickPathExtent];
308 (void) FormatLocaleString(message,MagickPathExtent,
"%s:%s",format,
310 (void) CopyMagickString(filename,message,MagickPathExtent);
313 GetPathComponent(filename,ExtensionPath,extension);
314 if ((LocaleCompare(extension,
"Z") == 0) ||
315 (LocaleCompare(extension,
"bz2") == 0) ||
316 (LocaleCompare(extension,
"gz") == 0) ||
317 (LocaleCompare(extension,
"wmz") == 0) ||
318 (LocaleCompare(extension,
"svgz") == 0))
320 GetPathComponent(filename,RootPath,root);
321 (void) CopyMagickString(filename,root,MagickPathExtent);
322 GetPathComponent(filename,RootPath,root);
323 (void) FormatLocaleString(filename,MagickPathExtent,
"%s.%s.%s",root,
327 GetPathComponent(filename,RootPath,root);
328 (void) FormatLocaleString(filename,MagickPathExtent,
"%s.%s",root,format);
357MagickExport
unsigned char *Base64Decode(
const char *source,
size_t *length)
372 assert(source != (
char *) NULL);
373 assert(length != (
size_t *) NULL);
374 if (IsEventLogging() != MagickFalse)
375 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
377 decode=(
unsigned char *) AcquireQuantumMemory((strlen(source)+3)/4,
379 if (decode == (
unsigned char *) NULL)
380 return((
unsigned char *) NULL);
383 for (p=source; *p !=
'\0'; p++)
385 if (isspace((
int) ((
unsigned char) *p)) != 0)
390 if (q == (
char *) NULL)
392 decode=(
unsigned char *) RelinquishMagickMemory(decode);
393 return((
unsigned char *) NULL);
399 decode[i]=(q-Base64) << 2;
405 decode[i++]|=(q-Base64) >> 4;
406 decode[i]=((q-Base64) & 0x0f) << 4;
412 decode[i++]|=(q-Base64) >> 2;
413 decode[i]=((q-Base64) & 0x03) << 6;
419 decode[i++]|=(q-Base64);
432 decode=(
unsigned char *) RelinquishMagickMemory(decode);
433 return((
unsigned char *) NULL);
447 decode=(
unsigned char *) RelinquishMagickMemory(decode);
448 return((
unsigned char *) NULL);
452 for ( ; *p !=
'\0'; p++)
453 if (isspace((
int) ((
unsigned char) *p)) == 0)
457 decode=(
unsigned char *) RelinquishMagickMemory(decode);
458 return((
unsigned char *) NULL);
464 for ( ; *p !=
'\0'; p++)
465 if (isspace((
int) ((
unsigned char) *p)) == 0)
467 decode=(
unsigned char *) RelinquishMagickMemory(decode);
468 return((
unsigned char *) NULL);
470 if ((
int) decode[i] != 0)
472 decode=(
unsigned char *) RelinquishMagickMemory(decode);
473 return((
unsigned char *) NULL);
513MagickExport
char *Base64Encode(
const unsigned char *blob,
514 const size_t blob_length,
size_t *encode_length)
528 assert(blob != (
const unsigned char *) NULL);
529 assert(blob_length != 0);
530 assert(encode_length != (
size_t *) NULL);
531 if (IsEventLogging() != MagickFalse)
532 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
534 encode=(
char *) AcquireQuantumMemory(blob_length/3+4,4*
sizeof(*encode));
535 if (encode == (
char *) NULL)
536 return((
char *) NULL);
538 for (p=blob; p < (blob+blob_length-2); p+=(ptrdiff_t) 3)
540 encode[i++]=Base64[(int) (*p >> 2)];
541 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
542 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
543 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
545 remainder=blob_length % 3;
557 for (j=0; j < (ssize_t) remainder; j++)
559 encode[i++]=Base64[(int) (code[0] >> 2)];
560 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
564 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
597MagickPrivate
void ChopPathComponents(
char *path,
const size_t components)
602 for (i=0; i < (ssize_t) components; i++)
603 GetPathComponent(path,HeadPath,path);
629MagickPrivate
void ExpandFilename(
char *path)
632 expand_path[MagickPathExtent];
634 if (path == (
char *) NULL)
638 (void) CopyMagickString(expand_path,path,MagickPathExtent);
639 if ((*(path+1) == *DirectorySeparator) || (*(path+1) ==
'\0'))
647 (void) CopyMagickString(expand_path,
".",MagickPathExtent);
648 (void) ConcatenateMagickString(expand_path,path+1,MagickPathExtent);
649 home=GetEnvironmentValue(
"HOME");
650 if (home == (
char *) NULL)
651 home=GetEnvironmentValue(
"USERPROFILE");
652 if (home != (
char *) NULL)
654 (void) CopyMagickString(expand_path,home,MagickPathExtent);
655 (void) ConcatenateMagickString(expand_path,path+1,MagickPathExtent);
656 home=DestroyString(home);
661#if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
663#if defined(MAGICKCORE_HAVE_GETPWNAM_R)
664 buffer[MagickPathExtent],
666 username[MagickPathExtent];
678 (void) CopyMagickString(username,path+1,MagickPathExtent);
679 p=strchr(username,
'/');
680 if (p != (
char *) NULL)
682#if !defined(MAGICKCORE_HAVE_GETPWNAM_R)
683 entry=getpwnam(username);
685 entry=(
struct passwd *) NULL;
686 if (getpwnam_r(username,&pwd,buffer,
sizeof(buffer),&entry) < 0)
689 if (entry == (
struct passwd *) NULL)
691 (void) CopyMagickString(expand_path,entry->pw_dir,MagickPathExtent);
692 if (p != (
char *) NULL)
694 (void) ConcatenateMagickString(expand_path,
"/",MagickPathExtent);
695 (void) ConcatenateMagickString(expand_path,p+1,MagickPathExtent);
699 (void) CopyMagickString(path,expand_path,MagickPathExtent);
758MagickExport MagickBooleanType ExpandFilenames(
int *number_arguments,
762 home_directory[MagickPathExtent],
779 assert(number_arguments != (
int *) NULL);
780 assert(arguments != (
char ***) NULL);
781 if (IsEventLogging() != MagickFalse)
782 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
783 vector=(
char **) AcquireQuantumMemory((
size_t) (*number_arguments+1),
785 if (vector == (
char **) NULL)
786 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
790 *home_directory=
'\0';
792 for (i=0; i < (ssize_t) *number_arguments; i++)
796 filename[MagickPathExtent],
797 magick[MagickPathExtent],
799 path[MagickPathExtent],
800 subimage[MagickPathExtent];
805 option=(*arguments)[i];
811 vector[count++]=ConstantString(option);
813 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
819 for (j=0; j < parameters; j++)
822 if (i == (ssize_t) *number_arguments)
824 option=(*arguments)[i];
825 vector[count++]=ConstantString(option);
829 if ((*option ==
'"') || (*option ==
'\''))
831 GetPathComponent(option,TailPath,filename);
832 GetPathComponent(option,MagickPath,magick);
833 if ((LocaleCompare(magick,
"CAPTION") == 0) ||
834 (LocaleCompare(magick,
"LABEL") == 0) ||
835 (LocaleCompare(magick,
"PANGO") == 0) ||
836 (LocaleCompare(magick,
"VID") == 0))
838 if ((IsGlob(filename) == MagickFalse) && (*option !=
'@'))
840 if (IsPathAccessible(option) != MagickFalse)
847 GetPathComponent(option,HeadPath,path);
848 GetPathComponent(option,SubimagePath,subimage);
849 ExpandFilename(path);
850 if (*home_directory ==
'\0')
851 getcwd_utf8(home_directory,MagickPathExtent-1);
852 filelist=ListFiles(*path ==
'\0' ? home_directory : path,filename,
869 exception=AcquireExceptionInfo();
870 files=FileToString(option,~0UL,exception);
871 exception=DestroyExceptionInfo(exception);
872 if (files == (
char *) NULL)
874 filelist=StringToArgv(files,&length);
875 if (filelist == (
char **) NULL)
877 files=DestroyString(files);
878 filelist[0]=DestroyString(filelist[0]);
879 for (j=0; j < (ssize_t) (length-1); j++)
880 filelist[j]=filelist[j+1];
881 number_files=(size_t) length-1;
883 if (filelist == (
char **) NULL)
885 for (j=0; j < (ssize_t) number_files; j++)
886 if (IsPathDirectory(filelist[j]) <= 0)
888 if (j == (ssize_t) number_files)
890 for (j=0; j < (ssize_t) number_files; j++)
891 filelist[j]=DestroyString(filelist[j]);
892 filelist=(
char **) RelinquishMagickMemory(filelist);
898 vector=(
char **) ResizeQuantumMemory(vector,(
size_t) ((ssize_t)
899 *number_arguments+count+(ssize_t) number_files+1),
sizeof(*vector));
900 if (vector == (
char **) NULL)
902 for (j=0; j < (ssize_t) number_files; j++)
903 filelist[j]=DestroyString(filelist[j]);
904 filelist=(
char **) RelinquishMagickMemory(filelist);
907 for (j=0; j < (ssize_t) number_files; j++)
910 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
919 vector[count++]=ConstantString(option);
920 for (k=0; k < parameters; k++)
923 if (j == (ssize_t) number_files)
926 vector[count++]=ConstantString(option);
930 (void) CopyMagickString(filename,path,MagickPathExtent);
932 (void) ConcatenateMagickString(filename,DirectorySeparator,
934 if (filelist[j] != (
char *) NULL)
935 (
void) ConcatenateMagickString(filename,filelist[j],MagickPathExtent);
936 filelist[j]=DestroyString(filelist[j]);
937 if (strlen(filename) >= (MagickPathExtent-1))
938 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
939 if (IsPathDirectory(filename) <= 0)
942 file_path[MagickPathExtent];
947 (void) ConcatenateMagickString(file_path,magick,
949 (void) ConcatenateMagickString(file_path,
":",MagickPathExtent);
951 (void) ConcatenateMagickString(file_path,filename,MagickPathExtent);
952 if (*subimage !=
'\0')
954 (void) ConcatenateMagickString(file_path,
"[",MagickPathExtent);
955 (void) ConcatenateMagickString(file_path,subimage,
957 (void) ConcatenateMagickString(file_path,
"]",MagickPathExtent);
959 if (strlen(file_path) >= (MagickPathExtent-1))
960 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
961 if (destroy != MagickFalse)
964 vector[count]=DestroyString(vector[count]);
967 vector[count++]=ConstantString(file_path);
970 filelist=(
char **) RelinquishMagickMemory(filelist);
972 vector[count]=(
char *) NULL;
973 if (IsEventLogging() != MagickFalse)
978 command_line=AcquireString(vector[0]);
979 for (i=1; i < count; i++)
981 (void) ConcatenateString(&command_line,
" {");
982 (void) ConcatenateString(&command_line,vector[i]);
983 (void) ConcatenateString(&command_line,
"}");
985 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
986 "Command line: %s",command_line);
987 command_line=DestroyString(command_line);
989 *number_arguments=(int) count;
1019MagickPrivate MagickBooleanType GetExecutionPath(
char *path,
const size_t extent)
1025 directory=getcwd(path,(
unsigned long) extent);
1027#if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
1030 execution_path[PATH_MAX+1],
1031 link_path[MagickPathExtent];
1036 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.20g/exe",
1038 count=readlink(link_path,execution_path,PATH_MAX);
1041 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.20g/file",
1043 count=readlink(link_path,execution_path,PATH_MAX);
1045 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
1047 execution_path[count]=
'\0';
1048 (void) CopyMagickString(path,execution_path,extent);
1052#if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
1055 executable_path[PATH_MAX << 1];
1060 length=
sizeof(executable_path);
1061 if (_NSGetExecutablePath(executable_path,&length) == 0)
1064 *real_path = realpath_utf8(executable_path);
1066 if (real_path != (
char *) NULL)
1068 (void) CopyMagickString(path,real_path,extent);
1069 real_path=DestroyString(real_path);
1074#if defined(MAGICKCORE_HAVE_GETEXECNAME)
1079 execution_path=(
const char *) getexecname();
1080 if (execution_path != (
const char *) NULL)
1082 if (*execution_path != *DirectorySeparator)
1083 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1084 (void) ConcatenateMagickString(path,execution_path,extent);
1088#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1089 NTGetExecutionPath(path,extent);
1100 program_name=program_invocation_name;
1101 if (*program_invocation_name !=
'/')
1106 extent=strlen(directory)+strlen(program_name)+2;
1107 program_name=AcquireQuantumMemory(extent,
sizeof(*program_name));
1108 if (program_name == (
char *) NULL)
1109 program_name=program_invocation_name;
1111 count=FormatLocaleString(program_name,extent,
"%s/%s",directory,
1112 program_invocation_name);
1117 *real_path = realpath_utf8(program_name);
1119 if (real_path != (
char *) NULL)
1121 (void) CopyMagickString(path,real_path,extent);
1122 real_path=DestroyString(real_path);
1125 if (program_name != program_invocation_name)
1126 program_name=(
char *) RelinquishMagickMemory(program_name);
1129#if defined(__OpenBSD__)
1134 (void) CopyMagickString(path,__progname,extent);
1137 return(IsPathAccessible(path));
1158MagickPrivate ssize_t GetMagickPageSize(
void)
1165#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1166 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1167#elif defined(MAGICKCORE_HAVE_GETPAGESIZE)
1168 page_size=(ssize_t) getpagesize();
1199MagickExport MagickBooleanType GetPathAttributes(
const char *path,
1205 if (path == (
const char *) NULL)
1208 return(MagickFalse);
1210 (void) memset(attributes,0,
sizeof(
struct stat));
1211 status=stat_utf8(path,(
struct stat *) attributes) == 0 ? MagickTrue :
1247MagickExport
void GetPathComponent(
const char *path,PathType type,
1261 assert(path != (
const char *) NULL);
1262 assert(component != (
char *) NULL);
1263 if (IsEventLogging() != MagickFalse)
1264 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",path);
1270 (void) CopyMagickString(component,path,MagickPathExtent);
1273 if (type != SubcanonicalPath)
1275 p=component+strlen(component)-1;
1276 if ((strlen(component) > 2) && (*p ==
']'))
1278 q=strrchr(component,
'[');
1279 if ((q != (
char *) NULL) && ((q == component) || (*(q-1) !=
']')) &&
1280 (IsPathAccessible(path) == MagickFalse))
1286 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1287 (IsGeometry(q+1) == MagickFalse))
1291 subimage_length=(size_t) (p-q);
1292 subimage_offset=(size_t) (q-component+1);
1302 for (p=component; *p !=
'\0'; p++)
1304 if ((*p ==
'%') && (*(p+1) ==
'['))
1309 for (p++; (*p !=
']') && (*p !=
'\0'); p++) ;
1313 if ((p != component) && (*p ==
':') && (IsPathDirectory(component) < 0) &&
1314 (IsPathAccessible(component) == MagickFalse))
1320 if (IsMagickConflict(component) != MagickFalse)
1324 magick_length=(size_t) (p-component+1);
1325 for (q=component; *(++p) !=
'\0'; q++)
1334 for (p=component+strlen(component)-1; p > component; p--)
1335 if (IsBasenameSeparator(*p) != MagickFalse)
1341 if (magick_length != 0)
1342 (void) CopyMagickString(component,path,magick_length);
1349 if (*component !=
'\0')
1351 for (p=component+(strlen(component)-1); p > component; p--)
1353 if (IsBasenameSeparator(*p) != MagickFalse)
1371 if (IsBasenameSeparator(*p) != MagickFalse)
1372 (
void) CopyMagickString(component,p+1,MagickPathExtent);
1377 if (IsBasenameSeparator(*p) != MagickFalse)
1378 (void) CopyMagickString(component,p+1,MagickPathExtent);
1379 if (*component !=
'\0')
1380 for (p=component+(strlen(component)-1); p > component; p--)
1388 case BasePathSansCompressExtension:
1391 extension[MagickPathExtent];
1396 GetPathComponent(path,ExtensionPath,extension);
1397 if ((LocaleCompare(extension,
"bz2") == 0) ||
1398 (LocaleCompare(extension,
"gz") == 0) ||
1399 (LocaleCompare(extension,
"svgz") == 0) ||
1400 (LocaleCompare(extension,
"wmz") == 0) ||
1401 (LocaleCompare(extension,
"Z") == 0))
1402 GetPathComponent(path,BasePath,component);
1407 if (IsBasenameSeparator(*p) != MagickFalse)
1408 (void) CopyMagickString(component,p+1,MagickPathExtent);
1409 if (*component !=
'\0')
1410 for (p=component+strlen(component)-1; p > component; p--)
1415 (void) CopyMagickString(component,p+1,MagickPathExtent);
1421 if ((subimage_length != 0) && (magick_length < subimage_offset))
1422 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1425 case SubcanonicalPath:
1457MagickPrivate
char **GetPathComponents(
const char *path,
1458 size_t *number_components)
1470 if (path == (
char *) NULL)
1471 return((
char **) NULL);
1472 *number_components=1;
1473 for (p=path; *p !=
'\0'; p++)
1474 if (IsBasenameSeparator(*p))
1475 (*number_components)++;
1476 components=(
char **) AcquireQuantumMemory((
size_t) *number_components+1UL,
1477 sizeof(*components));
1478 if (components == (
char **) NULL)
1479 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1481 for (i=0; i < (ssize_t) *number_components; i++)
1483 for (q=p; *q !=
'\0'; q++)
1484 if (IsBasenameSeparator(*q))
1486 components[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MagickPathExtent,
1487 sizeof(**components));
1488 if (components[i] == (
char *) NULL)
1489 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1490 (void) CopyMagickString(components[i],p,(
size_t) (q-p+1));
1493 components[i]=(
char *) NULL;
1521MagickExport MagickBooleanType GetPathTemplate(
char *path)
1536 (void) FormatLocaleString(path,MagickPathExtent,
"magick-"
1537 MagickPathTemplate);
1538 exception=AcquireExceptionInfo();
1539 directory=(
char *) GetImageRegistry(StringRegistryType,
"temporary-path",
1541 exception=DestroyExceptionInfo(exception);
1542 if (directory == (
char *) NULL)
1543 directory=GetEnvironmentValue(
"MAGICK_TEMPORARY_PATH");
1544 if (directory == (
char *) NULL)
1545 directory=GetEnvironmentValue(
"MAGICK_TMPDIR");
1546 if (directory == (
char *) NULL)
1547 directory=GetEnvironmentValue(
"TMPDIR");
1548#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__) || defined(__CYGWIN__)
1549 if (directory == (
char *) NULL)
1550 directory=GetEnvironmentValue(
"TMP");
1551 if (directory == (
char *) NULL)
1552 directory=GetEnvironmentValue(
"TEMP");
1555 if (directory == (
char *) NULL)
1556 directory=GetEnvironmentValue(
"MTMPDIR");
1558#if defined(P_tmpdir)
1559 if (directory == (
char *) NULL)
1560 directory=ConstantString(P_tmpdir);
1562 if (directory == (
char *) NULL)
1563 return(MagickFalse);
1564 value=GetPolicyValue(
"resource:temporary-path");
1565 if (value != (
char *) NULL)
1567 (void) CloneString(&directory,value);
1568 value=DestroyString(value);
1570 if (strlen(directory) > (MagickPathExtent-25))
1572 directory=DestroyString(directory);
1573 return(MagickFalse);
1575 status=GetPathAttributes(directory,&attributes);
1576 if ((status == MagickFalse) || !S_ISDIR(attributes.st_mode))
1578 directory=DestroyString(directory);
1579 return(MagickFalse);
1581 if (directory[strlen(directory)-1] == *DirectorySeparator)
1582 (void) FormatLocaleString(path,MagickPathExtent,
"%smagick-"
1583 MagickPathTemplate,directory);
1585 (
void) FormatLocaleString(path,MagickPathExtent,
1586 "%s%smagick-" MagickPathTemplate,directory,DirectorySeparator);
1587 directory=DestroyString(directory);
1588#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1597 for (p=(path[1] == *DirectorySeparator ? path+2 : path); *p !=
'\0'; p++)
1598 if (*p == *DirectorySeparator)
1628MagickExport MagickBooleanType IsPathAccessible(
const char *path)
1636 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1637 return(MagickFalse);
1638 if (LocaleCompare(path,
"-") == 0)
1640 status=GetPathAttributes(path,&attributes);
1641 if (status == MagickFalse)
1643 if (S_ISREG(attributes.st_mode) == 0)
1644 return(MagickFalse);
1645 if (access_utf8(path,F_OK) != 0)
1646 return(MagickFalse);
1673static int IsPathDirectory(
const char *path)
1681 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1682 return(MagickFalse);
1683 status=GetPathAttributes(path,&attributes);
1684 if (status == MagickFalse)
1686 if (S_ISDIR(attributes.st_mode) == 0)
1726#if defined(__cplusplus) || defined(c_plusplus)
1730static int FileCompare(
const void *x,
const void *y)
1736 p=(
const char **) x;
1737 q=(
const char **) y;
1738 return(LocaleCompare(*p,*q));
1741#if defined(__cplusplus) || defined(c_plusplus)
1745MagickPrivate
char **ListFiles(
const char *directory,
const char *pattern,
1746 size_t *number_entries)
1764 assert(directory != (
const char *) NULL);
1765 assert(pattern != (
const char *) NULL);
1766 assert(number_entries != (
size_t *) NULL);
1767 if (IsEventLogging() != MagickFalse)
1768 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",directory);
1770 current_directory=opendir(directory);
1771 if (current_directory == (DIR *) NULL)
1772 return((
char **) NULL);
1777 filelist=(
char **) AcquireQuantumMemory((
size_t) max_entries,
1779 if (filelist == (
char **) NULL)
1781 (void) closedir(current_directory);
1782 return((
char **) NULL);
1787 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
1788 if (buffer == (
struct dirent *) NULL)
1789 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1790 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1791 (entry != (
struct dirent *) NULL))
1793 if ((LocaleCompare(entry->d_name,
".") == 0) ||
1794 (LocaleCompare(entry->d_name,
"..") == 0))
1796 if ((IsPathDirectory(entry->d_name) > 0) ||
1797#
if defined(MAGICKCORE_WINDOWS_SUPPORT)
1798 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1800 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1803 if (*number_entries >= max_entries)
1809 filelist=(
char **) ResizeQuantumMemory(filelist,(
size_t)
1810 max_entries,
sizeof(*filelist));
1811 if (filelist == (
char **) NULL)
1819 p=strchr(entry->d_name,
';');
1822 if (*number_entries > 0)
1823 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1827 filelist[*number_entries]=(
char *) AcquireString(entry->d_name);
1828 (*number_entries)++;
1831 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
1832 (void) closedir(current_directory);
1833 if (filelist == (
char **) NULL)
1834 return((
char **) NULL);
1838 qsort((
void *) filelist,(
size_t) *number_entries,
sizeof(*filelist),
1867MagickExport
void MagickDelay(
const MagickSizeType milliseconds)
1869 if (milliseconds == 0)
1871#if defined(MAGICKCORE_HAVE_NANOSLEEP)
1876 timer.tv_sec=(time_t) (milliseconds/1000);
1877 timer.tv_nsec=(time_t) ((milliseconds % 1000)*1000*1000);
1878 (void) nanosleep(&timer,(
struct timespec *) NULL);
1880#elif defined(MAGICKCORE_HAVE_USLEEP)
1881 usleep(1000*milliseconds);
1882#elif defined(MAGICKCORE_HAVE_SELECT)
1887 timer.tv_sec=(long) milliseconds/1000;
1888 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1889 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1891#elif defined(MAGICKCORE_HAVE_POLL)
1892 (void) poll((
struct pollfd *) NULL,0,(int) milliseconds);
1893#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1894 Sleep((
long) milliseconds);
1900 timer=milliseconds/1000.0;
1903#elif defined(__BEOS__)
1904 snooze(1000*milliseconds);
1910 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1911 while (clock() < time_end)
1941MagickExport
size_t MultilineCensus(
const char *label)
1949 if (label == (
char *) NULL)
1951 for (number_lines=1; *label !=
'\0'; label++)
1954 return(number_lines);
1980MagickPrivate MagickBooleanType ShredFile(
const char *path)
2007 if ((path == (
const char *) NULL) || (*path ==
'\0'))
2008 return(MagickFalse);
2015 property=GetEnvironmentValue(
"MAGICK_SHRED_PASSES");
2016 if (property != (
char *) NULL)
2018 passes=(ssize_t) StringToInteger(property);
2019 property=DestroyString(property);
2021 property=GetPolicyValue(
"system:shred");
2022 if (property != (
char *) NULL)
2024 passes=(ssize_t) StringToInteger(property);
2025 property=DestroyString(property);
2033 file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
2035 return(MagickFalse);
2036 quantum=(size_t) MagickMinBufferExtent;
2037 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2038 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
2039 length=(MagickSizeType) file_stats.st_size;
2040 random_info=AcquireRandomInfo();
2041 key=GetRandomKey(random_info,quantum);
2042 for (i=0; i < passes; i++)
2050 if (lseek(file,0,SEEK_SET) < 0)
2052 for (j=0; j < (MagickOffsetType) length; j+=count)
2055 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
2056 count=write(file,GetStringInfoDatum(key),(
size_t)
2057 MagickMin((MagickOffsetType) quantum,(MagickOffsetType) length-j));
2065 if (j < (MagickOffsetType) length)
2068 key=DestroyStringInfo(key);
2069 random_info=DestroyRandomInfo(random_info);
2070 status=close_utf8(file);
2071 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);