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) && (IsPathAccessible(path) == MagickFalse))
1285 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1286 (IsGeometry(q+1) == MagickFalse))
1290 subimage_length=(size_t) (p-q);
1291 subimage_offset=(size_t) (q-component+1);
1301 for (p=component; *p !=
'\0'; p++)
1303 if ((*p ==
'%') && (*(p+1) ==
'['))
1308 for (p++; (*p !=
']') && (*p !=
'\0'); p++) ;
1312 if ((p != component) && (*p ==
':') && (IsPathDirectory(component) < 0) &&
1313 (IsPathAccessible(component) == MagickFalse))
1319 if (IsMagickConflict(component) != MagickFalse)
1323 magick_length=(size_t) (p-component+1);
1324 for (q=component; *(++p) !=
'\0'; q++)
1333 for (p=component+strlen(component)-1; p > component; p--)
1334 if (IsBasenameSeparator(*p) != MagickFalse)
1340 if (magick_length != 0)
1341 (void) CopyMagickString(component,path,magick_length);
1348 if (*component !=
'\0')
1350 for (p=component+(strlen(component)-1); p > component; p--)
1352 if (IsBasenameSeparator(*p) != MagickFalse)
1370 if (IsBasenameSeparator(*p) != MagickFalse)
1371 (
void) CopyMagickString(component,p+1,MagickPathExtent);
1376 if (IsBasenameSeparator(*p) != MagickFalse)
1377 (void) CopyMagickString(component,p+1,MagickPathExtent);
1378 if (*component !=
'\0')
1379 for (p=component+(strlen(component)-1); p > component; p--)
1387 case BasePathSansCompressExtension:
1390 extension[MagickPathExtent];
1395 GetPathComponent(path,ExtensionPath,extension);
1396 if ((LocaleCompare(extension,
"bz2") == 0) ||
1397 (LocaleCompare(extension,
"gz") == 0) ||
1398 (LocaleCompare(extension,
"svgz") == 0) ||
1399 (LocaleCompare(extension,
"wmz") == 0) ||
1400 (LocaleCompare(extension,
"Z") == 0))
1401 GetPathComponent(path,BasePath,component);
1406 if (IsBasenameSeparator(*p) != MagickFalse)
1407 (void) CopyMagickString(component,p+1,MagickPathExtent);
1408 if (*component !=
'\0')
1409 for (p=component+strlen(component)-1; p > component; p--)
1414 (void) CopyMagickString(component,p+1,MagickPathExtent);
1420 if ((subimage_length != 0) && (magick_length < subimage_offset))
1421 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1424 case SubcanonicalPath:
1456MagickPrivate
char **GetPathComponents(
const char *path,
1457 size_t *number_components)
1469 if (path == (
char *) NULL)
1470 return((
char **) NULL);
1471 *number_components=1;
1472 for (p=path; *p !=
'\0'; p++)
1473 if (IsBasenameSeparator(*p))
1474 (*number_components)++;
1475 components=(
char **) AcquireQuantumMemory((
size_t) *number_components+1UL,
1476 sizeof(*components));
1477 if (components == (
char **) NULL)
1478 return((
char **) NULL);
1480 for (i=0; i < (ssize_t) *number_components; i++)
1482 for (q=p; *q !=
'\0'; q++)
1483 if (IsBasenameSeparator(*q))
1485 components[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MagickPathExtent,
1486 sizeof(**components));
1487 if (components[i] == (
char*)NULL)
1492 for (j=0; j < i; j++)
1493 components[j]=DestroyString(components[j]);
1494 components=(
char **) RelinquishMagickMemory(components);
1495 return((
char **) NULL);
1497 (void) CopyMagickString(components[i],p,(
size_t) (q-p+1));
1500 components[i]=(
char *) NULL;
1528MagickExport MagickBooleanType GetPathTemplate(
char *path)
1543 (void) FormatLocaleString(path,MagickPathExtent,
"magick-"
1544 MagickPathTemplate);
1545 exception=AcquireExceptionInfo();
1546 directory=(
char *) GetImageRegistry(StringRegistryType,
"temporary-path",
1548 exception=DestroyExceptionInfo(exception);
1549 if (directory == (
char *) NULL)
1550 directory=GetEnvironmentValue(
"MAGICK_TEMPORARY_PATH");
1551 if (directory == (
char *) NULL)
1552 directory=GetEnvironmentValue(
"MAGICK_TMPDIR");
1553 if (directory == (
char *) NULL)
1554 directory=GetEnvironmentValue(
"TMPDIR");
1555#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__) || defined(__CYGWIN__)
1556 if (directory == (
char *) NULL)
1557 directory=GetEnvironmentValue(
"TMP");
1558 if (directory == (
char *) NULL)
1559 directory=GetEnvironmentValue(
"TEMP");
1562 if (directory == (
char *) NULL)
1563 directory=GetEnvironmentValue(
"MTMPDIR");
1565#if defined(P_tmpdir)
1566 if (directory == (
char *) NULL)
1567 directory=ConstantString(P_tmpdir);
1569 if (directory == (
char *) NULL)
1570 return(MagickFalse);
1571 value=GetPolicyValue(
"resource:temporary-path");
1572 if (value != (
char *) NULL)
1574 (void) CloneString(&directory,value);
1575 value=DestroyString(value);
1577 if (strlen(directory) > (MagickPathExtent-25))
1579 directory=DestroyString(directory);
1580 return(MagickFalse);
1582 status=GetPathAttributes(directory,&attributes);
1583 if ((status == MagickFalse) || !S_ISDIR(attributes.st_mode))
1585 directory=DestroyString(directory);
1586 return(MagickFalse);
1588 if (directory[strlen(directory)-1] == *DirectorySeparator)
1589 (void) FormatLocaleString(path,MagickPathExtent,
"%smagick-"
1590 MagickPathTemplate,directory);
1592 (
void) FormatLocaleString(path,MagickPathExtent,
1593 "%s%smagick-" MagickPathTemplate,directory,DirectorySeparator);
1594 directory=DestroyString(directory);
1595#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1604 for (p=(path[1] == *DirectorySeparator ? path+2 : path); *p !=
'\0'; p++)
1605 if (*p == *DirectorySeparator)
1635MagickExport MagickBooleanType IsPathAccessible(
const char *path)
1643 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1644 return(MagickFalse);
1645 if (LocaleCompare(path,
"-") == 0)
1647 status=GetPathAttributes(path,&attributes);
1648 if (status == MagickFalse)
1650 if (S_ISREG(attributes.st_mode) == 0)
1651 return(MagickFalse);
1652 if (access_utf8(path,F_OK) != 0)
1653 return(MagickFalse);
1680static int IsPathDirectory(
const char *path)
1688 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1689 return(MagickFalse);
1690 status=GetPathAttributes(path,&attributes);
1691 if (status == MagickFalse)
1693 if (S_ISDIR(attributes.st_mode) == 0)
1733#if defined(__cplusplus) || defined(c_plusplus)
1737static int FileCompare(
const void *x,
const void *y)
1743 p=(
const char **) x;
1744 q=(
const char **) y;
1745 return(LocaleCompare(*p,*q));
1748#if defined(__cplusplus) || defined(c_plusplus)
1752MagickPrivate
char **ListFiles(
const char *directory,
const char *pattern,
1753 size_t *number_entries)
1771 assert(directory != (
const char *) NULL);
1772 assert(pattern != (
const char *) NULL);
1773 assert(number_entries != (
size_t *) NULL);
1774 if (IsEventLogging() != MagickFalse)
1775 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",directory);
1777 current_directory=opendir(directory);
1778 if (current_directory == (DIR *) NULL)
1779 return((
char **) NULL);
1784 filelist=(
char **) AcquireQuantumMemory((
size_t) max_entries,
1786 if (filelist == (
char **) NULL)
1788 (void) closedir(current_directory);
1789 return((
char **) NULL);
1794 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
1795 if (buffer == (
struct dirent *) NULL)
1797 filelist=(
char **) RelinquishMagickMemory(filelist);
1798 (void) closedir(current_directory);
1799 return((
char **) NULL);
1801 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1802 (entry != (
struct dirent *) NULL))
1804 if ((LocaleCompare(entry->d_name,
".") == 0) ||
1805 (LocaleCompare(entry->d_name,
"..") == 0))
1807 if ((IsPathDirectory(entry->d_name) > 0) ||
1808#
if defined(MAGICKCORE_WINDOWS_SUPPORT)
1809 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1811 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1814 if (*number_entries >= max_entries)
1820 filelist=(
char **) ResizeQuantumMemory(filelist,(
size_t)
1821 max_entries,
sizeof(*filelist));
1822 if (filelist == (
char **) NULL)
1830 p=strchr(entry->d_name,
';');
1833 if (*number_entries > 0)
1834 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1838 filelist[*number_entries]=(
char *) AcquireString(entry->d_name);
1839 (*number_entries)++;
1842 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
1843 (void) closedir(current_directory);
1844 if (filelist == (
char **) NULL)
1845 return((
char **) NULL);
1849 qsort((
void *) filelist,(
size_t) *number_entries,
sizeof(*filelist),
1878MagickExport
void MagickDelay(
const MagickSizeType milliseconds)
1880 if (milliseconds == 0)
1882#if defined(MAGICKCORE_HAVE_NANOSLEEP)
1887 timer.tv_sec=(time_t) (milliseconds/1000);
1888 timer.tv_nsec=(time_t) ((milliseconds % 1000)*1000*1000);
1889 (void) nanosleep(&timer,(
struct timespec *) NULL);
1891#elif defined(MAGICKCORE_HAVE_USLEEP)
1892 usleep(1000*milliseconds);
1893#elif defined(MAGICKCORE_HAVE_SELECT)
1898 timer.tv_sec=(long) milliseconds/1000;
1899 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1900 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1902#elif defined(MAGICKCORE_HAVE_POLL)
1903 (void) poll((
struct pollfd *) NULL,0,(int) milliseconds);
1904#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1905 Sleep((
long) milliseconds);
1911 timer=milliseconds/1000.0;
1914#elif defined(__BEOS__)
1915 snooze(1000*milliseconds);
1921 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1922 while (clock() < time_end)
1952MagickExport
size_t MultilineCensus(
const char *label)
1960 if (label == (
char *) NULL)
1962 for (number_lines=1; *label !=
'\0'; label++)
1965 return(number_lines);
1991MagickPrivate MagickBooleanType ShredFile(
const char *path)
2018 if ((path == (
const char *) NULL) || (*path ==
'\0'))
2019 return(MagickFalse);
2026 property=GetEnvironmentValue(
"MAGICK_SHRED_PASSES");
2027 if (property != (
char *) NULL)
2029 passes=(ssize_t) StringToInteger(property);
2030 property=DestroyString(property);
2032 property=GetPolicyValue(
"system:shred");
2033 if (property != (
char *) NULL)
2035 passes=(ssize_t) StringToInteger(property);
2036 property=DestroyString(property);
2044 file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
2046 return(MagickFalse);
2047 quantum=(size_t) MagickMinBufferExtent;
2048 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2049 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
2050 length=(MagickSizeType) file_stats.st_size;
2051 random_info=AcquireRandomInfo();
2052 key=GetRandomKey(random_info,quantum);
2053 for (i=0; i < passes; i++)
2061 if (lseek(file,0,SEEK_SET) < 0)
2063 for (j=0; j < (MagickOffsetType) length; j+=count)
2066 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
2067 count=write(file,GetStringInfoDatum(key),(
size_t)
2068 MagickMin((MagickOffsetType) quantum,(MagickOffsetType) length-j));
2076 if (j < (MagickOffsetType) length)
2079 key=DestroyStringInfo(key);
2080 random_info=DestroyRandomInfo(random_info);
2081 status=close_utf8(file);
2082 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);