42#include "magick/studio.h"
43#include "magick/property.h"
44#include "magick/blob.h"
45#include "magick/color.h"
46#include "magick/exception.h"
47#include "magick/exception-private.h"
48#include "magick/geometry.h"
49#include "magick/image-private.h"
50#include "magick/list.h"
51#include "magick/log.h"
52#include "magick/memory_.h"
53#include "magick/nt-base-private.h"
54#include "magick/option.h"
55#include "magick/policy.h"
56#include "magick/random_.h"
57#include "magick/registry.h"
58#include "magick/resource_.h"
59#include "magick/semaphore.h"
60#include "magick/signature-private.h"
61#include "magick/statistic.h"
62#include "magick/string_.h"
63#include "magick/string-private.h"
64#include "magick/token.h"
65#include "magick/utility.h"
66#include "magick/utility-private.h"
67#if defined(MAGICKCORE_HAVE_PROCESS_H)
70#if defined(MAGICKCORE_HAVE_MACH_O_DYLD_H)
71#include <mach-o/dyld.h>
78 Base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
84 IsPathDirectory(
const char *);
109MagickExport MagickBooleanType AcquireUniqueFilename(
char *path)
114 file=AcquireUniqueFileResource(path);
151MagickExport MagickBooleanType AcquireUniqueSymbolicLink(
const char *source,
174 assert(source != (
const char *) NULL);
175 assert(destination != (
char *) NULL);
176#if defined(MAGICKCORE_HAVE_SYMLINK)
181 passes=GetPolicyValue(
"system:shred");
182 if (passes != (
char *) NULL)
183 passes=DestroyString(passes);
186 (void) AcquireUniqueFilename(destination);
187 (void) RelinquishUniqueFileResource(destination);
188 if (*source == *DirectorySeparator)
190 if (symlink(source,destination) == 0)
199 if (getcwd(path,MaxTextExtent) == (
char *) NULL)
201 (void) ConcatenateMagickString(path,DirectorySeparator,
203 (void) ConcatenateMagickString(path,source,MaxTextExtent);
204 if (symlink(path,destination) == 0)
210 destination_file=AcquireUniqueFileResource(destination);
211 if (destination_file == -1)
213 source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
214 if (source_file == -1)
216 (void) close(destination_file);
217 (void) RelinquishUniqueFileResource(destination);
220 quantum=(size_t) MagickMaxBufferExtent;
221 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
222 quantum=(size_t) MagickMin(attributes.st_size,MagickMaxBufferExtent);
223 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
224 if (buffer == (
unsigned char *) NULL)
226 (void) close(source_file);
227 (void) close(destination_file);
228 (void) RelinquishUniqueFileResource(destination);
234 count=(ssize_t) read(source_file,buffer,quantum);
237 length=(size_t) count;
238 count=(ssize_t) write(destination_file,buffer,length);
239 if ((
size_t) count != length)
241 (void) RelinquishUniqueFileResource(destination);
246 (void) close(destination_file);
247 (void) close(source_file);
248 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
279MagickExport
void AppendImageFormat(
const char *format,
char *filename)
282 extension[MaxTextExtent],
285 assert(format != (
char *) NULL);
286 assert(filename != (
char *) NULL);
287 if (IsEventLogging() != MagickFalse)
288 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
289 if ((*format ==
'\0') || (*filename ==
'\0'))
291 if (LocaleCompare(filename,
"-") == 0)
294 message[MaxTextExtent];
296 (void) FormatLocaleString(message,MaxTextExtent,
"%s:%s",format,filename);
297 (void) CopyMagickString(filename,message,MaxTextExtent);
300 GetPathComponent(filename,ExtensionPath,extension);
301 if ((LocaleCompare(extension,
"Z") == 0) ||
302 (LocaleCompare(extension,
"bz2") == 0) ||
303 (LocaleCompare(extension,
"gz") == 0) ||
304 (LocaleCompare(extension,
"wmz") == 0) ||
305 (LocaleCompare(extension,
"svgz") == 0))
307 GetPathComponent(filename,RootPath,root);
308 (void) CopyMagickString(filename,root,MaxTextExtent);
309 GetPathComponent(filename,RootPath,root);
310 (void) FormatLocaleString(filename,MaxTextExtent,
"%s.%s.%s",root,format,
314 GetPathComponent(filename,RootPath,root);
315 (void) FormatLocaleString(filename,MaxTextExtent,
"%s.%s",root,format);
344MagickExport
unsigned char *Base64Decode(
const char *source,
size_t *length)
359 assert(source != (
char *) NULL);
360 assert(length != (
size_t *) NULL);
361 if (IsEventLogging() != MagickFalse)
362 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
364 decode=(
unsigned char *) AcquireQuantumMemory((strlen(source)+3)/4,
366 if (decode == (
unsigned char *) NULL)
367 return((
unsigned char *) NULL);
370 for (p=source; *p !=
'\0'; p++)
372 if (isspace((
int) ((
unsigned char) *p)) != 0)
377 if (q == (
char *) NULL)
379 decode=(
unsigned char *) RelinquishMagickMemory(decode);
380 return((
unsigned char *) NULL);
386 decode[i]=(q-Base64) << 2;
392 decode[i++]|=(q-Base64) >> 4;
393 decode[i]=((q-Base64) & 0x0f) << 4;
399 decode[i++]|=(q-Base64) >> 2;
400 decode[i]=((q-Base64) & 0x03) << 6;
406 decode[i++]|=(q-Base64);
419 decode=(
unsigned char *) RelinquishMagickMemory(decode);
420 return((
unsigned char *) NULL);
434 decode=(
unsigned char *) RelinquishMagickMemory(decode);
435 return((
unsigned char *) NULL);
439 for ( ; *p !=
'\0'; p++)
440 if (isspace((
int) ((
unsigned char) *p)) == 0)
444 decode=(
unsigned char *) RelinquishMagickMemory(decode);
445 return((
unsigned char *) NULL);
451 for ( ; *p !=
'\0'; p++)
452 if (isspace((
int) ((
unsigned char) *p)) == 0)
454 decode=(
unsigned char *) RelinquishMagickMemory(decode);
455 return((
unsigned char *) NULL);
457 if ((
int) decode[i] != 0)
459 decode=(
unsigned char *) RelinquishMagickMemory(decode);
460 return((
unsigned char *) NULL);
500MagickExport
char *Base64Encode(
const unsigned char *blob,
501 const size_t blob_length,
size_t *encode_length)
515 assert(blob != (
const unsigned char *) NULL);
516 assert(blob_length != 0);
517 assert(encode_length != (
size_t *) NULL);
518 if (IsEventLogging() != MagickFalse)
519 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
521 encode=(
char *) AcquireQuantumMemory(blob_length/3+4,4*
sizeof(*encode));
522 if (encode == (
char *) NULL)
523 return((
char *) NULL);
525 for (p=blob; p < (blob+blob_length-2); p+=(ptrdiff_t) 3)
527 encode[i++]=Base64[(int) (*p >> 2)];
528 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
529 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
530 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
532 remainder=blob_length % 3;
544 for (j=0; j < (ssize_t) remainder; j++)
546 encode[i++]=Base64[(int) (code[0] >> 2)];
547 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
551 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
584MagickExport
void ChopPathComponents(
char *path,
const size_t components)
589 for (i=0; i < (ssize_t) components; i++)
590 GetPathComponent(path,HeadPath,path);
616MagickExport
void ExpandFilename(
char *path)
619 expand_path[MaxTextExtent];
621 if (path == (
char *) NULL)
625 (void) CopyMagickString(expand_path,path,MaxTextExtent);
626 if ((*(path+1) == *DirectorySeparator) || (*(path+1) ==
'\0'))
634 (void) CopyMagickString(expand_path,
".",MaxTextExtent);
635 (void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
636 home=GetEnvironmentValue(
"HOME");
637 if (home == (
char *) NULL)
638 home=GetEnvironmentValue(
"USERPROFILE");
639 if (home != (
char *) NULL)
641 (void) CopyMagickString(expand_path,home,MaxTextExtent);
642 (void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
643 home=DestroyString(home);
648#if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
650#if defined(MAGICKCORE_HAVE_GETPWNAM_R)
651 buffer[MagickPathExtent],
653 username[MaxTextExtent];
664 (void) CopyMagickString(username,path+1,MaxTextExtent);
665 p=strchr(username,
'/');
666 if (p != (
char *) NULL)
668#if !defined(MAGICKCORE_HAVE_GETPWNAM_R)
669 entry=getpwnam(username);
674 entry=(
struct passwd *) NULL;
675 if (getpwnam_r(username,&pwd,buffer,
sizeof(buffer),&entry) < 0)
678 if (entry == (
struct passwd *) NULL)
680 (void) CopyMagickString(expand_path,entry->pw_dir,MaxTextExtent);
681 if (p != (
char *) NULL)
683 (void) ConcatenateMagickString(expand_path,
"/",MaxTextExtent);
684 (void) ConcatenateMagickString(expand_path,p+1,MaxTextExtent);
688 (void) CopyMagickString(path,expand_path,MaxTextExtent);
747static inline void getcwd_utf8(
char *path,
size_t extent)
749#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
753 directory=getcwd(path,extent);
757 wide_path[MaxTextExtent];
759 (void) _wgetcwd(wide_path,MaxTextExtent-1);
760 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
764MagickExport MagickBooleanType ExpandFilenames(
int *number_arguments,
768 home_directory[MaxTextExtent],
785 assert(number_arguments != (
int *) NULL);
786 assert(arguments != (
char ***) NULL);
787 if (IsEventLogging() != MagickFalse)
788 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
789 vector=(
char **) AcquireQuantumMemory((
size_t) (*number_arguments+1),
791 if (vector == (
char **) NULL)
792 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
796 *home_directory=
'\0';
798 for (i=0; i < (ssize_t) *number_arguments; i++)
802 filename[MaxTextExtent],
803 magick[MaxTextExtent],
806 subimage[MaxTextExtent];
811 option=(*arguments)[i];
817 vector[count++]=ConstantString(option);
819 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
825 for (j=0; j < parameters; j++)
828 if (i == (ssize_t) *number_arguments)
830 option=(*arguments)[i];
831 vector[count++]=ConstantString(option);
835 if ((*option ==
'"') || (*option ==
'\''))
837 GetPathComponent(option,TailPath,filename);
838 GetPathComponent(option,MagickPath,magick);
839 if ((LocaleCompare(magick,
"CAPTION") == 0) ||
840 (LocaleCompare(magick,
"LABEL") == 0) ||
841 (LocaleCompare(magick,
"PANGO") == 0) ||
842 (LocaleCompare(magick,
"VID") == 0))
844 if ((IsGlob(filename) == MagickFalse) && (*option !=
'@'))
846 if (IsPathAccessible(option) != MagickFalse)
853 GetPathComponent(option,HeadPath,path);
854 GetPathComponent(option,SubimagePath,subimage);
855 ExpandFilename(path);
856 if (*home_directory ==
'\0')
857 getcwd_utf8(home_directory,MaxTextExtent-1);
858 filelist=ListFiles(*path ==
'\0' ? home_directory : path,filename,
875 exception=AcquireExceptionInfo();
876 files=FileToString(option,~0UL,exception);
877 exception=DestroyExceptionInfo(exception);
878 if (files == (
char *) NULL)
880 filelist=StringToArgv(files,&length);
881 if (filelist == (
char **) NULL)
883 files=DestroyString(files);
884 filelist[0]=DestroyString(filelist[0]);
885 for (j=0; j < (ssize_t) (length-1); j++)
886 filelist[j]=filelist[j+1];
887 number_files=(size_t) length-1;
889 if (filelist == (
char **) NULL)
891 for (j=0; j < (ssize_t) number_files; j++)
892 if (IsPathDirectory(filelist[j]) <= 0)
894 if (j == (ssize_t) number_files)
896 for (j=0; j < (ssize_t) number_files; j++)
897 filelist[j]=DestroyString(filelist[j]);
898 filelist=(
char **) RelinquishMagickMemory(filelist);
904 vector=(
char **) ResizeQuantumMemory(vector,(
size_t) *number_arguments+
905 count+number_files+1,
sizeof(*vector));
906 if (vector == (
char **) NULL)
908 for (j=0; j < (ssize_t) number_files; j++)
909 filelist[j]=DestroyString(filelist[j]);
910 filelist=(
char **) RelinquishMagickMemory(filelist);
913 for (j=0; j < (ssize_t) number_files; j++)
916 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
925 vector[count++]=ConstantString(option);
926 for (k=0; k < parameters; k++)
929 if (j == (ssize_t) number_files)
932 vector[count++]=ConstantString(option);
936 (void) CopyMagickString(filename,path,MaxTextExtent);
938 (void) ConcatenateMagickString(filename,DirectorySeparator,
940 if (filelist[j] != (
char *) NULL)
941 (void) ConcatenateMagickString(filename,filelist[j],MaxTextExtent);
942 filelist[j]=DestroyString(filelist[j]);
943 if (strlen(filename) >= (MaxTextExtent-1))
944 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
945 if (IsPathDirectory(filename) <= 0)
953 (void) ConcatenateMagickString(path,magick,MaxTextExtent);
954 (void) ConcatenateMagickString(path,
":",MaxTextExtent);
956 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
957 if (*subimage !=
'\0')
959 (void) ConcatenateMagickString(path,
"[",MaxTextExtent);
960 (void) ConcatenateMagickString(path,subimage,MaxTextExtent);
961 (void) ConcatenateMagickString(path,
"]",MaxTextExtent);
963 if (strlen(path) >= (MaxTextExtent-1))
964 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
965 if (destroy != MagickFalse)
968 vector[count]=DestroyString(vector[count]);
971 vector[count++]=ConstantString(path);
974 filelist=(
char **) RelinquishMagickMemory(filelist);
976 vector[count]=(
char *) NULL;
977 if (IsEventLogging() != MagickFalse)
982 command_line=AcquireString(vector[0]);
983 for (i=1; i < count; i++)
985 (void) ConcatenateString(&command_line,
" {");
986 (void) ConcatenateString(&command_line,vector[i]);
987 (void) ConcatenateString(&command_line,
"}");
989 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
990 "Command line: %s",command_line);
991 command_line=DestroyString(command_line);
993 *number_arguments=(int) count;
1023MagickExport MagickBooleanType GetExecutionPath(
char *path,
const size_t extent)
1029 directory=getcwd(path,(
unsigned long) extent);
1031#if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
1034 link_path[MaxTextExtent],
1035 execution_path[PATH_MAX+1];
1040 (void) FormatLocaleString(link_path,MaxTextExtent,
"/proc/%.20g/exe",
1042 count=readlink(link_path,execution_path,PATH_MAX);
1045 (void) FormatLocaleString(link_path,MaxTextExtent,
"/proc/%.20g/file",
1047 count=readlink(link_path,execution_path,PATH_MAX);
1049 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
1051 execution_path[count]=
'\0';
1052 (void) CopyMagickString(path,execution_path,extent);
1056#if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
1059 executable_path[PATH_MAX << 1];
1064 length=
sizeof(executable_path);
1065 if (_NSGetExecutablePath(executable_path,&length) == 0)
1068 *real_path = realpath_utf8(executable_path);
1070 if (real_path != (
char *) NULL)
1072 (void) CopyMagickString(path,real_path,extent);
1073 real_path=DestroyString(real_path);
1078#if defined(MAGICKCORE_HAVE_GETEXECNAME)
1083 execution_path=(
const char *) getexecname();
1084 if (execution_path != (
const char *) NULL)
1086 if (*execution_path != *DirectorySeparator)
1087 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1088 (void) ConcatenateMagickString(path,execution_path,extent);
1092#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1093 NTGetExecutionPath(path,extent);
1104 program_name=program_invocation_name;
1105 if (*program_invocation_name !=
'/')
1110 extent=strlen(directory)+strlen(program_name)+2;
1111 program_name=AcquireQuantumMemory(extent,
sizeof(*program_name));
1112 if (program_name == (
char *) NULL)
1113 program_name=program_invocation_name;
1115 count=FormatLocaleString(program_name,extent,
"%s/%s",directory,
1116 program_invocation_name);
1121 *real_path = realpath_utf8(program_name);
1123 if (real_path != (
char *) NULL)
1125 (void) CopyMagickString(path,real_path,extent);
1126 real_path=DestroyString(real_path);
1129 if (program_name != program_invocation_name)
1130 program_name=(
char *) RelinquishMagickMemory(program_name);
1133#if defined(__OpenBSD__)
1138 (void) CopyMagickString(path,__progname,extent);
1141 return(IsPathAccessible(path));
1162MagickExport ssize_t GetMagickPageSize(
void)
1169#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1170 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1171#elif defined(MAGICKCORE_HAVE_GETPAGESIZE)
1172 page_size=(ssize_t) getpagesize();
1203MagickExport MagickBooleanType GetPathAttributes(
const char *path,
1209 if (path == (
const char *) NULL)
1212 return(MagickFalse);
1214 (void) memset(attributes,0,
sizeof(
struct stat));
1215 status=stat_utf8(path,(
struct stat *) attributes) == 0 ? MagickTrue :
1251MagickExport
void GetPathComponent(
const char *path,PathType type,
1265 assert(path != (
const char *) NULL);
1266 assert(component != (
char *) NULL);
1267 if (IsEventLogging() != MagickFalse)
1268 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",path);
1274 (void) CopyMagickString(component,path,MagickPathExtent);
1277 if (type != SubcanonicalPath)
1279 p=component+strlen(component)-1;
1280 if ((strlen(component) > 2) && (*p ==
']'))
1282 q=strrchr(component,
'[');
1283 if ((q != (
char *) NULL) && ((q == component) || (*(q-1) !=
']')) &&
1284 (IsPathAccessible(path) == MagickFalse))
1290 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1291 (IsGeometry(q+1) == MagickFalse))
1295 subimage_length=(size_t) (p-q);
1296 subimage_offset=(size_t) (q-component+1);
1306 for (p=component; *p !=
'\0'; p++)
1308 if ((*p ==
'%') && (*(p+1) ==
'['))
1313 for (p++; (*p !=
']') && (*p !=
'\0'); p++) ;
1317 if ((p != component) && (*p ==
':') && (IsPathDirectory(component) < 0) &&
1318 (IsPathAccessible(component) == MagickFalse))
1324 if (IsMagickConflict(component) != MagickFalse)
1328 magick_length=(size_t) (p-component+1);
1329 for (q=component; *(++p) !=
'\0'; q++)
1338 for (p=component+strlen(component)-1; p > component; p--)
1339 if (IsBasenameSeparator(*p) != MagickFalse)
1345 if (magick_length != 0)
1346 (void) CopyMagickString(component,path,magick_length);
1353 if (*component !=
'\0')
1355 for (p=component+(strlen(component)-1); p > component; p--)
1357 if (IsBasenameSeparator(*p) != MagickFalse)
1375 if (IsBasenameSeparator(*p) != MagickFalse)
1376 (void) CopyMagickString(component,p+1,MagickPathExtent);
1381 if (IsBasenameSeparator(*p) != MagickFalse)
1382 (void) CopyMagickString(component,p+1,MagickPathExtent);
1383 if (*component !=
'\0')
1384 for (p=component+(strlen(component)-1); p > component; p--)
1392 case BasePathSansCompressExtension:
1395 extension[MagickPathExtent];
1400 GetPathComponent(path,ExtensionPath,extension);
1401 if ((LocaleCompare(extension,
"bz2") == 0) ||
1402 (LocaleCompare(extension,
"gz") == 0) ||
1403 (LocaleCompare(extension,
"svgz") == 0) ||
1404 (LocaleCompare(extension,
"wmz") == 0) ||
1405 (LocaleCompare(extension,
"Z") == 0))
1406 GetPathComponent(path,BasePath,component);
1411 if (IsBasenameSeparator(*p) != MagickFalse)
1412 (void) CopyMagickString(component,p+1,MagickPathExtent);
1413 if (*component !=
'\0')
1414 for (p=component+strlen(component)-1; p > component; p--)
1419 (void) CopyMagickString(component,p+1,MagickPathExtent);
1425 if ((subimage_length != 0) && (magick_length < subimage_offset))
1426 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1429 case SubcanonicalPath:
1461MagickExport
char **GetPathComponents(
const char *path,
1462 size_t *number_components)
1474 if (path == (
char *) NULL)
1475 return((
char **) NULL);
1476 *number_components=1;
1477 for (p=path; *p !=
'\0'; p++)
1478 if (IsBasenameSeparator(*p))
1479 (*number_components)++;
1480 components=(
char **) AcquireQuantumMemory((
size_t) *number_components+1UL,
1481 sizeof(*components));
1482 if (components == (
char **) NULL)
1483 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1485 for (i=0; i < (ssize_t) *number_components; i++)
1487 for (q=p; *q !=
'\0'; q++)
1488 if (IsBasenameSeparator(*q))
1490 components[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MaxTextExtent,
1491 sizeof(**components));
1492 if (components[i] == (
char *) NULL)
1493 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1494 (void) CopyMagickString(components[i],p,(
size_t) (q-p+1));
1497 components[i]=(
char *) NULL;
1524MagickExport MagickBooleanType IsPathAccessible(
const char *path)
1532 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1533 return(MagickFalse);
1534 if (LocaleCompare(path,
"-") == 0)
1536 status=GetPathAttributes(path,&attributes);
1537 if (status == MagickFalse)
1539 if (S_ISREG(attributes.st_mode) == 0)
1540 return(MagickFalse);
1541 if (access_utf8(path,F_OK) != 0)
1542 return(MagickFalse);
1569static int IsPathDirectory(
const char *path)
1577 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1578 return(MagickFalse);
1579 status=GetPathAttributes(path,&attributes);
1580 if (status == MagickFalse)
1582 if (S_ISDIR(attributes.st_mode) == 0)
1622#if defined(__cplusplus) || defined(c_plusplus)
1626static int FileCompare(
const void *x,
const void *y)
1632 p=(
const char **) x;
1633 q=(
const char **) y;
1634 return(LocaleCompare(*p,*q));
1637#if defined(__cplusplus) || defined(c_plusplus)
1641MagickExport
char **ListFiles(
const char *directory,
const char *pattern,
1642 size_t *number_entries)
1660 assert(directory != (
const char *) NULL);
1661 assert(pattern != (
const char *) NULL);
1662 assert(number_entries != (
size_t *) NULL);
1663 if (IsEventLogging() != MagickFalse)
1664 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",directory);
1666 current_directory=opendir(directory);
1667 if (current_directory == (DIR *) NULL)
1668 return((
char **) NULL);
1673 filelist=(
char **) AcquireQuantumMemory((
size_t) max_entries,
1675 if (filelist == (
char **) NULL)
1677 (void) closedir(current_directory);
1678 return((
char **) NULL);
1683 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
1684 if (buffer == (
struct dirent *) NULL)
1685 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1686 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1687 (entry != (
struct dirent *) NULL))
1689 if ((LocaleCompare(entry->d_name,
".") == 0) ||
1690 (LocaleCompare(entry->d_name,
"..") == 0))
1692 if ((IsPathDirectory(entry->d_name) > 0) ||
1693#
if defined(MAGICKCORE_WINDOWS_SUPPORT)
1694 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1696 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1699 if (*number_entries >= max_entries)
1705 filelist=(
char **) ResizeQuantumMemory(filelist,(
size_t)
1706 max_entries,
sizeof(*filelist));
1707 if (filelist == (
char **) NULL)
1715 p=strchr(entry->d_name,
';');
1718 if (*number_entries > 0)
1719 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1723 filelist[*number_entries]=(
char *) AcquireString(entry->d_name);
1724 (*number_entries)++;
1727 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
1728 (void) closedir(current_directory);
1729 if (filelist == (
char **) NULL)
1730 return((
char **) NULL);
1734 qsort((
void *) filelist,(
size_t) *number_entries,
sizeof(*filelist),
1763MagickExport
void MagickDelay(
const MagickSizeType milliseconds)
1765 if (milliseconds == 0)
1767#if defined(MAGICKCORE_HAVE_NANOSLEEP)
1772 timer.tv_sec=(time_t) (milliseconds/1000);
1773 timer.tv_nsec=(milliseconds % 1000)*1000*1000;
1774 (void) nanosleep(&timer,(
struct timespec *) NULL);
1776#elif defined(MAGICKCORE_HAVE_USLEEP)
1777 usleep(1000*milliseconds);
1778#elif defined(MAGICKCORE_HAVE_SELECT)
1783 timer.tv_sec=(long) milliseconds/1000;
1784 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1785 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1787#elif defined(MAGICKCORE_HAVE_POLL)
1788 (void) poll((
struct pollfd *) NULL,0,(
int) milliseconds);
1789#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1790 Sleep((
long) milliseconds);
1796 timer=milliseconds/1000.0;
1799#elif defined(__BEOS__)
1800 snooze(1000*milliseconds);
1806 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1807 while (clock() < time_end)
1838MagickExport
size_t MultilineCensus(
const char *label)
1846 if (label == (
char *) NULL)
1848 for (number_lines=1; *label !=
'\0'; label++)
1851 return(number_lines);
1878MagickPrivate MagickBooleanType ShredFile(
const char *path)
1905 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1906 return(MagickFalse);
1913 property=GetEnvironmentValue(
"MAGICK_SHRED_PASSES");
1914 if (property != (
char *) NULL)
1916 passes=(ssize_t) StringToInteger(property);
1917 property=DestroyString(property);
1919 property=GetPolicyValue(
"system:shred");
1920 if (property != (
char *) NULL)
1922 passes=(ssize_t) StringToInteger(property);
1923 property=DestroyString(property);
1928 file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
1930 return(MagickFalse);
1934 quantum=(size_t) MagickMinBufferExtent;
1935 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1936 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
1937 length=(MagickSizeType) file_stats.st_size;
1938 random_info=AcquireRandomInfo();
1939 key=GetRandomKey(random_info,quantum);
1940 for (i=0; i < passes; i++)
1948 if (lseek(file,0,SEEK_SET) < 0)
1950 for (j=0; j < (MagickOffsetType) length; j+=count)
1953 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
1954 count=write(file,GetStringInfoDatum(key),(
size_t)
1955 MagickMin((MagickSizeType) quantum,length-j));
1963 if (j < (MagickOffsetType) length)
1966 key=DestroyStringInfo(key);
1967 random_info=DestroyRandomInfo(random_info);
1969 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);