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);
153MagickExport MagickBooleanType AcquireUniqueSymbolicLink(
const char *source,
176 assert(source != (
const char *) NULL);
177 assert(destination != (
char *) NULL);
178#if defined(MAGICKCORE_HAVE_SYMLINK)
183 passes=GetPolicyValue(
"system:shred");
184 if (passes != (
char *) NULL)
185 passes=DestroyString(passes);
188 (void) AcquireUniqueFilename(destination);
189 (void) RelinquishUniqueFileResource(destination);
190 if (*source == *DirectorySeparator)
192 if (symlink(source,destination) == 0)
198 path[MagickPathExtent];
201 if (getcwd(path,MagickPathExtent) == (
char *) NULL)
203 (void) ConcatenateMagickString(path,DirectorySeparator,
205 (void) ConcatenateMagickString(path,source,MagickPathExtent);
206 if (symlink(path,destination) == 0)
212 destination_file=AcquireUniqueFileResource(destination);
213 if (destination_file == -1)
215 source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
216 if (source_file == -1)
218 (void) close(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);
281MagickExport
void AppendImageFormat(
const char *format,
char *filename)
284 extension[MagickPathExtent],
285 root[MagickPathExtent];
287 assert(format != (
char *) NULL);
288 assert(filename != (
char *) NULL);
289 if (IsEventLogging() != MagickFalse)
290 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
291 if ((*format ==
'\0') || (*filename ==
'\0'))
293 if (LocaleCompare(filename,
"-") == 0)
296 message[MagickPathExtent];
298 (void) FormatLocaleString(message,MagickPathExtent,
"%s:%s",format,
300 (void) CopyMagickString(filename,message,MagickPathExtent);
303 GetPathComponent(filename,ExtensionPath,extension);
304 if ((LocaleCompare(extension,
"Z") == 0) ||
305 (LocaleCompare(extension,
"bz2") == 0) ||
306 (LocaleCompare(extension,
"gz") == 0) ||
307 (LocaleCompare(extension,
"wmz") == 0) ||
308 (LocaleCompare(extension,
"svgz") == 0))
310 GetPathComponent(filename,RootPath,root);
311 (void) CopyMagickString(filename,root,MagickPathExtent);
312 GetPathComponent(filename,RootPath,root);
313 (void) FormatLocaleString(filename,MagickPathExtent,
"%s.%s.%s",root,
317 GetPathComponent(filename,RootPath,root);
318 (void) FormatLocaleString(filename,MagickPathExtent,
"%s.%s",root,format);
347MagickExport
unsigned char *Base64Decode(
const char *source,
size_t *length)
362 assert(source != (
char *) NULL);
363 assert(length != (
size_t *) NULL);
364 if (IsEventLogging() != MagickFalse)
365 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
367 decode=(
unsigned char *) AcquireQuantumMemory((strlen(source)+3)/4,
369 if (decode == (
unsigned char *) NULL)
370 return((
unsigned char *) NULL);
373 for (p=source; *p !=
'\0'; p++)
375 if (isspace((
int) ((
unsigned char) *p)) != 0)
380 if (q == (
char *) NULL)
382 decode=(
unsigned char *) RelinquishMagickMemory(decode);
383 return((
unsigned char *) NULL);
389 decode[i]=(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);
503MagickExport
char *Base64Encode(
const unsigned char *blob,
504 const size_t blob_length,
size_t *encode_length)
518 assert(blob != (
const unsigned char *) NULL);
519 assert(blob_length != 0);
520 assert(encode_length != (
size_t *) NULL);
521 if (IsEventLogging() != MagickFalse)
522 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
524 encode=(
char *) AcquireQuantumMemory(blob_length/3+4,4*
sizeof(*encode));
525 if (encode == (
char *) NULL)
526 return((
char *) NULL);
528 for (p=blob; p < (blob+blob_length-2); p+=3)
530 encode[i++]=Base64[(int) (*p >> 2)];
531 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
532 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
533 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
535 remainder=blob_length % 3;
547 for (j=0; j < (ssize_t) remainder; j++)
549 encode[i++]=Base64[(int) (code[0] >> 2)];
550 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
554 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
587MagickPrivate
void ChopPathComponents(
char *path,
const size_t components)
592 for (i=0; i < (ssize_t) components; i++)
593 GetPathComponent(path,HeadPath,path);
619MagickPrivate
void ExpandFilename(
char *path)
622 expand_path[MagickPathExtent];
624 if (path == (
char *) NULL)
628 (void) CopyMagickString(expand_path,path,MagickPathExtent);
629 if ((*(path+1) == *DirectorySeparator) || (*(path+1) ==
'\0'))
637 (void) CopyMagickString(expand_path,
".",MagickPathExtent);
638 (void) ConcatenateMagickString(expand_path,path+1,MagickPathExtent);
639 home=GetEnvironmentValue(
"HOME");
640 if (home == (
char *) NULL)
641 home=GetEnvironmentValue(
"USERPROFILE");
642 if (home != (
char *) NULL)
644 (void) CopyMagickString(expand_path,home,MagickPathExtent);
645 (void) ConcatenateMagickString(expand_path,path+1,MagickPathExtent);
646 home=DestroyString(home);
651#if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
653#if defined(MAGICKCORE_HAVE_GETPWNAM_R)
654 buffer[MagickPathExtent],
656 username[MagickPathExtent];
668 (void) CopyMagickString(username,path+1,MagickPathExtent);
669 p=strchr(username,
'/');
670 if (p != (
char *) NULL)
672#if !defined(MAGICKCORE_HAVE_GETPWNAM_R)
673 entry=getpwnam(username);
675 entry=(
struct passwd *) NULL;
676 if (getpwnam_r(username,&pwd,buffer,
sizeof(buffer),&entry) < 0)
679 if (entry == (
struct passwd *) NULL)
681 (void) CopyMagickString(expand_path,entry->pw_dir,MagickPathExtent);
682 if (p != (
char *) NULL)
684 (void) ConcatenateMagickString(expand_path,
"/",MagickPathExtent);
685 (void) ConcatenateMagickString(expand_path,p+1,MagickPathExtent);
689 (void) CopyMagickString(path,expand_path,MagickPathExtent);
748MagickExport 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 (IsPathAccessible(option) != MagickFalse)
837 GetPathComponent(option,HeadPath,path);
838 GetPathComponent(option,SubimagePath,subimage);
839 ExpandFilename(path);
840 if (*home_directory ==
'\0')
841 getcwd_utf8(home_directory,MagickPathExtent-1);
842 filelist=ListFiles(*path ==
'\0' ? home_directory : path,filename,
859 exception=AcquireExceptionInfo();
860 files=FileToString(option,~0UL,exception);
861 exception=DestroyExceptionInfo(exception);
862 if (files == (
char *) NULL)
864 filelist=StringToArgv(files,&length);
865 if (filelist == (
char **) NULL)
867 files=DestroyString(files);
868 filelist[0]=DestroyString(filelist[0]);
869 for (j=0; j < (ssize_t) (length-1); j++)
870 filelist[j]=filelist[j+1];
871 number_files=(size_t) length-1;
873 if (filelist == (
char **) NULL)
875 for (j=0; j < (ssize_t) number_files; j++)
876 if (IsPathDirectory(filelist[j]) <= 0)
878 if (j == (ssize_t) number_files)
880 for (j=0; j < (ssize_t) number_files; j++)
881 filelist[j]=DestroyString(filelist[j]);
882 filelist=(
char **) RelinquishMagickMemory(filelist);
888 vector=(
char **) ResizeQuantumMemory(vector,(
size_t) ((ssize_t)
889 *number_arguments+count+(ssize_t) number_files+1),
sizeof(*vector));
890 if (vector == (
char **) NULL)
892 for (j=0; j < (ssize_t) number_files; j++)
893 filelist[j]=DestroyString(filelist[j]);
894 filelist=(
char **) RelinquishMagickMemory(filelist);
897 for (j=0; j < (ssize_t) number_files; j++)
900 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
909 vector[count++]=ConstantString(option);
910 for (k=0; k < parameters; k++)
913 if (j == (ssize_t) number_files)
916 vector[count++]=ConstantString(option);
920 (void) CopyMagickString(filename,path,MagickPathExtent);
922 (void) ConcatenateMagickString(filename,DirectorySeparator,
924 if (filelist[j] != (
char *) NULL)
925 (void) ConcatenateMagickString(filename,filelist[j],MagickPathExtent);
926 filelist[j]=DestroyString(filelist[j]);
927 if (strlen(filename) >= (MagickPathExtent-1))
928 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
929 if (IsPathDirectory(filename) <= 0)
932 file_path[MagickPathExtent];
937 (void) ConcatenateMagickString(file_path,magick,
939 (void) ConcatenateMagickString(file_path,
":",MagickPathExtent);
941 (void) ConcatenateMagickString(file_path,filename,MagickPathExtent);
942 if (*subimage !=
'\0')
944 (void) ConcatenateMagickString(file_path,
"[",MagickPathExtent);
945 (void) ConcatenateMagickString(file_path,subimage,
947 (void) ConcatenateMagickString(file_path,
"]",MagickPathExtent);
949 if (strlen(file_path) >= (MagickPathExtent-1))
950 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
951 if (destroy != MagickFalse)
954 vector[count]=DestroyString(vector[count]);
957 vector[count++]=ConstantString(file_path);
960 filelist=(
char **) RelinquishMagickMemory(filelist);
962 vector[count]=(
char *) NULL;
963 if (IsEventLogging() != MagickFalse)
968 command_line=AcquireString(vector[0]);
969 for (i=1; i < count; i++)
971 (void) ConcatenateString(&command_line,
" {");
972 (void) ConcatenateString(&command_line,vector[i]);
973 (void) ConcatenateString(&command_line,
"}");
975 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
976 "Command line: %s",command_line);
977 command_line=DestroyString(command_line);
979 *number_arguments=(int) count;
1009MagickPrivate MagickBooleanType GetExecutionPath(
char *path,
const size_t extent)
1015 directory=getcwd(path,(
unsigned long) extent);
1017#if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
1020 execution_path[PATH_MAX+1],
1021 link_path[MagickPathExtent];
1026 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.20g/exe",
1028 count=readlink(link_path,execution_path,PATH_MAX);
1031 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.20g/file",
1033 count=readlink(link_path,execution_path,PATH_MAX);
1035 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
1037 execution_path[count]=
'\0';
1038 (void) CopyMagickString(path,execution_path,extent);
1042#if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
1045 executable_path[PATH_MAX << 1],
1046 execution_path[PATH_MAX+1];
1051 length=
sizeof(executable_path);
1052 if ((_NSGetExecutablePath(executable_path,&length) == 0) &&
1053 (realpath(executable_path,execution_path) != (
char *) NULL))
1054 (void) CopyMagickString(path,execution_path,extent);
1057#if defined(MAGICKCORE_HAVE_GETEXECNAME)
1062 execution_path=(
const char *) getexecname();
1063 if (execution_path != (
const char *) NULL)
1065 if (*execution_path != *DirectorySeparator)
1066 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1067 (void) ConcatenateMagickString(path,execution_path,extent);
1071#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1072 NTGetExecutionPath(path,extent);
1083 program_name=program_invocation_name;
1084 if (*program_invocation_name !=
'/')
1089 extent=strlen(directory)+strlen(program_name)+2;
1090 program_name=AcquireQuantumMemory(extent,
sizeof(*program_name));
1091 if (program_name == (
char *) NULL)
1092 program_name=program_invocation_name;
1094 count=FormatLocaleString(program_name,extent,
"%s/%s",directory,
1095 program_invocation_name);
1100 execution_path[PATH_MAX+1];
1102 if (realpath(program_name,execution_path) != (
char *) NULL)
1103 (void) CopyMagickString(path,execution_path,extent);
1105 if (program_name != program_invocation_name)
1106 program_name=(
char *) RelinquishMagickMemory(program_name);
1109#if defined(__OpenBSD__)
1114 (void) CopyMagickString(path,__progname,extent);
1117 return(IsPathAccessible(path));
1138MagickPrivate ssize_t GetMagickPageSize(
void)
1145#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1146 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1147#elif defined(MAGICKCORE_HAVE_GETPAGESIZE)
1148 page_size=(ssize_t) getpagesize();
1179MagickExport MagickBooleanType GetPathAttributes(
const char *path,
1185 if (path == (
const char *) NULL)
1188 return(MagickFalse);
1190 (void) memset(attributes,0,
sizeof(
struct stat));
1191 status=stat_utf8(path,(
struct stat *) attributes) == 0 ? MagickTrue :
1227MagickExport
void GetPathComponent(
const char *path,PathType type,
1241 assert(path != (
const char *) NULL);
1242 assert(component != (
char *) NULL);
1243 if (IsEventLogging() != MagickFalse)
1244 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",path);
1250 (void) CopyMagickString(component,path,MagickPathExtent);
1253 if (type != SubcanonicalPath)
1255 p=component+strlen(component)-1;
1256 if ((strlen(component) > 2) && (*p ==
']'))
1258 q=strrchr(component,
'[');
1259 if ((q != (
char *) NULL) && ((q == component) || (*(q-1) !=
']')) &&
1260 (IsPathAccessible(path) == MagickFalse))
1266 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1267 (IsGeometry(q+1) == MagickFalse))
1271 subimage_length=(size_t) (p-q);
1272 subimage_offset=(size_t) (q-component+1);
1282 for (p=component; *p !=
'\0'; p++)
1284 if ((*p ==
'%') && (*(p+1) ==
'['))
1289 for (p++; (*p !=
']') && (*p !=
'\0'); p++) ;
1293 if ((p != component) && (*p ==
':') && (IsPathDirectory(component) < 0) &&
1294 (IsPathAccessible(component) == MagickFalse))
1300 if (IsMagickConflict(component) != MagickFalse)
1304 magick_length=(size_t) (p-component+1);
1305 for (q=component; *(++p) !=
'\0'; q++)
1314 for (p=component+strlen(component)-1; p > component; p--)
1315 if (IsBasenameSeparator(*p) != MagickFalse)
1321 if (magick_length != 0)
1322 (void) CopyMagickString(component,path,magick_length);
1329 if (*component !=
'\0')
1331 for (p=component+(strlen(component)-1); p > component; p--)
1333 if (IsBasenameSeparator(*p) != MagickFalse)
1351 if (IsBasenameSeparator(*p) != MagickFalse)
1352 (void) CopyMagickString(component,p+1,MagickPathExtent);
1357 if (IsBasenameSeparator(*p) != MagickFalse)
1358 (void) CopyMagickString(component,p+1,MagickPathExtent);
1359 if (*component !=
'\0')
1360 for (p=component+(strlen(component)-1); p > component; p--)
1368 case BasePathSansCompressExtension:
1371 extension[MagickPathExtent];
1376 GetPathComponent(path,ExtensionPath,extension);
1377 if ((LocaleCompare(extension,
"bz2") == 0) ||
1378 (LocaleCompare(extension,
"gz") == 0) ||
1379 (LocaleCompare(extension,
"svgz") == 0) ||
1380 (LocaleCompare(extension,
"wmz") == 0) ||
1381 (LocaleCompare(extension,
"Z") == 0))
1382 GetPathComponent(path,BasePath,component);
1387 if (IsBasenameSeparator(*p) != MagickFalse)
1388 (void) CopyMagickString(component,p+1,MagickPathExtent);
1389 if (*component !=
'\0')
1390 for (p=component+strlen(component)-1; p > component; p--)
1395 (void) CopyMagickString(component,p+1,MagickPathExtent);
1401 if ((subimage_length != 0) && (magick_length < subimage_offset))
1402 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1405 case SubcanonicalPath:
1437MagickPrivate
char **GetPathComponents(
const char *path,
1438 size_t *number_components)
1450 if (path == (
char *) NULL)
1451 return((
char **) NULL);
1452 *number_components=1;
1453 for (p=path; *p !=
'\0'; p++)
1454 if (IsBasenameSeparator(*p))
1455 (*number_components)++;
1456 components=(
char **) AcquireQuantumMemory((
size_t) *number_components+1UL,
1457 sizeof(*components));
1458 if (components == (
char **) NULL)
1459 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1461 for (i=0; i < (ssize_t) *number_components; i++)
1463 for (q=p; *q !=
'\0'; q++)
1464 if (IsBasenameSeparator(*q))
1466 components[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MagickPathExtent,
1467 sizeof(**components));
1468 if (components[i] == (
char *) NULL)
1469 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1470 (void) CopyMagickString(components[i],p,(
size_t) (q-p+1));
1473 components[i]=(
char *) NULL;
1500MagickExport MagickBooleanType IsPathAccessible(
const char *path)
1508 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1509 return(MagickFalse);
1510 if (LocaleCompare(path,
"-") == 0)
1512 status=GetPathAttributes(path,&attributes);
1513 if (status == MagickFalse)
1515 if (S_ISREG(attributes.st_mode) == 0)
1516 return(MagickFalse);
1517 if (access_utf8(path,F_OK) != 0)
1518 return(MagickFalse);
1545static int IsPathDirectory(
const char *path)
1553 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1554 return(MagickFalse);
1555 status=GetPathAttributes(path,&attributes);
1556 if (status == MagickFalse)
1558 if (S_ISDIR(attributes.st_mode) == 0)
1598#if defined(__cplusplus) || defined(c_plusplus)
1602static int FileCompare(
const void *x,
const void *y)
1608 p=(
const char **) x;
1609 q=(
const char **) y;
1610 return(LocaleCompare(*p,*q));
1613#if defined(__cplusplus) || defined(c_plusplus)
1617MagickPrivate
char **ListFiles(
const char *directory,
const char *pattern,
1618 size_t *number_entries)
1636 assert(directory != (
const char *) NULL);
1637 assert(pattern != (
const char *) NULL);
1638 assert(number_entries != (
size_t *) NULL);
1639 if (IsEventLogging() != MagickFalse)
1640 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",directory);
1642 current_directory=opendir(directory);
1643 if (current_directory == (
DIR *) NULL)
1644 return((
char **) NULL);
1649 filelist=(
char **) AcquireQuantumMemory((
size_t) max_entries,
1651 if (filelist == (
char **) NULL)
1653 (void) closedir(current_directory);
1654 return((
char **) NULL);
1659 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
1660 if (buffer == (
struct dirent *) NULL)
1661 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1662 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1663 (entry != (
struct dirent *) NULL))
1665 if ((LocaleCompare(entry->d_name,
".") == 0) ||
1666 (LocaleCompare(entry->d_name,
"..") == 0))
1668 if ((IsPathDirectory(entry->d_name) > 0) ||
1669#
if defined(MAGICKCORE_WINDOWS_SUPPORT)
1670 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1672 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1675 if (*number_entries >= max_entries)
1681 filelist=(
char **) ResizeQuantumMemory(filelist,(
size_t)
1682 max_entries,
sizeof(*filelist));
1683 if (filelist == (
char **) NULL)
1691 p=strchr(entry->d_name,
';');
1694 if (*number_entries > 0)
1695 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1699 filelist[*number_entries]=(
char *) AcquireString(entry->d_name);
1700 (*number_entries)++;
1703 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
1704 (void) closedir(current_directory);
1705 if (filelist == (
char **) NULL)
1706 return((
char **) NULL);
1710 qsort((
void *) filelist,(
size_t) *number_entries,
sizeof(*filelist),
1739MagickExport
void MagickDelay(
const MagickSizeType milliseconds)
1741 if (milliseconds == 0)
1743#if defined(MAGICKCORE_HAVE_NANOSLEEP)
1748 timer.tv_sec=(time_t) (milliseconds/1000);
1749 timer.tv_nsec=(time_t) ((milliseconds % 1000)*1000*1000);
1750 (void) nanosleep(&timer,(
struct timespec *) NULL);
1752#elif defined(MAGICKCORE_HAVE_USLEEP)
1753 usleep(1000*milliseconds);
1754#elif defined(MAGICKCORE_HAVE_SELECT)
1759 timer.tv_sec=(long) milliseconds/1000;
1760 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1761 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1763#elif defined(MAGICKCORE_HAVE_POLL)
1764 (void) poll((
struct pollfd *) NULL,0,(
int) milliseconds);
1765#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1766 Sleep((
long) milliseconds);
1772 timer=milliseconds/1000.0;
1775#elif defined(__BEOS__)
1776 snooze(1000*milliseconds);
1782 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1783 while (clock() < time_end)
1813MagickExport
size_t MultilineCensus(
const char *label)
1821 if (label == (
char *) NULL)
1823 for (number_lines=1; *label !=
'\0'; label++)
1826 return(number_lines);
1852MagickPrivate MagickBooleanType ShredFile(
const char *path)
1879 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1880 return(MagickFalse);
1887 property=GetEnvironmentValue(
"MAGICK_SHRED_PASSES");
1888 if (property != (
char *) NULL)
1890 passes=(ssize_t) StringToInteger(property);
1891 property=DestroyString(property);
1893 property=GetPolicyValue(
"system:shred");
1894 if (property != (
char *) NULL)
1896 passes=(ssize_t) StringToInteger(property);
1897 property=DestroyString(property);
1905 file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
1907 return(MagickFalse);
1908 quantum=(size_t) MagickMinBufferExtent;
1909 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1910 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
1911 length=(MagickSizeType) file_stats.st_size;
1912 random_info=AcquireRandomInfo();
1913 key=GetRandomKey(random_info,quantum);
1914 for (i=0; i < passes; i++)
1922 if (lseek(file,0,SEEK_SET) < 0)
1924 for (j=0; j < (MagickOffsetType) length; j+=count)
1927 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
1928 count=write(file,GetStringInfoDatum(key),(
size_t)
1929 MagickMin((MagickOffsetType) quantum,(MagickOffsetType) length-j));
1937 if (j < (MagickOffsetType) length)
1940 key=DestroyStringInfo(key);
1941 random_info=DestroyRandomInfo(random_info);
1943 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);