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/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';
705 cwd=getcwd(home_directory,MaxTextExtent);
707 for (i=0; i < (long) *number_arguments; i++)
711 filename[MaxTextExtent],
712 magick[MaxTextExtent],
715 subimage[MaxTextExtent];
720 option=(*arguments)[i];
725 vector[count++]=ConstantString(option);
727 parameters=ParseMagickOption(MagickCommandOptions,MagickFalse,option);
731 Do not expand command option parameters.
733 for (j=0; j < parameters; j++)
736 if (i == (long) *number_arguments)
738 option=(*arguments)[i];
739 vector[count++]=ConstantString(option);
743 if ((*option == '"') || (*option == '\''))
745 GetPathComponent(option,TailPath,filename);
746 GetPathComponent(option,MagickPath,magick);
747 if ((LocaleCompare(magick,"CAPTION") == 0) ||
748 (LocaleCompare(magick,"LABEL") == 0) ||
749 (LocaleCompare(magick,"VID") == 0))
751 if ((IsGlob(filename) == MagickFalse) && (*filename != '@'))
753 if (*filename != '@')
756 Generate file list from wildcard filename (e.g. *.jpg).
758 GetPathComponent(option,HeadPath,path);
759 GetPathComponent(option,SubimagePath,subimage);
760 ExpandFilename(path);
761 filelist=ListFiles(*path == '\0' ? home_directory : path,filename,
776 Generate file list from file list (e.g. @filelist.txt).
778 exception=AcquireExceptionInfo();
779 files=FileToString(filename+1,~0,exception);
780 exception=DestroyExceptionInfo(exception);
781 if (files == (char *) NULL)
784 filelist=StringToArgv(files,&number_images);
785 files=DestroyString(files);
786 number_files=(unsigned long) number_images;
787 if (filelist != (char **) NULL)
790 for (j=0; j < (long) number_files; j++)
791 filelist[j]=filelist[j+1];
794 if (filelist == (char **) NULL)
796 for (j=0; j < (long) number_files; j++)
797 if (IsPathDirectory(filelist[j]) <= 0)
799 if (j == (long) number_files)
801 for (j=0; j < (long) number_files; j++)
802 filelist[j]=DestroyString(filelist[j]);
803 filelist=(char **) RelinquishMagickMemory(filelist);
807 Transfer file list to argument vector.
809 vector=(char **) ResizeQuantumMemory(vector,(size_t) *number_arguments+
810 count+number_files+1,sizeof(*vector));
811 if (vector == (char **) NULL)
813 for (j=0; j < (long) number_files; j++)
815 (void) CopyMagickString(filename,path,MaxTextExtent);
817 (void) ConcatenateMagickString(filename,DirectorySeparator,
819 (void) ConcatenateMagickString(filename,filelist[j],MaxTextExtent);
820 filelist[j]=DestroyString(filelist[j]);
821 if (strlen(filename) >= MaxTextExtent)
822 ThrowFatalException(OptionFatalError,"FilenameTruncated");
823 if (IsPathDirectory(filename) == 0)
831 (void) ConcatenateMagickString(path,magick,MaxTextExtent);
832 (void) ConcatenateMagickString(path,":",MaxTextExtent);
834 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
835 if (*subimage != '\0')
837 (void) ConcatenateMagickString(path,"[",MaxTextExtent);
838 (void) ConcatenateMagickString(path,subimage,MaxTextExtent);
839 (void) ConcatenateMagickString(path,"]",MaxTextExtent);
841 if (strlen(path) >= MaxTextExtent)
842 ThrowFatalException(OptionFatalError,"FilenameTruncated");
843 if (destroy != MagickFalse)
846 vector[count]=DestroyString(vector[count]);
849 vector[count++]=ConstantString(path);
852 filelist=(char **) RelinquishMagickMemory(filelist);
854 vector[count]=(char *) NULL;
855 if (IsEventLogging() != MagickFalse)
860 command_line=AcquireString(vector[0]);
861 for (i=1; i < count; i++)
863 (void) ConcatenateString(&command_line," {");
864 (void) ConcatenateString(&command_line,vector[i]);
865 (void) ConcatenateString(&command_line,"}");
867 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
868 "Command line: %s",command_line);
869 command_line=DestroyString(command_line);
871 *number_arguments=(int) count;
877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
881 % G e t E x e c u t i o n P a t h %
885 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
887 % GetExecutionPath() returns the pathname of the executable that started
888 % the process. On success MagickTrue is returned, otherwise MagickFalse.
890 % The format of the GetExecutionPath method is:
892 % MagickBooleanType GetExecutionPath(char *path,const size_t extent)
894 % A description of each parameter follows:
896 % o path: the pathname of the executable that started the process.
898 % o extent: the maximum extent of the path.
901 MagickExport MagickBooleanType GetExecutionPath(char *path,const size_t extent)
907 cwd=getcwd(path,(unsigned long) extent);
908 #if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK)
911 link_path[MaxTextExtent],
912 real_path[PATH_MAX+1];
917 (void) FormatMagickString(link_path,MaxTextExtent,"/proc/%ld/exe",
919 count=readlink(link_path,real_path,PATH_MAX);
922 (void) FormatMagickString(link_path,MaxTextExtent,"/proc/%ld/file",
924 count=readlink(link_path,real_path,PATH_MAX);
926 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
928 real_path[count]='\0';
929 (void) CopyMagickString(path,real_path,extent);
933 #if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
936 executable_path[PATH_MAX << 1],
937 real_path[PATH_MAX+1];
942 length=sizeof(executable_path);
943 if ((_NSGetExecutablePath(executable_path,&length) == 0) &&
944 (realpath(executable_path,real_path) != (char *) NULL))
945 (void) CopyMagickString(path,real_path,extent);
948 #if defined(MAGICKCORE_HAVE_GETEXECNAME)
953 execution_path=(const char *) getexecname();
954 if (execution_path != (const char *) NULL)
956 if (*execution_path != *DirectorySeparator)
957 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
958 (void) ConcatenateMagickString(path,execution_path,extent);
962 #if defined(__WINDOWS__)
963 NTGetExecutionPath(path,extent);
965 return(IsPathAccessible(path));
969 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
973 % G e t P a t h A t t r i b u t e s %
977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
979 % GetPathAttributes() returns attributes (e.g. size of file) about a path.
981 % The path of the GetPathAttributes method is:
983 % MagickBooleanType GetPathAttributes(const char *path,void *attributes)
985 % A description of each parameter follows.
987 % o path: the file path.
989 % o attributes: the path attributes are returned here.
993 #if defined(MAGICKCORE_HAVE__WFOPEN)
994 static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16)
996 register const unsigned char
999 if (utf16 != (wchar_t *) NULL)
1008 Convert UTF-8 to UTF-16.
1011 for (p=utf8; *p != '\0'; p++)
1013 if ((*p & 0x80) == 0)
1016 if ((*p & 0xE0) == 0xC0)
1021 if ((*p & 0xC0) != 0x80)
1026 if ((*p & 0xF0) == 0xE0)
1031 if ((*p & 0xC0) != 0x80)
1034 *q|=(c & 0x3F) << 6;
1036 if ((*p & 0xC0) != 0x80)
1048 Compute UTF-16 string length.
1050 for (p=utf8; *p != '\0'; p++)
1052 if ((*p & 0x80) == 0)
1055 if ((*p & 0xE0) == 0xC0)
1058 if ((*p & 0xC0) != 0x80)
1062 if ((*p & 0xF0) == 0xE0)
1065 if ((*p & 0xC0) != 0x80)
1068 if ((*p & 0xC0) != 0x80)
1077 static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source)
1085 length=UTF8ToUTF16(source,(wchar_t *) NULL);
1092 Not UTF-8, just copy.
1094 length=strlen(source);
1095 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
1096 if (utf16 == (wchar_t *) NULL)
1097 return((wchar_t *) NULL);
1098 for (i=0; i <= (long) length; i++)
1102 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
1103 if (utf16 == (wchar_t *) NULL)
1104 return((wchar_t *) NULL);
1105 length=UTF8ToUTF16(source,utf16);
1110 MagickExport MagickBooleanType GetPathAttributes(const char *path,
1116 if (path == (const char *) NULL)
1119 return(MagickFalse);
1121 #if !defined(MAGICKCORE_HAVE__WSTAT)
1122 status=stat(path,(struct stat *) attributes) == 0 ? MagickTrue : MagickFalse;
1128 unicode_path=ConvertUTF8ToUTF16(path);
1129 if (unicode_path == (wchar_t *) NULL)
1130 return(MagickFalse);
1131 status=wstat(unicode_path,(struct stat *) attributes) == 0 ? MagickTrue :
1133 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
1140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1144 % G e t P a t h C o m p o n e n t %
1148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1150 % GetPathComponent() returns the parent directory name, filename, basename, or
1151 % extension of a file path.
1153 % The format of the GetPathComponent function is:
1155 % GetPathComponent(const char *path,PathType type,char *component)
1157 % A description of each parameter follows:
1159 % o path: Specifies a pointer to a character array that contains the
1162 % o type: Specififies which file path component to return.
1164 % o component: the selected file path component is returned here.
1167 MagickExport void GetPathComponent(const char *path,PathType type,
1171 magick[MaxTextExtent],
1173 subimage[MaxTextExtent];
1178 assert(path != (const char *) NULL);
1179 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",path);
1180 assert(component != (char *) NULL);
1186 (void) CopyMagickString(component,path,MaxTextExtent);
1188 #if defined(__OS2__)
1191 for (p=component; *p != '\0'; p++)
1192 if ((*p == ':') && (IsPathDirectory(path) < 0) &&
1193 (IsPathAccessible(path) == MagickFalse))
1196 Look for image format specification (e.g. ps3:image).
1198 (void) CopyMagickString(magick,component,(size_t) (p-component+1));
1199 if (IsMagickConflict(magick) != MagickFalse)
1202 for (q=component; *q != '\0'; q++)
1209 p=component+strlen(component)-1;
1210 if ((*p == ']') && (strchr(component,'[') != (char *) NULL) &&
1211 (IsPathAccessible(path) == MagickFalse))
1214 Look for scene specification (e.g. img0001.pcd[4]).
1216 for (q=p-1; q > component; q--)
1221 (void) CopyMagickString(subimage,q+1,MaxTextExtent);
1222 subimage[p-q-1]='\0';
1223 if ((IsSceneGeometry(subimage,MagickFalse) == MagickFalse) &&
1224 (IsGeometry(subimage) == MagickFalse))
1232 for (p=component+strlen(component)-1; p > component; p--)
1233 if (IsBasenameSeparator(*p) != MagickFalse)
1239 (void) CopyMagickString(component,magick,MaxTextExtent);
1244 for (p=component+(strlen(component)-1); p > component; p--)
1246 if (IsBasenameSeparator(*p) != MagickFalse)
1262 if (IsBasenameSeparator(*p) != MagickFalse)
1263 (void) CopyMagickMemory((unsigned char *) component,
1264 (const unsigned char *) (p+1),strlen(p+1)+1);
1269 if (IsBasenameSeparator(*p) != MagickFalse)
1270 (void) CopyMagickString(component,p+1,MaxTextExtent);
1271 for (p=component+(strlen(component)-1); p > component; p--)
1281 if (IsBasenameSeparator(*p) != MagickFalse)
1282 (void) CopyMagickString(component,p+1,MaxTextExtent);
1285 for (p=component+strlen(component)-1; p > component; p--)
1290 (void) CopyMagickString(component,p+1,MaxTextExtent);
1295 (void) CopyMagickString(component,subimage,MaxTextExtent);
1305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1309 % G e t P a t h C o m p o n e n t s %
1313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1315 % GetPathComponents() returns a list of path components.
1317 % The format of the GetPathComponents method is:
1319 % char **GetPathComponents(const char *path,
1320 % unsigned long *number_componenets)
1322 % A description of each parameter follows:
1324 % o path: Specifies the string to segment into a list.
1326 % o number_components: return the number of components in the list
1329 MagickExport char **GetPathComponents(const char *path,
1330 unsigned long *number_components)
1342 if (path == (char *) NULL)
1343 return((char **) NULL);
1344 *number_components=1;
1345 for (p=path; *p != '\0'; p++)
1346 if (IsBasenameSeparator(*p))
1347 (*number_components)++;
1348 components=(char **) AcquireQuantumMemory((size_t) *number_components+1UL,
1349 sizeof(*components));
1350 if (components == (char **) NULL)
1351 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1353 for (i=0; i < (long) *number_components; i++)
1355 for (q=p; *q != '\0'; q++)
1356 if (IsBasenameSeparator(*q))
1358 components[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
1359 sizeof(*components));
1360 if (components[i] == (char *) NULL)
1361 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1362 (void) CopyMagickString(components[i],p,(size_t) (q-p+1));
1365 components[i]=(char *) NULL;
1370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1374 % I s P a t h A c c e s s i b l e %
1378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1380 % IsPathAccessible() returns MagickTrue if the file as defined by the path is
1383 % The format of the IsPathAccessible method is:
1385 % MagickBooleanType IsPathAccessible(const char *filename)
1387 % A description of each parameter follows.
1389 % o path: Specifies a path to a file.
1392 MagickExport MagickBooleanType IsPathAccessible(const char *path)
1400 if ((path == (const char *) NULL) || (*path == '\0'))
1401 return(MagickFalse);
1402 status=GetPathAttributes(path,&attributes);
1403 if (status == MagickFalse)
1405 if (S_ISREG(attributes.st_mode) == 0)
1406 return(MagickFalse);
1407 if (access(path,F_OK) != 0)
1408 return(MagickFalse);
1413 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1417 + I s P a t h D i r e c t o r y %
1421 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1423 % IsPathDirectory() returns -1 if the directory does not exist, 1 is returned
1424 % if the path represents a directory otherwise 0.
1426 % The format of the IsPathDirectory method is:
1428 % int IsPathDirectory(const char *path)
1430 % A description of each parameter follows.
1432 % o path: The directory path.
1435 static int IsPathDirectory(const char *path)
1443 if ((path == (const char *) NULL) || (*path == '\0'))
1444 return(MagickFalse);
1445 status=GetPathAttributes(path,&attributes);
1446 if (status == MagickFalse)
1448 if (S_ISDIR(attributes.st_mode) == 0)
1454 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1458 % I s M a g i c k T r u e %
1462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1464 % IsMagickTrue() returns MagickTrue if the value is "true", "on", "yes" or
1467 % The format of the IsMagickTrue method is:
1469 % MagickBooleanType IsMagickTrue(const char *value)
1471 % A description of each parameter follows:
1473 % o option: either MagickTrue or MagickFalse depending on the value
1476 % o value: Specifies a pointer to a character array.
1479 MagickExport MagickBooleanType IsMagickTrue(const char *value)
1481 if (value == (const char *) NULL)
1482 return(MagickFalse);
1483 if (LocaleCompare(value,"true") == 0)
1485 if (LocaleCompare(value,"on") == 0)
1487 if (LocaleCompare(value,"yes") == 0)
1489 if (LocaleCompare(value,"1") == 0)
1491 return(MagickFalse);
1495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1499 % L i s t F i l e s %
1503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1505 % ListFiles() reads the directory specified and returns a list of filenames
1506 % contained in the directory sorted in ascending alphabetic order.
1508 % The format of the ListFiles function is:
1510 % char **ListFiles(const char *directory,const char *pattern,
1511 % long *number_entries)
1513 % A description of each parameter follows:
1515 % o filelist: Method ListFiles returns a list of filenames contained
1516 % in the directory. If the directory specified cannot be read or it is
1517 % a file a NULL list is returned.
1519 % o directory: Specifies a pointer to a text string containing a directory
1522 % o pattern: Specifies a pointer to a text string containing a pattern.
1524 % o number_entries: This integer returns the number of filenames in the
1529 #if defined(__cplusplus) || defined(c_plusplus)
1533 static int FileCompare(const void *x,const void *y)
1539 p=(const char **) x;
1540 q=(const char **) y;
1541 return(LocaleCompare(*p,*q));
1544 #if defined(__cplusplus) || defined(c_plusplus)
1548 static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
1549 struct dirent **result)
1551 #if defined(MAGICKCORE_HAVE_READDIR_R)
1552 return(readdir_r(directory,entry,result));
1556 *result=readdir(directory);
1561 MagickExport char **ListFiles(const char *directory,const char *pattern,
1562 unsigned long *number_entries)
1580 assert(directory != (const char *) NULL);
1581 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",directory);
1582 assert(pattern != (const char *) NULL);
1583 assert(number_entries != (unsigned long *) NULL);
1585 current_directory=opendir(directory);
1586 if (current_directory == (DIR *) NULL)
1587 return((char **) NULL);
1592 filelist=(char **) AcquireQuantumMemory((size_t) max_entries,
1594 if (filelist == (char **) NULL)
1596 (void) closedir(current_directory);
1597 return((char **) NULL);
1600 Save the current and change to the new directory.
1602 buffer=(struct dirent *) AcquireMagickMemory(sizeof(*buffer)+FILENAME_MAX+1);
1603 if (buffer == (struct dirent *) NULL)
1604 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1605 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1606 (entry != (struct dirent *) NULL))
1608 if (*entry->d_name == '.')
1610 if ((IsPathDirectory(entry->d_name) > 0) ||
1611 #if defined(__WINDOWS__)
1612 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1614 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1617 if (*number_entries >= max_entries)
1620 Extend the file list.
1623 filelist=(char **) ResizeQuantumMemory(filelist,(size_t)
1624 max_entries,sizeof(*filelist));
1625 if (filelist == (char **) NULL)
1633 p=strchr(entry->d_name,';');
1636 if (*number_entries > 0)
1637 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1641 filelist[*number_entries]=(char *) AcquireString(entry->d_name);
1642 if (IsPathDirectory(entry->d_name) > 0)
1643 (void) ConcatenateMagickString(filelist[*number_entries],
1644 DirectorySeparator,MaxTextExtent);
1645 (*number_entries)++;
1648 buffer=(struct dirent *) RelinquishMagickMemory(buffer);
1649 (void) closedir(current_directory);
1650 if (filelist == (char **) NULL)
1651 return((char **) NULL);
1653 Sort filelist in ascending order.
1655 qsort((void *) filelist,(size_t) *number_entries,sizeof(*filelist),
1661 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1665 % M u l t i l i n e C e n s u s %
1669 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1671 % MultilineCensus() returns the number of lines within a label. A line is
1672 % represented by a \n character.
1674 % The format of the MultilineCenus method is:
1676 % unsigned long MultilineCensus(const char *label)
1678 % A description of each parameter follows.
1680 % o label: This character string is the label.
1684 MagickExport unsigned long MultilineCensus(const char *label)
1690 Determine the number of lines within this label.
1692 if (label == (char *) NULL)
1694 for (number_lines=1; *label != '\0'; label++)
1697 return(number_lines);
1701 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1705 % O p e n M a g i c k S t r e a m %
1709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1711 % OpenMagickStream() opens the file at the specified path and return the
1712 % associated stream.
1714 % The path of the OpenMagickStream method is:
1716 % FILE *OpenMagickStream(const char *path,const char *mode)
1718 % A description of each parameter follows.
1720 % o path: the file path.
1722 % o mode: the file mode.
1725 MagickExport FILE *OpenMagickStream(const char *path,const char *mode)
1730 if ((path == (const char *) NULL) || (mode == (const char *) NULL))
1733 return((FILE *) NULL);
1736 #if defined(MAGICKCORE_HAVE__WFOPEN)
1742 unicode_path=ConvertUTF8ToUTF16(path);
1743 if (unicode_path == (wchar_t *) NULL)
1744 return((FILE *) NULL);
1745 unicode_mode=ConvertUTF8ToUTF16(mode);
1746 if (unicode_mode == (wchar_t *) NULL)
1748 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
1749 return((FILE *) NULL);
1751 file=_wfopen(unicode_path,unicode_mode);
1752 unicode_mode=(wchar_t *) RelinquishMagickMemory(unicode_mode);
1753 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
1756 if (file == (FILE *) NULL)
1757 file=fopen(path,mode);
1762 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1766 % S y s t e m C o m m a n d %
1770 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1772 % SystemCommand() executes the specified command and waits until it
1773 % terminates. The returned value is the exit status of the command.
1775 % The format of the SystemCommand method is:
1777 % int SystemCommand(const MagickBooleanType verbose,const char *command,
1778 % ExceptionInfo *exception)
1780 % A description of each parameter follows:
1782 % o verbose: a value other than 0 prints the executed command before it is
1785 % o command: this string is the command to execute.
1787 % o exception: return any errors here.
1790 MagickExport int SystemCommand(const MagickBooleanType verbose,
1791 const char *command,ExceptionInfo *exception)
1810 arguments=StringToArgv(command,&number_arguments);
1811 if (arguments == (char **) NULL)
1813 domain=DelegatePolicyDomain;
1814 rights=ExecutePolicyRights;
1815 if (IsRightsAuthorized(domain,rights,arguments[1]) == MagickFalse)
1817 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1818 "NotAuthorized","`%s'",arguments[1]);
1819 for (i=0; i < number_arguments; i++)
1820 arguments[i]=DestroyString(arguments[i]);
1821 arguments=(char **) RelinquishMagickMemory(arguments);
1824 if (verbose != MagickFalse)
1826 (void) fprintf(stderr,"%s\n",command);
1827 (void) fflush(stderr);
1829 #if defined(MAGICKCORE_POSIX_SUPPORT)
1830 #if !defined(MAGICKCORE_HAVE_EXECVP)
1831 status=system(command);
1833 if (strspn(command,"&;<>|") == 0)
1834 status=system(command);
1841 Call application directly rather than from a shell.
1844 if (child_pid == (pid_t) -1)
1845 status=system(command);
1849 status=execvp(arguments[1],arguments+1);
1861 pid=waitpid(child_pid,&child_status,0);
1866 if (WIFEXITED(child_status) != 0)
1867 status=WEXITSTATUS(child_status);
1869 if (WIFSIGNALED(child_status))
1875 #elif defined(__WINDOWS__)
1876 status=NTSystemCommand(command);
1877 #elif defined(macintosh)
1878 status=MACSystemCommand(command);
1880 status=system(command);
1882 # error No suitable system() method.
1889 message=GetExceptionMessage(errno);
1890 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1891 "`%s': %s",command,message);
1892 message=DestroyString(message);
1894 for (i=0; i < number_arguments; i++)
1895 arguments[i]=DestroyString(arguments[i]);
1896 arguments=(char **) RelinquishMagickMemory(arguments);