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/memory_.h"
52 #include "MagickCore/option.h"
53 #include "MagickCore/policy.h"
54 #include "MagickCore/resource_.h"
55 #include "MagickCore/semaphore.h"
56 #include "MagickCore/signature-private.h"
57 #include "MagickCore/statistic.h"
58 #include "MagickCore/string_.h"
59 #include "MagickCore/token.h"
60 #include "MagickCore/utility.h"
61 #include "MagickCore/utility-private.h"
62 #if defined(MAGICKCORE_HAVE_PROCESS_H)
65 #if defined(MAGICKCORE_HAVE_MACH_O_DYLD_H)
66 #include <mach-o/dyld.h>
73 Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
79 IsPathDirectory(const char *);
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % A c q u i r e U n i q u e F i l e n a m e %
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % AcquireUniqueFilename() replaces the contents of path by a unique path name.
94 % The format of the AcquireUniqueFilename method is:
96 % MagickBooleanType AcquireUniqueFilename(char *path)
98 % A description of each parameter follows.
100 % o path: Specifies a pointer to an array of characters. The unique path
101 % name is returned in this array.
104 MagickExport MagickBooleanType AcquireUniqueFilename(char *path)
109 file=AcquireUniqueFileResource(path);
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 % A c q u i r e U n i q u e S ym b o l i c L i n k %
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 % AcquireUniqueSymbolicLink() creates a unique symbolic link to the specified
128 % source path and returns MagickTrue on success otherwise MagickFalse. If the
129 % symlink() method fails or is not available, a unique file name is generated
130 % and the source file copied to it. When you are finished with the file, use
131 % RelinquishUniqueFilename() to destroy it.
133 % The format of the AcquireUniqueSymbolicLink method is:
135 % MagickBooleanType AcquireUniqueSymbolicLink(const char *source,
138 % A description of each parameter follows.
140 % o source: the source path.
142 % o destination: the destination path.
146 static inline size_t MagickMin(const size_t x,const size_t y)
153 MagickExport MagickBooleanType AcquireUniqueSymbolicLink(const char *source,
173 assert(source != (const char *) NULL);
174 assert(destination != (char *) NULL);
175 #if defined(MAGICKCORE_HAVE_SYMLINK)
176 (void) AcquireUniqueFilename(destination);
177 (void) RelinquishUniqueFileResource(destination);
178 if (*source == *DirectorySeparator)
180 if (symlink(source,destination) == 0)
189 if (getcwd(path,MaxTextExtent) == (char *) NULL)
191 (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
192 (void) ConcatenateMagickString(path,source,MaxTextExtent);
193 if (symlink(path,destination) == 0)
197 destination_file=AcquireUniqueFileResource(destination);
198 if (destination_file == -1)
200 source_file=open(source,O_RDONLY | O_BINARY);
201 if (source_file == -1)
203 (void) close(destination_file);
204 (void) RelinquishUniqueFileResource(destination);
207 quantum=(size_t) MagickMaxBufferExtent;
208 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size != 0))
209 quantum=MagickMin((size_t) attributes.st_size,MagickMaxBufferExtent);
210 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
211 if (buffer == (unsigned char *) NULL)
213 (void) close(source_file);
214 (void) close(destination_file);
215 (void) RelinquishUniqueFileResource(destination);
220 count=(ssize_t) read(source_file,buffer,quantum);
223 length=(size_t) count;
224 count=(ssize_t) write(destination_file,buffer,length);
225 if ((size_t) count != length)
227 (void) close(destination_file);
228 (void) close(source_file);
229 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
230 (void) RelinquishUniqueFileResource(destination);
234 (void) close(destination_file);
235 (void) close(source_file);
236 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 % A p p e n d I m a g e F o r m a t %
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 % AppendImageFormat() appends the image format type to the filename. If an
252 % extension to the file already exists, it is first removed.
254 % The format of the AppendImageFormat method is:
256 % void AppendImageFormat(const char *format,char *filename)
258 % A description of each parameter follows.
260 % o format: Specifies a pointer to an array of characters. This the
261 % format of the image.
263 % o filename: Specifies a pointer to an array of characters. The unique
264 % file name is returned in this array.
267 MagickExport void AppendImageFormat(const char *format,char *filename)
270 extension[MaxTextExtent],
273 assert(format != (char *) NULL);
274 assert(filename != (char *) NULL);
275 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
276 if ((*format == '\0') || (*filename == '\0'))
278 if (LocaleCompare(filename,"-") == 0)
281 message[MaxTextExtent];
283 (void) FormatLocaleString(message,MaxTextExtent,"%s:%s",format,filename);
284 (void) CopyMagickString(filename,message,MaxTextExtent);
287 GetPathComponent(filename,ExtensionPath,extension);
288 if ((LocaleCompare(extension,"Z") == 0) ||
289 (LocaleCompare(extension,"bz2") == 0) ||
290 (LocaleCompare(extension,"gz") == 0) ||
291 (LocaleCompare(extension,"wmz") == 0) ||
292 (LocaleCompare(extension,"svgz") == 0))
294 GetPathComponent(filename,RootPath,root);
295 (void) CopyMagickString(filename,root,MaxTextExtent);
296 GetPathComponent(filename,RootPath,root);
297 (void) FormatLocaleString(filename,MaxTextExtent,"%s.%s.%s",root,format,
301 GetPathComponent(filename,RootPath,root);
302 (void) FormatLocaleString(filename,MaxTextExtent,"%s.%s",root,format);
306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 % B a s e 6 4 D e c o d e %
314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
316 % Base64Decode() decodes Base64-encoded text and returns its binary
317 % equivalent. NULL is returned if the text is not valid Base64 data, or a
318 % memory allocation failure occurs.
320 % The format of the Base64Decode method is:
322 % unsigned char *Base64Decode(const char *source,length_t *length)
324 % A description of each parameter follows:
326 % o source: A pointer to a Base64-encoded string.
328 % o length: the number of bytes decoded.
331 MagickExport unsigned char *Base64Decode(const char *source,size_t *length)
346 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
347 assert(source != (char *) NULL);
348 assert(length != (size_t *) NULL);
350 decode=(unsigned char *) AcquireQuantumMemory(strlen(source)/4+4,
352 if (decode == (unsigned char *) NULL)
353 return((unsigned char *) NULL);
356 for (p=source; *p != '\0'; p++)
358 if (isspace((int) ((unsigned char) *p)) != 0)
363 if (q == (char *) NULL)
365 decode=(unsigned char *) RelinquishMagickMemory(decode);
366 return((unsigned char *) NULL); /* non-Base64 character */
372 decode[i]=(q-Base64) << 2;
378 decode[i++]|=(q-Base64) >> 4;
379 decode[i]=((q-Base64) & 0x0f) << 4;
385 decode[i++]|=(q-Base64) >> 2;
386 decode[i]=((q-Base64) & 0x03) << 6;
392 decode[i++]|=(q-Base64);
399 Verify Base-64 string has proper terminal characters.
405 decode=(unsigned char *) RelinquishMagickMemory(decode);
406 return((unsigned char *) NULL);
418 Unrecognized '=' character.
420 decode=(unsigned char *) RelinquishMagickMemory(decode);
421 return((unsigned char *) NULL);
425 for ( ; *p != '\0'; p++)
426 if (isspace((int) ((unsigned char) *p)) == 0)
430 decode=(unsigned char *) RelinquishMagickMemory(decode);
431 return((unsigned char *) NULL);
437 for ( ; *p != '\0'; p++)
438 if (isspace((int) ((unsigned char) *p)) == 0)
440 decode=(unsigned char *) RelinquishMagickMemory(decode);
441 return((unsigned char *) NULL);
443 if ((int) decode[i] != 0)
445 decode=(unsigned char *) RelinquishMagickMemory(decode);
446 return((unsigned char *) NULL);
456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
460 % B a s e 6 4 E n c o d e %
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466 % Base64Encode() encodes arbitrary binary data to Base64 encoded format as
467 % described by the "Base64 Content-Transfer-Encoding" section of RFC 2045 and
468 % returns the result as a null-terminated ASCII string. NULL is returned if
469 % a memory allocation failure occurs.
471 % The format of the Base64Encode method is:
473 % char *Base64Encode(const unsigned char *blob,const size_t blob_length,
474 % size_t *encode_length)
476 % A description of each parameter follows:
478 % o blob: A pointer to binary data to encode.
480 % o blob_length: the number of bytes to encode.
482 % o encode_length: The number of bytes encoded.
485 MagickExport char *Base64Encode(const unsigned char *blob,
486 const size_t blob_length,size_t *encode_length)
491 register const unsigned char
500 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
501 assert(blob != (const unsigned char *) NULL);
502 assert(blob_length != 0);
503 assert(encode_length != (size_t *) NULL);
505 encode=(char *) AcquireQuantumMemory(blob_length/3+4,4*sizeof(*encode));
506 if (encode == (char *) NULL)
507 return((char *) NULL);
509 for (p=blob; p < (blob+blob_length-2); p+=3)
511 encode[i++]=Base64[(int) (*p >> 2)];
512 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
513 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
514 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
516 remainder=blob_length % 3;
528 for (j=0; j < (ssize_t) remainder; j++)
530 encode[i++]=Base64[(int) (code[0] >> 2)];
531 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
535 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
544 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
548 % C h o p P a t h C o m p o n e n t s %
552 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554 % ChopPathComponents() removes the number of specified file components from a
557 % The format of the ChopPathComponents method is:
559 % ChopPathComponents(char *path,size_t components)
561 % A description of each parameter follows:
565 % o components: The number of components to chop.
568 MagickPrivate void ChopPathComponents(char *path,const size_t components)
573 for (i=0; i < (ssize_t) components; i++)
574 GetPathComponent(path,HeadPath,path);
578 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582 % E x p a n d F i l e n a m e %
586 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
588 % ExpandFilename() expands '~' in a path.
590 % The format of the ExpandFilename function is:
592 % ExpandFilename(char *path)
594 % A description of each parameter follows:
596 % o path: Specifies a pointer to a character array that contains the
600 MagickPrivate void ExpandFilename(char *path)
603 expand_path[MaxTextExtent];
605 if (path == (char *) NULL)
609 (void) CopyMagickString(expand_path,path,MaxTextExtent);
610 if ((*(path+1) == *DirectorySeparator) || (*(path+1) == '\0'))
616 Substitute ~ with $HOME.
618 (void) CopyMagickString(expand_path,".",MaxTextExtent);
619 (void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
620 home=GetEnvironmentValue("HOME");
621 if (home == (char *) NULL)
622 home=GetEnvironmentValue("USERPROFILE");
623 if (home != (char *) NULL)
625 (void) CopyMagickString(expand_path,home,MaxTextExtent);
626 (void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
627 home=DestroyString(home);
632 #if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
634 username[MaxTextExtent];
643 Substitute ~ with home directory from password file.
645 (void) CopyMagickString(username,path+1,MaxTextExtent);
646 p=strchr(username,'/');
647 if (p != (char *) NULL)
649 entry=getpwnam(username);
650 if (entry == (struct passwd *) NULL)
652 (void) CopyMagickString(expand_path,entry->pw_dir,MaxTextExtent);
653 if (p != (char *) NULL)
655 (void) ConcatenateMagickString(expand_path,"/",MaxTextExtent);
656 (void) ConcatenateMagickString(expand_path,p+1,MaxTextExtent);
660 (void) CopyMagickString(path,expand_path,MaxTextExtent);
664 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
668 % E x p a n d F i l e n a m e s %
672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674 % ExpandFilenames() checks each argument of the command line vector and
675 % expands it if they have a wildcard character. For example, *.jpg might
676 % expand to: bird.jpg rose.jpg tiki.jpg.
678 % The format of the ExpandFilenames function is:
680 % status=ExpandFilenames(int *number_arguments,char ***arguments)
682 % A description of each parameter follows:
684 % o number_arguments: Specifies a pointer to an integer describing the
685 % number of elements in the argument vector.
687 % o arguments: Specifies a pointer to a text array containing the command
691 MagickExport MagickBooleanType ExpandFilenames(int *number_arguments,
696 home_directory[MaxTextExtent],
711 Allocate argument vector.
713 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
714 assert(number_arguments != (int *) NULL);
715 assert(arguments != (char ***) NULL);
716 vector=(char **) AcquireQuantumMemory((size_t) (*number_arguments+1),
718 if (vector == (char **) NULL)
719 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
721 Expand any wildcard filenames.
723 *home_directory='\0';
725 for (i=0; i < (ssize_t) *number_arguments; i++)
729 filename[MaxTextExtent],
730 magick[MaxTextExtent],
733 subimage[MaxTextExtent];
738 option=(*arguments)[i];
743 vector[count++]=ConstantString(option);
745 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
749 Do not expand command option parameters.
751 for (j=0; j < parameters; j++)
754 if (i == (ssize_t) *number_arguments)
756 option=(*arguments)[i];
757 vector[count++]=ConstantString(option);
761 if ((*option == '"') || (*option == '\''))
763 GetPathComponent(option,TailPath,filename);
764 GetPathComponent(option,MagickPath,magick);
765 if ((LocaleCompare(magick,"CAPTION") == 0) ||
766 (LocaleCompare(magick,"LABEL") == 0) ||
767 (LocaleCompare(magick,"VID") == 0))
769 if ((IsGlob(filename) == MagickFalse) && (*filename != '@'))
771 if (*filename != '@')
774 Generate file list from wildcard filename (e.g. *.jpg).
776 GetPathComponent(option,HeadPath,path);
777 GetPathComponent(option,SubimagePath,subimage);
778 ExpandFilename(path);
779 if (*home_directory == '\0')
780 directory=getcwd(home_directory,MaxTextExtent-1);
782 filelist=ListFiles(*path == '\0' ? home_directory : path,filename,
797 Generate file list from file list (e.g. @filelist.txt).
799 exception=AcquireExceptionInfo();
800 files=FileToString(filename+1,~0,exception);
801 exception=DestroyExceptionInfo(exception);
802 if (files == (char *) NULL)
804 filelist=StringToArgv(files,&length);
805 if (filelist == (char **) NULL)
807 files=DestroyString(files);
808 filelist[0]=DestroyString(filelist[0]);
809 for (j=0; j < (ssize_t) (length-1); j++)
810 filelist[j]=filelist[j+1];
811 number_files=(size_t) length-1;
813 if (filelist == (char **) NULL)
815 for (j=0; j < (ssize_t) number_files; j++)
816 if (IsPathDirectory(filelist[j]) <= 0)
818 if (j == (ssize_t) number_files)
820 for (j=0; j < (ssize_t) number_files; j++)
821 filelist[j]=DestroyString(filelist[j]);
822 filelist=(char **) RelinquishMagickMemory(filelist);
826 Transfer file list to argument vector.
828 vector=(char **) ResizeQuantumMemory(vector,(size_t) *number_arguments+
829 count+number_files+1,sizeof(*vector));
830 if (vector == (char **) NULL)
832 for (j=0; j < (ssize_t) number_files; j++)
835 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
842 Do not expand command option parameters.
844 vector[count++]=ConstantString(option);
845 for (k=0; k < parameters; k++)
848 if (j == (ssize_t) number_files)
851 vector[count++]=ConstantString(option);
855 (void) CopyMagickString(filename,path,MaxTextExtent);
857 (void) ConcatenateMagickString(filename,DirectorySeparator,
859 (void) ConcatenateMagickString(filename,filelist[j],MaxTextExtent);
860 filelist[j]=DestroyString(filelist[j]);
861 if (strlen(filename) >= (MaxTextExtent-1))
862 ThrowFatalException(OptionFatalError,"FilenameTruncated");
863 if (IsPathDirectory(filename) <= 0)
871 (void) ConcatenateMagickString(path,magick,MaxTextExtent);
872 (void) ConcatenateMagickString(path,":",MaxTextExtent);
874 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
875 if (*subimage != '\0')
877 (void) ConcatenateMagickString(path,"[",MaxTextExtent);
878 (void) ConcatenateMagickString(path,subimage,MaxTextExtent);
879 (void) ConcatenateMagickString(path,"]",MaxTextExtent);
881 if (strlen(path) >= (MaxTextExtent-1))
882 ThrowFatalException(OptionFatalError,"FilenameTruncated");
883 if (destroy != MagickFalse)
886 vector[count]=DestroyString(vector[count]);
889 vector[count++]=ConstantString(path);
892 filelist=(char **) RelinquishMagickMemory(filelist);
894 vector[count]=(char *) NULL;
895 if (IsEventLogging() != MagickFalse)
900 command_line=AcquireString(vector[0]);
901 for (i=1; i < count; i++)
903 (void) ConcatenateString(&command_line," {");
904 (void) ConcatenateString(&command_line,vector[i]);
905 (void) ConcatenateString(&command_line,"}");
907 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
908 "Command line: %s",command_line);
909 command_line=DestroyString(command_line);
911 *number_arguments=(int) count;
917 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
921 % G e t E x e c u t i o n P a t h %
925 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
927 % GetExecutionPath() returns the pathname of the executable that started
928 % the process. On success MagickTrue is returned, otherwise MagickFalse.
930 % The format of the GetExecutionPath method is:
932 % MagickBooleanType GetExecutionPath(char *path,const size_t extent)
934 % A description of each parameter follows:
936 % o path: the pathname of the executable that started the process.
938 % o extent: the maximum extent of the path.
941 MagickPrivate MagickBooleanType GetExecutionPath(char *path,const size_t extent)
947 directory=getcwd(path,(unsigned long) extent);
949 #if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
952 link_path[MaxTextExtent],
953 execution_path[PATH_MAX+1];
958 (void) FormatLocaleString(link_path,MaxTextExtent,"/proc/%.20g/exe",
960 count=readlink(link_path,execution_path,PATH_MAX);
963 (void) FormatLocaleString(link_path,MaxTextExtent,"/proc/%.20g/file",
965 count=readlink(link_path,execution_path,PATH_MAX);
967 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
969 execution_path[count]='\0';
970 (void) CopyMagickString(path,execution_path,extent);
974 #if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
977 executable_path[PATH_MAX << 1],
978 execution_path[PATH_MAX+1];
983 length=sizeof(executable_path);
984 if ((_NSGetExecutablePath(executable_path,&length) == 0) &&
985 (realpath(executable_path,execution_path) != (char *) NULL))
986 (void) CopyMagickString(path,execution_path,extent);
989 #if defined(MAGICKCORE_HAVE_GETEXECNAME)
994 execution_path=(const char *) getexecname();
995 if (execution_path != (const char *) NULL)
997 if (*execution_path != *DirectorySeparator)
998 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
999 (void) ConcatenateMagickString(path,execution_path,extent);
1003 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1004 NTGetExecutionPath(path,extent);
1006 #if defined(__GNU__)
1016 execution_path=(char *) NULL;
1017 program_name=program_invocation_name;
1018 if (*program_invocation_name != '/')
1023 extent=strlen(directory)+strlen(program_name)+2;
1024 program_name=AcquireQuantumMemory(extent,sizeof(*program_name));
1025 if (program_name == (char *) NULL)
1026 program_name=program_invocation_name;
1028 count=FormatLocaleString(program_name,extent,"%s/%s",directory,
1029 program_invocation_name);
1033 execution_path=realpath(program_name,NULL);
1034 if (execution_path != (char *) NULL)
1035 (void) CopyMagickString(path,execution_path,extent);
1037 if (program_name != program_invocation_name)
1038 program_name=(char *) RelinquishMagickMemory(program_name);
1039 execution_path=(char *) RelinquishMagickMemory(execution_path);
1042 return(IsPathAccessible(path));
1046 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1050 % G e t M a g i c k P a g e S i z e %
1054 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1056 % GetMagickPageSize() returns the memory page size.
1058 % The format of the GetMagickPageSize method is:
1060 % ssize_t GetMagickPageSize()
1063 MagickPrivate ssize_t GetMagickPageSize(void)
1070 #if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1071 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1073 #if defined(MAGICKCORE_HAVE_GETPAGESIZE)
1074 page_size=(ssize_t) getpagesize();
1083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1087 % G e t P a t h A t t r i b u t e s %
1091 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1093 % GetPathAttributes() returns attributes (e.g. size of file) about a path.
1095 % The path of the GetPathAttributes method is:
1097 % MagickBooleanType GetPathAttributes(const char *path,void *attributes)
1099 % A description of each parameter follows.
1101 % o path: the file path.
1103 % o attributes: the path attributes are returned here.
1107 #if defined(MAGICKCORE_HAVE__WFOPEN)
1108 static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16)
1110 register const unsigned char
1113 if (utf16 != (wchar_t *) NULL)
1122 Convert UTF-8 to UTF-16.
1125 for (p=utf8; *p != '\0'; p++)
1127 if ((*p & 0x80) == 0)
1130 if ((*p & 0xE0) == 0xC0)
1135 if ((*p & 0xC0) != 0x80)
1140 if ((*p & 0xF0) == 0xE0)
1145 if ((*p & 0xC0) != 0x80)
1148 *q|=(c & 0x3F) << 6;
1150 if ((*p & 0xC0) != 0x80)
1162 Compute UTF-16 string length.
1164 for (p=utf8; *p != '\0'; p++)
1166 if ((*p & 0x80) == 0)
1169 if ((*p & 0xE0) == 0xC0)
1172 if ((*p & 0xC0) != 0x80)
1176 if ((*p & 0xF0) == 0xE0)
1179 if ((*p & 0xC0) != 0x80)
1182 if ((*p & 0xC0) != 0x80)
1191 static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source)
1199 length=UTF8ToUTF16(source,(wchar_t *) NULL);
1206 Not UTF-8, just copy.
1208 length=strlen((const char *) source);
1209 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
1210 if (utf16 == (wchar_t *) NULL)
1211 return((wchar_t *) NULL);
1212 for (i=0; i <= (ssize_t) length; i++)
1216 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
1217 if (utf16 == (wchar_t *) NULL)
1218 return((wchar_t *) NULL);
1219 length=UTF8ToUTF16(source,utf16);
1224 MagickExport MagickBooleanType GetPathAttributes(const char *path,
1230 if (path == (const char *) NULL)
1233 return(MagickFalse);
1235 #if !defined(MAGICKCORE_HAVE__WSTAT)
1236 status=stat(path,(struct stat *) attributes) == 0 ? MagickTrue : MagickFalse;
1242 unicode_path=ConvertUTF8ToUTF16((const unsigned char *) path);
1243 if (unicode_path == (wchar_t *) NULL)
1244 return(MagickFalse);
1245 status=wstat(unicode_path,(struct stat *) attributes) == 0 ? MagickTrue :
1247 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
1254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1258 % G e t P a t h C o m p o n e n t %
1262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1264 % GetPathComponent() returns the parent directory name, filename, basename, or
1265 % extension of a file path.
1267 % The format of the GetPathComponent function is:
1269 % GetPathComponent(const char *path,PathType type,char *component)
1271 % A description of each parameter follows:
1273 % o path: Specifies a pointer to a character array that contains the
1276 % o type: Specififies which file path component to return.
1278 % o component: the selected file path component is returned here.
1281 MagickExport void GetPathComponent(const char *path,PathType type,
1285 magick[MaxTextExtent],
1287 subimage[MaxTextExtent];
1292 assert(path != (const char *) NULL);
1293 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",path);
1294 assert(component != (char *) NULL);
1300 (void) CopyMagickString(component,path,MaxTextExtent);
1305 p=component+strlen(component)-1;
1306 if ((*p == ']') && (strchr(component,'[') != (char *) NULL) &&
1307 (IsPathAccessible(path) == MagickFalse))
1310 Look for scene specification (e.g. img0001.pcd[4]).
1312 for (q=p-1; q > component; q--)
1317 (void) CopyMagickString(subimage,q+1,MaxTextExtent);
1318 subimage[p-q-1]='\0';
1319 if ((IsSceneGeometry(subimage,MagickFalse) == MagickFalse) &&
1320 (IsGeometry(subimage) == MagickFalse))
1326 #if defined(__OS2__)
1329 for (p=component; *p != '\0'; p++)
1331 if ((*p == '%') && (*(p+1) == '['))
1336 for (p++; (*p != ']') && (*p != '\0'); p++) ;
1340 if ((*p == ':') && (IsPathDirectory(component) < 0) &&
1341 (IsPathAccessible(component) == MagickFalse))
1344 Look for image format specification (e.g. ps3:image).
1346 (void) CopyMagickString(magick,component,(size_t) (p-component+1));
1347 if (IsMagickConflict(magick) != MagickFalse)
1350 for (q=component; *q != '\0'; q++)
1357 for (p=component+strlen(component)-1; p > component; p--)
1358 if (IsBasenameSeparator(*p) != MagickFalse)
1364 (void) CopyMagickString(component,magick,MaxTextExtent);
1369 for (p=component+(strlen(component)-1); p > component; p--)
1371 if (IsBasenameSeparator(*p) != MagickFalse)
1387 if (IsBasenameSeparator(*p) != MagickFalse)
1388 (void) CopyMagickMemory((unsigned char *) component,
1389 (const unsigned char *) (p+1),strlen(p+1)+1);
1394 if (IsBasenameSeparator(*p) != MagickFalse)
1395 (void) CopyMagickString(component,p+1,MaxTextExtent);
1396 for (p=component+(strlen(component)-1); p > component; p--)
1406 if (IsBasenameSeparator(*p) != MagickFalse)
1407 (void) CopyMagickString(component,p+1,MaxTextExtent);
1410 for (p=component+strlen(component)-1; p > component; p--)
1415 (void) CopyMagickString(component,p+1,MaxTextExtent);
1420 (void) CopyMagickString(component,subimage,MaxTextExtent);
1430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1434 % G e t P a t h C o m p o n e n t s %
1438 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1440 % GetPathComponents() returns a list of path components.
1442 % The format of the GetPathComponents method is:
1444 % char **GetPathComponents(const char *path,
1445 % size_t *number_componenets)
1447 % A description of each parameter follows:
1449 % o path: Specifies the string to segment into a list.
1451 % o number_components: return the number of components in the list
1454 MagickPrivate char **GetPathComponents(const char *path,
1455 size_t *number_components)
1467 if (path == (char *) NULL)
1468 return((char **) NULL);
1469 *number_components=1;
1470 for (p=path; *p != '\0'; p++)
1471 if (IsBasenameSeparator(*p))
1472 (*number_components)++;
1473 components=(char **) AcquireQuantumMemory((size_t) *number_components+1UL,
1474 sizeof(*components));
1475 if (components == (char **) NULL)
1476 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1478 for (i=0; i < (ssize_t) *number_components; i++)
1480 for (q=p; *q != '\0'; q++)
1481 if (IsBasenameSeparator(*q))
1483 components[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
1484 sizeof(**components));
1485 if (components[i] == (char *) NULL)
1486 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1487 (void) CopyMagickString(components[i],p,(size_t) (q-p+1));
1490 components[i]=(char *) NULL;
1495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1499 % I s P a t h A c c e s s i b l e %
1503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1505 % IsPathAccessible() returns MagickTrue if the file as defined by the path is
1508 % The format of the IsPathAccessible method is:
1510 % MagickBooleanType IsPathAccessible(const char *filename)
1512 % A description of each parameter follows.
1514 % o path: Specifies a path to a file.
1517 MagickExport MagickBooleanType IsPathAccessible(const char *path)
1525 if ((path == (const char *) NULL) || (*path == '\0'))
1526 return(MagickFalse);
1527 status=GetPathAttributes(path,&attributes);
1528 if (status == MagickFalse)
1530 if (S_ISREG(attributes.st_mode) == 0)
1531 return(MagickFalse);
1532 if (access(path,F_OK) != 0)
1533 return(MagickFalse);
1538 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1542 + I s P a t h D i r e c t o r y %
1546 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1548 % IsPathDirectory() returns -1 if the directory does not exist, 1 is returned
1549 % if the path represents a directory otherwise 0.
1551 % The format of the IsPathDirectory method is:
1553 % int IsPathDirectory(const char *path)
1555 % A description of each parameter follows.
1557 % o path: The directory path.
1560 static int IsPathDirectory(const char *path)
1568 if ((path == (const char *) NULL) || (*path == '\0'))
1569 return(MagickFalse);
1570 status=GetPathAttributes(path,&attributes);
1571 if (status == MagickFalse)
1573 if (S_ISDIR(attributes.st_mode) == 0)
1579 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1583 % I s M a g i c k T r u e %
1587 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1589 % IsMagickTrue() returns MagickTrue if the value is "true", "on", "yes" or
1592 % The format of the IsMagickTrue method is:
1594 % MagickBooleanType IsMagickTrue(const char *value)
1596 % A description of each parameter follows:
1598 % o option: either MagickTrue or MagickFalse depending on the value
1601 % o value: Specifies a pointer to a character array.
1604 MagickExport MagickBooleanType IsMagickTrue(const char *value)
1606 if (value == (const char *) NULL)
1607 return(MagickFalse);
1608 if (LocaleCompare(value,"true") == 0)
1610 if (LocaleCompare(value,"on") == 0)
1612 if (LocaleCompare(value,"yes") == 0)
1614 if (LocaleCompare(value,"1") == 0)
1616 return(MagickFalse);
1620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1624 % L i s t F i l e s %
1628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1630 % ListFiles() reads the directory specified and returns a list of filenames
1631 % contained in the directory sorted in ascending alphabetic order.
1633 % The format of the ListFiles function is:
1635 % char **ListFiles(const char *directory,const char *pattern,
1636 % ssize_t *number_entries)
1638 % A description of each parameter follows:
1640 % o filelist: Method ListFiles returns a list of filenames contained
1641 % in the directory. If the directory specified cannot be read or it is
1642 % a file a NULL list is returned.
1644 % o directory: Specifies a pointer to a text string containing a directory
1647 % o pattern: Specifies a pointer to a text string containing a pattern.
1649 % o number_entries: This integer returns the number of filenames in the
1654 #if defined(__cplusplus) || defined(c_plusplus)
1658 static int FileCompare(const void *x,const void *y)
1664 p=(const char **) x;
1665 q=(const char **) y;
1666 return(LocaleCompare(*p,*q));
1669 #if defined(__cplusplus) || defined(c_plusplus)
1673 static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
1674 struct dirent **result)
1676 #if defined(MAGICKCORE_HAVE_READDIR_R)
1677 return(readdir_r(directory,entry,result));
1681 *result=readdir(directory);
1686 MagickPrivate char **ListFiles(const char *directory,const char *pattern,
1687 size_t *number_entries)
1705 assert(directory != (const char *) NULL);
1706 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",directory);
1707 assert(pattern != (const char *) NULL);
1708 assert(number_entries != (size_t *) NULL);
1710 current_directory=opendir(directory);
1711 if (current_directory == (DIR *) NULL)
1712 return((char **) NULL);
1717 filelist=(char **) AcquireQuantumMemory((size_t) max_entries,
1719 if (filelist == (char **) NULL)
1721 (void) closedir(current_directory);
1722 return((char **) NULL);
1725 Save the current and change to the new directory.
1727 buffer=(struct dirent *) AcquireMagickMemory(sizeof(*buffer)+
1729 if (buffer == (struct dirent *) NULL)
1730 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1731 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1732 (entry != (struct dirent *) NULL))
1734 if (*entry->d_name == '.')
1736 if ((IsPathDirectory(entry->d_name) > 0) ||
1737 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1738 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1740 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1743 if (*number_entries >= max_entries)
1746 Extend the file list.
1749 filelist=(char **) ResizeQuantumMemory(filelist,(size_t)
1750 max_entries,sizeof(*filelist));
1751 if (filelist == (char **) NULL)
1759 p=strchr(entry->d_name,';');
1762 if (*number_entries > 0)
1763 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1767 filelist[*number_entries]=(char *) AcquireString(entry->d_name);
1768 (*number_entries)++;
1771 buffer=(struct dirent *) RelinquishMagickMemory(buffer);
1772 (void) closedir(current_directory);
1773 if (filelist == (char **) NULL)
1774 return((char **) NULL);
1776 Sort filelist in ascending order.
1778 qsort((void *) filelist,(size_t) *number_entries,sizeof(*filelist),
1784 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1788 % M a g i c k D e l a y %
1792 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1794 % MagickDelay() suspends program execution for the number of milliseconds
1797 % The format of the Delay method is:
1799 % void MagickDelay(const MagickSizeType milliseconds)
1801 % A description of each parameter follows:
1803 % o milliseconds: Specifies the number of milliseconds to delay before
1807 MagickPrivate void MagickDelay(const MagickSizeType milliseconds)
1809 if (milliseconds == 0)
1811 #if defined(MAGICKCORE_HAVE_NANOSLEEP)
1816 timer.tv_sec=(time_t) (milliseconds/1000);
1817 timer.tv_nsec=(milliseconds % 1000)*1000*1000;
1818 (void) nanosleep(&timer,(struct timespec *) NULL);
1820 #elif defined(MAGICKCORE_HAVE_USLEEP)
1821 usleep(1000*milliseconds);
1822 #elif defined(MAGICKCORE_HAVE_SELECT)
1827 timer.tv_sec=(long) milliseconds/1000;
1828 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1829 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1831 #elif defined(MAGICKCORE_HAVE_POLL)
1832 (void) poll((struct pollfd *) NULL,0,(int) milliseconds);
1833 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1834 Sleep((long) milliseconds);
1840 timer=milliseconds/1000.0;
1843 #elif defined(__BEOS__)
1844 snooze(1000*milliseconds);
1846 # error "Time delay method not defined."
1851 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1855 % M u l t i l i n e C e n s u s %
1859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1861 % MultilineCensus() returns the number of lines within a label. A line is
1862 % represented by a \n character.
1864 % The format of the MultilineCenus method is:
1866 % size_t MultilineCensus(const char *label)
1868 % A description of each parameter follows.
1870 % o label: This character string is the label.
1873 MagickExport size_t MultilineCensus(const char *label)
1879 Determine the number of lines within this label.
1881 if (label == (char *) NULL)
1883 for (number_lines=1; *label != '\0'; label++)
1886 return(number_lines);
1890 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1894 % O p e n M a g i c k S t r e a m %
1898 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1900 % OpenMagickStream() opens the file at the specified path and return the
1901 % associated stream.
1903 % The path of the OpenMagickStream method is:
1905 % FILE *OpenMagickStream(const char *path,const char *mode)
1907 % A description of each parameter follows.
1909 % o path: the file path.
1911 % o mode: the file mode.
1914 MagickExport FILE *OpenMagickStream(const char *path,const char *mode)
1919 if ((path == (const char *) NULL) || (mode == (const char *) NULL))
1922 return((FILE *) NULL);
1925 #if defined(MAGICKCORE_HAVE__WFOPEN)
1931 unicode_path=ConvertUTF8ToUTF16((const unsigned char *) path);
1932 if (unicode_path == (wchar_t *) NULL)
1933 return((FILE *) NULL);
1934 unicode_mode=ConvertUTF8ToUTF16((const unsigned char *) mode);
1935 if (unicode_mode == (wchar_t *) NULL)
1937 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
1938 return((FILE *) NULL);
1940 file=_wfopen(unicode_path,unicode_mode);
1941 unicode_mode=(wchar_t *) RelinquishMagickMemory(unicode_mode);
1942 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
1945 if (file == (FILE *) NULL)
1946 file=fopen(path,mode);
1951 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1955 % S y s t e m C o m m a n d %
1959 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1961 % SystemCommand() executes the specified command and waits until it
1962 % terminates. The returned value is the exit status of the command.
1964 % The format of the SystemCommand method is:
1966 % int SystemCommand(const MagickBooleanType asynchronous,
1967 % const MagickBooleanType verbose,const char *command,
1968 % ExceptionInfo *exception)
1970 % A description of each parameter follows:
1972 % o asynchronous: a value other than 0 executes the parent program
1973 % concurrently with the new child process.
1975 % o verbose: a value other than 0 prints the executed command before it is
1978 % o command: this string is the command to execute.
1980 % o exception: return any errors here.
1983 MagickExport int SystemCommand(const MagickBooleanType asynchronous,
1984 const MagickBooleanType verbose,const char *command,ExceptionInfo *exception)
2004 arguments=StringToArgv(command,&number_arguments);
2005 if (arguments == (char **) NULL)
2007 rights=ExecutePolicyRights;
2008 domain=DelegatePolicyDomain;
2009 if (IsRightsAuthorized(domain,rights,arguments[1]) == MagickFalse)
2012 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2013 "NotAuthorized","`%s'",arguments[1]);
2014 for (i=0; i < (ssize_t) number_arguments; i++)
2015 arguments[i]=DestroyString(arguments[i]);
2016 arguments=(char **) RelinquishMagickMemory(arguments);
2019 if (verbose != MagickFalse)
2021 (void) FormatLocaleFile(stderr,"%s\n",command);
2022 (void) fflush(stderr);
2024 shell_command=(char *) command;
2025 if (asynchronous != MagickFalse)
2027 shell_command=AcquireString(command);
2028 (void) ConcatenateMagickString(shell_command,"&",MaxTextExtent);
2030 #if defined(MAGICKCORE_POSIX_SUPPORT)
2031 #if !defined(MAGICKCORE_HAVE_EXECVP)
2032 status=system(shell_command);
2034 if ((asynchronous != MagickFalse) || (strspn(shell_command,"&;<>|") == 0))
2035 status=system(shell_command);
2042 Call application directly rather than from a shell.
2045 if (child_pid == (pid_t) -1)
2046 status=system(command);
2050 status=execvp(arguments[1],arguments+1);
2062 pid=waitpid(child_pid,&child_status,0);
2067 if (WIFEXITED(child_status) != 0)
2068 status=WEXITSTATUS(child_status);
2070 if (WIFSIGNALED(child_status))
2076 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
2082 if (asynchronous != MagickFalse)
2084 status=spawnvp(mode,arguments[1],(const char **) (arguments+1));
2086 #elif defined(macintosh)
2087 status=MACSystemCommand(shell_command);
2089 status=system(shell_command);
2091 # error No suitable system() method.
2094 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
2095 "`%s' (%d)",command,status);
2096 if (shell_command != command)
2097 shell_command=DestroyString(shell_command);
2098 for (i=0; i < (ssize_t) number_arguments; i++)
2099 arguments[i]=DestroyString(arguments[i]);
2100 arguments=(char **) RelinquishMagickMemory(arguments);