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-2011 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 "MagickCore/studio.h"
43 #include "MagickCore/property.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/color.h"
46 #include "MagickCore/exception.h"
47 #include "MagickCore/exception-private.h"
48 #include "MagickCore/geometry.h"
49 #include "MagickCore/list.h"
50 #include "MagickCore/log.h"
51 #include "MagickCore/magick-private.h"
52 #include "MagickCore/memory_.h"
53 #include "MagickCore/option.h"
54 #include "MagickCore/policy.h"
55 #include "MagickCore/resource_.h"
56 #include "MagickCore/semaphore.h"
57 #include "MagickCore/signature-private.h"
58 #include "MagickCore/statistic.h"
59 #include "MagickCore/string_.h"
60 #include "MagickCore/string-private.h"
61 #include "MagickCore/token.h"
62 #include "MagickCore/token-private.h"
63 #include "MagickCore/utility.h"
64 #include "MagickCore/utility-private.h"
65 #if defined(MAGICKCORE_HAVE_PROCESS_H)
68 #if defined(MAGICKCORE_HAVE_MACH_O_DYLD_H)
69 #include <mach-o/dyld.h>
76 Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
82 IsPathDirectory(const char *);
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 % A c q u i r e U n i q u e F i l e n a m e %
93 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95 % AcquireUniqueFilename() replaces the contents of path by a unique path name.
97 % The format of the AcquireUniqueFilename method is:
99 % MagickBooleanType AcquireUniqueFilename(char *path)
101 % A description of each parameter follows.
103 % o path: Specifies a pointer to an array of characters. The unique path
104 % name is returned in this array.
107 MagickExport MagickBooleanType AcquireUniqueFilename(char *path)
112 file=AcquireUniqueFileResource(path);
120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 % A c q u i r e U n i q u e S ym b o l i c L i n k %
128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 % AcquireUniqueSymbolicLink() creates a unique symbolic link to the specified
131 % source path and returns MagickTrue on success otherwise MagickFalse. If the
132 % symlink() method fails or is not available, a unique file name is generated
133 % and the source file copied to it. When you are finished with the file, use
134 % RelinquishUniqueFilename() to destroy it.
136 % The format of the AcquireUniqueSymbolicLink method is:
138 % MagickBooleanType AcquireUniqueSymbolicLink(const char *source,
141 % A description of each parameter follows.
143 % o source: the source path.
145 % o destination: the destination path.
149 static inline size_t MagickMin(const size_t x,const size_t y)
156 MagickExport MagickBooleanType AcquireUniqueSymbolicLink(const char *source,
176 assert(source != (const char *) NULL);
177 assert(destination != (char *) NULL);
178 #if defined(MAGICKCORE_HAVE_SYMLINK)
179 (void) AcquireUniqueFilename(destination);
180 (void) RelinquishUniqueFileResource(destination);
181 if (*source == *DirectorySeparator)
183 if (symlink(source,destination) == 0)
192 if (getcwd(path,MaxTextExtent) == (char *) NULL)
194 (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
195 (void) ConcatenateMagickString(path,source,MaxTextExtent);
196 if (symlink(path,destination) == 0)
200 destination_file=AcquireUniqueFileResource(destination);
201 if (destination_file == -1)
203 source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
204 if (source_file == -1)
206 (void) close(destination_file);
207 (void) RelinquishUniqueFileResource(destination);
210 quantum=(size_t) MagickMaxBufferExtent;
211 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size != 0))
212 quantum=MagickMin((size_t) attributes.st_size,MagickMaxBufferExtent);
213 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
214 if (buffer == (unsigned char *) NULL)
216 (void) close(source_file);
217 (void) close(destination_file);
218 (void) RelinquishUniqueFileResource(destination);
223 count=(ssize_t) read(source_file,buffer,quantum);
226 length=(size_t) count;
227 count=(ssize_t) write(destination_file,buffer,length);
228 if ((size_t) count != length)
230 (void) close(destination_file);
231 (void) close(source_file);
232 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
233 (void) RelinquishUniqueFileResource(destination);
237 (void) close(destination_file);
238 (void) close(source_file);
239 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 % A p p e n d I m a g e F o r m a t %
252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 % AppendImageFormat() appends the image format type to the filename. If an
255 % extension to the file already exists, it is first removed.
257 % The format of the AppendImageFormat method is:
259 % void AppendImageFormat(const char *format,char *filename)
261 % A description of each parameter follows.
263 % o format: Specifies a pointer to an array of characters. This the
264 % format of the image.
266 % o filename: Specifies a pointer to an array of characters. The unique
267 % file name is returned in this array.
270 MagickExport void AppendImageFormat(const char *format,char *filename)
273 extension[MaxTextExtent],
276 assert(format != (char *) NULL);
277 assert(filename != (char *) NULL);
278 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
279 if ((*format == '\0') || (*filename == '\0'))
281 if (LocaleCompare(filename,"-") == 0)
284 message[MaxTextExtent];
286 (void) FormatLocaleString(message,MaxTextExtent,"%s:%s",format,filename);
287 (void) CopyMagickString(filename,message,MaxTextExtent);
290 GetPathComponent(filename,ExtensionPath,extension);
291 if ((LocaleCompare(extension,"Z") == 0) ||
292 (LocaleCompare(extension,"bz2") == 0) ||
293 (LocaleCompare(extension,"gz") == 0) ||
294 (LocaleCompare(extension,"wmz") == 0) ||
295 (LocaleCompare(extension,"svgz") == 0))
297 GetPathComponent(filename,RootPath,root);
298 (void) CopyMagickString(filename,root,MaxTextExtent);
299 GetPathComponent(filename,RootPath,root);
300 (void) FormatLocaleString(filename,MaxTextExtent,"%s.%s.%s",root,format,
304 GetPathComponent(filename,RootPath,root);
305 (void) FormatLocaleString(filename,MaxTextExtent,"%s.%s",root,format);
309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313 % B a s e 6 4 D e c o d e %
317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 % Base64Decode() decodes Base64-encoded text and returns its binary
320 % equivalent. NULL is returned if the text is not valid Base64 data, or a
321 % memory allocation failure occurs.
323 % The format of the Base64Decode method is:
325 % unsigned char *Base64Decode(const char *source,length_t *length)
327 % A description of each parameter follows:
329 % o source: A pointer to a Base64-encoded string.
331 % o length: the number of bytes decoded.
334 MagickExport unsigned char *Base64Decode(const char *source,size_t *length)
349 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
350 assert(source != (char *) NULL);
351 assert(length != (size_t *) NULL);
353 decode=(unsigned char *) AcquireQuantumMemory(strlen(source)/4+4,
355 if (decode == (unsigned char *) NULL)
356 return((unsigned char *) NULL);
359 for (p=source; *p != '\0'; p++)
361 if (isspace((int) ((unsigned char) *p)) != 0)
366 if (q == (char *) NULL)
368 decode=(unsigned char *) RelinquishMagickMemory(decode);
369 return((unsigned char *) NULL); /* non-Base64 character */
375 decode[i]=(q-Base64) << 2;
381 decode[i++]|=(q-Base64) >> 4;
382 decode[i]=((q-Base64) & 0x0f) << 4;
388 decode[i++]|=(q-Base64) >> 2;
389 decode[i]=((q-Base64) & 0x03) << 6;
395 decode[i++]|=(q-Base64);
402 Verify Base-64 string has proper terminal characters.
408 decode=(unsigned char *) RelinquishMagickMemory(decode);
409 return((unsigned char *) NULL);
421 Unrecognized '=' character.
423 decode=(unsigned char *) RelinquishMagickMemory(decode);
424 return((unsigned char *) NULL);
428 for ( ; *p != '\0'; p++)
429 if (isspace((int) ((unsigned char) *p)) == 0)
433 decode=(unsigned char *) RelinquishMagickMemory(decode);
434 return((unsigned char *) NULL);
440 for ( ; *p != '\0'; p++)
441 if (isspace((int) ((unsigned char) *p)) == 0)
443 decode=(unsigned char *) RelinquishMagickMemory(decode);
444 return((unsigned char *) NULL);
446 if ((int) decode[i] != 0)
448 decode=(unsigned char *) RelinquishMagickMemory(decode);
449 return((unsigned char *) NULL);
459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463 % B a s e 6 4 E n c o d e %
467 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
469 % Base64Encode() encodes arbitrary binary data to Base64 encoded format as
470 % described by the "Base64 Content-Transfer-Encoding" section of RFC 2045 and
471 % returns the result as a null-terminated ASCII string. NULL is returned if
472 % a memory allocation failure occurs.
474 % The format of the Base64Encode method is:
476 % char *Base64Encode(const unsigned char *blob,const size_t blob_length,
477 % size_t *encode_length)
479 % A description of each parameter follows:
481 % o blob: A pointer to binary data to encode.
483 % o blob_length: the number of bytes to encode.
485 % o encode_length: The number of bytes encoded.
488 MagickExport char *Base64Encode(const unsigned char *blob,
489 const size_t blob_length,size_t *encode_length)
494 register const unsigned char
503 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
504 assert(blob != (const unsigned char *) NULL);
505 assert(blob_length != 0);
506 assert(encode_length != (size_t *) NULL);
508 encode=(char *) AcquireQuantumMemory(blob_length/3+4,4*sizeof(*encode));
509 if (encode == (char *) NULL)
510 return((char *) NULL);
512 for (p=blob; p < (blob+blob_length-2); p+=3)
514 encode[i++]=Base64[(int) (*p >> 2)];
515 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
516 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
517 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
519 remainder=blob_length % 3;
531 for (j=0; j < (ssize_t) remainder; j++)
533 encode[i++]=Base64[(int) (code[0] >> 2)];
534 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
538 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
551 % C h o p P a t h C o m p o n e n t s %
555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557 % ChopPathComponents() removes the number of specified file components from a
560 % The format of the ChopPathComponents method is:
562 % ChopPathComponents(char *path,size_t components)
564 % A description of each parameter follows:
568 % o components: The number of components to chop.
571 MagickPrivate void ChopPathComponents(char *path,const size_t components)
576 for (i=0; i < (ssize_t) components; i++)
577 GetPathComponent(path,HeadPath,path);
581 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
585 % E x p a n d F i l e n a m e %
589 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
591 % ExpandFilename() expands '~' in a path.
593 % The format of the ExpandFilename function is:
595 % ExpandFilename(char *path)
597 % A description of each parameter follows:
599 % o path: Specifies a pointer to a character array that contains the
603 MagickPrivate void ExpandFilename(char *path)
606 expand_path[MaxTextExtent];
608 if (path == (char *) NULL)
612 (void) CopyMagickString(expand_path,path,MaxTextExtent);
613 if ((*(path+1) == *DirectorySeparator) || (*(path+1) == '\0'))
619 Substitute ~ with $HOME.
621 (void) CopyMagickString(expand_path,".",MaxTextExtent);
622 (void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
623 home=GetEnvironmentValue("HOME");
624 if (home == (char *) NULL)
625 home=GetEnvironmentValue("USERPROFILE");
626 if (home != (char *) NULL)
628 (void) CopyMagickString(expand_path,home,MaxTextExtent);
629 (void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
630 home=DestroyString(home);
635 #if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
637 username[MaxTextExtent];
646 Substitute ~ with home directory from password file.
648 (void) CopyMagickString(username,path+1,MaxTextExtent);
649 p=strchr(username,'/');
650 if (p != (char *) NULL)
652 entry=getpwnam(username);
653 if (entry == (struct passwd *) NULL)
655 (void) CopyMagickString(expand_path,entry->pw_dir,MaxTextExtent);
656 if (p != (char *) NULL)
658 (void) ConcatenateMagickString(expand_path,"/",MaxTextExtent);
659 (void) ConcatenateMagickString(expand_path,p+1,MaxTextExtent);
663 (void) CopyMagickString(path,expand_path,MaxTextExtent);
667 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
671 % E x p a n d F i l e n a m e s %
675 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
677 % ExpandFilenames() checks each argument of the command line vector and
678 % expands it if they have a wildcard character. For example, *.jpg might
679 % expand to: bird.jpg rose.jpg tiki.jpg.
681 % The format of the ExpandFilenames function is:
683 % status=ExpandFilenames(int *number_arguments,char ***arguments)
685 % A description of each parameter follows:
687 % o number_arguments: Specifies a pointer to an integer describing the
688 % number of elements in the argument vector.
690 % o arguments: Specifies a pointer to a text array containing the command
694 MagickExport MagickBooleanType ExpandFilenames(int *number_arguments,
699 home_directory[MaxTextExtent],
714 Allocate argument vector.
716 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
717 assert(number_arguments != (int *) NULL);
718 assert(arguments != (char ***) NULL);
719 vector=(char **) AcquireQuantumMemory((size_t) (*number_arguments+1),
721 if (vector == (char **) NULL)
722 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
724 Expand any wildcard filenames.
726 *home_directory='\0';
728 for (i=0; i < (ssize_t) *number_arguments; i++)
732 filename[MaxTextExtent],
733 magick[MaxTextExtent],
736 subimage[MaxTextExtent];
741 option=(*arguments)[i];
746 vector[count++]=ConstantString(option);
748 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
752 Do not expand command option parameters.
754 for (j=0; j < parameters; j++)
757 if (i == (ssize_t) *number_arguments)
759 option=(*arguments)[i];
760 vector[count++]=ConstantString(option);
764 if ((*option == '"') || (*option == '\''))
766 GetPathComponent(option,TailPath,filename);
767 GetPathComponent(option,MagickPath,magick);
768 if ((LocaleCompare(magick,"CAPTION") == 0) ||
769 (LocaleCompare(magick,"LABEL") == 0) ||
770 (LocaleCompare(magick,"VID") == 0))
772 if ((IsGlob(filename) == MagickFalse) && (*filename != '@'))
774 if (*filename != '@')
777 Generate file list from wildcard filename (e.g. *.jpg).
779 GetPathComponent(option,HeadPath,path);
780 GetPathComponent(option,SubimagePath,subimage);
781 ExpandFilename(path);
782 if (*home_directory == '\0')
783 directory=getcwd(home_directory,MaxTextExtent-1);
785 filelist=ListFiles(*path == '\0' ? home_directory : path,filename,
800 Generate file list from file list (e.g. @filelist.txt).
802 exception=AcquireExceptionInfo();
803 files=FileToString(filename+1,~0,exception);
804 exception=DestroyExceptionInfo(exception);
805 if (files == (char *) NULL)
807 filelist=StringToArgv(files,&length);
808 if (filelist == (char **) NULL)
810 files=DestroyString(files);
811 filelist[0]=DestroyString(filelist[0]);
812 for (j=0; j < (ssize_t) (length-1); j++)
813 filelist[j]=filelist[j+1];
814 number_files=(size_t) length-1;
816 if (filelist == (char **) NULL)
818 for (j=0; j < (ssize_t) number_files; j++)
819 if (IsPathDirectory(filelist[j]) <= 0)
821 if (j == (ssize_t) number_files)
823 for (j=0; j < (ssize_t) number_files; j++)
824 filelist[j]=DestroyString(filelist[j]);
825 filelist=(char **) RelinquishMagickMemory(filelist);
829 Transfer file list to argument vector.
831 vector=(char **) ResizeQuantumMemory(vector,(size_t) *number_arguments+
832 count+number_files+1,sizeof(*vector));
833 if (vector == (char **) NULL)
835 for (j=0; j < (ssize_t) number_files; j++)
838 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
845 Do not expand command option parameters.
847 vector[count++]=ConstantString(option);
848 for (k=0; k < parameters; k++)
851 if (j == (ssize_t) number_files)
854 vector[count++]=ConstantString(option);
858 (void) CopyMagickString(filename,path,MaxTextExtent);
860 (void) ConcatenateMagickString(filename,DirectorySeparator,
862 (void) ConcatenateMagickString(filename,filelist[j],MaxTextExtent);
863 filelist[j]=DestroyString(filelist[j]);
864 if (strlen(filename) >= (MaxTextExtent-1))
865 ThrowFatalException(OptionFatalError,"FilenameTruncated");
866 if (IsPathDirectory(filename) <= 0)
874 (void) ConcatenateMagickString(path,magick,MaxTextExtent);
875 (void) ConcatenateMagickString(path,":",MaxTextExtent);
877 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
878 if (*subimage != '\0')
880 (void) ConcatenateMagickString(path,"[",MaxTextExtent);
881 (void) ConcatenateMagickString(path,subimage,MaxTextExtent);
882 (void) ConcatenateMagickString(path,"]",MaxTextExtent);
884 if (strlen(path) >= (MaxTextExtent-1))
885 ThrowFatalException(OptionFatalError,"FilenameTruncated");
886 if (destroy != MagickFalse)
889 vector[count]=DestroyString(vector[count]);
892 vector[count++]=ConstantString(path);
895 filelist=(char **) RelinquishMagickMemory(filelist);
897 vector[count]=(char *) NULL;
898 if (IsEventLogging() != MagickFalse)
903 command_line=AcquireString(vector[0]);
904 for (i=1; i < count; i++)
906 (void) ConcatenateString(&command_line," {");
907 (void) ConcatenateString(&command_line,vector[i]);
908 (void) ConcatenateString(&command_line,"}");
910 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
911 "Command line: %s",command_line);
912 command_line=DestroyString(command_line);
914 *number_arguments=(int) count;
920 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
924 % G e t E x e c u t i o n P a t h %
928 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
930 % GetExecutionPath() returns the pathname of the executable that started
931 % the process. On success MagickTrue is returned, otherwise MagickFalse.
933 % The format of the GetExecutionPath method is:
935 % MagickBooleanType GetExecutionPath(char *path,const size_t extent)
937 % A description of each parameter follows:
939 % o path: the pathname of the executable that started the process.
941 % o extent: the maximum extent of the path.
944 MagickPrivate MagickBooleanType GetExecutionPath(char *path,const size_t extent)
950 directory=getcwd(path,(unsigned long) extent);
952 #if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
955 link_path[MaxTextExtent],
956 execution_path[PATH_MAX+1];
961 (void) FormatLocaleString(link_path,MaxTextExtent,"/proc/%.20g/exe",
963 count=readlink(link_path,execution_path,PATH_MAX);
966 (void) FormatLocaleString(link_path,MaxTextExtent,"/proc/%.20g/file",
968 count=readlink(link_path,execution_path,PATH_MAX);
970 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
972 execution_path[count]='\0';
973 (void) CopyMagickString(path,execution_path,extent);
977 #if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
980 executable_path[PATH_MAX << 1],
981 execution_path[PATH_MAX+1];
986 length=sizeof(executable_path);
987 if ((_NSGetExecutablePath(executable_path,&length) == 0) &&
988 (realpath(executable_path,execution_path) != (char *) NULL))
989 (void) CopyMagickString(path,execution_path,extent);
992 #if defined(MAGICKCORE_HAVE_GETEXECNAME)
997 execution_path=(const char *) getexecname();
998 if (execution_path != (const char *) NULL)
1000 if (*execution_path != *DirectorySeparator)
1001 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1002 (void) ConcatenateMagickString(path,execution_path,extent);
1006 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1007 NTGetExecutionPath(path,extent);
1009 #if defined(__GNU__)
1019 execution_path=(char *) NULL;
1020 program_name=program_invocation_name;
1021 if (*program_invocation_name != '/')
1026 extent=strlen(directory)+strlen(program_name)+2;
1027 program_name=AcquireQuantumMemory(extent,sizeof(*program_name));
1028 if (program_name == (char *) NULL)
1029 program_name=program_invocation_name;
1031 count=FormatLocaleString(program_name,extent,"%s/%s",directory,
1032 program_invocation_name);
1036 execution_path=realpath(program_name,NULL);
1037 if (execution_path != (char *) NULL)
1038 (void) CopyMagickString(path,execution_path,extent);
1040 if (program_name != program_invocation_name)
1041 program_name=(char *) RelinquishMagickMemory(program_name);
1042 execution_path=(char *) RelinquishMagickMemory(execution_path);
1045 return(IsPathAccessible(path));
1049 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1053 % G e t M a g i c k P a g e S i z e %
1057 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1059 % GetMagickPageSize() returns the memory page size.
1061 % The format of the GetMagickPageSize method is:
1063 % ssize_t GetMagickPageSize()
1066 MagickPrivate ssize_t GetMagickPageSize(void)
1073 #if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1074 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1076 #if defined(MAGICKCORE_HAVE_GETPAGESIZE)
1077 page_size=(ssize_t) getpagesize();
1086 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1090 % G e t P a t h A t t r i b u t e s %
1094 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1096 % GetPathAttributes() returns attributes (e.g. size of file) about a path.
1098 % The path of the GetPathAttributes method is:
1100 % MagickBooleanType GetPathAttributes(const char *path,void *attributes)
1102 % A description of each parameter follows.
1104 % o path: the file path.
1106 % o attributes: the path attributes are returned here.
1109 MagickExport MagickBooleanType GetPathAttributes(const char *path,
1115 if (path == (const char *) NULL)
1118 return(MagickFalse);
1120 status=stat_utf8(path,(struct stat *) attributes) == 0 ? MagickTrue :
1126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1130 % G e t P a t h C o m p o n e n t %
1134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1136 % GetPathComponent() returns the parent directory name, filename, basename, or
1137 % extension of a file path.
1139 % The format of the GetPathComponent function is:
1141 % GetPathComponent(const char *path,PathType type,char *component)
1143 % A description of each parameter follows:
1145 % o path: Specifies a pointer to a character array that contains the
1148 % o type: Specififies which file path component to return.
1150 % o component: the selected file path component is returned here.
1153 MagickExport void GetPathComponent(const char *path,PathType type,
1157 magick[MaxTextExtent],
1159 subimage[MaxTextExtent];
1164 assert(path != (const char *) NULL);
1165 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",path);
1166 assert(component != (char *) NULL);
1172 (void) CopyMagickString(component,path,MaxTextExtent);
1177 p=component+strlen(component)-1;
1178 if ((*p == ']') && (strchr(component,'[') != (char *) NULL) &&
1179 (IsPathAccessible(path) == MagickFalse))
1182 Look for scene specification (e.g. img0001.pcd[4]).
1184 for (q=p-1; q > component; q--)
1189 (void) CopyMagickString(subimage,q+1,MaxTextExtent);
1190 subimage[p-q-1]='\0';
1191 if ((IsSceneGeometry(subimage,MagickFalse) == MagickFalse) &&
1192 (IsGeometry(subimage) == MagickFalse))
1198 #if defined(__OS2__)
1201 for (p=component; *p != '\0'; p++)
1203 if ((*p == '%') && (*(p+1) == '['))
1208 for (p++; (*p != ']') && (*p != '\0'); p++) ;
1212 if ((*p == ':') && (IsPathDirectory(component) < 0) &&
1213 (IsPathAccessible(component) == MagickFalse))
1216 Look for image format specification (e.g. ps3:image).
1218 (void) CopyMagickString(magick,component,(size_t) (p-component+1));
1219 if (IsMagickConflict(magick) != MagickFalse)
1222 for (q=component; *q != '\0'; q++)
1229 for (p=component+strlen(component)-1; p > component; p--)
1230 if (IsBasenameSeparator(*p) != MagickFalse)
1236 (void) CopyMagickString(component,magick,MaxTextExtent);
1241 for (p=component+(strlen(component)-1); p > component; p--)
1243 if (IsBasenameSeparator(*p) != MagickFalse)
1259 if (IsBasenameSeparator(*p) != MagickFalse)
1260 (void) CopyMagickMemory((unsigned char *) component,
1261 (const unsigned char *) (p+1),strlen(p+1)+1);
1266 if (IsBasenameSeparator(*p) != MagickFalse)
1267 (void) CopyMagickString(component,p+1,MaxTextExtent);
1268 for (p=component+(strlen(component)-1); p > component; p--)
1278 if (IsBasenameSeparator(*p) != MagickFalse)
1279 (void) CopyMagickString(component,p+1,MaxTextExtent);
1282 for (p=component+strlen(component)-1; p > component; p--)
1287 (void) CopyMagickString(component,p+1,MaxTextExtent);
1292 (void) CopyMagickString(component,subimage,MaxTextExtent);
1302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1306 % G e t P a t h C o m p o n e n t s %
1310 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1312 % GetPathComponents() returns a list of path components.
1314 % The format of the GetPathComponents method is:
1316 % char **GetPathComponents(const char *path,
1317 % size_t *number_componenets)
1319 % A description of each parameter follows:
1321 % o path: Specifies the string to segment into a list.
1323 % o number_components: return the number of components in the list
1326 MagickPrivate char **GetPathComponents(const char *path,
1327 size_t *number_components)
1339 if (path == (char *) NULL)
1340 return((char **) NULL);
1341 *number_components=1;
1342 for (p=path; *p != '\0'; p++)
1343 if (IsBasenameSeparator(*p))
1344 (*number_components)++;
1345 components=(char **) AcquireQuantumMemory((size_t) *number_components+1UL,
1346 sizeof(*components));
1347 if (components == (char **) NULL)
1348 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1350 for (i=0; i < (ssize_t) *number_components; i++)
1352 for (q=p; *q != '\0'; q++)
1353 if (IsBasenameSeparator(*q))
1355 components[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
1356 sizeof(**components));
1357 if (components[i] == (char *) NULL)
1358 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1359 (void) CopyMagickString(components[i],p,(size_t) (q-p+1));
1362 components[i]=(char *) NULL;
1367 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1371 % I s P a t h A c c e s s i b l e %
1375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1377 % IsPathAccessible() returns MagickTrue if the file as defined by the path is
1380 % The format of the IsPathAccessible method is:
1382 % MagickBooleanType IsPathAccessible(const char *filename)
1384 % A description of each parameter follows.
1386 % o path: Specifies a path to a file.
1389 MagickExport MagickBooleanType IsPathAccessible(const char *path)
1397 if ((path == (const char *) NULL) || (*path == '\0'))
1398 return(MagickFalse);
1399 status=GetPathAttributes(path,&attributes);
1400 if (status == MagickFalse)
1402 if (S_ISREG(attributes.st_mode) == 0)
1403 return(MagickFalse);
1404 if (access_utf8(path,F_OK) != 0)
1405 return(MagickFalse);
1410 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1414 + I s P a t h D i r e c t o r y %
1418 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1420 % IsPathDirectory() returns -1 if the directory does not exist, 1 is returned
1421 % if the path represents a directory otherwise 0.
1423 % The format of the IsPathDirectory method is:
1425 % int IsPathDirectory(const char *path)
1427 % A description of each parameter follows.
1429 % o path: The directory path.
1432 static int IsPathDirectory(const char *path)
1440 if ((path == (const char *) NULL) || (*path == '\0'))
1441 return(MagickFalse);
1442 status=GetPathAttributes(path,&attributes);
1443 if (status == MagickFalse)
1445 if (S_ISDIR(attributes.st_mode) == 0)
1451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1455 % I s M a g i c k T r u e %
1459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1461 % IsMagickTrue() returns MagickTrue if the value is "true", "on", "yes" or
1464 % The format of the IsMagickTrue method is:
1466 % MagickBooleanType IsMagickTrue(const char *value)
1468 % A description of each parameter follows:
1470 % o option: either MagickTrue or MagickFalse depending on the value
1473 % o value: Specifies a pointer to a character array.
1476 MagickExport MagickBooleanType IsMagickTrue(const char *value)
1478 if (value == (const char *) NULL)
1479 return(MagickFalse);
1480 if (LocaleCompare(value,"true") == 0)
1482 if (LocaleCompare(value,"on") == 0)
1484 if (LocaleCompare(value,"yes") == 0)
1486 if (LocaleCompare(value,"1") == 0)
1488 return(MagickFalse);
1492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1496 % L i s t F i l e s %
1500 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1502 % ListFiles() reads the directory specified and returns a list of filenames
1503 % contained in the directory sorted in ascending alphabetic order.
1505 % The format of the ListFiles function is:
1507 % char **ListFiles(const char *directory,const char *pattern,
1508 % ssize_t *number_entries)
1510 % A description of each parameter follows:
1512 % o filelist: Method ListFiles returns a list of filenames contained
1513 % in the directory. If the directory specified cannot be read or it is
1514 % a file a NULL list is returned.
1516 % o directory: Specifies a pointer to a text string containing a directory
1519 % o pattern: Specifies a pointer to a text string containing a pattern.
1521 % o number_entries: This integer returns the number of filenames in the
1526 #if defined(__cplusplus) || defined(c_plusplus)
1530 static int FileCompare(const void *x,const void *y)
1536 p=(const char **) x;
1537 q=(const char **) y;
1538 return(LocaleCompare(*p,*q));
1541 #if defined(__cplusplus) || defined(c_plusplus)
1545 static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
1546 struct dirent **result)
1548 #if defined(MAGICKCORE_HAVE_READDIR_R)
1549 return(readdir_r(directory,entry,result));
1553 *result=readdir(directory);
1558 MagickPrivate char **ListFiles(const char *directory,const char *pattern,
1559 size_t *number_entries)
1577 assert(directory != (const char *) NULL);
1578 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",directory);
1579 assert(pattern != (const char *) NULL);
1580 assert(number_entries != (size_t *) NULL);
1582 current_directory=opendir(directory);
1583 if (current_directory == (DIR *) NULL)
1584 return((char **) NULL);
1589 filelist=(char **) AcquireQuantumMemory((size_t) max_entries,
1591 if (filelist == (char **) NULL)
1593 (void) closedir(current_directory);
1594 return((char **) NULL);
1597 Save the current and change to the new directory.
1599 buffer=(struct dirent *) AcquireMagickMemory(sizeof(*buffer)+
1601 if (buffer == (struct dirent *) NULL)
1602 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1603 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1604 (entry != (struct dirent *) NULL))
1606 if (*entry->d_name == '.')
1608 if ((IsPathDirectory(entry->d_name) > 0) ||
1609 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1610 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1612 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1615 if (*number_entries >= max_entries)
1618 Extend the file list.
1621 filelist=(char **) ResizeQuantumMemory(filelist,(size_t)
1622 max_entries,sizeof(*filelist));
1623 if (filelist == (char **) NULL)
1631 p=strchr(entry->d_name,';');
1634 if (*number_entries > 0)
1635 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1639 filelist[*number_entries]=(char *) AcquireString(entry->d_name);
1640 (*number_entries)++;
1643 buffer=(struct dirent *) RelinquishMagickMemory(buffer);
1644 (void) closedir(current_directory);
1645 if (filelist == (char **) NULL)
1646 return((char **) NULL);
1648 Sort filelist in ascending order.
1650 qsort((void *) filelist,(size_t) *number_entries,sizeof(*filelist),
1656 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1660 % M a g i c k D e l a y %
1664 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1666 % MagickDelay() suspends program execution for the number of milliseconds
1669 % The format of the Delay method is:
1671 % void MagickDelay(const MagickSizeType milliseconds)
1673 % A description of each parameter follows:
1675 % o milliseconds: Specifies the number of milliseconds to delay before
1679 MagickPrivate void MagickDelay(const MagickSizeType milliseconds)
1681 if (milliseconds == 0)
1683 #if defined(MAGICKCORE_HAVE_NANOSLEEP)
1688 timer.tv_sec=(time_t) (milliseconds/1000);
1689 timer.tv_nsec=(milliseconds % 1000)*1000*1000;
1690 (void) nanosleep(&timer,(struct timespec *) NULL);
1692 #elif defined(MAGICKCORE_HAVE_USLEEP)
1693 usleep(1000*milliseconds);
1694 #elif defined(MAGICKCORE_HAVE_SELECT)
1699 timer.tv_sec=(long) milliseconds/1000;
1700 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1701 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1703 #elif defined(MAGICKCORE_HAVE_POLL)
1704 (void) poll((struct pollfd *) NULL,0,(int) milliseconds);
1705 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1706 Sleep((long) milliseconds);
1712 timer=milliseconds/1000.0;
1715 #elif defined(__BEOS__)
1716 snooze(1000*milliseconds);
1718 # error "Time delay method not defined."
1723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1727 % M u l t i l i n e C e n s u s %
1731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1733 % MultilineCensus() returns the number of lines within a label. A line is
1734 % represented by a \n character.
1736 % The format of the MultilineCenus method is:
1738 % size_t MultilineCensus(const char *label)
1740 % A description of each parameter follows.
1742 % o label: This character string is the label.
1745 MagickExport size_t MultilineCensus(const char *label)
1751 Determine the number of lines within this label.
1753 if (label == (char *) NULL)
1755 for (number_lines=1; *label != '\0'; label++)
1758 return(number_lines);
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 asynchronous,
1778 % const MagickBooleanType verbose,const char *command,
1779 % ExceptionInfo *exception)
1781 % A description of each parameter follows:
1783 % o asynchronous: a value other than 0 executes the parent program
1784 % concurrently with the new child process.
1786 % o verbose: a value other than 0 prints the executed command before it is
1789 % o command: this string is the command to execute.
1791 % o exception: return any errors here.
1794 MagickExport int SystemCommand(const MagickBooleanType asynchronous,
1795 const MagickBooleanType verbose,const char *command,ExceptionInfo *exception)
1815 arguments=StringToArgv(command,&number_arguments);
1816 if (arguments == (char **) NULL)
1818 rights=ExecutePolicyRights;
1819 domain=DelegatePolicyDomain;
1820 if (IsRightsAuthorized(domain,rights,arguments[1]) == MagickFalse)
1823 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1824 "NotAuthorized","`%s'",arguments[1]);
1825 for (i=0; i < (ssize_t) number_arguments; i++)
1826 arguments[i]=DestroyString(arguments[i]);
1827 arguments=(char **) RelinquishMagickMemory(arguments);
1830 if (verbose != MagickFalse)
1832 (void) FormatLocaleFile(stderr,"%s\n",command);
1833 (void) fflush(stderr);
1835 shell_command=(char *) command;
1836 if (asynchronous != MagickFalse)
1838 shell_command=AcquireString(command);
1839 (void) ConcatenateMagickString(shell_command,"&",MaxTextExtent);
1841 #if defined(MAGICKCORE_POSIX_SUPPORT)
1842 #if !defined(MAGICKCORE_HAVE_EXECVP)
1843 status=system(shell_command);
1845 if ((asynchronous != MagickFalse) || (strspn(shell_command,"&;<>|") == 0))
1846 status=system(shell_command);
1853 Call application directly rather than from a shell.
1856 if (child_pid == (pid_t) -1)
1857 status=system(command);
1861 status=execvp(arguments[1],arguments+1);
1873 pid=waitpid(child_pid,&child_status,0);
1878 if (WIFEXITED(child_status) != 0)
1879 status=WEXITSTATUS(child_status);
1881 if (WIFSIGNALED(child_status))
1887 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1893 if (asynchronous != MagickFalse)
1895 status=spawnvp(mode,arguments[1],(const char **) (arguments+1));
1897 #elif defined(macintosh)
1898 status=MACSystemCommand(shell_command);
1900 status=system(shell_command);
1902 # error No suitable system() method.
1905 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1906 "`%s' (%d)",command,status);
1907 if (shell_command != command)
1908 shell_command=DestroyString(shell_command);
1909 for (i=0; i < (ssize_t) number_arguments; i++)
1910 arguments[i]=DestroyString(arguments[i]);
1911 arguments=(char **) RelinquishMagickMemory(arguments);