2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % U U TTTTT IIIII L IIIII TTTTT Y Y %
10 % UUU T IIIII LLLLL IIIII T Y %
13 % MagickCore Utility Methods %
20 % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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/list.h"
50 #include "magick/log.h"
51 #include "magick/memory_.h"
52 #include "magick/option.h"
53 #include "magick/policy.h"
54 #include "magick/resource_.h"
55 #include "magick/semaphore.h"
56 #include "magick/signature-private.h"
57 #include "magick/statistic.h"
58 #include "magick/string_.h"
59 #include "magick/token.h"
60 #include "magick/utility.h"
61 #if defined(MAGICKCORE_HAVE_PROCESS_H)
64 #if defined(MAGICKCORE_HAVE_MACH_O_DYLD_H)
65 #include <mach-o/dyld.h>
72 Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
78 IsPathDirectory(const char *);
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 % A c q u i r e U n i q u e F i l e n a m e %
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 % AcquireUniqueFilename() replaces the contents of path by a unique path name.
93 % The format of the AcquireUniqueFilename method is:
95 % MagickBooleanType AcquireUniqueFilename(char *path)
97 % A description of each parameter follows.
99 % o path: Specifies a pointer to an array of characters. The unique path
100 % name is returned in this array.
103 MagickExport MagickBooleanType AcquireUniqueFilename(char *path)
108 file=AcquireUniqueFileResource(path);
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120 % A c q u i r e U n i q u e S ym b o l i c L i n k %
124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126 % AcquireUniqueSymbolicLink() creates a unique symbolic link to the specified
127 % source path and returns MagickTrue on success otherwise MagickFalse. If the
128 % symlink() method fails or is not available, a unique file name is generated
129 % and the source file copied to it. When you are finished with the file, use
130 % RelinquishUniqueFilename() to destroy it.
132 % The format of the AcquireUniqueSymbolicLink method is:
134 % MagickBooleanType AcquireUniqueSymbolicLink(const char *source,
137 % A description of each parameter follows.
139 % o source: the source path.
141 % o destination: the destination path.
145 static inline size_t MagickMin(const size_t x,const size_t y)
152 MagickExport MagickBooleanType AcquireUniqueSymbolicLink(const char *source,
172 assert(source != (const char *) NULL);
173 assert(destination != (char *) NULL);
174 #if defined(MAGICKCORE_HAVE_SYMLINK)
175 (void) AcquireUniqueFilename(destination);
176 (void) RelinquishUniqueFileResource(destination);
177 if (*source == *DirectorySeparator)
179 if (symlink(source,destination) == 0)
188 if (getcwd(path,MaxTextExtent) == (char *) NULL)
190 (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
191 (void) ConcatenateMagickString(path,source,MaxTextExtent);
192 if (symlink(path,destination) == 0)
196 destination_file=AcquireUniqueFileResource(destination);
197 if (destination_file == -1)
199 source_file=open(source,O_RDONLY | O_BINARY);
200 if (source_file == -1)
202 (void) close(destination_file);
203 (void) RelinquishUniqueFileResource(destination);
206 quantum=(size_t) MagickMaxBufferExtent;
207 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size != 0))
208 quantum=MagickMin((size_t) attributes.st_size,MagickMaxBufferExtent);
209 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
210 if (buffer == (unsigned char *) NULL)
212 (void) close(source_file);
213 (void) close(destination_file);
214 (void) RelinquishUniqueFileResource(destination);
219 count=(ssize_t) read(source_file,buffer,quantum);
222 length=(size_t) count;
223 count=(ssize_t) write(destination_file,buffer,length);
224 if ((size_t) count != length)
226 (void) close(destination_file);
227 (void) close(source_file);
228 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
229 (void) RelinquishUniqueFileResource(destination);
233 (void) close(destination_file);
234 (void) close(source_file);
235 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 % A p p e n d I m a g e F o r m a t %
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 % AppendImageFormat() appends the image format type to the filename. If an
251 % extension to the file already exists, it is first removed.
253 % The format of the AppendImageFormat method is:
255 % void AppendImageFormat(const char *format,char *filename)
257 % A description of each parameter follows.
259 % o format: Specifies a pointer to an array of characters. This the
260 % format of the image.
262 % o filename: Specifies a pointer to an array of characters. The unique
263 % file name is returned in this array.
266 MagickExport void AppendImageFormat(const char *format,char *filename)
271 assert(format != (char *) NULL);
272 assert(filename != (char *) NULL);
273 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
274 if ((*format == '\0') || (*filename == '\0'))
276 if (LocaleCompare(filename,"-") == 0)
279 message[MaxTextExtent];
281 (void) FormatMagickString(message,MaxTextExtent,"%s:%s",format,filename);
282 (void) CopyMagickString(filename,message,MaxTextExtent);
285 GetPathComponent(filename,RootPath,root);
286 (void) FormatMagickString(filename,MaxTextExtent,"%s.%s",root,format);
290 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 % B a s e 6 4 D e c o d e %
298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300 % Base64Decode() decodes Base64-encoded text and returns its binary
301 % equivalent. NULL is returned if the text is not valid Base64 data, or a
302 % memory allocation failure occurs.
304 % The format of the Base64Decode method is:
306 % unsigned char *Base64Decode(const char *source,length_t *length)
308 % A description of each parameter follows:
310 % o source: A pointer to a Base64-encoded string.
312 % o length: the number of bytes decoded.
315 MagickExport unsigned char *Base64Decode(const char *source,size_t *length)
330 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
331 assert(source != (char *) NULL);
332 assert(length != (size_t *) NULL);
334 decode=(unsigned char *) AcquireQuantumMemory(strlen(source)/4+4,
336 if (decode == (unsigned char *) NULL)
337 return((unsigned char *) NULL);
340 for (p=source; *p != '\0'; p++)
342 if (isspace((int) ((unsigned char) *p)) != 0)
347 if (q == (char *) NULL)
349 decode=(unsigned char *) RelinquishMagickMemory(decode);
350 return((unsigned char *) NULL); /* non-Base64 character */
356 decode[i]=(q-Base64) << 2;
362 decode[i++]|=(q-Base64) >> 4;
363 decode[i]=((q-Base64) & 0x0f) << 4;
369 decode[i++]|=(q-Base64) >> 2;
370 decode[i]=((q-Base64) & 0x03) << 6;
376 decode[i++]|=(q-Base64);
383 Verify Base-64 string has proper terminal characters.
389 decode=(unsigned char *) RelinquishMagickMemory(decode);
390 return((unsigned char *) NULL);
402 Unrecognized '=' character.
404 decode=(unsigned char *) RelinquishMagickMemory(decode);
405 return((unsigned char *) NULL);
409 for ( ; *p != '\0'; p++)
410 if (isspace((int) ((unsigned char) *p)) == 0)
414 decode=(unsigned char *) RelinquishMagickMemory(decode);
415 return((unsigned char *) NULL);
421 for ( ; *p != '\0'; p++)
422 if (isspace((int) ((unsigned char) *p)) == 0)
424 decode=(unsigned char *) RelinquishMagickMemory(decode);
425 return((unsigned char *) NULL);
427 if ((int) decode[i] != 0)
429 decode=(unsigned char *) RelinquishMagickMemory(decode);
430 return((unsigned char *) NULL);
440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444 % B a s e 6 4 E n c o d e %
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
450 % Base64Encode() encodes arbitrary binary data to Base64 encoded format as
451 % described by the "Base64 Content-Transfer-Encoding" section of RFC 2045 and
452 % returns the result as a null-terminated ASCII string. NULL is returned if
453 % a memory allocation failure occurs.
455 % The format of the Base64Encode method is:
457 % char *Base64Encode(const unsigned char *blob,const size_t blob_length,
458 % size_t *encode_length)
460 % A description of each parameter follows:
462 % o blob: A pointer to binary data to encode.
464 % o blob_length: the number of bytes to encode.
466 % o encode_length: The number of bytes encoded.
469 MagickExport char *Base64Encode(const unsigned char *blob,
470 const size_t blob_length,size_t *encode_length)
475 register const unsigned char
484 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
485 assert(blob != (const unsigned char *) NULL);
486 assert(blob_length != 0);
487 assert(encode_length != (size_t *) NULL);
489 encode=(char *) AcquireQuantumMemory(blob_length/3+4,4*sizeof(*encode));
490 if (encode == (char *) NULL)
491 return((char *) NULL);
493 for (p=blob; p < (blob+blob_length-2); p+=3)
495 encode[i++]=Base64[(int) (*p >> 2)];
496 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
497 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
498 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
500 remainder=blob_length % 3;
512 for (j=0; j < (ssize_t) remainder; j++)
514 encode[i++]=Base64[(int) (code[0] >> 2)];
515 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
519 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
528 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
532 % C h o p P a t h C o m p o n e n t s %
536 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
538 % ChopPathComponents() removes the number of specified file components from a
541 % The format of the ChopPathComponents method is:
543 % ChopPathComponents(char *path,size_t components)
545 % A description of each parameter follows:
549 % o components: The number of components to chop.
552 MagickExport void ChopPathComponents(char *path,const size_t components)
557 for (i=0; i < (ssize_t) components; i++)
558 GetPathComponent(path,HeadPath,path);
562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566 % E x p a n d F i l e n a m e %
570 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
572 % ExpandFilename() expands '~' in a path.
574 % The format of the ExpandFilename function is:
576 % ExpandFilename(char *path)
578 % A description of each parameter follows:
580 % o path: Specifies a pointer to a character array that contains the
584 MagickExport void ExpandFilename(char *path)
587 expand_path[MaxTextExtent];
589 if (path == (char *) NULL)
593 (void) CopyMagickString(expand_path,path,MaxTextExtent);
594 if ((*(path+1) == *DirectorySeparator) || (*(path+1) == '\0'))
600 Substitute ~ with $HOME.
602 (void) CopyMagickString(expand_path,".",MaxTextExtent);
603 (void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
604 home=GetEnvironmentValue("HOME");
605 if (home == (char *) NULL)
606 home=GetEnvironmentValue("USERPROFILE");
607 if (home != (char *) NULL)
609 (void) CopyMagickString(expand_path,home,MaxTextExtent);
610 (void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
611 home=DestroyString(home);
616 #if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
618 username[MaxTextExtent];
627 Substitute ~ with home directory from password file.
629 (void) CopyMagickString(username,path+1,MaxTextExtent);
630 p=strchr(username,'/');
631 if (p != (char *) NULL)
633 entry=getpwnam(username);
634 if (entry == (struct passwd *) NULL)
636 (void) CopyMagickString(expand_path,entry->pw_dir,MaxTextExtent);
637 if (p != (char *) NULL)
639 (void) ConcatenateMagickString(expand_path,"/",MaxTextExtent);
640 (void) ConcatenateMagickString(expand_path,p+1,MaxTextExtent);
644 (void) CopyMagickString(path,expand_path,MaxTextExtent);
648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
652 % E x p a n d F i l e n a m e s %
656 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
658 % ExpandFilenames() checks each argument of the command line vector and
659 % expands it if they have a wildcard character. For example, *.jpg might
660 % expand to: bird.jpg rose.jpg tiki.jpg.
662 % The format of the ExpandFilenames function is:
664 % status=ExpandFilenames(int *number_arguments,char ***arguments)
666 % A description of each parameter follows:
668 % o number_arguments: Specifies a pointer to an integer describing the
669 % number of elements in the argument vector.
671 % o arguments: Specifies a pointer to a text array containing the command
675 MagickExport MagickBooleanType ExpandFilenames(int *number_arguments,
680 home_directory[MaxTextExtent],
695 Allocate argument vector.
697 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
698 assert(number_arguments != (int *) NULL);
699 assert(arguments != (char ***) NULL);
700 vector=(char **) AcquireQuantumMemory((size_t) (*number_arguments+1),
702 if (vector == (char **) NULL)
703 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
705 Expand any wildcard filenames.
707 *home_directory='\0';
709 for (i=0; i < (ssize_t) *number_arguments; i++)
713 filename[MaxTextExtent],
714 magick[MaxTextExtent],
717 subimage[MaxTextExtent];
722 option=(*arguments)[i];
727 vector[count++]=ConstantString(option);
729 parameters=ParseMagickOption(MagickCommandOptions,MagickFalse,option);
733 Do not expand command option parameters.
735 for (j=0; j < parameters; j++)
738 if (i == (ssize_t) *number_arguments)
740 option=(*arguments)[i];
741 vector[count++]=ConstantString(option);
745 if ((*option == '"') || (*option == '\''))
747 GetPathComponent(option,TailPath,filename);
748 GetPathComponent(option,MagickPath,magick);
749 if ((LocaleCompare(magick,"CAPTION") == 0) ||
750 (LocaleCompare(magick,"LABEL") == 0) ||
751 (LocaleCompare(magick,"VID") == 0))
753 if ((IsGlob(filename) == MagickFalse) && (*filename != '@'))
755 if (*filename != '@')
758 Generate file list from wildcard filename (e.g. *.jpg).
760 GetPathComponent(option,HeadPath,path);
761 GetPathComponent(option,SubimagePath,subimage);
762 ExpandFilename(path);
763 if (*home_directory == '\0')
764 cwd=getcwd(home_directory,MaxTextExtent-1);
765 filelist=ListFiles(*path == '\0' ? home_directory : path,filename,
780 Generate file list from file list (e.g. @filelist.txt).
782 exception=AcquireExceptionInfo();
783 files=FileToString(filename+1,~0,exception);
784 exception=DestroyExceptionInfo(exception);
785 if (files == (char *) NULL)
788 filelist=StringToArgv(files,&number_images);
789 files=DestroyString(files);
790 number_files=(size_t) number_images;
793 if (filelist == (char **) NULL)
795 for (j=0; j < (ssize_t) number_files; j++)
796 if (IsPathDirectory(filelist[j]) <= 0)
798 if (j == (ssize_t) number_files)
800 for (j=0; j < (ssize_t) number_files; j++)
801 filelist[j]=DestroyString(filelist[j]);
802 filelist=(char **) RelinquishMagickMemory(filelist);
806 Transfer file list to argument vector.
808 vector=(char **) ResizeQuantumMemory(vector,(size_t) *number_arguments+
809 count+number_files+1,sizeof(*vector));
810 if (vector == (char **) NULL)
812 for (j=0; j < (ssize_t) number_files; j++)
815 parameters=ParseMagickOption(MagickCommandOptions,MagickFalse,option);
822 Do not expand command option parameters.
824 vector[count++]=ConstantString(option);
825 for (k=0; k < parameters; k++)
828 if (j == (ssize_t) number_files)
831 vector[count++]=ConstantString(option);
835 (void) CopyMagickString(filename,path,MaxTextExtent);
837 (void) ConcatenateMagickString(filename,DirectorySeparator,
839 (void) ConcatenateMagickString(filename,filelist[j],MaxTextExtent);
840 filelist[j]=DestroyString(filelist[j]);
841 if (strlen(filename) >= (MaxTextExtent-1))
842 ThrowFatalException(OptionFatalError,"FilenameTruncated");
843 if (IsPathDirectory(filename) <= 0)
851 (void) ConcatenateMagickString(path,magick,MaxTextExtent);
852 (void) ConcatenateMagickString(path,":",MaxTextExtent);
854 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
855 if (*subimage != '\0')
857 (void) ConcatenateMagickString(path,"[",MaxTextExtent);
858 (void) ConcatenateMagickString(path,subimage,MaxTextExtent);
859 (void) ConcatenateMagickString(path,"]",MaxTextExtent);
861 if (strlen(path) >= MaxTextExtent)
862 ThrowFatalException(OptionFatalError,"FilenameTruncated");
863 if (destroy != MagickFalse)
866 vector[count]=DestroyString(vector[count]);
869 vector[count++]=ConstantString(path);
872 filelist=(char **) RelinquishMagickMemory(filelist);
874 vector[count]=(char *) NULL;
875 if (IsEventLogging() != MagickFalse)
880 command_line=AcquireString(vector[0]);
881 for (i=1; i < count; i++)
883 (void) ConcatenateString(&command_line," {");
884 (void) ConcatenateString(&command_line,vector[i]);
885 (void) ConcatenateString(&command_line,"}");
887 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
888 "Command line: %s",command_line);
889 command_line=DestroyString(command_line);
891 *number_arguments=(int) count;
897 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
901 % G e t E x e c u t i o n P a t h %
905 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
907 % GetExecutionPath() returns the pathname of the executable that started
908 % the process. On success MagickTrue is returned, otherwise MagickFalse.
910 % The format of the GetExecutionPath method is:
912 % MagickBooleanType GetExecutionPath(char *path,const size_t extent)
914 % A description of each parameter follows:
916 % o path: the pathname of the executable that started the process.
918 % o extent: the maximum extent of the path.
921 MagickExport MagickBooleanType GetExecutionPath(char *path,const size_t extent)
927 cwd=getcwd(path,(unsigned long) extent);
928 #if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
931 link_path[MaxTextExtent],
932 execution_path[PATH_MAX+1];
937 (void) FormatMagickString(link_path,MaxTextExtent,"/proc/%.20g/exe",
939 count=readlink(link_path,execution_path,PATH_MAX);
942 (void) FormatMagickString(link_path,MaxTextExtent,"/proc/%.20g/file",
944 count=readlink(link_path,execution_path,PATH_MAX);
946 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
948 execution_path[count]='\0';
949 (void) CopyMagickString(path,execution_path,extent);
953 #if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
956 executable_path[PATH_MAX << 1],
957 execution_path[PATH_MAX+1];
962 length=sizeof(executable_path);
963 if ((_NSGetExecutablePath(executable_path,&length) == 0) &&
964 (realpath(executable_path,execution_path) != (char *) NULL))
965 (void) CopyMagickString(path,execution_path,extent);
968 #if defined(MAGICKCORE_HAVE_GETEXECNAME)
973 execution_path=(const char *) getexecname();
974 if (execution_path != (const char *) NULL)
976 if (*execution_path != *DirectorySeparator)
977 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
978 (void) ConcatenateMagickString(path,execution_path,extent);
982 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
983 NTGetExecutionPath(path,extent);
995 execution_path=(char *) NULL;
996 program_name=program_invocation_name;
997 if (*program_invocation_name != '/')
1002 extent=strlen(cwd)+strlen(program_name)+1;
1003 program_name=AcquireQuantumMemory(extent,sizeof(*program_name));
1004 if (program_name == (char *) NULL)
1005 program_name=program_invocation_name;
1007 count=FormatMagickString(program_name,extent,"%s/%s",cwd,
1008 program_invocation_name);
1012 execution_path=realpath(program_name,NULL);
1013 if (execution_path != (char *) NULL)
1014 (void) CopyMagickString(path,execution_path,extent);
1016 if (program_name != program_invocation_name)
1017 program_name=(char *) RelinquishMagickMemory(program_name);
1018 execution_path=(char *) RelinquishMagickMemory(execution_path);
1021 return(IsPathAccessible(path));
1025 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1029 % G e t M a g i c k P a g e S i z e %
1033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1035 % GetMagickPageSize() returns the memory page size.
1037 % The format of the GetMagickPageSize method is:
1039 % ssize_t GetMagickPageSize()
1042 MagickExport ssize_t GetMagickPageSize(void)
1049 #if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1050 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1052 #if defined(MAGICKCORE_HAVE_GETPAGESIZE)
1053 page_size=(ssize_t) getpagesize();
1062 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1066 % G e t P a t h A t t r i b u t e s %
1070 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1072 % GetPathAttributes() returns attributes (e.g. size of file) about a path.
1074 % The path of the GetPathAttributes method is:
1076 % MagickBooleanType GetPathAttributes(const char *path,void *attributes)
1078 % A description of each parameter follows.
1080 % o path: the file path.
1082 % o attributes: the path attributes are returned here.
1086 #if defined(MAGICKCORE_HAVE__WFOPEN)
1087 static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16)
1089 register const unsigned char
1092 if (utf16 != (wchar_t *) NULL)
1101 Convert UTF-8 to UTF-16.
1104 for (p=utf8; *p != '\0'; p++)
1106 if ((*p & 0x80) == 0)
1109 if ((*p & 0xE0) == 0xC0)
1114 if ((*p & 0xC0) != 0x80)
1119 if ((*p & 0xF0) == 0xE0)
1124 if ((*p & 0xC0) != 0x80)
1127 *q|=(c & 0x3F) << 6;
1129 if ((*p & 0xC0) != 0x80)
1141 Compute UTF-16 string length.
1143 for (p=utf8; *p != '\0'; p++)
1145 if ((*p & 0x80) == 0)
1148 if ((*p & 0xE0) == 0xC0)
1151 if ((*p & 0xC0) != 0x80)
1155 if ((*p & 0xF0) == 0xE0)
1158 if ((*p & 0xC0) != 0x80)
1161 if ((*p & 0xC0) != 0x80)
1170 static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source)
1178 length=UTF8ToUTF16(source,(wchar_t *) NULL);
1185 Not UTF-8, just copy.
1187 length=strlen((const char *) source);
1188 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
1189 if (utf16 == (wchar_t *) NULL)
1190 return((wchar_t *) NULL);
1191 for (i=0; i <= (ssize_t) length; i++)
1195 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
1196 if (utf16 == (wchar_t *) NULL)
1197 return((wchar_t *) NULL);
1198 length=UTF8ToUTF16(source,utf16);
1203 MagickExport MagickBooleanType GetPathAttributes(const char *path,
1209 if (path == (const char *) NULL)
1212 return(MagickFalse);
1214 #if !defined(MAGICKCORE_HAVE__WSTAT)
1215 status=stat(path,(struct stat *) attributes) == 0 ? MagickTrue : MagickFalse;
1221 unicode_path=ConvertUTF8ToUTF16((const unsigned char *) path);
1222 if (unicode_path == (wchar_t *) NULL)
1223 return(MagickFalse);
1224 status=wstat(unicode_path,(struct stat *) attributes) == 0 ? MagickTrue :
1226 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
1233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1237 % G e t P a t h C o m p o n e n t %
1241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1243 % GetPathComponent() returns the parent directory name, filename, basename, or
1244 % extension of a file path.
1246 % The format of the GetPathComponent function is:
1248 % GetPathComponent(const char *path,PathType type,char *component)
1250 % A description of each parameter follows:
1252 % o path: Specifies a pointer to a character array that contains the
1255 % o type: Specififies which file path component to return.
1257 % o component: the selected file path component is returned here.
1260 MagickExport void GetPathComponent(const char *path,PathType type,
1264 magick[MaxTextExtent],
1266 subimage[MaxTextExtent];
1271 assert(path != (const char *) NULL);
1272 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",path);
1273 assert(component != (char *) NULL);
1279 (void) CopyMagickString(component,path,MaxTextExtent);
1281 #if defined(__OS2__)
1284 for (p=component; *p != '\0'; p++)
1286 if ((*p == '%') && (*(p+1) == '['))
1291 for (p++; (*p != ']') && (*p != '\0'); p++) ;
1295 if ((*p == ':') && (IsPathDirectory(path) < 0) &&
1296 (IsPathAccessible(path) == MagickFalse))
1299 Look for image format specification (e.g. ps3:image).
1301 (void) CopyMagickString(magick,component,(size_t) (p-component+1));
1302 if (IsMagickConflict(magick) != MagickFalse)
1305 for (q=component; *q != '\0'; q++)
1313 p=component+strlen(component)-1;
1314 if ((*p == ']') && (strchr(component,'[') != (char *) NULL) &&
1315 (IsPathAccessible(path) == MagickFalse))
1318 Look for scene specification (e.g. img0001.pcd[4]).
1320 for (q=p-1; q > component; q--)
1325 (void) CopyMagickString(subimage,q+1,MaxTextExtent);
1326 subimage[p-q-1]='\0';
1327 if ((IsSceneGeometry(subimage,MagickFalse) == MagickFalse) &&
1328 (IsGeometry(subimage) == MagickFalse))
1336 for (p=component+strlen(component)-1; p > component; p--)
1337 if (IsBasenameSeparator(*p) != MagickFalse)
1343 (void) CopyMagickString(component,magick,MaxTextExtent);
1348 for (p=component+(strlen(component)-1); p > component; p--)
1350 if (IsBasenameSeparator(*p) != MagickFalse)
1366 if (IsBasenameSeparator(*p) != MagickFalse)
1367 (void) CopyMagickMemory((unsigned char *) component,
1368 (const unsigned char *) (p+1),strlen(p+1)+1);
1373 if (IsBasenameSeparator(*p) != MagickFalse)
1374 (void) CopyMagickString(component,p+1,MaxTextExtent);
1375 for (p=component+(strlen(component)-1); p > component; p--)
1385 if (IsBasenameSeparator(*p) != MagickFalse)
1386 (void) CopyMagickString(component,p+1,MaxTextExtent);
1389 for (p=component+strlen(component)-1; p > component; p--)
1394 (void) CopyMagickString(component,p+1,MaxTextExtent);
1399 (void) CopyMagickString(component,subimage,MaxTextExtent);
1409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1413 % G e t P a t h C o m p o n e n t s %
1417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1419 % GetPathComponents() returns a list of path components.
1421 % The format of the GetPathComponents method is:
1423 % char **GetPathComponents(const char *path,
1424 % size_t *number_componenets)
1426 % A description of each parameter follows:
1428 % o path: Specifies the string to segment into a list.
1430 % o number_components: return the number of components in the list
1433 MagickExport char **GetPathComponents(const char *path,
1434 size_t *number_components)
1446 if (path == (char *) NULL)
1447 return((char **) NULL);
1448 *number_components=1;
1449 for (p=path; *p != '\0'; p++)
1450 if (IsBasenameSeparator(*p))
1451 (*number_components)++;
1452 components=(char **) AcquireQuantumMemory((size_t) *number_components+1UL,
1453 sizeof(*components));
1454 if (components == (char **) NULL)
1455 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1457 for (i=0; i < (ssize_t) *number_components; i++)
1459 for (q=p; *q != '\0'; q++)
1460 if (IsBasenameSeparator(*q))
1462 components[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
1463 sizeof(*components));
1464 if (components[i] == (char *) NULL)
1465 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1466 (void) CopyMagickString(components[i],p,(size_t) (q-p+1));
1469 components[i]=(char *) NULL;
1474 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1478 % I s P a t h A c c e s s i b l e %
1482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1484 % IsPathAccessible() returns MagickTrue if the file as defined by the path is
1487 % The format of the IsPathAccessible method is:
1489 % MagickBooleanType IsPathAccessible(const char *filename)
1491 % A description of each parameter follows.
1493 % o path: Specifies a path to a file.
1496 MagickExport MagickBooleanType IsPathAccessible(const char *path)
1504 if ((path == (const char *) NULL) || (*path == '\0'))
1505 return(MagickFalse);
1506 status=GetPathAttributes(path,&attributes);
1507 if (status == MagickFalse)
1509 if (S_ISREG(attributes.st_mode) == 0)
1510 return(MagickFalse);
1511 if (access(path,F_OK) != 0)
1512 return(MagickFalse);
1517 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1521 + I s P a t h D i r e c t o r y %
1525 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1527 % IsPathDirectory() returns -1 if the directory does not exist, 1 is returned
1528 % if the path represents a directory otherwise 0.
1530 % The format of the IsPathDirectory method is:
1532 % int IsPathDirectory(const char *path)
1534 % A description of each parameter follows.
1536 % o path: The directory path.
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)
1558 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1562 % I s M a g i c k T r u e %
1566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1568 % IsMagickTrue() returns MagickTrue if the value is "true", "on", "yes" or
1571 % The format of the IsMagickTrue method is:
1573 % MagickBooleanType IsMagickTrue(const char *value)
1575 % A description of each parameter follows:
1577 % o option: either MagickTrue or MagickFalse depending on the value
1580 % o value: Specifies a pointer to a character array.
1583 MagickExport MagickBooleanType IsMagickTrue(const char *value)
1585 if (value == (const char *) NULL)
1586 return(MagickFalse);
1587 if (LocaleCompare(value,"true") == 0)
1589 if (LocaleCompare(value,"on") == 0)
1591 if (LocaleCompare(value,"yes") == 0)
1593 if (LocaleCompare(value,"1") == 0)
1595 return(MagickFalse);
1599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1603 % L i s t F i l e s %
1607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1609 % ListFiles() reads the directory specified and returns a list of filenames
1610 % contained in the directory sorted in ascending alphabetic order.
1612 % The format of the ListFiles function is:
1614 % char **ListFiles(const char *directory,const char *pattern,
1615 % ssize_t *number_entries)
1617 % A description of each parameter follows:
1619 % o filelist: Method ListFiles returns a list of filenames contained
1620 % in the directory. If the directory specified cannot be read or it is
1621 % a file a NULL list is returned.
1623 % o directory: Specifies a pointer to a text string containing a directory
1626 % o pattern: Specifies a pointer to a text string containing a pattern.
1628 % o number_entries: This integer returns the number of filenames in the
1633 #if defined(__cplusplus) || defined(c_plusplus)
1637 static int FileCompare(const void *x,const void *y)
1643 p=(const char **) x;
1644 q=(const char **) y;
1645 return(LocaleCompare(*p,*q));
1648 #if defined(__cplusplus) || defined(c_plusplus)
1652 static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
1653 struct dirent **result)
1655 #if defined(MAGICKCORE_HAVE_READDIR_R)
1656 return(readdir_r(directory,entry,result));
1660 *result=readdir(directory);
1665 MagickExport char **ListFiles(const char *directory,const char *pattern,
1666 size_t *number_entries)
1684 assert(directory != (const char *) NULL);
1685 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",directory);
1686 assert(pattern != (const char *) NULL);
1687 assert(number_entries != (size_t *) NULL);
1689 current_directory=opendir(directory);
1690 if (current_directory == (DIR *) NULL)
1691 return((char **) NULL);
1696 filelist=(char **) AcquireQuantumMemory((size_t) max_entries,
1698 if (filelist == (char **) NULL)
1700 (void) closedir(current_directory);
1701 return((char **) NULL);
1704 Save the current and change to the new directory.
1706 buffer=(struct dirent *) AcquireAlignedMemory(1,sizeof(*buffer)+FILENAME_MAX+1);
1707 if (buffer == (struct dirent *) NULL)
1708 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1709 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1710 (entry != (struct dirent *) NULL))
1712 if (*entry->d_name == '.')
1714 if ((IsPathDirectory(entry->d_name) > 0) ||
1715 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1716 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1718 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1721 if (*number_entries >= max_entries)
1724 Extend the file list.
1727 filelist=(char **) ResizeQuantumMemory(filelist,(size_t)
1728 max_entries,sizeof(*filelist));
1729 if (filelist == (char **) NULL)
1737 p=strchr(entry->d_name,';');
1740 if (*number_entries > 0)
1741 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1745 filelist[*number_entries]=(char *) AcquireString(entry->d_name);
1746 if (IsPathDirectory(entry->d_name) > 0)
1747 (void) ConcatenateMagickString(filelist[*number_entries],
1748 DirectorySeparator,MaxTextExtent);
1749 (*number_entries)++;
1752 buffer=(struct dirent *) RelinquishMagickMemory(buffer);
1753 (void) closedir(current_directory);
1754 if (filelist == (char **) NULL)
1755 return((char **) NULL);
1757 Sort filelist in ascending order.
1759 qsort((void *) filelist,(size_t) *number_entries,sizeof(*filelist),
1765 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1769 % M u l t i l i n e C e n s u s %
1773 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1775 % MultilineCensus() returns the number of lines within a label. A line is
1776 % represented by a \n character.
1778 % The format of the MultilineCenus method is:
1780 % size_t MultilineCensus(const char *label)
1782 % A description of each parameter follows.
1784 % o label: This character string is the label.
1788 MagickExport size_t MultilineCensus(const char *label)
1794 Determine the number of lines within this label.
1796 if (label == (char *) NULL)
1798 for (number_lines=1; *label != '\0'; label++)
1801 return(number_lines);
1805 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1809 % O p e n M a g i c k S t r e a m %
1813 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1815 % OpenMagickStream() opens the file at the specified path and return the
1816 % associated stream.
1818 % The path of the OpenMagickStream method is:
1820 % FILE *OpenMagickStream(const char *path,const char *mode)
1822 % A description of each parameter follows.
1824 % o path: the file path.
1826 % o mode: the file mode.
1829 MagickExport FILE *OpenMagickStream(const char *path,const char *mode)
1834 if ((path == (const char *) NULL) || (mode == (const char *) NULL))
1837 return((FILE *) NULL);
1840 #if defined(MAGICKCORE_HAVE__WFOPEN)
1846 unicode_path=ConvertUTF8ToUTF16((const unsigned char *) path);
1847 if (unicode_path == (wchar_t *) NULL)
1848 return((FILE *) NULL);
1849 unicode_mode=ConvertUTF8ToUTF16((const unsigned char *) mode);
1850 if (unicode_mode == (wchar_t *) NULL)
1852 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
1853 return((FILE *) NULL);
1855 file=_wfopen(unicode_path,unicode_mode);
1856 unicode_mode=(wchar_t *) RelinquishMagickMemory(unicode_mode);
1857 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
1860 if (file == (FILE *) NULL)
1861 file=fopen(path,mode);
1866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1870 % S y s t e m C o m m a n d %
1874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1876 % SystemCommand() executes the specified command and waits until it
1877 % terminates. The returned value is the exit status of the command.
1879 % The format of the SystemCommand method is:
1881 % int SystemCommand(const MagickBooleanType asynchronous,
1882 % const MagickBooleanType verbose,const char *command,
1883 % ExceptionInfo *exception)
1885 % A description of each parameter follows:
1887 % o asynchronous: a value other than 0 executes the parent program
1888 % concurrently with the new child process.
1890 % o verbose: a value other than 0 prints the executed command before it is
1893 % o command: this string is the command to execute.
1895 % o exception: return any errors here.
1898 MagickExport int SystemCommand(const MagickBooleanType asynchronous,
1899 const MagickBooleanType verbose,const char *command,ExceptionInfo *exception)
1919 arguments=StringToArgv(command,&number_arguments);
1920 if (arguments == (char **) NULL)
1922 rights=ExecutePolicyRights;
1923 domain=DelegatePolicyDomain;
1924 if (IsRightsAuthorized(domain,rights,arguments[1]) == MagickFalse)
1927 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1928 "NotAuthorized","`%s'",arguments[1]);
1929 for (i=0; i < (ssize_t) number_arguments; i++)
1930 arguments[i]=DestroyString(arguments[i]);
1931 arguments=(char **) RelinquishMagickMemory(arguments);
1934 if (verbose != MagickFalse)
1936 (void) fprintf(stderr,"%s\n",command);
1937 (void) fflush(stderr);
1939 shell_command=(char *) command;
1940 if (asynchronous != MagickFalse)
1942 shell_command=AcquireString(command);
1943 (void) ConcatenateMagickString(shell_command,"&",MaxTextExtent);
1945 #if defined(MAGICKCORE_POSIX_SUPPORT)
1946 #if !defined(MAGICKCORE_HAVE_EXECVP)
1947 status=system(shell_command);
1949 if ((asynchronous != MagickFalse) || (strspn(shell_command,"&;<>|") == 0))
1950 status=system(shell_command);
1957 Call application directly rather than from a shell.
1960 if (child_pid == (pid_t) -1)
1961 status=system(command);
1965 status=execvp(arguments[1],arguments+1);
1977 pid=waitpid(child_pid,&child_status,0);
1982 if (WIFEXITED(child_status) != 0)
1983 status=WEXITSTATUS(child_status);
1985 if (WIFSIGNALED(child_status))
1991 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1997 if (asynchronous != MagickFalse)
1999 status=spawnvp(mode,arguments[1],(const char **) (arguments+1));
2001 #elif defined(macintosh)
2002 status=MACSystemCommand(shell_command);
2004 status=system(shell_command);
2006 # error No suitable system() method.
2009 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
2010 "`%s' (%d)",command,status);
2011 if (shell_command != command)
2012 shell_command=DestroyString(shell_command);
2013 for (i=0; i < (ssize_t) number_arguments; i++)
2014 arguments[i]=DestroyString(arguments[i]);
2015 arguments=(char **) RelinquishMagickMemory(arguments);