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);
117 file=close_utf8(file)-1;
151MagickExport MagickBooleanType AcquireUniqueSymbolicLink(
const char *source,
174 assert(source != (
const char *) NULL);
175 assert(destination != (
char *) NULL);
176#if defined(MAGICKCORE_HAVE_SYMLINK)
184 status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights |
185 WritePolicyRights,
"follow");
186 passes=GetPolicyValue(
"system:shred");
187 if ((passes != (
char *) NULL) || (status == MagickFalse))
188 passes=DestroyString(passes);
191 (void) AcquireUniqueFilename(destination);
192 (void) RelinquishUniqueFileResource(destination);
193 if (*source == *DirectorySeparator)
195 if (symlink(source,destination) == 0)
204 if (getcwd(path,MaxTextExtent) == (
char *) NULL)
206 (void) ConcatenateMagickString(path,DirectorySeparator,
208 (void) ConcatenateMagickString(path,source,MaxTextExtent);
209 if (symlink(path,destination) == 0)
218 destination_file=AcquireUniqueFileResource(destination);
219 if (destination_file == -1)
221 source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
222 if (source_file == -1)
224 (void) close(destination_file);
225 (void) RelinquishUniqueFileResource(destination);
228 quantum=(size_t) MagickMaxBufferExtent;
229 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
230 quantum=(size_t) MagickMin(attributes.st_size,MagickMaxBufferExtent);
231 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
232 if (buffer == (
unsigned char *) NULL)
234 (void) close(source_file);
235 (void) close(destination_file);
236 (void) RelinquishUniqueFileResource(destination);
242 count=(ssize_t) read(source_file,buffer,quantum);
245 length=(size_t) count;
246 count=(ssize_t) write(destination_file,buffer,length);
247 if ((
size_t) count != length)
249 (void) RelinquishUniqueFileResource(destination);
254 (void) close(destination_file);
255 (void) close(source_file);
256 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
287MagickExport
void AppendImageFormat(
const char *format,
char *filename)
290 extension[MaxTextExtent],
293 assert(format != (
char *) NULL);
294 assert(filename != (
char *) NULL);
295 if (IsEventLogging() != MagickFalse)
296 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
297 if ((*format ==
'\0') || (*filename ==
'\0'))
299 if (LocaleCompare(filename,
"-") == 0)
302 message[MaxTextExtent];
304 (void) FormatLocaleString(message,MaxTextExtent,
"%s:%s",format,filename);
305 (void) CopyMagickString(filename,message,MaxTextExtent);
308 GetPathComponent(filename,ExtensionPath,extension);
309 if ((LocaleCompare(extension,
"Z") == 0) ||
310 (LocaleCompare(extension,
"bz2") == 0) ||
311 (LocaleCompare(extension,
"gz") == 0) ||
312 (LocaleCompare(extension,
"wmz") == 0) ||
313 (LocaleCompare(extension,
"svgz") == 0))
315 GetPathComponent(filename,RootPath,root);
316 (void) CopyMagickString(filename,root,MaxTextExtent);
317 GetPathComponent(filename,RootPath,root);
318 (void) FormatLocaleString(filename,MaxTextExtent,
"%s.%s.%s",root,format,
322 GetPathComponent(filename,RootPath,root);
323 (void) FormatLocaleString(filename,MaxTextExtent,
"%s.%s",root,format);
352MagickExport
unsigned char *Base64Decode(
const char *source,
size_t *length)
367 assert(source != (
char *) NULL);
368 assert(length != (
size_t *) NULL);
369 if (IsEventLogging() != MagickFalse)
370 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
372 decode=(
unsigned char *) AcquireQuantumMemory((strlen(source)+3)/4,
374 if (decode == (
unsigned char *) NULL)
375 return((
unsigned char *) NULL);
378 for (p=source; *p !=
'\0'; p++)
380 if (isspace((
int) ((
unsigned char) *p)) != 0)
385 if (q == (
char *) NULL)
387 decode=(
unsigned char *) RelinquishMagickMemory(decode);
388 return((
unsigned char *) NULL);
394 decode[i]=(q-Base64) << 2;
400 decode[i++]|=(q-Base64) >> 4;
401 decode[i]=((q-Base64) & 0x0f) << 4;
407 decode[i++]|=(q-Base64) >> 2;
408 decode[i]=((q-Base64) & 0x03) << 6;
414 decode[i++]|=(q-Base64);
427 decode=(
unsigned char *) RelinquishMagickMemory(decode);
428 return((
unsigned char *) NULL);
442 decode=(
unsigned char *) RelinquishMagickMemory(decode);
443 return((
unsigned char *) NULL);
447 for ( ; *p !=
'\0'; p++)
448 if (isspace((
int) ((
unsigned char) *p)) == 0)
452 decode=(
unsigned char *) RelinquishMagickMemory(decode);
453 return((
unsigned char *) NULL);
459 for ( ; *p !=
'\0'; p++)
460 if (isspace((
int) ((
unsigned char) *p)) == 0)
462 decode=(
unsigned char *) RelinquishMagickMemory(decode);
463 return((
unsigned char *) NULL);
465 if ((
int) decode[i] != 0)
467 decode=(
unsigned char *) RelinquishMagickMemory(decode);
468 return((
unsigned char *) NULL);
508MagickExport
char *Base64Encode(
const unsigned char *blob,
509 const size_t blob_length,
size_t *encode_length)
523 assert(blob != (
const unsigned char *) NULL);
524 assert(blob_length != 0);
525 assert(encode_length != (
size_t *) NULL);
526 if (IsEventLogging() != MagickFalse)
527 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
529 encode=(
char *) AcquireQuantumMemory(blob_length/3+4,4*
sizeof(*encode));
530 if (encode == (
char *) NULL)
531 return((
char *) NULL);
533 for (p=blob; p < (blob+blob_length-2); p+=(ptrdiff_t) 3)
535 encode[i++]=Base64[(int) (*p >> 2)];
536 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
537 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
538 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
540 remainder=blob_length % 3;
552 for (j=0; j < (ssize_t) remainder; j++)
554 encode[i++]=Base64[(int) (code[0] >> 2)];
555 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
559 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
592MagickExport
void ChopPathComponents(
char *path,
const size_t components)
597 for (i=0; i < (ssize_t) components; i++)
598 GetPathComponent(path,HeadPath,path);
624MagickExport
void ExpandFilename(
char *path)
627 expand_path[MaxTextExtent];
629 if (path == (
char *) NULL)
633 (void) CopyMagickString(expand_path,path,MaxTextExtent);
634 if ((*(path+1) == *DirectorySeparator) || (*(path+1) ==
'\0'))
642 (void) CopyMagickString(expand_path,
".",MaxTextExtent);
643 (void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
644 home=GetEnvironmentValue(
"HOME");
645 if (home == (
char *) NULL)
646 home=GetEnvironmentValue(
"USERPROFILE");
647 if (home != (
char *) NULL)
649 (void) CopyMagickString(expand_path,home,MaxTextExtent);
650 (void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
651 home=DestroyString(home);
656#if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
658#if defined(MAGICKCORE_HAVE_GETPWNAM_R)
659 buffer[MagickPathExtent],
661 username[MaxTextExtent];
672 (void) CopyMagickString(username,path+1,MaxTextExtent);
673 p=strchr(username,
'/');
674 if (p != (
char *) NULL)
676#if !defined(MAGICKCORE_HAVE_GETPWNAM_R)
677 entry=getpwnam(username);
682 entry=(
struct passwd *) NULL;
683 if (getpwnam_r(username,&pwd,buffer,
sizeof(buffer),&entry) < 0)
686 if (entry == (
struct passwd *) NULL)
688 (void) CopyMagickString(expand_path,entry->pw_dir,MaxTextExtent);
689 if (p != (
char *) NULL)
691 (void) ConcatenateMagickString(expand_path,
"/",MaxTextExtent);
692 (void) ConcatenateMagickString(expand_path,p+1,MaxTextExtent);
696 (void) CopyMagickString(path,expand_path,MaxTextExtent);
755static inline void getcwd_utf8(
char *path,
size_t extent)
757#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
761 directory=getcwd(path,extent);
765 wide_path[MaxTextExtent];
767 (void) _wgetcwd(wide_path,MaxTextExtent-1);
768 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
772MagickExport MagickBooleanType ExpandFilenames(
int *number_arguments,
776 home_directory[MaxTextExtent],
793 assert(number_arguments != (
int *) NULL);
794 assert(arguments != (
char ***) NULL);
795 if (IsEventLogging() != MagickFalse)
796 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
797 vector=(
char **) AcquireQuantumMemory((
size_t) (*number_arguments+1),
799 if (vector == (
char **) NULL)
800 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
804 *home_directory=
'\0';
806 for (i=0; i < (ssize_t) *number_arguments; i++)
810 filename[MaxTextExtent],
811 magick[MaxTextExtent],
814 subimage[MaxTextExtent];
819 option=(*arguments)[i];
825 vector[count++]=ConstantString(option);
827 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
833 for (j=0; j < parameters; j++)
836 if (i == (ssize_t) *number_arguments)
838 option=(*arguments)[i];
839 vector[count++]=ConstantString(option);
843 if ((*option ==
'"') || (*option ==
'\''))
845 GetPathComponent(option,TailPath,filename);
846 GetPathComponent(option,MagickPath,magick);
847 if ((LocaleCompare(magick,
"CAPTION") == 0) ||
848 (LocaleCompare(magick,
"LABEL") == 0) ||
849 (LocaleCompare(magick,
"PANGO") == 0) ||
850 (LocaleCompare(magick,
"VID") == 0))
852 if ((IsGlob(filename) == MagickFalse) && (*option !=
'@'))
854 if (IsPathAccessible(option) != MagickFalse)
861 GetPathComponent(option,HeadPath,path);
862 GetPathComponent(option,SubimagePath,subimage);
863 ExpandFilename(path);
864 if (*home_directory ==
'\0')
865 getcwd_utf8(home_directory,MaxTextExtent-1);
866 filelist=ListFiles(*path ==
'\0' ? home_directory : path,filename,
883 exception=AcquireExceptionInfo();
884 files=FileToString(option,~0UL,exception);
885 exception=DestroyExceptionInfo(exception);
886 if (files == (
char *) NULL)
888 filelist=StringToArgv(files,&length);
889 if (filelist == (
char **) NULL)
891 files=DestroyString(files);
892 filelist[0]=DestroyString(filelist[0]);
893 for (j=0; j < (ssize_t) (length-1); j++)
894 filelist[j]=filelist[j+1];
895 number_files=(size_t) length-1;
897 if (filelist == (
char **) NULL)
899 for (j=0; j < (ssize_t) number_files; j++)
900 if (IsPathDirectory(filelist[j]) <= 0)
902 if (j == (ssize_t) number_files)
904 for (j=0; j < (ssize_t) number_files; j++)
905 filelist[j]=DestroyString(filelist[j]);
906 filelist=(
char **) RelinquishMagickMemory(filelist);
912 vector=(
char **) ResizeQuantumMemory(vector,(
size_t) *number_arguments+
913 count+number_files+1,
sizeof(*vector));
914 if (vector == (
char **) NULL)
916 for (j=0; j < (ssize_t) number_files; j++)
917 filelist[j]=DestroyString(filelist[j]);
918 filelist=(
char **) RelinquishMagickMemory(filelist);
921 for (j=0; j < (ssize_t) number_files; j++)
924 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
933 vector[count++]=ConstantString(option);
934 for (k=0; k < parameters; k++)
937 if (j == (ssize_t) number_files)
940 vector[count++]=ConstantString(option);
944 (void) CopyMagickString(filename,path,MaxTextExtent);
946 (void) ConcatenateMagickString(filename,DirectorySeparator,
948 if (filelist[j] != (
char *) NULL)
949 (void) ConcatenateMagickString(filename,filelist[j],MaxTextExtent);
950 filelist[j]=DestroyString(filelist[j]);
951 if (strlen(filename) >= (MaxTextExtent-1))
952 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
953 if (IsPathDirectory(filename) <= 0)
961 (void) ConcatenateMagickString(path,magick,MaxTextExtent);
962 (void) ConcatenateMagickString(path,
":",MaxTextExtent);
964 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
965 if (*subimage !=
'\0')
967 (void) ConcatenateMagickString(path,
"[",MaxTextExtent);
968 (void) ConcatenateMagickString(path,subimage,MaxTextExtent);
969 (void) ConcatenateMagickString(path,
"]",MaxTextExtent);
971 if (strlen(path) >= (MaxTextExtent-1))
972 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
973 if (destroy != MagickFalse)
976 vector[count]=DestroyString(vector[count]);
979 vector[count++]=ConstantString(path);
982 filelist=(
char **) RelinquishMagickMemory(filelist);
984 vector[count]=(
char *) NULL;
985 if (IsEventLogging() != MagickFalse)
990 command_line=AcquireString(vector[0]);
991 for (i=1; i < count; i++)
993 (void) ConcatenateString(&command_line,
" {");
994 (void) ConcatenateString(&command_line,vector[i]);
995 (void) ConcatenateString(&command_line,
"}");
997 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
998 "Command line: %s",command_line);
999 command_line=DestroyString(command_line);
1001 *number_arguments=(int) count;
1031MagickExport MagickBooleanType GetExecutionPath(
char *path,
const size_t extent)
1037 directory=getcwd(path,(
unsigned long) extent);
1039#if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
1042 link_path[MaxTextExtent],
1043 execution_path[PATH_MAX+1];
1048 (void) FormatLocaleString(link_path,MaxTextExtent,
"/proc/%.20g/exe",
1050 count=readlink(link_path,execution_path,PATH_MAX);
1053 (void) FormatLocaleString(link_path,MaxTextExtent,
"/proc/%.20g/file",
1055 count=readlink(link_path,execution_path,PATH_MAX);
1057 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
1059 execution_path[count]=
'\0';
1060 (void) CopyMagickString(path,execution_path,extent);
1064#if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
1067 executable_path[PATH_MAX << 1];
1072 length=
sizeof(executable_path);
1073 if (_NSGetExecutablePath(executable_path,&length) == 0)
1076 *real_path = realpath_utf8(executable_path);
1078 if (real_path != (
char *) NULL)
1080 (void) CopyMagickString(path,real_path,extent);
1081 real_path=DestroyString(real_path);
1086#if defined(MAGICKCORE_HAVE_GETEXECNAME)
1091 execution_path=(
const char *) getexecname();
1092 if (execution_path != (
const char *) NULL)
1094 if (*execution_path != *DirectorySeparator)
1095 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1096 (void) ConcatenateMagickString(path,execution_path,extent);
1100#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1101 NTGetExecutionPath(path,extent);
1112 program_name=program_invocation_name;
1113 if (*program_invocation_name !=
'/')
1118 extent=strlen(directory)+strlen(program_name)+2;
1119 program_name=AcquireQuantumMemory(extent,
sizeof(*program_name));
1120 if (program_name == (
char *) NULL)
1121 program_name=program_invocation_name;
1123 count=FormatLocaleString(program_name,extent,
"%s/%s",directory,
1124 program_invocation_name);
1129 *real_path = realpath_utf8(program_name);
1131 if (real_path != (
char *) NULL)
1133 (void) CopyMagickString(path,real_path,extent);
1134 real_path=DestroyString(real_path);
1137 if (program_name != program_invocation_name)
1138 program_name=(
char *) RelinquishMagickMemory(program_name);
1141#if defined(__OpenBSD__)
1146 (void) CopyMagickString(path,__progname,extent);
1149 return(IsPathAccessible(path));
1170MagickExport ssize_t GetMagickPageSize(
void)
1177#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1178 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1179#elif defined(MAGICKCORE_HAVE_GETPAGESIZE)
1180 page_size=(ssize_t) getpagesize();
1211MagickExport MagickBooleanType GetPathAttributes(
const char *path,
1217 if (path == (
const char *) NULL)
1220 return(MagickFalse);
1222 (void) memset(attributes,0,
sizeof(
struct stat));
1223 status=stat_utf8(path,(
struct stat *) attributes) == 0 ? MagickTrue :
1259MagickExport
void GetPathComponent(
const char *path,PathType type,
1273 assert(path != (
const char *) NULL);
1274 assert(component != (
char *) NULL);
1275 if (IsEventLogging() != MagickFalse)
1276 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",path);
1282 (void) CopyMagickString(component,path,MagickPathExtent);
1285 if (type != SubcanonicalPath)
1287 p=component+strlen(component)-1;
1288 if ((strlen(component) > 2) && (*p ==
']'))
1290 q=strrchr(component,
'[');
1291 if ((q != (
char *) NULL) && ((q == component) || (*(q-1) !=
']')) &&
1292 (IsPathAccessible(path) == MagickFalse))
1298 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1299 (IsGeometry(q+1) == MagickFalse))
1303 subimage_length=(size_t) (p-q);
1304 subimage_offset=(size_t) (q-component+1);
1314 for (p=component; *p !=
'\0'; p++)
1316 if ((*p ==
'%') && (*(p+1) ==
'['))
1321 for (p++; (*p !=
']') && (*p !=
'\0'); p++) ;
1325 if ((p != component) && (*p ==
':') && (IsPathDirectory(component) < 0) &&
1326 (IsPathAccessible(component) == MagickFalse))
1332 if (IsMagickConflict(component) != MagickFalse)
1336 magick_length=(size_t) (p-component+1);
1337 for (q=component; *(++p) !=
'\0'; q++)
1346 for (p=component+strlen(component)-1; p > component; p--)
1347 if (IsBasenameSeparator(*p) != MagickFalse)
1353 if (magick_length != 0)
1354 (void) CopyMagickString(component,path,magick_length);
1361 if (*component !=
'\0')
1363 for (p=component+(strlen(component)-1); p > component; p--)
1365 if (IsBasenameSeparator(*p) != MagickFalse)
1383 if (IsBasenameSeparator(*p) != MagickFalse)
1384 (void) CopyMagickString(component,p+1,MagickPathExtent);
1389 if (IsBasenameSeparator(*p) != MagickFalse)
1390 (void) CopyMagickString(component,p+1,MagickPathExtent);
1391 if (*component !=
'\0')
1392 for (p=component+(strlen(component)-1); p > component; p--)
1400 case BasePathSansCompressExtension:
1403 extension[MagickPathExtent];
1408 GetPathComponent(path,ExtensionPath,extension);
1409 if ((LocaleCompare(extension,
"bz2") == 0) ||
1410 (LocaleCompare(extension,
"gz") == 0) ||
1411 (LocaleCompare(extension,
"svgz") == 0) ||
1412 (LocaleCompare(extension,
"wmz") == 0) ||
1413 (LocaleCompare(extension,
"Z") == 0))
1414 GetPathComponent(path,BasePath,component);
1419 if (IsBasenameSeparator(*p) != MagickFalse)
1420 (void) CopyMagickString(component,p+1,MagickPathExtent);
1421 if (*component !=
'\0')
1422 for (p=component+strlen(component)-1; p > component; p--)
1427 (void) CopyMagickString(component,p+1,MagickPathExtent);
1433 if ((subimage_length != 0) && (magick_length < subimage_offset))
1434 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1437 case SubcanonicalPath:
1469MagickExport
char **GetPathComponents(
const char *path,
1470 size_t *number_components)
1482 if (path == (
char *) NULL)
1483 return((
char **) NULL);
1484 *number_components=1;
1485 for (p=path; *p !=
'\0'; p++)
1486 if (IsBasenameSeparator(*p))
1487 (*number_components)++;
1488 components=(
char **) AcquireQuantumMemory((
size_t) *number_components+1UL,
1489 sizeof(*components));
1490 if (components == (
char **) NULL)
1491 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1493 for (i=0; i < (ssize_t) *number_components; i++)
1495 for (q=p; *q !=
'\0'; q++)
1496 if (IsBasenameSeparator(*q))
1498 components[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MaxTextExtent,
1499 sizeof(**components));
1500 if (components[i] == (
char *) NULL)
1501 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1502 (void) CopyMagickString(components[i],p,(
size_t) (q-p+1));
1505 components[i]=(
char *) NULL;
1532MagickExport MagickBooleanType IsPathAccessible(
const char *path)
1540 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1541 return(MagickFalse);
1542 if (LocaleCompare(path,
"-") == 0)
1544 status=GetPathAttributes(path,&attributes);
1545 if (status == MagickFalse)
1547 if (S_ISREG(attributes.st_mode) == 0)
1548 return(MagickFalse);
1549 if (access_utf8(path,F_OK) != 0)
1550 return(MagickFalse);
1577static int IsPathDirectory(
const char *path)
1585 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1586 return(MagickFalse);
1587 status=GetPathAttributes(path,&attributes);
1588 if (status == MagickFalse)
1590 if (S_ISDIR(attributes.st_mode) == 0)
1630#if defined(__cplusplus) || defined(c_plusplus)
1634static int FileCompare(
const void *x,
const void *y)
1640 p=(
const char **) x;
1641 q=(
const char **) y;
1642 return(LocaleCompare(*p,*q));
1645#if defined(__cplusplus) || defined(c_plusplus)
1649MagickExport
char **ListFiles(
const char *directory,
const char *pattern,
1650 size_t *number_entries)
1668 assert(directory != (
const char *) NULL);
1669 assert(pattern != (
const char *) NULL);
1670 assert(number_entries != (
size_t *) NULL);
1671 if (IsEventLogging() != MagickFalse)
1672 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",directory);
1674 current_directory=opendir(directory);
1675 if (current_directory == (DIR *) NULL)
1676 return((
char **) NULL);
1681 filelist=(
char **) AcquireQuantumMemory((
size_t) max_entries,
1683 if (filelist == (
char **) NULL)
1685 (void) closedir(current_directory);
1686 return((
char **) NULL);
1691 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
1692 if (buffer == (
struct dirent *) NULL)
1693 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1694 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1695 (entry != (
struct dirent *) NULL))
1697 if ((LocaleCompare(entry->d_name,
".") == 0) ||
1698 (LocaleCompare(entry->d_name,
"..") == 0))
1700 if ((IsPathDirectory(entry->d_name) > 0) ||
1701#
if defined(MAGICKCORE_WINDOWS_SUPPORT)
1702 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1704 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1707 if (*number_entries >= max_entries)
1713 filelist=(
char **) ResizeQuantumMemory(filelist,(
size_t)
1714 max_entries,
sizeof(*filelist));
1715 if (filelist == (
char **) NULL)
1723 p=strchr(entry->d_name,
';');
1726 if (*number_entries > 0)
1727 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1731 filelist[*number_entries]=(
char *) AcquireString(entry->d_name);
1732 (*number_entries)++;
1735 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
1736 (void) closedir(current_directory);
1737 if (filelist == (
char **) NULL)
1738 return((
char **) NULL);
1742 qsort((
void *) filelist,(
size_t) *number_entries,
sizeof(*filelist),
1771MagickExport
void MagickDelay(
const MagickSizeType milliseconds)
1773 if (milliseconds == 0)
1775#if defined(MAGICKCORE_HAVE_NANOSLEEP)
1780 timer.tv_sec=(time_t) (milliseconds/1000);
1781 timer.tv_nsec=(milliseconds % 1000)*1000*1000;
1782 (void) nanosleep(&timer,(
struct timespec *) NULL);
1784#elif defined(MAGICKCORE_HAVE_USLEEP)
1785 usleep(1000*milliseconds);
1786#elif defined(MAGICKCORE_HAVE_SELECT)
1791 timer.tv_sec=(long) milliseconds/1000;
1792 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1793 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1795#elif defined(MAGICKCORE_HAVE_POLL)
1796 (void) poll((
struct pollfd *) NULL,0,(
int) milliseconds);
1797#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1798 Sleep((
long) milliseconds);
1804 timer=milliseconds/1000.0;
1807#elif defined(__BEOS__)
1808 snooze(1000*milliseconds);
1814 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1815 while (clock() < time_end)
1846MagickExport
size_t MultilineCensus(
const char *label)
1854 if (label == (
char *) NULL)
1856 for (number_lines=1; *label !=
'\0'; label++)
1859 return(number_lines);
1886MagickPrivate MagickBooleanType ShredFile(
const char *path)
1913 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1914 return(MagickFalse);
1921 property=GetEnvironmentValue(
"MAGICK_SHRED_PASSES");
1922 if (property != (
char *) NULL)
1924 passes=(ssize_t) StringToInteger(property);
1925 property=DestroyString(property);
1927 property=GetPolicyValue(
"system:shred");
1928 if (property != (
char *) NULL)
1930 passes=(ssize_t) StringToInteger(property);
1931 property=DestroyString(property);
1936 file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
1938 return(MagickFalse);
1942 quantum=(size_t) MagickMinBufferExtent;
1943 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1944 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
1945 length=(MagickSizeType) file_stats.st_size;
1946 random_info=AcquireRandomInfo();
1947 key=GetRandomKey(random_info,quantum);
1948 for (i=0; i < passes; i++)
1956 if (lseek(file,0,SEEK_SET) < 0)
1958 for (j=0; j < (MagickOffsetType) length; j+=count)
1961 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
1962 count=write(file,GetStringInfoDatum(key),(
size_t)
1963 MagickMin((MagickSizeType) quantum,length-j));
1971 if (j < (MagickOffsetType) length)
1974 key=DestroyStringInfo(key);
1975 random_info=DestroyRandomInfo(random_info);
1976 status=close_utf8(file);
1977 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);