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-2009 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/resource_.h"
54 #include "magick/semaphore.h"
55 #include "magick/signature-private.h"
56 #include "magick/statistic.h"
57 #include "magick/string_.h"
58 #include "magick/token.h"
59 #include "magick/utility.h"
60 #if defined(MAGICKCORE_HAVE_MACH_O_DYLD_H)
61 #include <mach-o/dyld.h>
68 Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
74 IsPathDirectory(const char *);
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % A c q u i r e U n i q u e F i l e n a m e %
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 % AcquireUniqueFilename() replaces the contents of path by a unique path name.
89 % The format of the AcquireUniqueFilename method is:
91 % MagickBooleanType AcquireUniqueFilename(char *path)
93 % A description of each parameter follows.
95 % o path: Specifies a pointer to an array of characters. The unique path
96 % name is returned in this array.
99 MagickExport MagickBooleanType AcquireUniqueFilename(char *path)
104 file=AcquireUniqueFileResource(path);
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 % A c q u i r e U n i q u e S ym b o l i c L i n k %
120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122 % AcquireUniqueSymbolicLink() creates a unique symbolic link to the specified
123 % source path and returns MagickTrue on success otherwise MagickFalse. If the
124 % symlink() method fails or is not available, a unique file name is generated
125 % and the source file copied to it. When you are finished with the file, use
126 % RelinquishUniqueFilename() to destroy it.
128 % The format of the AcquireUniqueSymbolicLink method is:
130 % MagickBooleanType AcquireUniqueSymbolicLink(const char *source,
133 % A description of each parameter follows.
135 % o source: the source path.
137 % o destination: the destination path.
141 static inline size_t MagickMin(const size_t x,const size_t y)
148 MagickExport MagickBooleanType AcquireUniqueSymbolicLink(const char *source,
168 assert(source != (const char *) NULL);
169 assert(destination != (char *) NULL);
170 #if defined(MAGICKCORE_HAVE_SYMLINK)
171 (void) AcquireUniqueFilename(destination);
172 (void) RelinquishUniqueFileResource(destination);
173 if (*source == *DirectorySeparator)
175 if (symlink(source,destination) == 0)
184 if (getcwd(path,MaxTextExtent) == (char *) NULL)
186 (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
187 (void) ConcatenateMagickString(path,source,MaxTextExtent);
188 if (symlink(path,destination) == 0)
192 destination_file=AcquireUniqueFileResource(destination);
193 if (destination_file == -1)
195 source_file=open(source,O_RDONLY | O_BINARY);
196 if (source_file == -1)
198 (void) close(destination_file);
199 (void) RelinquishUniqueFileResource(destination);
202 quantum=(size_t) MagickMaxBufferExtent;
203 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size != 0))
204 quantum=MagickMin((size_t) attributes.st_size,MagickMaxBufferExtent);
205 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
206 if (buffer == (unsigned char *) NULL)
208 (void) close(source_file);
209 (void) close(destination_file);
210 (void) RelinquishUniqueFileResource(destination);
215 count=(ssize_t) read(source_file,buffer,quantum);
218 length=(size_t) count;
219 count=(ssize_t) write(destination_file,buffer,length);
220 if ((size_t) count != length)
222 (void) close(destination_file);
223 (void) close(source_file);
224 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
225 (void) RelinquishUniqueFileResource(destination);
229 (void) close(destination_file);
230 (void) close(source_file);
231 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 % A p p e n d I m a g e F o r m a t %
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 % AppendImageFormat() appends the image format type to the filename. If an
247 % extension to the file already exists, it is first removed.
249 % The format of the AppendImageFormat method is:
251 % void AppendImageFormat(const char *format,char *filename)
253 % A description of each parameter follows.
255 % o format: Specifies a pointer to an array of characters. This the
256 % format of the image.
258 % o filename: Specifies a pointer to an array of characters. The unique
259 % file name is returned in this array.
262 MagickExport void AppendImageFormat(const char *format,char *filename)
267 assert(format != (char *) NULL);
268 assert(filename != (char *) NULL);
269 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
270 if ((*format == '\0') || (*filename == '\0'))
272 if (LocaleCompare(filename,"-") == 0)
275 message[MaxTextExtent];
277 (void) FormatMagickString(message,MaxTextExtent,"%s:%s",format,filename);
278 (void) CopyMagickString(filename,message,MaxTextExtent);
281 GetPathComponent(filename,RootPath,root);
282 (void) FormatMagickString(filename,MaxTextExtent,"%s.%s",root,format);
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 % B a s e 6 4 D e c o d e %
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 % Base64Decode() decodes Base64-encoded text and returns its binary
297 % equivalent. NULL is returned if the text is not valid Base64 data, or a
298 % memory allocation failure occurs.
300 % The format of the Base64Decode method is:
302 % unsigned char *Base64Decode(const char *source,length_t *length)
304 % A description of each parameter follows:
306 % o source: A pointer to a Base64-encoded string.
308 % o length: the number of bytes decoded.
311 MagickExport unsigned char *Base64Decode(const char *source,size_t *length)
326 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
327 assert(source != (char *) NULL);
328 assert(length != (size_t *) NULL);
330 decode=(unsigned char *) AcquireQuantumMemory(strlen(source)/4+4,
332 if (decode == (unsigned char *) NULL)
333 return((unsigned char *) NULL);
336 for (p=source; *p != '\0'; p++)
338 if (isspace((int) ((unsigned char) *p)) != 0)
343 if (q == (char *) NULL)
345 decode=(unsigned char *) RelinquishMagickMemory(decode);
346 return((unsigned char *) NULL); /* non-Base64 character */
352 decode[i]=(q-Base64) << 2;
358 decode[i++]|=(q-Base64) >> 4;
359 decode[i]=((q-Base64) & 0x0f) << 4;
365 decode[i++]|=(q-Base64) >> 2;
366 decode[i]=((q-Base64) & 0x03) << 6;
372 decode[i++]|=(q-Base64);
379 Verify Base-64 string has proper terminal characters.
385 decode=(unsigned char *) RelinquishMagickMemory(decode);
386 return((unsigned char *) NULL);
398 Unrecognized '=' character.
400 decode=(unsigned char *) RelinquishMagickMemory(decode);
401 return((unsigned char *) NULL);
405 for ( ; *p != '\0'; p++)
406 if (isspace((int) ((unsigned char) *p)) == 0)
410 decode=(unsigned char *) RelinquishMagickMemory(decode);
411 return((unsigned char *) NULL);
417 for ( ; *p != '\0'; p++)
418 if (isspace((int) ((unsigned char) *p)) == 0)
420 decode=(unsigned char *) RelinquishMagickMemory(decode);
421 return((unsigned char *) NULL);
423 if ((int) decode[i] != 0)
425 decode=(unsigned char *) RelinquishMagickMemory(decode);
426 return((unsigned char *) NULL);
436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
440 % B a s e 6 4 E n c o d e %
444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
446 % Base64Encode() encodes arbitrary binary data to Base64 encoded format as
447 % described by the "Base64 Content-Transfer-Encoding" section of RFC 2045 and
448 % returns the result as a null-terminated ASCII string. NULL is returned if
449 % a memory allocation failure occurs.
451 % The format of the Base64Encode method is:
453 % char *Base64Encode(const unsigned char *blob,const size_t blob_length,
454 % size_t *encode_length)
456 % A description of each parameter follows:
458 % o blob: A pointer to binary data to encode.
460 % o blob_length: the number of bytes to encode.
462 % o encode_length: The number of bytes encoded.
465 MagickExport char *Base64Encode(const unsigned char *blob,
466 const size_t blob_length,size_t *encode_length)
471 register const unsigned char
480 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
481 assert(blob != (const unsigned char *) NULL);
482 assert(blob_length != 0);
483 assert(encode_length != (size_t *) NULL);
485 encode=(char *) AcquireQuantumMemory(blob_length/3+4,4*sizeof(*encode));
486 if (encode == (char *) NULL)
487 return((char *) NULL);
489 for (p=blob; p < (blob+blob_length-2); p+=3)
491 encode[i++]=Base64[(int) (*p >> 2)];
492 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
493 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
494 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
496 remainder=blob_length % 3;
508 for (j=0; j < (long) remainder; j++)
510 encode[i++]=Base64[(int) (code[0] >> 2)];
511 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
515 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
524 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
528 % C h o p P a t h C o m p o n e n t s %
532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
534 % ChopPathComponents() removes the number of specified file components from a
537 % The format of the ChopPathComponents method is:
539 % ChopPathComponents(char *path,unsigned long components)
541 % A description of each parameter follows:
545 % o components: The number of components to chop.
548 MagickExport void ChopPathComponents(char *path,const unsigned long components)
553 for (i=0; i < (long) components; i++)
554 GetPathComponent(path,HeadPath,path);
558 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
562 % E x p a n d F i l e n a m e %
566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
568 % ExpandFilename() expands '~' in a path.
570 % The format of the ExpandFilename function is:
572 % ExpandFilename(char *path)
574 % A description of each parameter follows:
576 % o path: Specifies a pointer to a character array that contains the
580 MagickExport void ExpandFilename(char *path)
583 expand_path[MaxTextExtent];
585 if (path == (char *) NULL)
589 (void) CopyMagickString(expand_path,path,MaxTextExtent);
590 if ((*(path+1) == *DirectorySeparator) || (*(path+1) == '\0'))
596 Substitute ~ with $HOME.
598 (void) CopyMagickString(expand_path,".",MaxTextExtent);
599 (void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
600 home=GetEnvironmentValue("HOME");
601 if (home == (char *) NULL)
602 home=GetEnvironmentValue("USERPROFILE");
603 if (home != (char *) NULL)
605 (void) CopyMagickString(expand_path,home,MaxTextExtent);
606 (void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
607 home=DestroyString(home);
612 #if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
614 username[MaxTextExtent];
623 Substitute ~ with home directory from password file.
625 (void) CopyMagickString(username,path+1,MaxTextExtent);
626 p=strchr(username,'/');
627 if (p != (char *) NULL)
629 entry=getpwnam(username);
630 if (entry == (struct passwd *) NULL)
632 (void) CopyMagickString(expand_path,entry->pw_dir,MaxTextExtent);
633 if (p != (char *) NULL)
635 (void) ConcatenateMagickString(expand_path,"/",MaxTextExtent);
636 (void) ConcatenateMagickString(expand_path,p+1,MaxTextExtent);
640 (void) CopyMagickString(path,expand_path,MaxTextExtent);
644 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
648 % E x p a n d F i l e n a m e s %
652 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
654 % ExpandFilenames() checks each argument of the command line vector and
655 % expands it if they have a wildcard character. For example, *.jpg might
656 % expand to: bird.jpg rose.jpg tiki.jpg.
658 % The format of the ExpandFilenames function is:
660 % status=ExpandFilenames(int *number_arguments,char ***arguments)
662 % A description of each parameter follows:
664 % o number_arguments: Specifies a pointer to an integer describing the
665 % number of elements in the argument vector.
667 % o arguments: Specifies a pointer to a text array containing the command
671 MagickExport MagickBooleanType ExpandFilenames(int *number_arguments,
676 home_directory[MaxTextExtent],
691 Allocate argument vector.
693 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
694 assert(number_arguments != (int *) NULL);
695 assert(arguments != (char ***) NULL);
696 vector=(char **) AcquireQuantumMemory((size_t) (*number_arguments+1),
698 if (vector == (char **) NULL)
699 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
701 Expand any wildcard filenames.
703 *home_directory='\0';
704 cwd=getcwd(home_directory,MaxTextExtent);
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 filelist=ListFiles(*path == '\0' ? home_directory : path,filename,
775 Generate file list from file list (e.g. @filelist.txt).
777 exception=AcquireExceptionInfo();
778 files=FileToString(filename+1,~0,exception);
779 exception=DestroyExceptionInfo(exception);
780 if (files == (char *) NULL)
783 filelist=StringToArgv(files,&number_images);
784 files=DestroyString(files);
785 number_files=(unsigned long) number_images;
786 if (filelist != (char **) NULL)
789 for (j=0; j < (long) number_files; j++)
790 filelist[j]=filelist[j+1];
793 if (filelist == (char **) NULL)
795 for (j=0; j < (long) number_files; j++)
796 if (IsPathDirectory(filelist[j]) <= 0)
798 if (j == (long) number_files)
800 for (j=0; j < (long) 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 < (long) number_files; j++)
814 (void) CopyMagickString(filename,path,MaxTextExtent);
816 (void) ConcatenateMagickString(filename,DirectorySeparator,
818 (void) ConcatenateMagickString(filename,filelist[j],MaxTextExtent);
819 filelist[j]=DestroyString(filelist[j]);
820 if (strlen(filename) >= MaxTextExtent)
821 ThrowFatalException(OptionFatalError,"FilenameTruncated");
822 if (IsPathDirectory(filename) == 0)
830 (void) ConcatenateMagickString(path,magick,MaxTextExtent);
831 (void) ConcatenateMagickString(path,":",MaxTextExtent);
833 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
834 if (*subimage != '\0')
836 (void) ConcatenateMagickString(path,"[",MaxTextExtent);
837 (void) ConcatenateMagickString(path,subimage,MaxTextExtent);
838 (void) ConcatenateMagickString(path,"]",MaxTextExtent);
840 if (strlen(path) >= MaxTextExtent)
841 ThrowFatalException(OptionFatalError,"FilenameTruncated");
842 if (destroy != MagickFalse)
845 vector[count]=DestroyString(vector[count]);
848 vector[count++]=ConstantString(path);
851 filelist=(char **) RelinquishMagickMemory(filelist);
853 vector[count]=(char *) NULL;
854 if (IsEventLogging() != MagickFalse)
859 command_line=AcquireString(vector[0]);
860 for (i=1; i < count; i++)
862 (void) ConcatenateString(&command_line," {");
863 (void) ConcatenateString(&command_line,vector[i]);
864 (void) ConcatenateString(&command_line,"}");
866 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
867 "Command line: %s",command_line);
868 command_line=DestroyString(command_line);
870 *number_arguments=(int) count;
876 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
880 % G e t E x e c u t i o n P a t h %
884 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
886 % GetExecutionPath() returns the pathname of the executable that started
887 % the process. On success MagickTrue is returned, otherwise MagickFalse.
889 % The format of the GetExecutionPath method is:
891 % MagickBooleanType GetExecutionPath(char *path,const size_t extent)
893 % A description of each parameter follows:
895 % o path: the pathname of the executable that started the process.
897 % o extent: the maximum extent of the path.
900 MagickExport MagickBooleanType GetExecutionPath(char *path,const size_t extent)
906 cwd=getcwd(path,(unsigned long) extent);
907 #if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK)
910 link_path[MaxTextExtent],
911 real_path[PATH_MAX+1];
916 (void) FormatMagickString(link_path,MaxTextExtent,"/proc/%ld/exe",
918 count=readlink(link_path,real_path,PATH_MAX);
921 (void) FormatMagickString(link_path,MaxTextExtent,"/proc/%ld/file",
923 count=readlink(link_path,real_path,PATH_MAX);
925 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
927 real_path[count]='\0';
928 (void) CopyMagickString(path,real_path,extent);
932 #if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
935 executable_path[PATH_MAX << 1],
936 real_path[PATH_MAX+1];
941 length=sizeof(executable_path);
942 if ((_NSGetExecutablePath(executable_path,&length) == 0) &&
943 (realpath(executable_path,real_path) != (char *) NULL))
944 (void) CopyMagickString(path,real_path,extent);
947 #if defined(MAGICKCORE_HAVE_GETEXECNAME)
952 execution_path=(const char *) getexecname();
953 if (execution_path != (const char *) NULL)
955 if (*execution_path != *DirectorySeparator)
956 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
957 (void) ConcatenateMagickString(path,execution_path,extent);
961 #if defined(__WINDOWS__)
962 NTGetExecutionPath(path,extent);
964 return(IsPathAccessible(path));
968 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
972 % G e t P a t h A t t r i b u t e s %
976 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
978 % GetPathAttributes() returns attributes (e.g. size of file) about a path.
980 % The path of the GetPathAttributes method is:
982 % MagickBooleanType GetPathAttributes(const char *path,void *attributes)
984 % A description of each parameter follows.
986 % o path: the file path.
988 % o attributes: the path attributes are returned here.
992 #if defined(MAGICKCORE_HAVE__WFOPEN)
993 static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16)
995 register const unsigned char
998 if (utf16 != (wchar_t *) NULL)
1007 Convert UTF-8 to UTF-16.
1010 for (p=utf8; *p != '\0'; p++)
1012 if ((*p & 0x80) == 0)
1015 if ((*p & 0xE0) == 0xC0)
1020 if ((*p & 0xC0) != 0x80)
1025 if ((*p & 0xF0) == 0xE0)
1030 if ((*p & 0xC0) != 0x80)
1033 *q|=(c & 0x3F) << 6;
1035 if ((*p & 0xC0) != 0x80)
1047 Compute UTF-16 string length.
1049 for (p=utf8; *p != '\0'; p++)
1051 if ((*p & 0x80) == 0)
1054 if ((*p & 0xE0) == 0xC0)
1057 if ((*p & 0xC0) != 0x80)
1061 if ((*p & 0xF0) == 0xE0)
1064 if ((*p & 0xC0) != 0x80)
1067 if ((*p & 0xC0) != 0x80)
1076 static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source)
1084 length=UTF8ToUTF16(source,(wchar_t *) NULL);
1091 Not UTF-8, just copy.
1093 length=strlen(source);
1094 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
1095 if (utf16 == (wchar_t *) NULL)
1096 return((wchar_t *) NULL);
1097 for (i=0; i <= (long) length; i++)
1101 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
1102 if (utf16 == (wchar_t *) NULL)
1103 return((wchar_t *) NULL);
1104 length=UTF8ToUTF16(source,utf16);
1109 MagickExport MagickBooleanType GetPathAttributes(const char *path,
1115 if (path == (const char *) NULL)
1118 return(MagickFalse);
1120 #if !defined(MAGICKCORE_HAVE__WSTAT)
1121 status=stat(path,(struct stat *) attributes) == 0 ? MagickTrue : MagickFalse;
1127 unicode_path=ConvertUTF8ToUTF16(path);
1128 if (unicode_path == (wchar_t *) NULL)
1129 return(MagickFalse);
1130 status=wstat(unicode_path,(struct stat *) attributes) == 0 ? MagickTrue :
1132 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
1139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1143 % G e t P a t h C o m p o n e n t %
1147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1149 % GetPathComponent() returns the parent directory name, filename, basename, or
1150 % extension of a file path.
1152 % The format of the GetPathComponent function is:
1154 % GetPathComponent(const char *path,PathType type,char *component)
1156 % A description of each parameter follows:
1158 % o path: Specifies a pointer to a character array that contains the
1161 % o type: Specififies which file path component to return.
1163 % o component: the selected file path component is returned here.
1166 MagickExport void GetPathComponent(const char *path,PathType type,
1170 magick[MaxTextExtent],
1172 subimage[MaxTextExtent];
1177 assert(path != (const char *) NULL);
1178 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",path);
1179 assert(component != (char *) NULL);
1185 (void) CopyMagickString(component,path,MaxTextExtent);
1187 #if defined(__OS2__)
1190 for (p=component; *p != '\0'; p++)
1191 if ((*p == ':') && (IsPathDirectory(path) < 0) &&
1192 (IsPathAccessible(path) == MagickFalse))
1195 Look for image format specification (e.g. ps3:image).
1197 (void) CopyMagickString(magick,component,(size_t) (p-component+1));
1198 if (IsMagickConflict(magick) != MagickFalse)
1201 for (q=component; *q != '\0'; q++)
1208 p=component+strlen(component)-1;
1209 if ((*p == ']') && (strchr(component,'[') != (char *) NULL) &&
1210 (IsPathAccessible(path) == MagickFalse))
1213 Look for scene specification (e.g. img0001.pcd[4]).
1215 for (q=p-1; q > component; q--)
1220 (void) CopyMagickString(subimage,q+1,MaxTextExtent);
1221 subimage[p-q-1]='\0';
1222 if ((IsSceneGeometry(subimage,MagickFalse) == MagickFalse) &&
1223 (IsGeometry(subimage) == MagickFalse))
1231 for (p=component+strlen(component)-1; p > component; p--)
1232 if (IsBasenameSeparator(*p) != MagickFalse)
1238 (void) CopyMagickString(component,magick,MaxTextExtent);
1243 for (p=component+(strlen(component)-1); p > component; p--)
1245 if (IsBasenameSeparator(*p) != MagickFalse)
1261 if (IsBasenameSeparator(*p) != MagickFalse)
1262 (void) CopyMagickMemory((unsigned char *) component,
1263 (const unsigned char *) (p+1),strlen(p+1)+1);
1268 if (IsBasenameSeparator(*p) != MagickFalse)
1269 (void) CopyMagickString(component,p+1,MaxTextExtent);
1270 for (p=component+(strlen(component)-1); p > component; p--)
1280 if (IsBasenameSeparator(*p) != MagickFalse)
1281 (void) CopyMagickString(component,p+1,MaxTextExtent);
1284 for (p=component+strlen(component)-1; p > component; p--)
1289 (void) CopyMagickString(component,p+1,MaxTextExtent);
1294 (void) CopyMagickString(component,subimage,MaxTextExtent);
1304 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1308 % G e t P a t h C o m p o n e n t s %
1312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1314 % GetPathComponents() returns a list of path components.
1316 % The format of the GetPathComponents method is:
1318 % char **GetPathComponents(const char *path,
1319 % unsigned long *number_componenets)
1321 % A description of each parameter follows:
1323 % o path: Specifies the string to segment into a list.
1325 % o number_components: return the number of components in the list
1328 MagickExport char **GetPathComponents(const char *path,
1329 unsigned long *number_components)
1341 if (path == (char *) NULL)
1342 return((char **) NULL);
1343 *number_components=1;
1344 for (p=path; *p != '\0'; p++)
1345 if (IsBasenameSeparator(*p))
1346 (*number_components)++;
1347 components=(char **) AcquireQuantumMemory((size_t) *number_components+1UL,
1348 sizeof(*components));
1349 if (components == (char **) NULL)
1350 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1352 for (i=0; i < (long) *number_components; i++)
1354 for (q=p; *q != '\0'; q++)
1355 if (IsBasenameSeparator(*q))
1357 components[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
1358 sizeof(*components));
1359 if (components[i] == (char *) NULL)
1360 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1361 (void) CopyMagickString(components[i],p,(size_t) (q-p+1));
1364 components[i]=(char *) NULL;
1369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1373 % I s P a t h A c c e s s i b l e %
1377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1379 % IsPathAccessible() returns MagickTrue if the file as defined by the path is
1382 % The format of the IsPathAccessible method is:
1384 % MagickBooleanType IsPathAccessible(const char *filename)
1386 % A description of each parameter follows.
1388 % o path: Specifies a path to a file.
1391 MagickExport MagickBooleanType IsPathAccessible(const char *path)
1399 if ((path == (const char *) NULL) || (*path == '\0'))
1400 return(MagickFalse);
1401 status=GetPathAttributes(path,&attributes);
1402 if (status == MagickFalse)
1404 if (S_ISREG(attributes.st_mode) == 0)
1405 return(MagickFalse);
1406 if (access(path,F_OK) != 0)
1407 return(MagickFalse);
1412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1416 + I s P a t h D i r e c t o r y %
1420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1422 % IsPathDirectory() returns -1 if the directory does not exist, 1 is returned
1423 % if the path represents a directory otherwise 0.
1425 % The format of the IsPathDirectory method is:
1427 % int IsPathDirectory(const char *path)
1429 % A description of each parameter follows.
1431 % o path: The directory path.
1434 static int IsPathDirectory(const char *path)
1442 if ((path == (const char *) NULL) || (*path == '\0'))
1443 return(MagickFalse);
1444 status=GetPathAttributes(path,&attributes);
1445 if (status == MagickFalse)
1447 if (S_ISDIR(attributes.st_mode) == 0)
1453 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1457 % I s M a g i c k T r u e %
1461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1463 % IsMagickTrue() returns MagickTrue if the value is "true", "on", "yes" or
1466 % The format of the IsMagickTrue method is:
1468 % MagickBooleanType IsMagickTrue(const char *value)
1470 % A description of each parameter follows:
1472 % o option: either MagickTrue or MagickFalse depending on the value
1475 % o value: Specifies a pointer to a character array.
1478 MagickExport MagickBooleanType IsMagickTrue(const char *value)
1480 if (value == (const char *) NULL)
1481 return(MagickFalse);
1482 if (LocaleCompare(value,"true") == 0)
1484 if (LocaleCompare(value,"on") == 0)
1486 if (LocaleCompare(value,"yes") == 0)
1488 if (LocaleCompare(value,"1") == 0)
1490 return(MagickFalse);
1494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1498 % L i s t F i l e s %
1502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1504 % ListFiles() reads the directory specified and returns a list of filenames
1505 % contained in the directory sorted in ascending alphabetic order.
1507 % The format of the ListFiles function is:
1509 % char **ListFiles(const char *directory,const char *pattern,
1510 % long *number_entries)
1512 % A description of each parameter follows:
1514 % o filelist: Method ListFiles returns a list of filenames contained
1515 % in the directory. If the directory specified cannot be read or it is
1516 % a file a NULL list is returned.
1518 % o directory: Specifies a pointer to a text string containing a directory
1521 % o pattern: Specifies a pointer to a text string containing a pattern.
1523 % o number_entries: This integer returns the number of filenames in the
1528 #if defined(__cplusplus) || defined(c_plusplus)
1532 static int FileCompare(const void *x,const void *y)
1538 p=(const char **) x;
1539 q=(const char **) y;
1540 return(LocaleCompare(*p,*q));
1543 #if defined(__cplusplus) || defined(c_plusplus)
1547 static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
1548 struct dirent **result)
1550 #if defined(MAGICKCORE_HAVE_READDIR_R)
1551 return(readdir_r(directory,entry,result));
1555 *result=readdir(directory);
1560 MagickExport char **ListFiles(const char *directory,const char *pattern,
1561 unsigned long *number_entries)
1579 assert(directory != (const char *) NULL);
1580 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",directory);
1581 assert(pattern != (const char *) NULL);
1582 assert(number_entries != (unsigned long *) NULL);
1584 current_directory=opendir(directory);
1585 if (current_directory == (DIR *) NULL)
1586 return((char **) NULL);
1591 filelist=(char **) AcquireQuantumMemory((size_t) max_entries,
1593 if (filelist == (char **) NULL)
1595 (void) closedir(current_directory);
1596 return((char **) NULL);
1599 Save the current and change to the new directory.
1601 buffer=(struct dirent *) AcquireMagickMemory(sizeof(*buffer)+FILENAME_MAX+1);
1602 if (buffer == (struct dirent *) NULL)
1603 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1604 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1605 (entry != (struct dirent *) NULL))
1607 if (*entry->d_name == '.')
1609 if ((IsPathDirectory(entry->d_name) > 0) ||
1610 #if defined(__WINDOWS__)
1611 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1613 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1616 if (*number_entries >= max_entries)
1619 Extend the file list.
1622 filelist=(char **) ResizeQuantumMemory(filelist,(size_t)
1623 max_entries,sizeof(*filelist));
1624 if (filelist == (char **) NULL)
1632 p=strchr(entry->d_name,';');
1635 if (*number_entries > 0)
1636 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1640 filelist[*number_entries]=(char *) AcquireString(entry->d_name);
1641 if (IsPathDirectory(entry->d_name) > 0)
1642 (void) ConcatenateMagickString(filelist[*number_entries],
1643 DirectorySeparator,MaxTextExtent);
1644 (*number_entries)++;
1647 buffer=(struct dirent *) RelinquishMagickMemory(buffer);
1648 (void) closedir(current_directory);
1649 if (filelist == (char **) NULL)
1650 return((char **) NULL);
1652 Sort filelist in ascending order.
1654 qsort((void *) filelist,(size_t) *number_entries,sizeof(*filelist),
1660 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1664 % M u l t i l i n e C e n s u s %
1668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1670 % MultilineCensus() returns the number of lines within a label. A line is
1671 % represented by a \n character.
1673 % The format of the MultilineCenus method is:
1675 % unsigned long MultilineCensus(const char *label)
1677 % A description of each parameter follows.
1679 % o label: This character string is the label.
1683 MagickExport unsigned long MultilineCensus(const char *label)
1689 Determine the number of lines within this label.
1691 if (label == (char *) NULL)
1693 for (number_lines=1; *label != '\0'; label++)
1696 return(number_lines);
1700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1704 % O p e n M a g i c k S t r e a m %
1708 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1710 % OpenMagickStream() opens the file at the specified path and return the
1711 % associated stream.
1713 % The path of the OpenMagickStream method is:
1715 % FILE *OpenMagickStream(const char *path,const char *mode)
1717 % A description of each parameter follows.
1719 % o path: the file path.
1721 % o mode: the file mode.
1724 MagickExport FILE *OpenMagickStream(const char *path,const char *mode)
1729 if ((path == (const char *) NULL) || (mode == (const char *) NULL))
1732 return((FILE *) NULL);
1735 #if defined(MAGICKCORE_HAVE__WFOPEN)
1741 unicode_path=ConvertUTF8ToUTF16(path);
1742 if (unicode_path == (wchar_t *) NULL)
1743 return((FILE *) NULL);
1744 unicode_mode=ConvertUTF8ToUTF16(mode);
1745 if (unicode_mode == (wchar_t *) NULL)
1747 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
1748 return((FILE *) NULL);
1750 file=_wfopen(unicode_path,unicode_mode);
1751 unicode_mode=(wchar_t *) RelinquishMagickMemory(unicode_mode);
1752 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
1755 if (file == (FILE *) NULL)
1756 file=fopen(path,mode);
1761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1765 % S y s t e m C o m m a n d %
1769 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1771 % SystemCommand() executes the specified command and waits until it
1772 % terminates. The returned value is the exit status of the command.
1774 % The format of the SystemCommand method is:
1776 % int SystemCommand(const MagickBooleanType verbose,const char *command)
1778 % A description of each parameter follows:
1780 % o verbose: A value other than 0 prints the executed command before it is
1783 % o command: This string is the command to execute.
1786 MagickExport int SystemCommand(const MagickBooleanType verbose,
1787 const char *command)
1792 if (verbose != MagickFalse)
1794 (void) fprintf(stderr,"%s\n",command);
1795 (void) fflush(stderr);
1798 #if defined(MAGICKCORE_POSIX_SUPPORT)
1799 #if !defined(MAGICKCORE_HAVE_EXECVP)
1800 status=system(command);
1802 if (strspn(command,"&;<>|") == 0)
1803 status=system(command);
1812 arguments=StringToArgv(command,&number_arguments);
1813 if (arguments == (char **) NULL)
1814 status=system(command);
1824 Call application directly rather than from a shell.
1827 if (child_pid == (pid_t) -1)
1828 status=system(command);
1832 status=execvp(arguments[1],arguments+1);
1844 pid=waitpid(child_pid,&child_status,0);
1849 if (WIFEXITED(child_status) != 0)
1850 status=WEXITSTATUS(child_status);
1852 if (WIFSIGNALED(child_status))
1856 for (i=0; i < number_arguments; i++)
1857 arguments[i]=DestroyString(arguments[i]);
1858 arguments=(char **) RelinquishMagickMemory(arguments);
1862 #elif defined(__WINDOWS__)
1863 status=NTSystemCommand(command);
1864 #elif defined(macintosh)
1865 status=MACSystemCommand(command);
1867 status=system(command);
1869 # error No suitable system() method.
1879 exception=AcquireExceptionInfo();
1880 message=GetExceptionMessage(errno);
1881 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1882 "`%s': %s",command,message);
1883 message=DestroyString(message);
1884 CatchException(exception);
1885 exception=DestroyExceptionInfo(exception);