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 (void) AcquireUniqueFilename(destination);
184 (void) RelinquishUniqueFileResource(destination);
185 passes=GetPolicyValue(
"system:shred");
186 if (passes != (
char *) NULL)
187 passes=DestroyString(passes);
190 if (*source == *DirectorySeparator)
192 if (symlink(source,destination) == 0)
198 path[MagickPathExtent];
201 if (getcwd(path,MagickPathExtent) == (
char *) NULL)
203 (void) ConcatenateMagickString(path,DirectorySeparator,
205 (void) ConcatenateMagickString(path,source,MagickPathExtent);
206 if (symlink(path,destination) == 0)
212 destination_file=AcquireUniqueFileResource(destination);
213 if (destination_file == -1)
215 source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
216 if (source_file == -1)
218 (void) close(destination_file);
219 (void) RelinquishUniqueFileResource(destination);
222 quantum=(size_t) MagickMaxBufferExtent;
223 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
224 quantum=(
size_t) MagickMin(attributes.st_size,MagickMaxBufferExtent);
225 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
226 if (buffer == (
unsigned char *) NULL)
228 (void) close(source_file);
229 (void) close(destination_file);
230 (void) RelinquishUniqueFileResource(destination);
236 count=(ssize_t) read(source_file,buffer,quantum);
239 length=(size_t) count;
240 count=(ssize_t) write(destination_file,buffer,length);
241 if ((
size_t) count != length)
243 (void) RelinquishUniqueFileResource(destination);
248 (void) close(destination_file);
249 (void) close(source_file);
250 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
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) *number_arguments+
889 count+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)
1350 if (IsBasenameSeparator(*p) != MagickFalse)
1351 (
void) CopyMagickString(component,p+1,MagickPathExtent);
1356 if (IsBasenameSeparator(*p) != MagickFalse)
1357 (void) CopyMagickString(component,p+1,MagickPathExtent);
1358 if (*component !=
'\0')
1359 for (p=component+(strlen(component)-1); p > component; p--)
1367 case BasePathSansCompressExtension:
1370 extension[MagickPathExtent];
1375 GetPathComponent(path,ExtensionPath,extension);
1376 if ((LocaleCompare(extension,
"bz2") == 0) ||
1377 (LocaleCompare(extension,
"gz") == 0) ||
1378 (LocaleCompare(extension,
"svgz") == 0) ||
1379 (LocaleCompare(extension,
"wmz") == 0) ||
1380 (LocaleCompare(extension,
"Z") == 0))
1381 GetPathComponent(path,BasePath,component);
1386 if (IsBasenameSeparator(*p) != MagickFalse)
1387 (void) CopyMagickString(component,p+1,MagickPathExtent);
1388 if (*component !=
'\0')
1389 for (p=component+strlen(component)-1; p > component; p--)
1394 (void) CopyMagickString(component,p+1,MagickPathExtent);
1400 if ((subimage_length != 0) && (magick_length < subimage_offset))
1401 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1404 case SubcanonicalPath:
1436MagickPrivate
char **GetPathComponents(
const char *path,
1437 size_t *number_components)
1449 if (path == (
char *) NULL)
1450 return((
char **) NULL);
1451 *number_components=1;
1452 for (p=path; *p !=
'\0'; p++)
1453 if (IsBasenameSeparator(*p))
1454 (*number_components)++;
1455 components=(
char **) AcquireQuantumMemory((
size_t) *number_components+1UL,
1456 sizeof(*components));
1457 if (components == (
char **) NULL)
1458 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1460 for (i=0; i < (ssize_t) *number_components; i++)
1462 for (q=p; *q !=
'\0'; q++)
1463 if (IsBasenameSeparator(*q))
1465 components[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MagickPathExtent,
1466 sizeof(**components));
1467 if (components[i] == (
char *) NULL)
1468 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1469 (void) CopyMagickString(components[i],p,(
size_t) (q-p+1));
1472 components[i]=(
char *) NULL;
1499MagickExport MagickBooleanType IsPathAccessible(
const char *path)
1507 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1508 return(MagickFalse);
1509 if (LocaleCompare(path,
"-") == 0)
1511 status=GetPathAttributes(path,&attributes);
1512 if (status == MagickFalse)
1514 if (S_ISREG(attributes.st_mode) == 0)
1515 return(MagickFalse);
1516 if (access_utf8(path,F_OK) != 0)
1517 return(MagickFalse);
1544static int IsPathDirectory(
const char *path)
1552 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1553 return(MagickFalse);
1554 status=GetPathAttributes(path,&attributes);
1555 if (status == MagickFalse)
1557 if (S_ISDIR(attributes.st_mode) == 0)
1597#if defined(__cplusplus) || defined(c_plusplus)
1601static int FileCompare(
const void *x,
const void *y)
1607 p=(
const char **) x;
1608 q=(
const char **) y;
1609 return(LocaleCompare(*p,*q));
1612#if defined(__cplusplus) || defined(c_plusplus)
1616MagickPrivate
char **ListFiles(
const char *directory,
const char *pattern,
1617 size_t *number_entries)
1635 assert(directory != (
const char *) NULL);
1636 assert(pattern != (
const char *) NULL);
1637 assert(number_entries != (
size_t *) NULL);
1638 if (IsEventLogging() != MagickFalse)
1639 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",directory);
1641 current_directory=opendir(directory);
1642 if (current_directory == (
DIR *) NULL)
1643 return((
char **) NULL);
1648 filelist=(
char **) AcquireQuantumMemory((
size_t) max_entries,
1650 if (filelist == (
char **) NULL)
1652 (void) closedir(current_directory);
1653 return((
char **) NULL);
1658 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
1659 if (buffer == (
struct dirent *) NULL)
1660 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1661 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1662 (entry != (
struct dirent *) NULL))
1664 if ((LocaleCompare(entry->d_name,
".") == 0) ||
1665 (LocaleCompare(entry->d_name,
"..") == 0))
1667 if ((IsPathDirectory(entry->d_name) > 0) ||
1668#
if defined(MAGICKCORE_WINDOWS_SUPPORT)
1669 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1671 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1674 if (*number_entries >= max_entries)
1680 filelist=(
char **) ResizeQuantumMemory(filelist,(
size_t)
1681 max_entries,
sizeof(*filelist));
1682 if (filelist == (
char **) NULL)
1690 p=strchr(entry->d_name,
';');
1693 if (*number_entries > 0)
1694 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1698 filelist[*number_entries]=(
char *) AcquireString(entry->d_name);
1699 (*number_entries)++;
1702 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
1703 (void) closedir(current_directory);
1704 if (filelist == (
char **) NULL)
1705 return((
char **) NULL);
1709 qsort((
void *) filelist,(
size_t) *number_entries,
sizeof(*filelist),
1738MagickExport
void MagickDelay(
const MagickSizeType milliseconds)
1740 if (milliseconds == 0)
1742#if defined(MAGICKCORE_HAVE_NANOSLEEP)
1747 timer.tv_sec=(time_t) (milliseconds/1000);
1748 timer.tv_nsec=(milliseconds % 1000)*1000*1000;
1749 (void) nanosleep(&timer,(
struct timespec *) NULL);
1751#elif defined(MAGICKCORE_HAVE_USLEEP)
1752 usleep(1000*milliseconds);
1753#elif defined(MAGICKCORE_HAVE_SELECT)
1758 timer.tv_sec=(long) milliseconds/1000;
1759 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1760 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1762#elif defined(MAGICKCORE_HAVE_POLL)
1763 (void) poll((
struct pollfd *) NULL,0,(int) milliseconds);
1764#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1765 Sleep((
long) milliseconds);
1771 timer=milliseconds/1000.0;
1774#elif defined(__BEOS__)
1775 snooze(1000*milliseconds);
1781 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1782 while (clock() < time_end)
1812MagickExport
size_t MultilineCensus(
const char *label)
1820 if (label == (
char *) NULL)
1822 for (number_lines=1; *label !=
'\0'; label++)
1825 return(number_lines);
1851MagickPrivate MagickBooleanType ShredFile(
const char *path)
1878 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1879 return(MagickFalse);
1886 property=GetEnvironmentValue(
"MAGICK_SHRED_PASSES");
1887 if (property != (
char *) NULL)
1889 passes=(ssize_t) StringToInteger(property);
1890 property=DestroyString(property);
1892 property=GetPolicyValue(
"system:shred");
1893 if (property != (
char *) NULL)
1895 passes=(ssize_t) StringToInteger(property);
1896 property=DestroyString(property);
1904 file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
1906 return(MagickFalse);
1907 quantum=(size_t) MagickMinBufferExtent;
1908 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1909 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
1910 length=(MagickSizeType) file_stats.st_size;
1911 random_info=AcquireRandomInfo();
1912 key=GetRandomKey(random_info,quantum);
1913 for (i=0; i < passes; i++)
1921 if (lseek(file,0,SEEK_SET) < 0)
1923 for (j=0; j < (MagickOffsetType) length; j+=count)
1926 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
1927 count=write(file,GetStringInfoDatum(key),(
size_t)
1928 MagickMin((MagickSizeType) quantum,length-j));
1936 if (j < (MagickOffsetType) length)
1939 key=DestroyStringInfo(key);
1940 random_info=DestroyRandomInfo(random_info);
1942 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);