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/policy-private.h"
58#include "MagickCore/random_.h"
59#include "MagickCore/registry.h"
60#include "MagickCore/resource_.h"
61#include "MagickCore/semaphore.h"
62#include "MagickCore/signature-private.h"
63#include "MagickCore/statistic.h"
64#include "MagickCore/string_.h"
65#include "MagickCore/string-private.h"
66#include "MagickCore/token.h"
67#include "MagickCore/token-private.h"
68#include "MagickCore/utility.h"
69#include "MagickCore/utility-private.h"
70#if defined(MAGICKCORE_HAVE_PROCESS_H)
73#if defined(MAGICKCORE_HAVE_MACH_O_DYLD_H)
74#include <mach-o/dyld.h>
81 Base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
87 IsPathDirectory(
const char *);
112MagickExport MagickBooleanType AcquireUniqueFilename(
char *path)
117 file=AcquireUniqueFileResource(path);
120 file=close_utf8(file)-1;
154MagickExport MagickBooleanType AcquireUniqueSymbolicLink(
const char *source,
177 assert(source != (
const char *) NULL);
178 assert(destination != (
char *) NULL);
179#if defined(MAGICKCORE_HAVE_SYMLINK)
187 status=IsRightsAuthorizedByName(SystemPolicyDomain,
"symlink",(PolicyRights)
188 (ReadPolicyRights | WritePolicyRights),
"follow");
189 passes=GetPolicyValue(
"system:shred");
190 if ((passes != (
char *) NULL) || (status == MagickFalse))
191 passes=DestroyString(passes);
194 (void) AcquireUniqueFilename(destination);
195 (void) RelinquishUniqueFileResource(destination);
196 if (*source == *DirectorySeparator)
198 if (symlink(source,destination) == 0)
204 path[MagickPathExtent];
207 if (getcwd(path,MagickPathExtent) == (
char *) NULL)
209 (void) ConcatenateMagickString(path,DirectorySeparator,
211 (void) ConcatenateMagickString(path,source,MagickPathExtent);
212 if (symlink(path,destination) == 0)
221 destination_file=AcquireUniqueFileResource(destination);
222 if (destination_file == -1)
224 source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
225 if (source_file == -1)
227 (void) close_utf8(destination_file);
228 (void) RelinquishUniqueFileResource(destination);
231 quantum=(size_t) MagickMaxBufferExtent;
232 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
233 quantum=(size_t) MagickMin(attributes.st_size,MagickMaxBufferExtent);
234 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
235 if (buffer == (
unsigned char *) NULL)
237 (void) close_utf8(source_file);
238 (void) close_utf8(destination_file);
239 (void) RelinquishUniqueFileResource(destination);
245 count=(ssize_t) read(source_file,buffer,quantum);
248 length=(size_t) count;
249 count=(ssize_t) write(destination_file,buffer,length);
250 if ((
size_t) count != length)
252 (void) RelinquishUniqueFileResource(destination);
257 (void) close_utf8(destination_file);
258 (void) close_utf8(source_file);
259 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
290MagickExport
void AppendImageFormat(
const char *format,
char *filename)
293 extension[MagickPathExtent],
294 root[MagickPathExtent];
296 assert(format != (
char *) NULL);
297 assert(filename != (
char *) NULL);
298 if (IsEventLogging() != MagickFalse)
299 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
300 if ((*format ==
'\0') || (*filename ==
'\0'))
302 if (LocaleCompare(filename,
"-") == 0)
305 message[MagickPathExtent];
307 (void) FormatLocaleString(message,MagickPathExtent,
"%s:%s",format,
309 (void) CopyMagickString(filename,message,MagickPathExtent);
312 GetPathComponent(filename,ExtensionPath,extension);
313 if ((LocaleCompare(extension,
"Z") == 0) ||
314 (LocaleCompare(extension,
"bz2") == 0) ||
315 (LocaleCompare(extension,
"gz") == 0) ||
316 (LocaleCompare(extension,
"wmz") == 0) ||
317 (LocaleCompare(extension,
"svgz") == 0))
319 GetPathComponent(filename,RootPath,root);
320 (void) CopyMagickString(filename,root,MagickPathExtent);
321 GetPathComponent(filename,RootPath,root);
322 (void) FormatLocaleString(filename,MagickPathExtent,
"%s.%s.%s",root,
326 GetPathComponent(filename,RootPath,root);
327 (void) FormatLocaleString(filename,MagickPathExtent,
"%s.%s",root,format);
356MagickExport
unsigned char *Base64Decode(
const char *source,
size_t *length)
371 assert(source != (
char *) NULL);
372 assert(length != (
size_t *) NULL);
373 if (IsEventLogging() != MagickFalse)
374 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
376 decode=(
unsigned char *) AcquireQuantumMemory((strlen(source)+3)/4,
378 if (decode == (
unsigned char *) NULL)
379 return((
unsigned char *) NULL);
382 for (p=source; *p !=
'\0'; p++)
384 if (isspace((
int) ((
unsigned char) *p)) != 0)
389 if (q == (
char *) NULL)
391 decode=(
unsigned char *) RelinquishMagickMemory(decode);
392 return((
unsigned char *) NULL);
398 decode[i]=(
unsigned char)((q-Base64) << 2);
404 decode[i++]|=(
unsigned char)((q-Base64) >> 4);
405 decode[i]=(
unsigned char)(((q-Base64) & 0x0f) << 4);
411 decode[i++]|=(
unsigned char)((q-Base64) >> 2);
412 decode[i]=(
unsigned char)(((q-Base64) & 0x03) << 6);
418 decode[i++]|=(
unsigned char)(q-Base64);
431 decode=(
unsigned char *) RelinquishMagickMemory(decode);
432 return((
unsigned char *) NULL);
446 decode=(
unsigned char *) RelinquishMagickMemory(decode);
447 return((
unsigned char *) NULL);
451 for ( ; *p !=
'\0'; p++)
452 if (isspace((
int) ((
unsigned char) *p)) == 0)
456 decode=(
unsigned char *) RelinquishMagickMemory(decode);
457 return((
unsigned char *) NULL);
463 for ( ; *p !=
'\0'; p++)
464 if (isspace((
int) ((
unsigned char) *p)) == 0)
466 decode=(
unsigned char *) RelinquishMagickMemory(decode);
467 return((
unsigned char *) NULL);
469 if ((
int) decode[i] != 0)
471 decode=(
unsigned char *) RelinquishMagickMemory(decode);
472 return((
unsigned char *) NULL);
512MagickExport
char *Base64Encode(
const unsigned char *blob,
513 const size_t blob_length,
size_t *encode_length)
527 assert(blob != (
const unsigned char *) NULL);
528 assert(blob_length != 0);
529 assert(encode_length != (
size_t *) NULL);
530 if (IsEventLogging() != MagickFalse)
531 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
533 encode=(
char *) AcquireQuantumMemory(blob_length/3+4,4*
sizeof(*encode));
534 if (encode == (
char *) NULL)
535 return((
char *) NULL);
537 for (p=blob; p < (blob+blob_length-2); p+=(ptrdiff_t) 3)
539 encode[i++]=Base64[(int) (*p >> 2)];
540 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
541 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
542 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
544 remainder=blob_length % 3;
556 for (j=0; j < (ssize_t) remainder; j++)
558 encode[i++]=Base64[(int) (code[0] >> 2)];
559 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
563 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
596MagickPrivate
void ChopPathComponents(
char *path,
const size_t components)
601 for (i=0; i < (ssize_t) components; i++)
602 GetPathComponent(path,HeadPath,path);
628MagickPrivate
void ExpandFilename(
char *path)
631 expand_path[MagickPathExtent];
633 if (path == (
char *) NULL)
637 (void) CopyMagickString(expand_path,path,MagickPathExtent);
638 if ((*(path+1) == *DirectorySeparator) || (*(path+1) ==
'\0'))
646 (void) CopyMagickString(expand_path,
".",MagickPathExtent);
647 (void) ConcatenateMagickString(expand_path,path+1,MagickPathExtent);
648 home=GetEnvironmentValue(
"HOME");
649 if (home == (
char *) NULL)
650 home=GetEnvironmentValue(
"USERPROFILE");
651 if (home != (
char *) NULL)
653 (void) CopyMagickString(expand_path,home,MagickPathExtent);
654 (void) ConcatenateMagickString(expand_path,path+1,MagickPathExtent);
655 home=DestroyString(home);
660#if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
662#if defined(MAGICKCORE_HAVE_GETPWNAM_R)
663 buffer[MagickPathExtent],
665 username[MagickPathExtent];
677 (void) CopyMagickString(username,path+1,MagickPathExtent);
678 p=strchr(username,
'/');
679 if (p != (
char *) NULL)
681#if !defined(MAGICKCORE_HAVE_GETPWNAM_R)
682 entry=getpwnam(username);
684 entry=(
struct passwd *) NULL;
685 if (getpwnam_r(username,&pwd,buffer,
sizeof(buffer),&entry) < 0)
688 if (entry == (
struct passwd *) NULL)
690 (void) CopyMagickString(expand_path,entry->pw_dir,MagickPathExtent);
691 if (p != (
char *) NULL)
693 (void) ConcatenateMagickString(expand_path,
"/",MagickPathExtent);
694 (void) ConcatenateMagickString(expand_path,p+1,MagickPathExtent);
698 (void) CopyMagickString(path,expand_path,MagickPathExtent);
757static inline void getcwd_utf8(
char *path,
size_t extent)
759#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
763 directory=getcwd(path,extent);
767 wide_path[MagickPathExtent];
769 (void) _wgetcwd(wide_path,MagickPathExtent-1);
770 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
774MagickExport MagickBooleanType ExpandFilenames(
int *number_arguments,
778 home_directory[MagickPathExtent],
795 assert(number_arguments != (
int *) NULL);
796 assert(arguments != (
char ***) NULL);
797 if (IsEventLogging() != MagickFalse)
798 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
799 vector=(
char **) AcquireQuantumMemory((
size_t) (*number_arguments+1),
801 if (vector == (
char **) NULL)
802 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
806 *home_directory=
'\0';
808 for (i=0; i < (ssize_t) *number_arguments; i++)
812 filename[MagickPathExtent],
813 magick[MagickPathExtent],
815 path[MagickPathExtent],
816 subimage[MagickPathExtent];
821 option=(*arguments)[i];
827 vector[count++]=ConstantString(option);
829 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
835 for (j=0; j < parameters; j++)
838 if (i == (ssize_t) *number_arguments)
840 option=(*arguments)[i];
841 vector[count++]=ConstantString(option);
845 if ((*option ==
'"') || (*option ==
'\''))
847 GetPathComponent(option,TailPath,filename);
848 GetPathComponent(option,MagickPath,magick);
849 if ((LocaleCompare(magick,
"CAPTION") == 0) ||
850 (LocaleCompare(magick,
"LABEL") == 0) ||
851 (LocaleCompare(magick,
"PANGO") == 0) ||
852 (LocaleCompare(magick,
"VID") == 0))
854 if ((IsGlob(filename) == MagickFalse) && (*option !=
'@'))
856 if (IsPathAccessible(option) != MagickFalse)
863 GetPathComponent(option,HeadPath,path);
864 GetPathComponent(option,SubimagePath,subimage);
865 ExpandFilename(path);
866 if (*home_directory ==
'\0')
867 getcwd_utf8(home_directory,MagickPathExtent-1);
868 filelist=ListFiles(*path ==
'\0' ? home_directory : path,filename,
885 exception=AcquireExceptionInfo();
886 files=FileToString(option,~0UL,exception);
887 exception=DestroyExceptionInfo(exception);
888 if (files == (
char *) NULL)
890 filelist=StringToArgv(files,&length);
891 if (filelist == (
char **) NULL)
893 files=DestroyString(files);
894 filelist[0]=DestroyString(filelist[0]);
895 for (j=0; j < (ssize_t) (length-1); j++)
896 filelist[j]=filelist[j+1];
897 number_files=(size_t) length-1;
899 if (filelist == (
char **) NULL)
901 for (j=0; j < (ssize_t) number_files; j++)
902 if (IsPathDirectory(filelist[j]) <= 0)
904 if (j == (ssize_t) number_files)
906 for (j=0; j < (ssize_t) number_files; j++)
907 filelist[j]=DestroyString(filelist[j]);
908 filelist=(
char **) RelinquishMagickMemory(filelist);
914 vector=(
char **) ResizeQuantumMemory(vector,(
size_t) ((ssize_t)
915 *number_arguments+count+(ssize_t) number_files+1),
sizeof(*vector));
916 if (vector == (
char **) NULL)
918 for (j=0; j < (ssize_t) number_files; j++)
919 filelist[j]=DestroyString(filelist[j]);
920 filelist=(
char **) RelinquishMagickMemory(filelist);
923 for (j=0; j < (ssize_t) number_files; j++)
926 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
935 vector[count++]=ConstantString(option);
936 for (k=0; k < parameters; k++)
939 if (j == (ssize_t) number_files)
942 vector[count++]=ConstantString(option);
946 (void) CopyMagickString(filename,path,MagickPathExtent);
948 (void) ConcatenateMagickString(filename,DirectorySeparator,
950 if (filelist[j] != (
char *) NULL)
951 (void) ConcatenateMagickString(filename,filelist[j],MagickPathExtent);
952 filelist[j]=DestroyString(filelist[j]);
953 if (strlen(filename) >= (MagickPathExtent-1))
954 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
955 if (IsPathDirectory(filename) <= 0)
958 file_path[MagickPathExtent];
963 (void) ConcatenateMagickString(file_path,magick,
965 (void) ConcatenateMagickString(file_path,
":",MagickPathExtent);
967 (void) ConcatenateMagickString(file_path,filename,MagickPathExtent);
968 if (*subimage !=
'\0')
970 (void) ConcatenateMagickString(file_path,
"[",MagickPathExtent);
971 (void) ConcatenateMagickString(file_path,subimage,
973 (void) ConcatenateMagickString(file_path,
"]",MagickPathExtent);
975 if (strlen(file_path) >= (MagickPathExtent-1))
976 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
977 if (destroy != MagickFalse)
980 vector[count]=DestroyString(vector[count]);
983 vector[count++]=ConstantString(file_path);
986 filelist=(
char **) RelinquishMagickMemory(filelist);
988 vector[count]=(
char *) NULL;
989 if (IsEventLogging() != MagickFalse)
994 command_line=AcquireString(vector[0]);
995 for (i=1; i < count; i++)
997 (void) ConcatenateString(&command_line,
" {");
998 (void) ConcatenateString(&command_line,vector[i]);
999 (void) ConcatenateString(&command_line,
"}");
1001 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1002 "Command line: %s",command_line);
1003 command_line=DestroyString(command_line);
1005 *number_arguments=(int) count;
1035MagickPrivate MagickBooleanType GetExecutionPath(
char *path,
const size_t extent)
1041 directory=getcwd(path,(
unsigned long) extent);
1043#if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
1046 execution_path[PATH_MAX+1],
1047 link_path[MagickPathExtent];
1052 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.17g/exe",
1054 count=readlink(link_path,execution_path,PATH_MAX);
1057 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.17g/file",
1059 count=readlink(link_path,execution_path,PATH_MAX);
1061 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
1063 execution_path[count]=
'\0';
1064 (void) CopyMagickString(path,execution_path,extent);
1068#if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
1071 executable_path[PATH_MAX << 1];
1076 length=
sizeof(executable_path);
1077 if (_NSGetExecutablePath(executable_path,&length) == 0)
1080 *real_path = realpath_utf8(executable_path);
1082 if (real_path != (
char *) NULL)
1084 (void) CopyMagickString(path,real_path,extent);
1085 real_path=DestroyString(real_path);
1090#if defined(MAGICKCORE_HAVE_GETEXECNAME)
1095 execution_path=(
const char *) getexecname();
1096 if (execution_path != (
const char *) NULL)
1098 if (*execution_path != *DirectorySeparator)
1099 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1100 (void) ConcatenateMagickString(path,execution_path,extent);
1104#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1105 NTGetExecutionPath(path,extent);
1116 program_name=program_invocation_name;
1117 if (*program_invocation_name !=
'/')
1122 extent=strlen(directory)+strlen(program_name)+2;
1123 program_name=AcquireQuantumMemory(extent,
sizeof(*program_name));
1124 if (program_name == (
char *) NULL)
1125 program_name=program_invocation_name;
1127 count=FormatLocaleString(program_name,extent,
"%s/%s",directory,
1128 program_invocation_name);
1133 *real_path = realpath_utf8(program_name);
1135 if (real_path != (
char *) NULL)
1137 (void) CopyMagickString(path,real_path,extent);
1138 real_path=DestroyString(real_path);
1141 if (program_name != program_invocation_name)
1142 program_name=(
char *) RelinquishMagickMemory(program_name);
1145#if defined(__OpenBSD__)
1150 (void) CopyMagickString(path,__progname,extent);
1153 return(IsPathAccessible(path));
1174MagickPrivate ssize_t GetMagickPageSize(
void)
1181#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1182 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1183#elif defined(MAGICKCORE_HAVE_GETPAGESIZE)
1184 page_size=(ssize_t) getpagesize();
1215MagickExport MagickBooleanType GetPathAttributes(
const char *path,
1221 if (path == (
const char *) NULL)
1224 return(MagickFalse);
1226 (void) memset(attributes,0,
sizeof(
struct stat));
1227 status=stat_utf8(path,(
struct stat *) attributes) == 0 ? MagickTrue :
1263MagickExport
void GetPathComponent(
const char *path,PathType type,
1277 assert(path != (
const char *) NULL);
1278 assert(component != (
char *) NULL);
1279 if (IsEventLogging() != MagickFalse)
1280 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",path);
1286 (void) CopyMagickString(component,path,MagickPathExtent);
1289 if (type != SubcanonicalPath)
1291 p=component+strlen(component)-1;
1292 if ((strlen(component) > 2) && (*p ==
']'))
1294 q=strrchr(component,
'[');
1295 if (q != (
char *) NULL)
1297 ExceptionInfo *exception = AcquireExceptionInfo();
1298 char *literal = (
char *) GetImageRegistry(StringRegistryType,
1299 "filename:literal",exception);
1300 exception=DestroyExceptionInfo(exception);
1301 if ((IsStringTrue(literal) == MagickFalse) ||
1302 (IsPathAccessible(path) == MagickFalse))
1308 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1309 (IsGeometry(q+1) == MagickFalse))
1313 subimage_length=(size_t) (p-q);
1314 subimage_offset=(size_t) (q-component+1);
1318 if (literal != (
char *) NULL)
1319 literal=DestroyString(literal);
1327 for (p=component; *p !=
'\0'; p++)
1329 if ((*p ==
'%') && (*(p+1) ==
'['))
1334 for (p++; (*p !=
']') && (*p !=
'\0'); p++) ;
1338 if ((p != component) && (*p ==
':') && (IsPathDirectory(component) < 0) &&
1339 (IsPathAccessible(component) == MagickFalse))
1345 if (IsMagickConflict(component) != MagickFalse)
1349 magick_length=(size_t) (p-component+1);
1350 for (q=component; *(++p) !=
'\0'; q++)
1359 for (p=component+strlen(component)-1; p > component; p--)
1360 if (IsBasenameSeparator(*p) != MagickFalse)
1366 if (magick_length != 0)
1367 (void) CopyMagickString(component,path,magick_length);
1374 if (*component !=
'\0')
1376 for (p=component+(strlen(component)-1); p > component; p--)
1378 if (IsBasenameSeparator(*p) != MagickFalse)
1396 if (IsBasenameSeparator(*p) != MagickFalse)
1397 (void) CopyMagickString(component,p+1,MagickPathExtent);
1402 if (IsBasenameSeparator(*p) != MagickFalse)
1403 (void) CopyMagickString(component,p+1,MagickPathExtent);
1404 if (*component !=
'\0')
1405 for (p=component+(strlen(component)-1); p > component; p--)
1413 case BasePathSansCompressExtension:
1416 extension[MagickPathExtent];
1421 GetPathComponent(path,ExtensionPath,extension);
1422 if ((LocaleCompare(extension,
"bz2") == 0) ||
1423 (LocaleCompare(extension,
"gz") == 0) ||
1424 (LocaleCompare(extension,
"svgz") == 0) ||
1425 (LocaleCompare(extension,
"wmz") == 0) ||
1426 (LocaleCompare(extension,
"Z") == 0))
1427 GetPathComponent(path,BasePath,component);
1432 if (IsBasenameSeparator(*p) != MagickFalse)
1433 (void) CopyMagickString(component,p+1,MagickPathExtent);
1434 if (*component !=
'\0')
1435 for (p=component+strlen(component)-1; p > component; p--)
1440 (void) CopyMagickString(component,p+1,MagickPathExtent);
1446 if ((subimage_length != 0) && (magick_length < subimage_offset))
1447 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1450 case SubcanonicalPath:
1482MagickPrivate
char **GetPathComponents(
const char *path,
1483 size_t *number_components)
1495 if (path == (
char *) NULL)
1496 return((
char **) NULL);
1497 *number_components=1;
1498 for (p=path; *p !=
'\0'; p++)
1499 if (IsBasenameSeparator(*p))
1500 (*number_components)++;
1501 components=(
char **) AcquireQuantumMemory((
size_t) *number_components+1UL,
1502 sizeof(*components));
1503 if (components == (
char **) NULL)
1504 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1506 for (i=0; i < (ssize_t) *number_components; i++)
1508 for (q=p; *q !=
'\0'; q++)
1509 if (IsBasenameSeparator(*q))
1511 components[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MagickPathExtent,
1512 sizeof(**components));
1513 if (components[i] == (
char *) NULL)
1514 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1515 (void) CopyMagickString(components[i],p,(
size_t) (q-p+1));
1518 components[i]=(
char *) NULL;
1546MagickExport MagickBooleanType GetPathTemplate(
char *path)
1561 (void) FormatLocaleString(path,MagickPathExtent,
"magick-"
1562 MagickPathTemplate);
1563 exception=AcquireExceptionInfo();
1564 directory=(
char *) GetImageRegistry(StringRegistryType,
"temporary-path",
1566 exception=DestroyExceptionInfo(exception);
1567 if (directory == (
char *) NULL)
1568 directory=GetEnvironmentValue(
"MAGICK_TEMPORARY_PATH");
1569 if (directory == (
char *) NULL)
1570 directory=GetEnvironmentValue(
"MAGICK_TMPDIR");
1571 if (directory == (
char *) NULL)
1572 directory=GetEnvironmentValue(
"TMPDIR");
1573#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__) || defined(__CYGWIN__)
1574 if (directory == (
char *) NULL)
1575 directory=GetEnvironmentValue(
"TMP");
1576 if (directory == (
char *) NULL)
1577 directory=GetEnvironmentValue(
"TEMP");
1580 if (directory == (
char *) NULL)
1581 directory=GetEnvironmentValue(
"MTMPDIR");
1583#if defined(P_tmpdir)
1584 if (directory == (
char *) NULL)
1585 directory=ConstantString(P_tmpdir);
1587 if (directory == (
char *) NULL)
1588 return(MagickFalse);
1589 value=GetPolicyValue(
"resource:temporary-path");
1590 if (value != (
char *) NULL)
1592 (void) CloneString(&directory,value);
1593 value=DestroyString(value);
1595 if (strlen(directory) > (MagickPathExtent-25))
1597 directory=DestroyString(directory);
1598 return(MagickFalse);
1600 status=GetPathAttributes(directory,&attributes);
1601 if ((status == MagickFalse) || !S_ISDIR(attributes.st_mode))
1603 directory=DestroyString(directory);
1604 return(MagickFalse);
1606 if (directory[strlen(directory)-1] == *DirectorySeparator)
1607 (void) FormatLocaleString(path,MagickPathExtent,
"%smagick-"
1608 MagickPathTemplate,directory);
1610 (
void) FormatLocaleString(path,MagickPathExtent,
1611 "%s%smagick-" MagickPathTemplate,directory,DirectorySeparator);
1612 directory=DestroyString(directory);
1613#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1622 for (p=(path[1] == *DirectorySeparator ? path+2 : path); *p !=
'\0'; p++)
1623 if (*p == *DirectorySeparator)
1653MagickExport MagickBooleanType IsPathAccessible(
const char *path)
1661 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1662 return(MagickFalse);
1663 if (LocaleCompare(path,
"-") == 0)
1665 status=GetPathAttributes(path,&attributes);
1666 if (status == MagickFalse)
1668 if (S_ISREG(attributes.st_mode) == 0)
1669 return(MagickFalse);
1670 if (access_utf8(path,F_OK) != 0)
1671 return(MagickFalse);
1698static int IsPathDirectory(
const char *path)
1706 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1707 return(MagickFalse);
1708 status=GetPathAttributes(path,&attributes);
1709 if (status == MagickFalse)
1711 if (S_ISDIR(attributes.st_mode) == 0)
1751#if defined(__cplusplus) || defined(c_plusplus)
1755static int FileCompare(
const void *x,
const void *y)
1761 p=(
const char **) x;
1762 q=(
const char **) y;
1763 return(LocaleCompare(*p,*q));
1766#if defined(__cplusplus) || defined(c_plusplus)
1770MagickPrivate
char **ListFiles(
const char *directory,
const char *pattern,
1771 size_t *number_entries)
1789 assert(directory != (
const char *) NULL);
1790 assert(pattern != (
const char *) NULL);
1791 assert(number_entries != (
size_t *) NULL);
1792 if (IsEventLogging() != MagickFalse)
1793 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",directory);
1795 current_directory=opendir(directory);
1796 if (current_directory == (DIR *) NULL)
1797 return((
char **) NULL);
1802 filelist=(
char **) AcquireQuantumMemory((
size_t) max_entries,
1804 if (filelist == (
char **) NULL)
1806 (void) closedir(current_directory);
1807 return((
char **) NULL);
1812 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
1813 if (buffer == (
struct dirent *) NULL)
1814 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1815 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1816 (entry != (
struct dirent *) NULL))
1818 if ((LocaleCompare(entry->d_name,
".") == 0) ||
1819 (LocaleCompare(entry->d_name,
"..") == 0))
1821 if ((IsPathDirectory(entry->d_name) > 0) ||
1822#
if defined(MAGICKCORE_WINDOWS_SUPPORT)
1823 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1825 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1828 if (*number_entries >= max_entries)
1834 filelist=(
char **) ResizeQuantumMemory(filelist,(
size_t)
1835 max_entries,
sizeof(*filelist));
1836 if (filelist == (
char **) NULL)
1844 p=strchr(entry->d_name,
';');
1847 if (*number_entries > 0)
1848 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1852 filelist[*number_entries]=(
char *) AcquireString(entry->d_name);
1853 (*number_entries)++;
1856 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
1857 (void) closedir(current_directory);
1858 if (filelist == (
char **) NULL)
1859 return((
char **) NULL);
1863 qsort((
void *) filelist,(
size_t) *number_entries,
sizeof(*filelist),
1892MagickExport
void MagickDelay(
const MagickSizeType milliseconds)
1894 if (milliseconds == 0)
1896#if defined(MAGICKCORE_HAVE_NANOSLEEP)
1901 timer.tv_sec=(time_t) (milliseconds/1000);
1902 timer.tv_nsec=(time_t) ((milliseconds % 1000)*1000*1000);
1903 (void) nanosleep(&timer,(
struct timespec *) NULL);
1905#elif defined(MAGICKCORE_HAVE_USLEEP)
1906 usleep(1000*milliseconds);
1907#elif defined(MAGICKCORE_HAVE_SELECT)
1912 timer.tv_sec=(long) milliseconds/1000;
1913 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1914 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1916#elif defined(MAGICKCORE_HAVE_POLL)
1917 (void) poll((
struct pollfd *) NULL,0,(
int) milliseconds);
1918#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1919 Sleep((
long) milliseconds);
1925 timer=milliseconds/1000.0;
1928#elif defined(__BEOS__)
1929 snooze(1000*milliseconds);
1935 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1936 while (clock() < time_end)
1966MagickExport
size_t MultilineCensus(
const char *label)
1974 if (label == (
char *) NULL)
1976 for (number_lines=1; *label !=
'\0'; label++)
1979 return(number_lines);
2005MagickPrivate MagickBooleanType ShredFile(
const char *path)
2032 if ((path == (
const char *) NULL) || (*path ==
'\0'))
2033 return(MagickFalse);
2040 property=GetEnvironmentValue(
"MAGICK_SHRED_PASSES");
2041 if (property != (
char *) NULL)
2043 passes=(ssize_t) StringToInteger(property);
2044 property=DestroyString(property);
2046 property=GetPolicyValue(
"system:shred");
2047 if (property != (
char *) NULL)
2049 passes=(ssize_t) StringToInteger(property);
2050 property=DestroyString(property);
2058 file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
2060 return(MagickFalse);
2061 quantum=(size_t) MagickMinBufferExtent;
2062 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2063 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
2064 length=(MagickSizeType) file_stats.st_size;
2065 random_info=AcquireRandomInfo();
2066 key=GetRandomKey(random_info,quantum);
2067 for (i=0; i < passes; i++)
2075 if (lseek(file,0,SEEK_SET) < 0)
2077 for (j=0; j < (MagickOffsetType) length; j+=count)
2080 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
2081 count=write(file,GetStringInfoDatum(key),(
size_t)
2082 MagickMin((MagickOffsetType) quantum,(MagickOffsetType) length-j));
2090 if (j < (MagickOffsetType) length)
2093 key=DestroyStringInfo(key);
2094 random_info=DestroyRandomInfo(random_info);
2095 status=close_utf8(file);
2096 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);