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_MACH_O_DYLD_H)
62 #include <mach-o/dyld.h>
69 Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
75 IsPathDirectory(const char *);
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % A c q u i r e U n i q u e F i l e n a m e %
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 % AcquireUniqueFilename() replaces the contents of path by a unique path name.
90 % The format of the AcquireUniqueFilename method is:
92 % MagickBooleanType AcquireUniqueFilename(char *path)
94 % A description of each parameter follows.
96 % o path: Specifies a pointer to an array of characters. The unique path
97 % name is returned in this array.
100 MagickExport MagickBooleanType AcquireUniqueFilename(char *path)
105 file=AcquireUniqueFileResource(path);
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % A c q u i r e U n i q u e S ym b o l i c L i n k %
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 % AcquireUniqueSymbolicLink() creates a unique symbolic link to the specified
124 % source path and returns MagickTrue on success otherwise MagickFalse. If the
125 % symlink() method fails or is not available, a unique file name is generated
126 % and the source file copied to it. When you are finished with the file, use
127 % RelinquishUniqueFilename() to destroy it.
129 % The format of the AcquireUniqueSymbolicLink method is:
131 % MagickBooleanType AcquireUniqueSymbolicLink(const char *source,
134 % A description of each parameter follows.
136 % o source: the source path.
138 % o destination: the destination path.
142 static inline size_t MagickMin(const size_t x,const size_t y)
149 MagickExport MagickBooleanType AcquireUniqueSymbolicLink(const char *source,
169 assert(source != (const char *) NULL);
170 assert(destination != (char *) NULL);
171 #if defined(MAGICKCORE_HAVE_SYMLINK)
172 (void) AcquireUniqueFilename(destination);
173 (void) RelinquishUniqueFileResource(destination);
174 if (*source == *DirectorySeparator)
176 if (symlink(source,destination) == 0)
185 if (getcwd(path,MaxTextExtent) == (char *) NULL)
187 (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
188 (void) ConcatenateMagickString(path,source,MaxTextExtent);
189 if (symlink(path,destination) == 0)
193 destination_file=AcquireUniqueFileResource(destination);
194 if (destination_file == -1)
196 source_file=open(source,O_RDONLY | O_BINARY);
197 if (source_file == -1)
199 (void) close(destination_file);
200 (void) RelinquishUniqueFileResource(destination);
203 quantum=(size_t) MagickMaxBufferExtent;
204 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size != 0))
205 quantum=MagickMin((size_t) attributes.st_size,MagickMaxBufferExtent);
206 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
207 if (buffer == (unsigned char *) NULL)
209 (void) close(source_file);
210 (void) close(destination_file);
211 (void) RelinquishUniqueFileResource(destination);
216 count=(ssize_t) read(source_file,buffer,quantum);
219 length=(size_t) count;
220 count=(ssize_t) write(destination_file,buffer,length);
221 if ((size_t) count != length)
223 (void) close(destination_file);
224 (void) close(source_file);
225 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
226 (void) RelinquishUniqueFileResource(destination);
230 (void) close(destination_file);
231 (void) close(source_file);
232 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 % A p p e n d I m a g e F o r m a t %
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 % AppendImageFormat() appends the image format type to the filename. If an
248 % extension to the file already exists, it is first removed.
250 % The format of the AppendImageFormat method is:
252 % void AppendImageFormat(const char *format,char *filename)
254 % A description of each parameter follows.
256 % o format: Specifies a pointer to an array of characters. This the
257 % format of the image.
259 % o filename: Specifies a pointer to an array of characters. The unique
260 % file name is returned in this array.
263 MagickExport void AppendImageFormat(const char *format,char *filename)
268 assert(format != (char *) NULL);
269 assert(filename != (char *) NULL);
270 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
271 if ((*format == '\0') || (*filename == '\0'))
273 if (LocaleCompare(filename,"-") == 0)
276 message[MaxTextExtent];
278 (void) FormatMagickString(message,MaxTextExtent,"%s:%s",format,filename);
279 (void) CopyMagickString(filename,message,MaxTextExtent);
282 GetPathComponent(filename,RootPath,root);
283 (void) FormatMagickString(filename,MaxTextExtent,"%s.%s",root,format);
287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291 % B a s e 6 4 D e c o d e %
295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 % Base64Decode() decodes Base64-encoded text and returns its binary
298 % equivalent. NULL is returned if the text is not valid Base64 data, or a
299 % memory allocation failure occurs.
301 % The format of the Base64Decode method is:
303 % unsigned char *Base64Decode(const char *source,length_t *length)
305 % A description of each parameter follows:
307 % o source: A pointer to a Base64-encoded string.
309 % o length: the number of bytes decoded.
312 MagickExport unsigned char *Base64Decode(const char *source,size_t *length)
327 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
328 assert(source != (char *) NULL);
329 assert(length != (size_t *) NULL);
331 decode=(unsigned char *) AcquireQuantumMemory(strlen(source)/4+4,
333 if (decode == (unsigned char *) NULL)
334 return((unsigned char *) NULL);
337 for (p=source; *p != '\0'; p++)
339 if (isspace((int) ((unsigned char) *p)) != 0)
344 if (q == (char *) NULL)
346 decode=(unsigned char *) RelinquishMagickMemory(decode);
347 return((unsigned char *) NULL); /* non-Base64 character */
353 decode[i]=(q-Base64) << 2;
359 decode[i++]|=(q-Base64) >> 4;
360 decode[i]=((q-Base64) & 0x0f) << 4;
366 decode[i++]|=(q-Base64) >> 2;
367 decode[i]=((q-Base64) & 0x03) << 6;
373 decode[i++]|=(q-Base64);
380 Verify Base-64 string has proper terminal characters.
386 decode=(unsigned char *) RelinquishMagickMemory(decode);
387 return((unsigned char *) NULL);
399 Unrecognized '=' character.
401 decode=(unsigned char *) RelinquishMagickMemory(decode);
402 return((unsigned char *) NULL);
406 for ( ; *p != '\0'; p++)
407 if (isspace((int) ((unsigned char) *p)) == 0)
411 decode=(unsigned char *) RelinquishMagickMemory(decode);
412 return((unsigned char *) NULL);
418 for ( ; *p != '\0'; p++)
419 if (isspace((int) ((unsigned char) *p)) == 0)
421 decode=(unsigned char *) RelinquishMagickMemory(decode);
422 return((unsigned char *) NULL);
424 if ((int) decode[i] != 0)
426 decode=(unsigned char *) RelinquishMagickMemory(decode);
427 return((unsigned char *) NULL);
437 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
441 % B a s e 6 4 E n c o d e %
445 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
447 % Base64Encode() encodes arbitrary binary data to Base64 encoded format as
448 % described by the "Base64 Content-Transfer-Encoding" section of RFC 2045 and
449 % returns the result as a null-terminated ASCII string. NULL is returned if
450 % a memory allocation failure occurs.
452 % The format of the Base64Encode method is:
454 % char *Base64Encode(const unsigned char *blob,const size_t blob_length,
455 % size_t *encode_length)
457 % A description of each parameter follows:
459 % o blob: A pointer to binary data to encode.
461 % o blob_length: the number of bytes to encode.
463 % o encode_length: The number of bytes encoded.
466 MagickExport char *Base64Encode(const unsigned char *blob,
467 const size_t blob_length,size_t *encode_length)
472 register const unsigned char
481 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
482 assert(blob != (const unsigned char *) NULL);
483 assert(blob_length != 0);
484 assert(encode_length != (size_t *) NULL);
486 encode=(char *) AcquireQuantumMemory(blob_length/3+4,4*sizeof(*encode));
487 if (encode == (char *) NULL)
488 return((char *) NULL);
490 for (p=blob; p < (blob+blob_length-2); p+=3)
492 encode[i++]=Base64[(int) (*p >> 2)];
493 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
494 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
495 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
497 remainder=blob_length % 3;
509 for (j=0; j < (long) remainder; j++)
511 encode[i++]=Base64[(int) (code[0] >> 2)];
512 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
516 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
525 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
529 % C h o p P a t h C o m p o n e n t s %
533 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
535 % ChopPathComponents() removes the number of specified file components from a
538 % The format of the ChopPathComponents method is:
540 % ChopPathComponents(char *path,unsigned long components)
542 % A description of each parameter follows:
546 % o components: The number of components to chop.
549 MagickExport void ChopPathComponents(char *path,const unsigned long components)
554 for (i=0; i < (long) components; i++)
555 GetPathComponent(path,HeadPath,path);
559 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563 % E x p a n d F i l e n a m e %
567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
569 % ExpandFilename() expands '~' in a path.
571 % The format of the ExpandFilename function is:
573 % ExpandFilename(char *path)
575 % A description of each parameter follows:
577 % o path: Specifies a pointer to a character array that contains the
581 MagickExport void ExpandFilename(char *path)
584 expand_path[MaxTextExtent];
586 if (path == (char *) NULL)
590 (void) CopyMagickString(expand_path,path,MaxTextExtent);
591 if ((*(path+1) == *DirectorySeparator) || (*(path+1) == '\0'))
597 Substitute ~ with $HOME.
599 (void) CopyMagickString(expand_path,".",MaxTextExtent);
600 (void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
601 home=GetEnvironmentValue("HOME");
602 if (home == (char *) NULL)
603 home=GetEnvironmentValue("USERPROFILE");
604 if (home != (char *) NULL)
606 (void) CopyMagickString(expand_path,home,MaxTextExtent);
607 (void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
608 home=DestroyString(home);
613 #if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
615 username[MaxTextExtent];
624 Substitute ~ with home directory from password file.
626 (void) CopyMagickString(username,path+1,MaxTextExtent);
627 p=strchr(username,'/');
628 if (p != (char *) NULL)
630 entry=getpwnam(username);
631 if (entry == (struct passwd *) NULL)
633 (void) CopyMagickString(expand_path,entry->pw_dir,MaxTextExtent);
634 if (p != (char *) NULL)
636 (void) ConcatenateMagickString(expand_path,"/",MaxTextExtent);
637 (void) ConcatenateMagickString(expand_path,p+1,MaxTextExtent);
641 (void) CopyMagickString(path,expand_path,MaxTextExtent);
645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649 % E x p a n d F i l e n a m e s %
653 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
655 % ExpandFilenames() checks each argument of the command line vector and
656 % expands it if they have a wildcard character. For example, *.jpg might
657 % expand to: bird.jpg rose.jpg tiki.jpg.
659 % The format of the ExpandFilenames function is:
661 % status=ExpandFilenames(int *number_arguments,char ***arguments)
663 % A description of each parameter follows:
665 % o number_arguments: Specifies a pointer to an integer describing the
666 % number of elements in the argument vector.
668 % o arguments: Specifies a pointer to a text array containing the command
672 MagickExport MagickBooleanType ExpandFilenames(int *number_arguments,
677 home_directory[MaxTextExtent],
692 Allocate argument vector.
694 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
695 assert(number_arguments != (int *) NULL);
696 assert(arguments != (char ***) NULL);
697 vector=(char **) AcquireQuantumMemory((size_t) (*number_arguments+1),
699 if (vector == (char **) NULL)
700 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
702 Expand any wildcard filenames.
704 *home_directory='\0';
706 for (i=0; i < (long) *number_arguments; i++)
710 filename[MaxTextExtent],
711 magick[MaxTextExtent],
714 subimage[MaxTextExtent];
719 option=(*arguments)[i];
724 vector[count++]=ConstantString(option);
726 parameters=ParseMagickOption(MagickCommandOptions,MagickFalse,option);
730 Do not expand command option parameters.
732 for (j=0; j < parameters; j++)
735 if (i == (long) *number_arguments)
737 option=(*arguments)[i];
738 vector[count++]=ConstantString(option);
742 if ((*option == '"') || (*option == '\''))
744 GetPathComponent(option,TailPath,filename);
745 GetPathComponent(option,MagickPath,magick);
746 if ((LocaleCompare(magick,"CAPTION") == 0) ||
747 (LocaleCompare(magick,"LABEL") == 0) ||
748 (LocaleCompare(magick,"VID") == 0))
750 if ((IsGlob(filename) == MagickFalse) && (*filename != '@'))
752 if (*filename != '@')
755 Generate file list from wildcard filename (e.g. *.jpg).
757 GetPathComponent(option,HeadPath,path);
758 GetPathComponent(option,SubimagePath,subimage);
759 ExpandFilename(path);
760 if (*home_directory == '\0')
761 cwd=getcwd(home_directory,MaxTextExtent-1);
762 filelist=ListFiles(*path == '\0' ? home_directory : path,filename,
777 Generate file list from file list (e.g. @filelist.txt).
779 exception=AcquireExceptionInfo();
780 files=FileToString(filename+1,~0,exception);
781 exception=DestroyExceptionInfo(exception);
782 if (files == (char *) NULL)
785 filelist=StringToArgv(files,&number_images);
786 files=DestroyString(files);
787 number_files=(unsigned long) number_images;
788 if (filelist != (char **) NULL)
791 for (j=0; j < (long) number_files; j++)
792 filelist[j]=filelist[j+1];
795 if (filelist == (char **) NULL)
797 for (j=0; j < (long) number_files; j++)
798 if (IsPathDirectory(filelist[j]) <= 0)
800 if (j == (long) number_files)
802 for (j=0; j < (long) number_files; j++)
803 filelist[j]=DestroyString(filelist[j]);
804 filelist=(char **) RelinquishMagickMemory(filelist);
808 Transfer file list to argument vector.
810 vector=(char **) ResizeQuantumMemory(vector,(size_t) *number_arguments+
811 count+number_files+1,sizeof(*vector));
812 if (vector == (char **) NULL)
814 for (j=0; j < (long) number_files; j++)
816 (void) CopyMagickString(filename,path,MaxTextExtent);
818 (void) ConcatenateMagickString(filename,DirectorySeparator,
820 (void) ConcatenateMagickString(filename,filelist[j],MaxTextExtent);
821 filelist[j]=DestroyString(filelist[j]);
822 if (strlen(filename) >= MaxTextExtent)
823 ThrowFatalException(OptionFatalError,"FilenameTruncated");
824 if (IsPathDirectory(filename) == 0)
832 (void) ConcatenateMagickString(path,magick,MaxTextExtent);
833 (void) ConcatenateMagickString(path,":",MaxTextExtent);
835 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
836 if (*subimage != '\0')
838 (void) ConcatenateMagickString(path,"[",MaxTextExtent);
839 (void) ConcatenateMagickString(path,subimage,MaxTextExtent);
840 (void) ConcatenateMagickString(path,"]",MaxTextExtent);
842 if (strlen(path) >= MaxTextExtent)
843 ThrowFatalException(OptionFatalError,"FilenameTruncated");
844 if (destroy != MagickFalse)
847 vector[count]=DestroyString(vector[count]);
850 vector[count++]=ConstantString(path);
853 filelist=(char **) RelinquishMagickMemory(filelist);
855 vector[count]=(char *) NULL;
856 if (IsEventLogging() != MagickFalse)
861 command_line=AcquireString(vector[0]);
862 for (i=1; i < count; i++)
864 (void) ConcatenateString(&command_line," {");
865 (void) ConcatenateString(&command_line,vector[i]);
866 (void) ConcatenateString(&command_line,"}");
868 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
869 "Command line: %s",command_line);
870 command_line=DestroyString(command_line);
872 *number_arguments=(int) count;
878 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
882 % G e t E x e c u t i o n P a t h %
886 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
888 % GetExecutionPath() returns the pathname of the executable that started
889 % the process. On success MagickTrue is returned, otherwise MagickFalse.
891 % The format of the GetExecutionPath method is:
893 % MagickBooleanType GetExecutionPath(char *path,const size_t extent)
895 % A description of each parameter follows:
897 % o path: the pathname of the executable that started the process.
899 % o extent: the maximum extent of the path.
902 MagickExport MagickBooleanType GetExecutionPath(char *path,const size_t extent)
908 cwd=getcwd(path,(unsigned long) extent);
909 #if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
912 link_path[MaxTextExtent],
913 execution_path[PATH_MAX+1];
918 (void) FormatMagickString(link_path,MaxTextExtent,"/proc/%ld/exe",
920 count=readlink(link_path,execution_path,PATH_MAX);
923 (void) FormatMagickString(link_path,MaxTextExtent,"/proc/%ld/file",
925 count=readlink(link_path,execution_path,PATH_MAX);
927 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
929 execution_path[count]='\0';
930 (void) CopyMagickString(path,execution_path,extent);
934 #if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
937 executable_path[PATH_MAX << 1],
938 execution_path[PATH_MAX+1];
943 length=sizeof(executable_path);
944 if ((_NSGetExecutablePath(executable_path,&length) == 0) &&
945 (realpath(executable_path,execution_path) != (char *) NULL))
946 (void) CopyMagickString(path,execution_path,extent);
949 #if defined(MAGICKCORE_HAVE_GETEXECNAME)
954 execution_path=(const char *) getexecname();
955 if (execution_path != (const char *) NULL)
957 if (*execution_path != *DirectorySeparator)
958 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
959 (void) ConcatenateMagickString(path,execution_path,extent);
963 #if defined(__WINDOWS__)
964 NTGetExecutionPath(path,extent);
976 execution_path=(char *) NULL;
977 program_name=program_invocation_name;
978 if (*program_invocation_name != '/')
983 extent=strlen(cwd)+strlen(program_name)+1;
984 program_name=AcquireQuantumMemory(extent,sizeof(*program_name));
985 if (program_name == (char *) NULL)
986 program_name=program_invocation_name;
988 count=FormatMagickString(program_name,extent,"%s/%s",cwd,
989 program_invocation_name);
993 execution_path=realpath(program_name,NULL);
994 if (execution_path != (char *) NULL)
995 (void) CopyMagickString(path,execution_path,extent);
997 if (program_name != program_invocation_name)
998 program_name=(char *) RelinquishMagickMemory(program_name);
999 execution_path=(char *) RelinquishMagickMemory(execution_path);
1002 return(IsPathAccessible(path));
1006 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1010 % G e t M a g i c k P a g e S i z e %
1014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1016 % GetMagickPageSize() returns the memory page size.
1018 % The format of the GetMagickPageSize method is:
1020 % long GetMagickPageSize()
1023 MagickExport long GetMagickPageSize(void)
1030 #if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1031 page_size=sysconf(_SC_PAGE_SIZE);
1033 #if defined(MAGICKCORE_HAVE_GETPAGESIZE)
1035 page_size=getpagesize();
1043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1047 % G e t P a t h A t t r i b u t e s %
1051 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1053 % GetPathAttributes() returns attributes (e.g. size of file) about a path.
1055 % The path of the GetPathAttributes method is:
1057 % MagickBooleanType GetPathAttributes(const char *path,void *attributes)
1059 % A description of each parameter follows.
1061 % o path: the file path.
1063 % o attributes: the path attributes are returned here.
1067 #if defined(MAGICKCORE_HAVE__WFOPEN)
1068 static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16)
1070 register const unsigned char
1073 if (utf16 != (wchar_t *) NULL)
1082 Convert UTF-8 to UTF-16.
1085 for (p=utf8; *p != '\0'; p++)
1087 if ((*p & 0x80) == 0)
1090 if ((*p & 0xE0) == 0xC0)
1095 if ((*p & 0xC0) != 0x80)
1100 if ((*p & 0xF0) == 0xE0)
1105 if ((*p & 0xC0) != 0x80)
1108 *q|=(c & 0x3F) << 6;
1110 if ((*p & 0xC0) != 0x80)
1122 Compute UTF-16 string length.
1124 for (p=utf8; *p != '\0'; p++)
1126 if ((*p & 0x80) == 0)
1129 if ((*p & 0xE0) == 0xC0)
1132 if ((*p & 0xC0) != 0x80)
1136 if ((*p & 0xF0) == 0xE0)
1139 if ((*p & 0xC0) != 0x80)
1142 if ((*p & 0xC0) != 0x80)
1151 static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source)
1159 length=UTF8ToUTF16(source,(wchar_t *) NULL);
1166 Not UTF-8, just copy.
1168 length=strlen(source);
1169 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
1170 if (utf16 == (wchar_t *) NULL)
1171 return((wchar_t *) NULL);
1172 for (i=0; i <= (long) length; i++)
1176 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
1177 if (utf16 == (wchar_t *) NULL)
1178 return((wchar_t *) NULL);
1179 length=UTF8ToUTF16(source,utf16);
1184 MagickExport MagickBooleanType GetPathAttributes(const char *path,
1190 if (path == (const char *) NULL)
1193 return(MagickFalse);
1195 #if !defined(MAGICKCORE_HAVE__WSTAT)
1196 status=stat(path,(struct stat *) attributes) == 0 ? MagickTrue : MagickFalse;
1202 unicode_path=ConvertUTF8ToUTF16(path);
1203 if (unicode_path == (wchar_t *) NULL)
1204 return(MagickFalse);
1205 status=wstat(unicode_path,(struct stat *) attributes) == 0 ? MagickTrue :
1207 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
1214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1218 % G e t P a t h C o m p o n e n t %
1222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1224 % GetPathComponent() returns the parent directory name, filename, basename, or
1225 % extension of a file path.
1227 % The format of the GetPathComponent function is:
1229 % GetPathComponent(const char *path,PathType type,char *component)
1231 % A description of each parameter follows:
1233 % o path: Specifies a pointer to a character array that contains the
1236 % o type: Specififies which file path component to return.
1238 % o component: the selected file path component is returned here.
1241 MagickExport void GetPathComponent(const char *path,PathType type,
1245 magick[MaxTextExtent],
1247 subimage[MaxTextExtent];
1252 assert(path != (const char *) NULL);
1253 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",path);
1254 assert(component != (char *) NULL);
1260 (void) CopyMagickString(component,path,MaxTextExtent);
1262 #if defined(__OS2__)
1265 for (p=component; *p != '\0'; p++)
1266 if ((*p == ':') && (IsPathDirectory(path) < 0) &&
1267 (IsPathAccessible(path) == MagickFalse))
1270 Look for image format specification (e.g. ps3:image).
1272 (void) CopyMagickString(magick,component,(size_t) (p-component+1));
1273 if (IsMagickConflict(magick) != MagickFalse)
1276 for (q=component; *q != '\0'; q++)
1283 p=component+strlen(component)-1;
1284 if ((*p == ']') && (strchr(component,'[') != (char *) NULL) &&
1285 (IsPathAccessible(path) == MagickFalse))
1288 Look for scene specification (e.g. img0001.pcd[4]).
1290 for (q=p-1; q > component; q--)
1295 (void) CopyMagickString(subimage,q+1,MaxTextExtent);
1296 subimage[p-q-1]='\0';
1297 if ((IsSceneGeometry(subimage,MagickFalse) == MagickFalse) &&
1298 (IsGeometry(subimage) == MagickFalse))
1306 for (p=component+strlen(component)-1; p > component; p--)
1307 if (IsBasenameSeparator(*p) != MagickFalse)
1313 (void) CopyMagickString(component,magick,MaxTextExtent);
1318 for (p=component+(strlen(component)-1); p > component; p--)
1320 if (IsBasenameSeparator(*p) != MagickFalse)
1336 if (IsBasenameSeparator(*p) != MagickFalse)
1337 (void) CopyMagickMemory((unsigned char *) component,
1338 (const unsigned char *) (p+1),strlen(p+1)+1);
1343 if (IsBasenameSeparator(*p) != MagickFalse)
1344 (void) CopyMagickString(component,p+1,MaxTextExtent);
1345 for (p=component+(strlen(component)-1); p > component; p--)
1355 if (IsBasenameSeparator(*p) != MagickFalse)
1356 (void) CopyMagickString(component,p+1,MaxTextExtent);
1359 for (p=component+strlen(component)-1; p > component; p--)
1364 (void) CopyMagickString(component,p+1,MaxTextExtent);
1369 (void) CopyMagickString(component,subimage,MaxTextExtent);
1379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1383 % G e t P a t h C o m p o n e n t s %
1387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1389 % GetPathComponents() returns a list of path components.
1391 % The format of the GetPathComponents method is:
1393 % char **GetPathComponents(const char *path,
1394 % unsigned long *number_componenets)
1396 % A description of each parameter follows:
1398 % o path: Specifies the string to segment into a list.
1400 % o number_components: return the number of components in the list
1403 MagickExport char **GetPathComponents(const char *path,
1404 unsigned long *number_components)
1416 if (path == (char *) NULL)
1417 return((char **) NULL);
1418 *number_components=1;
1419 for (p=path; *p != '\0'; p++)
1420 if (IsBasenameSeparator(*p))
1421 (*number_components)++;
1422 components=(char **) AcquireQuantumMemory((size_t) *number_components+1UL,
1423 sizeof(*components));
1424 if (components == (char **) NULL)
1425 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1427 for (i=0; i < (long) *number_components; i++)
1429 for (q=p; *q != '\0'; q++)
1430 if (IsBasenameSeparator(*q))
1432 components[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
1433 sizeof(*components));
1434 if (components[i] == (char *) NULL)
1435 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1436 (void) CopyMagickString(components[i],p,(size_t) (q-p+1));
1439 components[i]=(char *) NULL;
1444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1448 % I s P a t h A c c e s s i b l e %
1452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1454 % IsPathAccessible() returns MagickTrue if the file as defined by the path is
1457 % The format of the IsPathAccessible method is:
1459 % MagickBooleanType IsPathAccessible(const char *filename)
1461 % A description of each parameter follows.
1463 % o path: Specifies a path to a file.
1466 MagickExport MagickBooleanType IsPathAccessible(const char *path)
1474 if ((path == (const char *) NULL) || (*path == '\0'))
1475 return(MagickFalse);
1476 status=GetPathAttributes(path,&attributes);
1477 if (status == MagickFalse)
1479 if (S_ISREG(attributes.st_mode) == 0)
1480 return(MagickFalse);
1481 if (access(path,F_OK) != 0)
1482 return(MagickFalse);
1487 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1491 + I s P a t h D i r e c t o r y %
1495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1497 % IsPathDirectory() returns -1 if the directory does not exist, 1 is returned
1498 % if the path represents a directory otherwise 0.
1500 % The format of the IsPathDirectory method is:
1502 % int IsPathDirectory(const char *path)
1504 % A description of each parameter follows.
1506 % o path: The directory path.
1509 static int IsPathDirectory(const char *path)
1517 if ((path == (const char *) NULL) || (*path == '\0'))
1518 return(MagickFalse);
1519 status=GetPathAttributes(path,&attributes);
1520 if (status == MagickFalse)
1522 if (S_ISDIR(attributes.st_mode) == 0)
1528 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1532 % I s M a g i c k T r u e %
1536 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1538 % IsMagickTrue() returns MagickTrue if the value is "true", "on", "yes" or
1541 % The format of the IsMagickTrue method is:
1543 % MagickBooleanType IsMagickTrue(const char *value)
1545 % A description of each parameter follows:
1547 % o option: either MagickTrue or MagickFalse depending on the value
1550 % o value: Specifies a pointer to a character array.
1553 MagickExport MagickBooleanType IsMagickTrue(const char *value)
1555 if (value == (const char *) NULL)
1556 return(MagickFalse);
1557 if (LocaleCompare(value,"true") == 0)
1559 if (LocaleCompare(value,"on") == 0)
1561 if (LocaleCompare(value,"yes") == 0)
1563 if (LocaleCompare(value,"1") == 0)
1565 return(MagickFalse);
1569 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1573 % L i s t F i l e s %
1577 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1579 % ListFiles() reads the directory specified and returns a list of filenames
1580 % contained in the directory sorted in ascending alphabetic order.
1582 % The format of the ListFiles function is:
1584 % char **ListFiles(const char *directory,const char *pattern,
1585 % long *number_entries)
1587 % A description of each parameter follows:
1589 % o filelist: Method ListFiles returns a list of filenames contained
1590 % in the directory. If the directory specified cannot be read or it is
1591 % a file a NULL list is returned.
1593 % o directory: Specifies a pointer to a text string containing a directory
1596 % o pattern: Specifies a pointer to a text string containing a pattern.
1598 % o number_entries: This integer returns the number of filenames in the
1603 #if defined(__cplusplus) || defined(c_plusplus)
1607 static int FileCompare(const void *x,const void *y)
1613 p=(const char **) x;
1614 q=(const char **) y;
1615 return(LocaleCompare(*p,*q));
1618 #if defined(__cplusplus) || defined(c_plusplus)
1622 static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
1623 struct dirent **result)
1625 #if defined(MAGICKCORE_HAVE_READDIR_R)
1626 return(readdir_r(directory,entry,result));
1630 *result=readdir(directory);
1635 MagickExport char **ListFiles(const char *directory,const char *pattern,
1636 unsigned long *number_entries)
1654 assert(directory != (const char *) NULL);
1655 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",directory);
1656 assert(pattern != (const char *) NULL);
1657 assert(number_entries != (unsigned long *) NULL);
1659 current_directory=opendir(directory);
1660 if (current_directory == (DIR *) NULL)
1661 return((char **) NULL);
1666 filelist=(char **) AcquireQuantumMemory((size_t) max_entries,
1668 if (filelist == (char **) NULL)
1670 (void) closedir(current_directory);
1671 return((char **) NULL);
1674 Save the current and change to the new directory.
1676 buffer=(struct dirent *) AcquireMagickMemory(sizeof(*buffer)+FILENAME_MAX+1);
1677 if (buffer == (struct dirent *) NULL)
1678 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1679 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1680 (entry != (struct dirent *) NULL))
1682 if (*entry->d_name == '.')
1684 if ((IsPathDirectory(entry->d_name) > 0) ||
1685 #if defined(__WINDOWS__)
1686 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1688 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1691 if (*number_entries >= max_entries)
1694 Extend the file list.
1697 filelist=(char **) ResizeQuantumMemory(filelist,(size_t)
1698 max_entries,sizeof(*filelist));
1699 if (filelist == (char **) NULL)
1707 p=strchr(entry->d_name,';');
1710 if (*number_entries > 0)
1711 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1715 filelist[*number_entries]=(char *) AcquireString(entry->d_name);
1716 if (IsPathDirectory(entry->d_name) > 0)
1717 (void) ConcatenateMagickString(filelist[*number_entries],
1718 DirectorySeparator,MaxTextExtent);
1719 (*number_entries)++;
1722 buffer=(struct dirent *) RelinquishMagickMemory(buffer);
1723 (void) closedir(current_directory);
1724 if (filelist == (char **) NULL)
1725 return((char **) NULL);
1727 Sort filelist in ascending order.
1729 qsort((void *) filelist,(size_t) *number_entries,sizeof(*filelist),
1735 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1739 % M u l t i l i n e C e n s u s %
1743 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1745 % MultilineCensus() returns the number of lines within a label. A line is
1746 % represented by a \n character.
1748 % The format of the MultilineCenus method is:
1750 % unsigned long MultilineCensus(const char *label)
1752 % A description of each parameter follows.
1754 % o label: This character string is the label.
1758 MagickExport unsigned long MultilineCensus(const char *label)
1764 Determine the number of lines within this label.
1766 if (label == (char *) NULL)
1768 for (number_lines=1; *label != '\0'; label++)
1771 return(number_lines);
1775 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1779 % O p e n M a g i c k S t r e a m %
1783 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1785 % OpenMagickStream() opens the file at the specified path and return the
1786 % associated stream.
1788 % The path of the OpenMagickStream method is:
1790 % FILE *OpenMagickStream(const char *path,const char *mode)
1792 % A description of each parameter follows.
1794 % o path: the file path.
1796 % o mode: the file mode.
1799 MagickExport FILE *OpenMagickStream(const char *path,const char *mode)
1804 if ((path == (const char *) NULL) || (mode == (const char *) NULL))
1807 return((FILE *) NULL);
1810 #if defined(MAGICKCORE_HAVE__WFOPEN)
1816 unicode_path=ConvertUTF8ToUTF16(path);
1817 if (unicode_path == (wchar_t *) NULL)
1818 return((FILE *) NULL);
1819 unicode_mode=ConvertUTF8ToUTF16(mode);
1820 if (unicode_mode == (wchar_t *) NULL)
1822 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
1823 return((FILE *) NULL);
1825 file=_wfopen(unicode_path,unicode_mode);
1826 unicode_mode=(wchar_t *) RelinquishMagickMemory(unicode_mode);
1827 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
1830 if (file == (FILE *) NULL)
1831 file=fopen(path,mode);
1836 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1840 % S y s t e m C o m m a n d %
1844 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1846 % SystemCommand() executes the specified command and waits until it
1847 % terminates. The returned value is the exit status of the command.
1849 % The format of the SystemCommand method is:
1851 % int SystemCommand(const MagickBooleanType verbose,const char *command,
1852 % ExceptionInfo *exception)
1854 % A description of each parameter follows:
1856 % o verbose: a value other than 0 prints the executed command before it is
1859 % o command: this string is the command to execute.
1861 % o exception: return any errors here.
1864 MagickExport int SystemCommand(const MagickBooleanType verbose,
1865 const char *command,ExceptionInfo *exception)
1884 arguments=StringToArgv(command,&number_arguments);
1885 if (arguments == (char **) NULL)
1887 domain=DelegatePolicyDomain;
1888 rights=ExecutePolicyRights;
1889 if (IsRightsAuthorized(domain,rights,arguments[1]) == MagickFalse)
1891 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1892 "NotAuthorized","`%s'",arguments[1]);
1893 for (i=0; i < number_arguments; i++)
1894 arguments[i]=DestroyString(arguments[i]);
1895 arguments=(char **) RelinquishMagickMemory(arguments);
1898 if (verbose != MagickFalse)
1900 (void) fprintf(stderr,"%s\n",command);
1901 (void) fflush(stderr);
1903 #if defined(MAGICKCORE_POSIX_SUPPORT)
1904 #if !defined(MAGICKCORE_HAVE_EXECVP)
1905 status=system(command);
1907 if (strspn(command,"&;<>|") == 0)
1908 status=system(command);
1915 Call application directly rather than from a shell.
1918 if (child_pid == (pid_t) -1)
1919 status=system(command);
1923 status=execvp(arguments[1],arguments+1);
1935 pid=waitpid(child_pid,&child_status,0);
1940 if (WIFEXITED(child_status) != 0)
1941 status=WEXITSTATUS(child_status);
1943 if (WIFSIGNALED(child_status))
1949 #elif defined(__WINDOWS__)
1950 status=NTSystemCommand(command);
1951 #elif defined(macintosh)
1952 status=MACSystemCommand(command);
1954 status=system(command);
1956 # error No suitable system() method.
1963 message=GetExceptionMessage(errno);
1964 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1965 "`%s': %s",command,message);
1966 message=DestroyString(message);
1968 for (i=0; i < number_arguments; i++)
1969 arguments[i]=DestroyString(arguments[i]);
1970 arguments=(char **) RelinquishMagickMemory(arguments);