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 *);
109 MagickExport MagickBooleanType AcquireUniqueFilename(
char *path)
114 file=AcquireUniqueFileResource(path);
151 MagickExport MagickBooleanType AcquireUniqueSymbolicLink(
const char *source,
174 assert(source != (
const char *) NULL);
175 assert(destination != (
char *) NULL);
176 #if defined(MAGICKCORE_HAVE_SYMLINK)
181 (void) AcquireUniqueFilename(destination);
182 (void) RelinquishUniqueFileResource(destination);
183 passes=GetPolicyValue(
"system:shred");
184 if (passes != (
char *) NULL)
185 passes=DestroyString(passes);
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);
279 MagickExport
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);
344 MagickExport
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);
500 MagickExport
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+=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))];
584 MagickExport
void ChopPathComponents(
char *path,
const size_t components)
589 for (i=0; i < (ssize_t) components; i++)
590 GetPathComponent(path,HeadPath,path);
616 MagickExport
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);
747 MagickExport MagickBooleanType ExpandFilenames(
int *number_arguments,
751 home_directory[MaxTextExtent],
768 assert(number_arguments != (
int *) NULL);
769 assert(arguments != (
char ***) NULL);
770 if (IsEventLogging() != MagickFalse)
771 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
772 vector=(
char **) AcquireQuantumMemory((
size_t) (*number_arguments+1),
774 if (vector == (
char **) NULL)
775 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
779 *home_directory=
'\0';
781 for (i=0; i < (ssize_t) *number_arguments; i++)
785 filename[MaxTextExtent],
786 magick[MaxTextExtent],
789 subimage[MaxTextExtent];
794 option=(*arguments)[i];
800 vector[count++]=ConstantString(option);
802 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
808 for (j=0; j < parameters; j++)
811 if (i == (ssize_t) *number_arguments)
813 option=(*arguments)[i];
814 vector[count++]=ConstantString(option);
818 if ((*option ==
'"') || (*option ==
'\''))
820 GetPathComponent(option,TailPath,filename);
821 GetPathComponent(option,MagickPath,magick);
822 if ((LocaleCompare(magick,
"CAPTION") == 0) ||
823 (LocaleCompare(magick,
"LABEL") == 0) ||
824 (LocaleCompare(magick,
"PANGO") == 0) ||
825 (LocaleCompare(magick,
"VID") == 0))
827 if ((IsGlob(filename) == MagickFalse) && (*option !=
'@'))
829 if ((*option !=
'@') && (IsPathAccessible(option) == MagickFalse))
834 GetPathComponent(option,HeadPath,path);
835 GetPathComponent(option,SubimagePath,subimage);
836 ExpandFilename(path);
837 if (*home_directory ==
'\0')
838 getcwd_utf8(home_directory,MaxTextExtent-1);
839 filelist=ListFiles(*path ==
'\0' ? home_directory : path,filename,
856 exception=AcquireExceptionInfo();
857 files=FileToString(option+1,~0UL,exception);
858 exception=DestroyExceptionInfo(exception);
859 if (files == (
char *) NULL)
861 filelist=StringToArgv(files,&length);
862 if (filelist == (
char **) NULL)
864 files=DestroyString(files);
865 filelist[0]=DestroyString(filelist[0]);
866 for (j=0; j < (ssize_t) (length-1); j++)
867 filelist[j]=filelist[j+1];
868 number_files=(size_t) length-1;
870 if (filelist == (
char **) NULL)
872 for (j=0; j < (ssize_t) number_files; j++)
873 if (IsPathDirectory(filelist[j]) <= 0)
875 if (j == (ssize_t) number_files)
877 for (j=0; j < (ssize_t) number_files; j++)
878 filelist[j]=DestroyString(filelist[j]);
879 filelist=(
char **) RelinquishMagickMemory(filelist);
885 vector=(
char **) ResizeQuantumMemory(vector,(
size_t) *number_arguments+
886 count+number_files+1,
sizeof(*vector));
887 if (vector == (
char **) NULL)
889 for (j=0; j < (ssize_t) number_files; j++)
890 filelist[j]=DestroyString(filelist[j]);
891 filelist=(
char **) RelinquishMagickMemory(filelist);
894 for (j=0; j < (ssize_t) number_files; j++)
897 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
906 vector[count++]=ConstantString(option);
907 for (k=0; k < parameters; k++)
910 if (j == (ssize_t) number_files)
913 vector[count++]=ConstantString(option);
917 (void) CopyMagickString(filename,path,MaxTextExtent);
919 (void) ConcatenateMagickString(filename,DirectorySeparator,
921 if (filelist[j] != (
char *) NULL)
922 (
void) ConcatenateMagickString(filename,filelist[j],MaxTextExtent);
923 filelist[j]=DestroyString(filelist[j]);
924 if (strlen(filename) >= (MaxTextExtent-1))
925 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
926 if (IsPathDirectory(filename) <= 0)
934 (void) ConcatenateMagickString(path,magick,MaxTextExtent);
935 (void) ConcatenateMagickString(path,
":",MaxTextExtent);
937 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
938 if (*subimage !=
'\0')
940 (void) ConcatenateMagickString(path,
"[",MaxTextExtent);
941 (void) ConcatenateMagickString(path,subimage,MaxTextExtent);
942 (void) ConcatenateMagickString(path,
"]",MaxTextExtent);
944 if (strlen(path) >= (MaxTextExtent-1))
945 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
946 if (destroy != MagickFalse)
949 vector[count]=DestroyString(vector[count]);
952 vector[count++]=ConstantString(path);
955 filelist=(
char **) RelinquishMagickMemory(filelist);
957 vector[count]=(
char *) NULL;
958 if (IsEventLogging() != MagickFalse)
963 command_line=AcquireString(vector[0]);
964 for (i=1; i < count; i++)
966 (void) ConcatenateString(&command_line,
" {");
967 (void) ConcatenateString(&command_line,vector[i]);
968 (void) ConcatenateString(&command_line,
"}");
970 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
971 "Command line: %s",command_line);
972 command_line=DestroyString(command_line);
974 *number_arguments=(int) count;
1004 MagickExport MagickBooleanType GetExecutionPath(
char *path,
const size_t extent)
1010 directory=getcwd(path,(
unsigned long) extent);
1012 #if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
1015 link_path[MaxTextExtent],
1016 execution_path[PATH_MAX+1];
1021 (void) FormatLocaleString(link_path,MaxTextExtent,
"/proc/%.20g/exe",
1023 count=readlink(link_path,execution_path,PATH_MAX);
1026 (void) FormatLocaleString(link_path,MaxTextExtent,
"/proc/%.20g/file",
1028 count=readlink(link_path,execution_path,PATH_MAX);
1030 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
1032 execution_path[count]=
'\0';
1033 (void) CopyMagickString(path,execution_path,extent);
1037 #if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
1040 executable_path[PATH_MAX << 1],
1041 execution_path[PATH_MAX+1];
1046 length=
sizeof(executable_path);
1047 if ((_NSGetExecutablePath(executable_path,&length) == 0) &&
1048 (realpath(executable_path,execution_path) != (
char *) NULL))
1049 (
void) CopyMagickString(path,execution_path,extent);
1052 #if defined(MAGICKCORE_HAVE_GETEXECNAME)
1057 execution_path=(
const char *) getexecname();
1058 if (execution_path != (
const char *) NULL)
1060 if (*execution_path != *DirectorySeparator)
1061 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1062 (void) ConcatenateMagickString(path,execution_path,extent);
1066 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1067 NTGetExecutionPath(path,extent);
1069 #if defined(__GNU__)
1078 program_name=program_invocation_name;
1079 if (*program_invocation_name !=
'/')
1084 extent=strlen(directory)+strlen(program_name)+2;
1085 program_name=AcquireQuantumMemory(extent,
sizeof(*program_name));
1086 if (program_name == (
char *) NULL)
1087 program_name=program_invocation_name;
1089 count=FormatLocaleString(program_name,extent,
"%s/%s",directory,
1090 program_invocation_name);
1095 execution_path[PATH_MAX+1];
1097 if (realpath(program_name,execution_path) != (
char *) NULL)
1098 (void) CopyMagickString(path,execution_path,extent);
1100 if (program_name != program_invocation_name)
1101 program_name=(
char *) RelinquishMagickMemory(program_name);
1104 #if defined(__OpenBSD__)
1109 (void) CopyMagickString(path,__progname,extent);
1112 return(IsPathAccessible(path));
1133 MagickExport ssize_t GetMagickPageSize(
void)
1140 #if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1141 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1142 #elif defined(MAGICKCORE_HAVE_GETPAGESIZE)
1143 page_size=(ssize_t) getpagesize();
1174 MagickExport MagickBooleanType GetPathAttributes(
const char *path,
1180 if (path == (
const char *) NULL)
1183 return(MagickFalse);
1185 (void) memset(attributes,0,
sizeof(
struct stat));
1186 status=stat_utf8(path,(
struct stat *) attributes) == 0 ? MagickTrue :
1222 MagickExport
void GetPathComponent(
const char *path,PathType type,
1236 assert(path != (
const char *) NULL);
1237 assert(component != (
char *) NULL);
1238 if (IsEventLogging() != MagickFalse)
1239 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",path);
1245 (void) CopyMagickString(component,path,MagickPathExtent);
1248 if (type != SubcanonicalPath)
1250 p=component+strlen(component)-1;
1251 if ((strlen(component) > 2) && (*p ==
']'))
1253 q=strrchr(component,
'[');
1254 if ((q != (
char *) NULL) && ((q == component) || (*(q-1) !=
']')) &&
1255 (IsPathAccessible(path) == MagickFalse))
1261 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1262 (IsGeometry(q+1) == MagickFalse))
1266 subimage_length=(size_t) (p-q);
1267 subimage_offset=(size_t) (q-component+1);
1274 #if defined(__OS2__)
1277 for (p=component; *p !=
'\0'; p++)
1279 if ((*p ==
'%') && (*(p+1) ==
'['))
1284 for (p++; (*p !=
']') && (*p !=
'\0'); p++) ;
1288 if ((p != component) && (*p ==
':') && (IsPathDirectory(component) < 0) &&
1289 (IsPathAccessible(component) == MagickFalse))
1295 if (IsMagickConflict(component) != MagickFalse)
1299 magick_length=(size_t) (p-component+1);
1300 for (q=component; *(++p) !=
'\0'; q++)
1309 for (p=component+strlen(component)-1; p > component; p--)
1310 if (IsBasenameSeparator(*p) != MagickFalse)
1316 if (magick_length != 0)
1317 (void) CopyMagickString(component,path,magick_length);
1324 if (*component !=
'\0')
1326 for (p=component+(strlen(component)-1); p > component; p--)
1328 if (IsBasenameSeparator(*p) != MagickFalse)
1345 if (IsBasenameSeparator(*p) != MagickFalse)
1346 (
void) CopyMagickString(component,p+1,MagickPathExtent);
1351 if (IsBasenameSeparator(*p) != MagickFalse)
1352 (void) CopyMagickString(component,p+1,MagickPathExtent);
1353 if (*component !=
'\0')
1354 for (p=component+(strlen(component)-1); p > component; p--)
1362 case BasePathSansCompressExtension:
1365 extension[MagickPathExtent];
1370 GetPathComponent(path,ExtensionPath,extension);
1371 if ((LocaleCompare(extension,
"bz2") == 0) ||
1372 (LocaleCompare(extension,
"gz") == 0) ||
1373 (LocaleCompare(extension,
"svgz") == 0) ||
1374 (LocaleCompare(extension,
"wmz") == 0) ||
1375 (LocaleCompare(extension,
"Z") == 0))
1376 GetPathComponent(path,BasePath,component);
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--)
1389 (void) CopyMagickString(component,p+1,MagickPathExtent);
1395 if ((subimage_length != 0) && (magick_length < subimage_offset))
1396 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1399 case SubcanonicalPath:
1431 MagickExport
char **GetPathComponents(
const char *path,
1432 size_t *number_components)
1444 if (path == (
char *) NULL)
1445 return((
char **) NULL);
1446 *number_components=1;
1447 for (p=path; *p !=
'\0'; p++)
1448 if (IsBasenameSeparator(*p))
1449 (*number_components)++;
1450 components=(
char **) AcquireQuantumMemory((
size_t) *number_components+1UL,
1451 sizeof(*components));
1452 if (components == (
char **) NULL)
1453 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1455 for (i=0; i < (ssize_t) *number_components; i++)
1457 for (q=p; *q !=
'\0'; q++)
1458 if (IsBasenameSeparator(*q))
1460 components[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MaxTextExtent,
1461 sizeof(**components));
1462 if (components[i] == (
char *) NULL)
1463 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1464 (void) CopyMagickString(components[i],p,(
size_t) (q-p+1));
1467 components[i]=(
char *) NULL;
1494 MagickExport MagickBooleanType IsPathAccessible(
const char *path)
1502 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1503 return(MagickFalse);
1504 if (LocaleCompare(path,
"-") == 0)
1506 status=GetPathAttributes(path,&attributes);
1507 if (status == MagickFalse)
1509 if (S_ISREG(attributes.st_mode) == 0)
1510 return(MagickFalse);
1511 if (access_utf8(path,F_OK) != 0)
1512 return(MagickFalse);
1539 static int IsPathDirectory(
const char *path)
1547 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1548 return(MagickFalse);
1549 status=GetPathAttributes(path,&attributes);
1550 if (status == MagickFalse)
1552 if (S_ISDIR(attributes.st_mode) == 0)
1592 #if defined(__cplusplus) || defined(c_plusplus)
1596 static int FileCompare(
const void *x,
const void *y)
1602 p=(
const char **) x;
1603 q=(
const char **) y;
1604 return(LocaleCompare(*p,*q));
1607 #if defined(__cplusplus) || defined(c_plusplus)
1611 MagickExport
char **ListFiles(
const char *directory,
const char *pattern,
1612 size_t *number_entries)
1630 assert(directory != (
const char *) NULL);
1631 assert(pattern != (
const char *) NULL);
1632 assert(number_entries != (
size_t *) NULL);
1633 if (IsEventLogging() != MagickFalse)
1634 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",directory);
1636 current_directory=opendir(directory);
1637 if (current_directory == (
DIR *) NULL)
1638 return((
char **) NULL);
1643 filelist=(
char **) AcquireQuantumMemory((
size_t) max_entries,
1645 if (filelist == (
char **) NULL)
1647 (void) closedir(current_directory);
1648 return((
char **) NULL);
1653 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
1654 if (buffer == (
struct dirent *) NULL)
1655 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1656 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1657 (entry != (
struct dirent *) NULL))
1659 if ((LocaleCompare(entry->d_name,
".") == 0) ||
1660 (LocaleCompare(entry->d_name,
"..") == 0))
1662 if ((IsPathDirectory(entry->d_name) > 0) ||
1663 #
if defined(MAGICKCORE_WINDOWS_SUPPORT)
1664 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1666 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1669 if (*number_entries >= max_entries)
1675 filelist=(
char **) ResizeQuantumMemory(filelist,(
size_t)
1676 max_entries,
sizeof(*filelist));
1677 if (filelist == (
char **) NULL)
1685 p=strchr(entry->d_name,
';');
1688 if (*number_entries > 0)
1689 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1693 filelist[*number_entries]=(
char *) AcquireString(entry->d_name);
1694 (*number_entries)++;
1697 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
1698 (void) closedir(current_directory);
1699 if (filelist == (
char **) NULL)
1700 return((
char **) NULL);
1704 qsort((
void *) filelist,(
size_t) *number_entries,
sizeof(*filelist),
1733 MagickExport
void MagickDelay(
const MagickSizeType milliseconds)
1735 if (milliseconds == 0)
1737 #if defined(MAGICKCORE_HAVE_NANOSLEEP)
1742 timer.tv_sec=(time_t) (milliseconds/1000);
1743 timer.tv_nsec=(milliseconds % 1000)*1000*1000;
1744 (void) nanosleep(&timer,(
struct timespec *) NULL);
1746 #elif defined(MAGICKCORE_HAVE_USLEEP)
1747 usleep(1000*milliseconds);
1748 #elif defined(MAGICKCORE_HAVE_SELECT)
1753 timer.tv_sec=(long) milliseconds/1000;
1754 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1755 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1757 #elif defined(MAGICKCORE_HAVE_POLL)
1758 (void) poll((
struct pollfd *) NULL,0,(int) milliseconds);
1759 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1760 Sleep((
long) milliseconds);
1766 timer=milliseconds/1000.0;
1769 #elif defined(__BEOS__)
1770 snooze(1000*milliseconds);
1776 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1777 while (clock() < time_end)
1808 MagickExport
size_t MultilineCensus(
const char *label)
1816 if (label == (
char *) NULL)
1818 for (number_lines=1; *label !=
'\0'; label++)
1821 return(number_lines);
1848 MagickPrivate MagickBooleanType ShredFile(
const char *path)
1875 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1876 return(MagickFalse);
1883 property=GetEnvironmentValue(
"MAGICK_SHRED_PASSES");
1884 if (property != (
char *) NULL)
1886 passes=(ssize_t) StringToInteger(property);
1887 property=DestroyString(property);
1889 property=GetPolicyValue(
"system:shred");
1890 if (property != (
char *) NULL)
1892 passes=(ssize_t) StringToInteger(property);
1893 property=DestroyString(property);
1898 file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
1900 return(MagickFalse);
1904 quantum=(size_t) MagickMinBufferExtent;
1905 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1906 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
1907 length=(MagickSizeType) file_stats.st_size;
1908 random_info=AcquireRandomInfo();
1909 key=GetRandomKey(random_info,quantum);
1910 for (i=0; i < passes; i++)
1918 if (lseek(file,0,SEEK_SET) < 0)
1920 for (j=0; j < (MagickOffsetType) length; j+=count)
1923 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
1924 count=write(file,GetStringInfoDatum(key),(
size_t)
1925 MagickMin((MagickSizeType) quantum,length-j));
1933 if (j < (MagickOffsetType) length)
1936 key=DestroyStringInfo(key);
1937 random_info=DestroyRandomInfo(random_info);
1939 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);