MagickCore 7.1.2-33
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
utility.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% U U TTTTT IIIII L IIIII TTTTT Y Y %
7% U U T I L I T Y Y %
8% U U T I L I T Y %
9% U U T I L I T Y %
10% UUU T IIIII LLLLL IIIII T Y %
11% %
12% %
13% MagickCore Utility Methods %
14% %
15% Software Design %
16% Cristy %
17% January 1993 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/license/ %
27% %
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. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38␌
39/*
40 Include declarations.
41*/
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/image-private.h"
50#include "MagickCore/list.h"
51#include "MagickCore/log.h"
52#include "MagickCore/magick-private.h"
53#include "MagickCore/memory_.h"
54#include "MagickCore/nt-base-private.h"
55#include "MagickCore/option.h"
56#include "MagickCore/policy.h"
57#include "MagickCore/policy-private.h"
58#include "MagickCore/random_.h"
59#include "MagickCore/registry.h"
60#include "MagickCore/resource_.h"
61#include "MagickCore/semaphore.h"
62#include "MagickCore/signature-private.h"
63#include "MagickCore/statistic.h"
64#include "MagickCore/string_.h"
65#include "MagickCore/string-private.h"
66#include "MagickCore/token.h"
67#include "MagickCore/token-private.h"
68#include "MagickCore/utility.h"
69#include "MagickCore/utility-private.h"
70#if defined(MAGICKCORE_HAVE_PROCESS_H)
71#include <process.h>
72#endif
73#if defined(MAGICKCORE_HAVE_MACH_O_DYLD_H)
74#include <mach-o/dyld.h>
75#endif
76␌
77/*
78 Static declarations.
79*/
80static const char
81 Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
82␌
83/*
84 Forward declaration.
85*/
86static int
87 IsPathDirectory(const char *);
88␌
89/*
90%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91% %
92% %
93% %
94% A c q u i r e U n i q u e F i l e n a m e %
95% %
96% %
97% %
98%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99%
100% AcquireUniqueFilename() replaces the contents of path by a unique path name.
101%
102% The format of the AcquireUniqueFilename method is:
103%
104% MagickBooleanType AcquireUniqueFilename(char *path)
105%
106% A description of each parameter follows.
107%
108% o path: Specifies a pointer to an array of characters. The unique path
109% name is returned in this array.
110%
111*/
112MagickExport MagickBooleanType AcquireUniqueFilename(char *path)
113{
114 int
115 file;
116
117 file=AcquireUniqueFileResource(path);
118 if (file == -1)
119 return(MagickFalse);
120 file=close_utf8(file)-1;
121 return(MagickTrue);
122}
123␌
124/*
125%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126% %
127% %
128% %
129% A c q u i r e U n i q u e S ym b o l i c L i n k %
130% %
131% %
132% %
133%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134%
135% AcquireUniqueSymbolicLink() creates a unique symbolic link to the specified
136% source path and returns MagickTrue on success otherwise MagickFalse. If the
137% symlink() method fails or is not available, a unique file name is generated
138% and the source file copied to it. When you are finished with the file, use
139% RelinquishUniqueFileResource() to destroy it.
140%
141% The format of the AcquireUniqueSymbolicLink method is:
142%
143% MagickBooleanType AcquireUniqueSymbolicLink(const char *source,
144% char destination)
145%
146% A description of each parameter follows.
147%
148% o source: the source path.
149%
150% o destination: the destination path.
151%
152*/
153
154MagickExport MagickBooleanType AcquireUniqueSymbolicLink(const char *source,
155 char *destination)
156{
157 int
158 destination_file,
159 source_file;
160
161 MagickBooleanType
162 status;
163
164 size_t
165 length,
166 quantum;
167
168 ssize_t
169 count;
170
171 static ssize_t
172 passes = -1;
173
174 struct stat
175 attributes;
176
177 unsigned char
178 *buffer;
179
180 assert(source != (const char *) NULL);
181 assert(destination != (char *) NULL);
182#if defined(MAGICKCORE_HAVE_SYMLINK)
183 {
184 /*
185 Does policy permit symbolic links?
186 */
187 status=IsRightsAuthorizedByName(SystemPolicyDomain,"symlink",(PolicyRights)
188 (ReadPolicyRights | WritePolicyRights),"follow");
189 if (passes == -1)
190 passes=GetShredPasses();
191 if ((passes == 0) && (status != MagickFalse))
192 {
193 (void) AcquireUniqueFilename(destination);
194 (void) RelinquishUniqueFileResource(destination);
195 if (*source == *DirectorySeparator)
196 {
197 if (symlink(source,destination) == 0)
198 return(MagickTrue);
199 }
200 else
201 {
202 char
203 path[MagickPathExtent];
204
205 *path='\0';
206 if (getcwd(path,MagickPathExtent) == (char *) NULL)
207 return(MagickFalse);
208 (void) ConcatenateMagickString(path,DirectorySeparator,
209 MagickPathExtent);
210 (void) ConcatenateMagickString(path,source,MagickPathExtent);
211 if (symlink(path,destination) == 0)
212 return(MagickTrue);
213 }
214 }
215 }
216#endif
217 /*
218 Copy file from source to destination.
219 */
220 destination_file=AcquireUniqueFileResource(destination);
221 if (destination_file == -1)
222 return(MagickFalse);
223 source_file=open_utf8(source,O_RDONLY | O_CLOEXEC | O_BINARY,0);
224 if (source_file == -1)
225 {
226 (void) close_utf8(destination_file);
227 (void) RelinquishUniqueFileResource(destination);
228 return(MagickFalse);
229 }
230 quantum=(size_t) MagickMaxBufferExtent;
231 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
232 quantum=(size_t) MagickMin(attributes.st_size,MagickMaxBufferExtent);
233 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
234 if (buffer == (unsigned char *) NULL)
235 {
236 (void) close_utf8(source_file);
237 (void) close_utf8(destination_file);
238 (void) RelinquishUniqueFileResource(destination);
239 return(MagickFalse);
240 }
241 status=MagickTrue;
242 for (length=0; ; )
243 {
244 count=(ssize_t) read(source_file,buffer,quantum);
245 if (count <= 0)
246 break;
247 length=(size_t) count;
248 count=(ssize_t) write(destination_file,buffer,length);
249 if ((size_t) count != length)
250 {
251 (void) RelinquishUniqueFileResource(destination);
252 status=MagickFalse;
253 break;
254 }
255 }
256 (void) close_utf8(destination_file);
257 (void) close_utf8(source_file);
258 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
259 return(status);
260}
261␌
262/*
263%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264% %
265% %
266% %
267% A p p e n d I m a g e F o r m a t %
268% %
269% %
270% %
271%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272%
273% AppendImageFormat() appends the image format type to the filename. If an
274% extension to the file already exists, it is first removed.
275%
276% The format of the AppendImageFormat method is:
277%
278% void AppendImageFormat(const char *format,char *filename)
279%
280% A description of each parameter follows.
281%
282% o format: Specifies a pointer to an array of characters. This the
283% format of the image.
284%
285% o filename: Specifies a pointer to an array of characters. The unique
286% file name is returned in this array.
287%
288*/
289MagickExport void AppendImageFormat(const char *format,char *filename)
290{
291 char
292 extension[MagickPathExtent],
293 root[MagickPathExtent];
294
295 assert(format != (char *) NULL);
296 assert(filename != (char *) NULL);
297 if (IsEventLogging() != MagickFalse)
298 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
299 if ((*format == '\0') || (*filename == '\0'))
300 return;
301 if (LocaleCompare(filename,"-") == 0)
302 {
303 char
304 message[MagickPathExtent];
305
306 (void) FormatLocaleString(message,MagickPathExtent,"%s:%s",format,
307 filename);
308 (void) CopyMagickString(filename,message,MagickPathExtent);
309 return;
310 }
311 GetPathComponent(filename,ExtensionPath,extension);
312 if ((LocaleCompare(extension,"Z") == 0) ||
313 (LocaleCompare(extension,"bz2") == 0) ||
314 (LocaleCompare(extension,"gz") == 0) ||
315 (LocaleCompare(extension,"wmz") == 0) ||
316 (LocaleCompare(extension,"svgz") == 0))
317 {
318 GetPathComponent(filename,RootPath,root);
319 (void) CopyMagickString(filename,root,MagickPathExtent);
320 GetPathComponent(filename,RootPath,root);
321 (void) FormatLocaleString(filename,MagickPathExtent,"%s.%s.%s",root,
322 format,extension);
323 return;
324 }
325 GetPathComponent(filename,RootPath,root);
326 (void) FormatLocaleString(filename,MagickPathExtent,"%s.%s",root,format);
327}
328␌
329/*
330%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331% %
332% %
333% %
334% B a s e 6 4 D e c o d e %
335% %
336% %
337% %
338%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339%
340% Base64Decode() decodes Base64-encoded text and returns its binary
341% equivalent. NULL is returned if the text is not valid Base64 data, or a
342% memory allocation failure occurs.
343%
344% The format of the Base64Decode method is:
345%
346% unsigned char *Base64Decode(const char *source,length_t *length)
347%
348% A description of each parameter follows:
349%
350% o source: A pointer to a Base64-encoded string.
351%
352% o length: the number of bytes decoded.
353%
354*/
355MagickExport unsigned char *Base64Decode(const char *source,size_t *length)
356{
357 int
358 state;
359
360 const char
361 *p,
362 *q;
363
364 size_t
365 i;
366
367 unsigned char
368 *decode;
369
370 assert(source != (char *) NULL);
371 assert(length != (size_t *) NULL);
372 if (IsEventLogging() != MagickFalse)
373 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
374 *length=0;
375 decode=(unsigned char *) AcquireQuantumMemory((strlen(source)+3)/4,
376 3*sizeof(*decode));
377 if (decode == (unsigned char *) NULL)
378 return((unsigned char *) NULL);
379 i=0;
380 state=0;
381 for (p=source; *p != '\0'; p++)
382 {
383 if (isspace((int) ((unsigned char) *p)) != 0)
384 continue;
385 if (*p == '=')
386 break;
387 q=strchr(Base64,*p);
388 if (q == (char *) NULL)
389 {
390 decode=(unsigned char *) RelinquishMagickMemory(decode);
391 return((unsigned char *) NULL); /* non-Base64 character */
392 }
393 switch (state)
394 {
395 case 0:
396 {
397 decode[i]=(unsigned char)((q-Base64) << 2);
398 state++;
399 break;
400 }
401 case 1:
402 {
403 decode[i++]|=(unsigned char)((q-Base64) >> 4);
404 decode[i]=(unsigned char)(((q-Base64) & 0x0f) << 4);
405 state++;
406 break;
407 }
408 case 2:
409 {
410 decode[i++]|=(unsigned char)((q-Base64) >> 2);
411 decode[i]=(unsigned char)(((q-Base64) & 0x03) << 6);
412 state++;
413 break;
414 }
415 case 3:
416 {
417 decode[i++]|=(unsigned char)(q-Base64);
418 state=0;
419 break;
420 }
421 }
422 }
423 /*
424 Verify Base-64 string has proper terminal characters.
425 */
426 if (*p != '=')
427 {
428 if (state != 0)
429 {
430 decode=(unsigned char *) RelinquishMagickMemory(decode);
431 return((unsigned char *) NULL);
432 }
433 }
434 else
435 {
436 p++;
437 switch (state)
438 {
439 case 0:
440 case 1:
441 {
442 /*
443 Unrecognized '=' character.
444 */
445 decode=(unsigned char *) RelinquishMagickMemory(decode);
446 return((unsigned char *) NULL);
447 }
448 case 2:
449 {
450 for ( ; *p != '\0'; p++)
451 if (isspace((int) ((unsigned char) *p)) == 0)
452 break;
453 if (*p != '=')
454 {
455 decode=(unsigned char *) RelinquishMagickMemory(decode);
456 return((unsigned char *) NULL);
457 }
458 p++;
459 }
460 case 3:
461 {
462 for ( ; *p != '\0'; p++)
463 if (isspace((int) ((unsigned char) *p)) == 0)
464 {
465 decode=(unsigned char *) RelinquishMagickMemory(decode);
466 return((unsigned char *) NULL);
467 }
468 if ((int) decode[i] != 0)
469 {
470 decode=(unsigned char *) RelinquishMagickMemory(decode);
471 return((unsigned char *) NULL);
472 }
473 break;
474 }
475 }
476 }
477 *length=i;
478 return(decode);
479}
480␌
481/*
482%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
483% %
484% %
485% %
486% B a s e 6 4 E n c o d e %
487% %
488% %
489% %
490%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
491%
492% Base64Encode() encodes arbitrary binary data to Base64 encoded format as
493% described by the "Base64 Content-Transfer-Encoding" section of RFC 2045 and
494% returns the result as a null-terminated ASCII string. NULL is returned if
495% a memory allocation failure occurs.
496%
497% The format of the Base64Encode method is:
498%
499% char *Base64Encode(const unsigned char *blob,const size_t blob_length,
500% size_t *encode_length)
501%
502% A description of each parameter follows:
503%
504% o blob: A pointer to binary data to encode.
505%
506% o blob_length: the number of bytes to encode.
507%
508% o encode_length: The number of bytes encoded.
509%
510*/
511MagickExport char *Base64Encode(const unsigned char *blob,
512 const size_t blob_length,size_t *encode_length)
513{
514 char
515 *encode;
516
517 const unsigned char
518 *p;
519
520 size_t
521 i;
522
523 size_t
524 remainder;
525
526 assert(blob != (const unsigned char *) NULL);
527 assert(blob_length != 0);
528 assert(encode_length != (size_t *) NULL);
529 if (IsEventLogging() != MagickFalse)
530 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
531 *encode_length=0;
532 encode=(char *) AcquireQuantumMemory(blob_length/3+4,4*sizeof(*encode));
533 if (encode == (char *) NULL)
534 return((char *) NULL);
535 i=0;
536 for (p=blob; p < (blob+blob_length-2); p+=(ptrdiff_t) 3)
537 {
538 encode[i++]=Base64[(int) (*p >> 2)];
539 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
540 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
541 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
542 }
543 remainder=blob_length % 3;
544 if (remainder != 0)
545 {
546 ssize_t
547 j;
548
549 unsigned char
550 code[3];
551
552 code[0]='\0';
553 code[1]='\0';
554 code[2]='\0';
555 for (j=0; j < (ssize_t) remainder; j++)
556 code[j]=(*p++);
557 encode[i++]=Base64[(int) (code[0] >> 2)];
558 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
559 if (remainder == 1)
560 encode[i++]='=';
561 else
562 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
563 encode[i++]='=';
564 }
565 *encode_length=i;
566 encode[i++]='\0';
567 return(encode);
568}
569␌
570/*
571%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
572% %
573% %
574% %
575% C h o p P a t h C o m p o n e n t s %
576% %
577% %
578% %
579%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
580%
581% ChopPathComponents() removes the number of specified file components from a
582% path.
583%
584% The format of the ChopPathComponents method is:
585%
586% ChopPathComponents(char *path,size_t components)
587%
588% A description of each parameter follows:
589%
590% o path: The path.
591%
592% o components: The number of components to chop.
593%
594*/
595MagickPrivate void ChopPathComponents(char *path,const size_t components)
596{
597 ssize_t
598 i;
599
600 for (i=0; i < (ssize_t) components; i++)
601 GetPathComponent(path,HeadPath,path);
602}
603␌
604/*
605%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
606% %
607% %
608% %
609% E x p a n d F i l e n a m e %
610% %
611% %
612% %
613%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
614%
615% ExpandFilename() expands '~' in a path.
616%
617% The format of the ExpandFilename function is:
618%
619% ExpandFilename(char *path)
620%
621% A description of each parameter follows:
622%
623% o path: Specifies a pointer to a character array that contains the
624% path.
625%
626*/
627MagickPrivate void ExpandFilename(char *path)
628{
629 char
630 expand_path[MagickPathExtent];
631
632 if (path == (char *) NULL)
633 return;
634 if (*path != '~')
635 return;
636 (void) CopyMagickString(expand_path,path,MagickPathExtent);
637 if ((*(path+1) == *DirectorySeparator) || (*(path+1) == '\0'))
638 {
639 char
640 *home;
641
642 /*
643 Substitute ~ with $HOME.
644 */
645 (void) CopyMagickString(expand_path,".",MagickPathExtent);
646 (void) ConcatenateMagickString(expand_path,path+1,MagickPathExtent);
647 home=GetEnvironmentValue("HOME");
648 if (home == (char *) NULL)
649 home=GetEnvironmentValue("USERPROFILE");
650 if (home != (char *) NULL)
651 {
652 (void) CopyMagickString(expand_path,home,MagickPathExtent);
653 (void) ConcatenateMagickString(expand_path,path+1,MagickPathExtent);
654 home=DestroyString(home);
655 }
656 }
657 else
658 {
659#if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
660 char
661#if defined(MAGICKCORE_HAVE_GETPWNAM_R)
662 buffer[MagickPathExtent],
663#endif
664 username[MagickPathExtent];
665
666 char
667 *p;
668
669 struct passwd
670 *entry,
671 pwd;
672
673 /*
674 Substitute ~ with home directory from password file.
675 */
676 (void) CopyMagickString(username,path+1,MagickPathExtent);
677 p=strchr(username,'/');
678 if (p != (char *) NULL)
679 *p='\0';
680#if !defined(MAGICKCORE_HAVE_GETPWNAM_R)
681 entry=getpwnam(username);
682#else
683 entry=(struct passwd *) NULL;
684 if (getpwnam_r(username,&pwd,buffer,sizeof(buffer),&entry) < 0)
685 return;
686#endif
687 if (entry == (struct passwd *) NULL)
688 return;
689 (void) CopyMagickString(expand_path,entry->pw_dir,MagickPathExtent);
690 if (p != (char *) NULL)
691 {
692 (void) ConcatenateMagickString(expand_path,"/",MagickPathExtent);
693 (void) ConcatenateMagickString(expand_path,p+1,MagickPathExtent);
694 }
695#endif
696 }
697 (void) CopyMagickString(path,expand_path,MagickPathExtent);
698}
699␌
700/*
701%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
702% %
703% %
704% %
705% E x p a n d F i l e n a m e s %
706% %
707% %
708% %
709%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
710%
711% ExpandFilenames() checks each argument of the given argument array, and
712% expands it if they have a wildcard character.
713%
714% Any coder prefix (EG: 'coder:filename') or read modifier postfix (EG:
715% 'filename[...]') are ignored during the file the expansion, but will be
716% included in the final argument. If no filename matching the meta-character
717% 'glob' is found the original argument is returned.
718%
719% For example, an argument of '*.gif[20x20]' will be replaced by the list
720% 'abc.gif[20x20]', 'foobar.gif[20x20]', 'xyzzy.gif[20x20]'
721% if such filenames exist, (in the current directory in this case).
722%
723% Meta-characters handled...
724% @ read a list of filenames (no further expansion performed)
725% ~ At start of filename expands to HOME environment variable
726% * matches any string including an empty string
727% ? matches by any single character
728%
729% WARNING: filenames starting with '.' (hidden files in a UNIX file system)
730% will never be expanded. Attempting to expand '.*' will produce no change.
731%
732% Expansion is ignored for coders "label:" "caption:" "pango:" and "vid:".
733% Which provide their own '@' meta-character handling.
734%
735% You can see the results of the expansion using "Configure" log events.
736%
737% The returned list should be freed using DestroyStringList().
738%
739% However the strings in the original pointed to argv are not
740% freed (TO BE CHECKED). So a copy of the original pointer (and count)
741% should be kept separate if they need to be freed later.
742%
743% The format of the ExpandFilenames function is:
744%
745% status=ExpandFilenames(int *number_arguments,char ***arguments)
746%
747% A description of each parameter follows:
748%
749% o number_arguments: Specifies a pointer to an integer describing the
750% number of elements in the argument vector.
751%
752% o arguments: Specifies a pointer to a text array containing the command
753% line arguments.
754%
755*/
756static inline void getcwd_utf8(char *path,size_t extent)
757{
758#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
759 char
760 *directory;
761
762 directory=getcwd(path,extent);
763 (void) directory;
764#else
765 wchar_t
766 wide_path[MagickPathExtent];
767
768 (void) _wgetcwd(wide_path,MagickPathExtent-1);
769 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,NULL);
770#endif
771}
772
773MagickExport MagickBooleanType ExpandFilenames(int *number_arguments,
774 char ***arguments)
775{
776 char
777 home_directory[MagickPathExtent],
778 **vector;
779
780 ssize_t
781 i,
782 j;
783
784 size_t
785 number_files;
786
787 ssize_t
788 count,
789 parameters;
790
791 /*
792 Allocate argument vector.
793 */
794 assert(number_arguments != (int *) NULL);
795 assert(arguments != (char ***) NULL);
796 if (IsEventLogging() != MagickFalse)
797 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
798 vector=(char **) AcquireQuantumMemory((size_t) (*number_arguments+1),
799 sizeof(*vector));
800 if (vector == (char **) NULL)
801 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
802 /*
803 Expand any wildcard filenames.
804 */
805 *home_directory='\0';
806 count=0;
807 for (i=0; i < (ssize_t) *number_arguments; i++)
808 {
809 char
810 **filelist,
811 filename[MagickPathExtent],
812 magick[MagickPathExtent],
813 *option,
814 path[MagickPathExtent],
815 subimage[MagickPathExtent];
816
817 MagickBooleanType
818 destroy;
819
820 option=(*arguments)[i];
821 *magick='\0';
822 *path='\0';
823 *filename='\0';
824 *subimage='\0';
825 number_files=0;
826 vector[count++]=ConstantString(option);
827 destroy=MagickTrue;
828 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
829 if (parameters > 0)
830 {
831 /*
832 Do not expand command option parameters.
833 */
834 for (j=0; j < parameters; j++)
835 {
836 i++;
837 if (i == (ssize_t) *number_arguments)
838 break;
839 option=(*arguments)[i];
840 vector[count++]=ConstantString(option);
841 }
842 continue;
843 }
844 if ((*option == '"') || (*option == '\''))
845 continue;
846 GetPathComponent(option,TailPath,filename);
847 GetPathComponent(option,MagickPath,magick);
848 if ((LocaleCompare(magick,"CAPTION") == 0) ||
849 (LocaleCompare(magick,"LABEL") == 0) ||
850 (LocaleCompare(magick,"PANGO") == 0) ||
851 (LocaleCompare(magick,"VID") == 0))
852 continue;
853 if ((IsGlob(filename) == MagickFalse) && (*option != '@'))
854 continue;
855 if (IsPathAccessible(option) != MagickFalse)
856 continue;
857 if (*option != '@')
858 {
859 /*
860 Generate file list from wildcard filename (e.g. *.jpg).
861 */
862 GetPathComponent(option,HeadPath,path);
863 GetPathComponent(option,SubimagePath,subimage);
864 ExpandFilename(path);
865 if (*home_directory == '\0')
866 getcwd_utf8(home_directory,MagickPathExtent-1);
867 filelist=ListFiles(*path == '\0' ? home_directory : path,filename,
868 &number_files);
869 }
870 else
871 {
872 char
873 *files;
874
875 ExceptionInfo
876 *exception;
877
878 int
879 length;
880
881 /*
882 Generate file list from file list (e.g. @filelist.txt).
883 */
884 exception=AcquireExceptionInfo();
885 files=FileToString(option,~0UL,exception);
886 exception=DestroyExceptionInfo(exception);
887 if (files == (char *) NULL)
888 continue;
889 filelist=StringToArgv(files,&length);
890 if (filelist == (char **) NULL)
891 continue;
892 files=DestroyString(files);
893 filelist[0]=DestroyString(filelist[0]);
894 for (j=0; j < (ssize_t) (length-1); j++)
895 filelist[j]=filelist[j+1];
896 number_files=(size_t) length-1;
897 }
898 if (filelist == (char **) NULL)
899 continue;
900 for (j=0; j < (ssize_t) number_files; j++)
901 if (IsPathDirectory(filelist[j]) <= 0)
902 break;
903 if (j == (ssize_t) number_files)
904 {
905 for (j=0; j < (ssize_t) number_files; j++)
906 filelist[j]=DestroyString(filelist[j]);
907 filelist=(char **) RelinquishMagickMemory(filelist);
908 continue;
909 }
910 /*
911 Transfer file list to argument vector.
912 */
913 vector=(char **) ResizeQuantumMemory(vector,(size_t) ((ssize_t)
914 *number_arguments+count+(ssize_t) number_files+1),sizeof(*vector));
915 if (vector == (char **) NULL)
916 {
917 for (j=0; j < (ssize_t) number_files; j++)
918 filelist[j]=DestroyString(filelist[j]);
919 filelist=(char **) RelinquishMagickMemory(filelist);
920 return(MagickFalse);
921 }
922 for (j=0; j < (ssize_t) number_files; j++)
923 {
924 option=filelist[j];
925 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
926 if (parameters > 0)
927 {
928 ssize_t
929 k;
930
931 /*
932 Do not expand command option parameters.
933 */
934 vector[count++]=ConstantString(option);
935 for (k=0; k < parameters; k++)
936 {
937 j++;
938 if (j == (ssize_t) number_files)
939 break;
940 option=filelist[j];
941 vector[count++]=ConstantString(option);
942 }
943 continue;
944 }
945 (void) CopyMagickString(filename,path,MagickPathExtent);
946 if (*path != '\0')
947 (void) ConcatenateMagickString(filename,DirectorySeparator,
948 MagickPathExtent);
949 if (filelist[j] != (char *) NULL)
950 (void) ConcatenateMagickString(filename,filelist[j],MagickPathExtent);
951 filelist[j]=DestroyString(filelist[j]);
952 if (strlen(filename) >= (MagickPathExtent-1))
953 ThrowFatalException(OptionFatalError,"FilenameTruncated");
954 if (IsPathDirectory(filename) <= 0)
955 {
956 char
957 file_path[MagickPathExtent];
958
959 *file_path='\0';
960 if (*magick != '\0')
961 {
962 (void) ConcatenateMagickString(file_path,magick,
963 MagickPathExtent);
964 (void) ConcatenateMagickString(file_path,":",MagickPathExtent);
965 }
966 (void) ConcatenateMagickString(file_path,filename,MagickPathExtent);
967 if (*subimage != '\0')
968 {
969 (void) ConcatenateMagickString(file_path,"[",MagickPathExtent);
970 (void) ConcatenateMagickString(file_path,subimage,
971 MagickPathExtent);
972 (void) ConcatenateMagickString(file_path,"]",MagickPathExtent);
973 }
974 if (strlen(file_path) >= (MagickPathExtent-1))
975 ThrowFatalException(OptionFatalError,"FilenameTruncated");
976 if (destroy != MagickFalse)
977 {
978 count--;
979 vector[count]=DestroyString(vector[count]);
980 destroy=MagickFalse;
981 }
982 vector[count++]=ConstantString(file_path);
983 }
984 }
985 filelist=(char **) RelinquishMagickMemory(filelist);
986 }
987 vector[count]=(char *) NULL;
988 if (IsEventLogging() != MagickFalse)
989 {
990 char
991 *command_line;
992
993 command_line=AcquireString(vector[0]);
994 for (i=1; i < count; i++)
995 {
996 (void) ConcatenateString(&command_line," {");
997 (void) ConcatenateString(&command_line,vector[i]);
998 (void) ConcatenateString(&command_line,"}");
999 }
1000 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1001 "Command line: %s",command_line);
1002 command_line=DestroyString(command_line);
1003 }
1004 *number_arguments=(int) count;
1005 *arguments=vector;
1006 return(MagickTrue);
1007}
1008␌
1009/*
1010%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1011% %
1012% %
1013% %
1014% G e t E x e c u t i o n P a t h %
1015% %
1016% %
1017% %
1018%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1019%
1020% GetExecutionPath() returns the pathname of the executable that started
1021% the process. On success MagickTrue is returned, otherwise MagickFalse.
1022%
1023% The format of the GetExecutionPath method is:
1024%
1025% MagickBooleanType GetExecutionPath(char *path,const size_t extent)
1026%
1027% A description of each parameter follows:
1028%
1029% o path: the pathname of the executable that started the process.
1030%
1031% o extent: the maximum extent of the path.
1032%
1033*/
1034MagickPrivate MagickBooleanType GetExecutionPath(char *path,const size_t extent)
1035{
1036 char
1037 *directory;
1038
1039 *path='\0';
1040 directory=getcwd(path,(unsigned long) extent);
1041 (void) directory;
1042#if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
1043 {
1044 char
1045 execution_path[PATH_MAX+1],
1046 link_path[MagickPathExtent];
1047
1048 ssize_t
1049 count;
1050
1051 (void) FormatLocaleString(link_path,MagickPathExtent,"/proc/%.17g/exe",
1052 (double) getpid());
1053 count=readlink(link_path,execution_path,PATH_MAX);
1054 if (count == -1)
1055 {
1056 (void) FormatLocaleString(link_path,MagickPathExtent,"/proc/%.17g/file",
1057 (double) getpid());
1058 count=readlink(link_path,execution_path,PATH_MAX);
1059 }
1060 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
1061 {
1062 execution_path[count]='\0';
1063 (void) CopyMagickString(path,execution_path,extent);
1064 }
1065 }
1066#endif
1067#if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
1068 {
1069 char
1070 executable_path[PATH_MAX << 1];
1071
1072 uint32_t
1073 length;
1074
1075 length=sizeof(executable_path);
1076 if (_NSGetExecutablePath(executable_path,&length) == 0)
1077 {
1078 char
1079 *real_path = realpath_utf8(executable_path);
1080
1081 if (real_path != (char *) NULL)
1082 {
1083 (void) CopyMagickString(path,real_path,extent);
1084 real_path=DestroyString(real_path);
1085 }
1086 }
1087 }
1088#endif
1089#if defined(MAGICKCORE_HAVE_GETEXECNAME)
1090 {
1091 const char
1092 *execution_path;
1093
1094 execution_path=(const char *) getexecname();
1095 if (execution_path != (const char *) NULL)
1096 {
1097 if (*execution_path != *DirectorySeparator)
1098 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1099 (void) ConcatenateMagickString(path,execution_path,extent);
1100 }
1101 }
1102#endif
1103#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1104 NTGetExecutionPath(path,extent);
1105#endif
1106#if defined(__GNU__)
1107 {
1108 char
1109 *program_name;
1110
1111 ssize_t
1112 count;
1113
1114 count=0;
1115 program_name=program_invocation_name;
1116 if (*program_invocation_name != '/')
1117 {
1118 size_t
1119 extent;
1120
1121 extent=strlen(directory)+strlen(program_name)+2;
1122 program_name=AcquireQuantumMemory(extent,sizeof(*program_name));
1123 if (program_name == (char *) NULL)
1124 program_name=program_invocation_name;
1125 else
1126 count=FormatLocaleString(program_name,extent,"%s/%s",directory,
1127 program_invocation_name);
1128 }
1129 if (count != -1)
1130 {
1131 char
1132 *real_path = realpath_utf8(program_name);
1133
1134 if (real_path != (char *) NULL)
1135 {
1136 (void) CopyMagickString(path,real_path,extent);
1137 real_path=DestroyString(real_path);
1138 }
1139 }
1140 if (program_name != program_invocation_name)
1141 program_name=(char *) RelinquishMagickMemory(program_name);
1142 }
1143#endif
1144#if defined(__OpenBSD__)
1145 {
1146 extern char
1147 *__progname;
1148
1149 (void) CopyMagickString(path,__progname,extent);
1150 }
1151#endif
1152 return(IsPathAccessible(path));
1153}
1154␌
1155/*
1156%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1157% %
1158% %
1159% %
1160% G e t M a g i c k P a g e S i z e %
1161% %
1162% %
1163% %
1164%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1165%
1166% GetMagickPageSize() returns the memory page size.
1167%
1168% The format of the GetMagickPageSize method is:
1169%
1170% ssize_t GetMagickPageSize()
1171%
1172*/
1173MagickPrivate ssize_t GetMagickPageSize(void)
1174{
1175 static ssize_t
1176 page_size = -1;
1177
1178 if (page_size > 0)
1179 return(page_size);
1180#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1181 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1182#elif defined(MAGICKCORE_HAVE_GETPAGESIZE)
1183 page_size=(ssize_t) getpagesize();
1184#endif
1185 if (page_size <= 0)
1186 page_size=4096;
1187 return(page_size);
1188}
1189␌
1190/*
1191%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1192% %
1193% %
1194% %
1195% G e t P a t h A t t r i b u t e s %
1196% %
1197% %
1198% %
1199%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1200%
1201% GetPathAttributes() returns attributes (e.g. size of file) about a path.
1202%
1203% The path of the GetPathAttributes method is:
1204%
1205% MagickBooleanType GetPathAttributes(const char *path,void *attributes)
1206%
1207% A description of each parameter follows.
1208%
1209% o path: the file path.
1210%
1211% o attributes: the path attributes are returned here.
1212%
1213*/
1214MagickExport MagickBooleanType GetPathAttributes(const char *path,
1215 void *attributes)
1216{
1217 MagickBooleanType
1218 status;
1219
1220 if (path == (const char *) NULL)
1221 {
1222 errno=EINVAL;
1223 return(MagickFalse);
1224 }
1225 (void) memset(attributes,0,sizeof(struct stat));
1226 status=stat_utf8(path,(struct stat *) attributes) == 0 ? MagickTrue :
1227 MagickFalse;
1228 return(status);
1229}
1230␌
1231/*
1232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1233% %
1234% %
1235% %
1236% G e t P a t h C o m p o n e n t %
1237% %
1238% %
1239% %
1240%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1241%
1242% GetPathComponent() returns the parent directory name, filename, basename, or
1243% extension of a file path.
1244%
1245% The component string pointed to must have at least MagickPathExtent space
1246% for the results to be stored.
1247%
1248% The format of the GetPathComponent function is:
1249%
1250% GetPathComponent(const char *path,PathType type,char *component)
1251%
1252% A description of each parameter follows:
1253%
1254% o path: Specifies a pointer to a character array that contains the
1255% file path.
1256%
1257% o type: Specifies which file path component to return.
1258%
1259% o component: the selected file path component is returned here.
1260%
1261*/
1262MagickExport void GetPathComponent(const char *path,PathType type,
1263 char *component)
1264{
1265 char
1266 *q;
1267
1268 char
1269 *p;
1270
1271 size_t
1272 magick_length,
1273 subimage_offset,
1274 subimage_length;
1275
1276 assert(path != (const char *) NULL);
1277 assert(component != (char *) NULL);
1278 if (IsEventLogging() != MagickFalse)
1279 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",path);
1280 if (*path == '\0')
1281 {
1282 *component='\0';
1283 return;
1284 }
1285 (void) CopyMagickString(component,path,MagickPathExtent);
1286 subimage_length=0;
1287 subimage_offset=0;
1288 if (type != SubcanonicalPath)
1289 {
1290 p=component+strlen(component)-1;
1291 if ((strlen(component) > 2) && (*p == ']'))
1292 {
1293 q=strrchr(component,'[');
1294 if (q != (char *) NULL)
1295 {
1296 ExceptionInfo *exception = AcquireExceptionInfo();
1297 char *literal = (char *) GetImageRegistry(StringRegistryType,
1298 "filename:literal",exception);
1299 exception=DestroyExceptionInfo(exception);
1300 if ((IsStringTrue(literal) == MagickFalse) ||
1301 (IsPathAccessible(path) == MagickFalse))
1302 {
1303 /*
1304 Look for scene specification (e.g. img0001.pcd[4]).
1305 */
1306 *p='\0';
1307 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1308 (IsGeometry(q+1) == MagickFalse))
1309 *p=']';
1310 else
1311 {
1312 subimage_length=(size_t) (p-q);
1313 subimage_offset=(size_t) (q-component+1);
1314 *q='\0';
1315 }
1316 }
1317 if (literal != (char *) NULL)
1318 literal=DestroyString(literal);
1319 }
1320 }
1321 }
1322 magick_length=0;
1323#if defined(__OS2__)
1324 if (path[1] != ":")
1325#endif
1326 for (p=component; *p != '\0'; p++)
1327 {
1328 if ((*p == '%') && (*(p+1) == '['))
1329 {
1330 /*
1331 Skip over %[...].
1332 */
1333 for (p++; (*p != ']') && (*p != '\0'); p++) ;
1334 if (*p == '\0')
1335 break;
1336 }
1337 if ((p != component) && (*p == ':') && (IsPathDirectory(component) < 0) &&
1338 (IsPathAccessible(component) == MagickFalse))
1339 {
1340 /*
1341 Look for image format specification (e.g. ps3:image).
1342 */
1343 *p='\0';
1344 if (IsMagickConflict(component) != MagickFalse)
1345 *p=':';
1346 else
1347 {
1348 magick_length=(size_t) (p-component+1);
1349 for (q=component; *(++p) != '\0'; q++)
1350 *q=(*p);
1351 *q='\0';
1352 }
1353 break;
1354 }
1355 }
1356 p=component;
1357 if (*p != '\0')
1358 for (p=component+strlen(component)-1; p > component; p--)
1359 if (IsBasenameSeparator(*p) != MagickFalse)
1360 break;
1361 switch (type)
1362 {
1363 case MagickPath:
1364 {
1365 if (magick_length != 0)
1366 (void) CopyMagickString(component,path,magick_length);
1367 else
1368 *component='\0';
1369 break;
1370 }
1371 case RootPath:
1372 {
1373 if (*component != '\0')
1374 {
1375 for (p=component+(strlen(component)-1); p > component; p--)
1376 {
1377 if (IsBasenameSeparator(*p) != MagickFalse)
1378 break;
1379 if (*p == '.')
1380 break;
1381 }
1382 if (*p == '.')
1383 *p='\0';
1384 break;
1385 }
1386 magick_fallthrough;
1387 }
1388 case HeadPath:
1389 {
1390 *p='\0';
1391 break;
1392 }
1393 case TailPath:
1394 {
1395 if (IsBasenameSeparator(*p) != MagickFalse)
1396 (void) CopyMagickString(component,p+1,MagickPathExtent);
1397 break;
1398 }
1399 case BasePath:
1400 {
1401 if (IsBasenameSeparator(*p) != MagickFalse)
1402 (void) CopyMagickString(component,p+1,MagickPathExtent);
1403 if (*component != '\0')
1404 for (p=component+(strlen(component)-1); p > component; p--)
1405 if (*p == '.')
1406 {
1407 *p='\0';
1408 break;
1409 }
1410 break;
1411 }
1412 case BasePathSansCompressExtension:
1413 {
1414 char
1415 extension[MagickPathExtent];
1416
1417 /*
1418 Base path sans any compression extension.
1419 */
1420 GetPathComponent(path,ExtensionPath,extension);
1421 if ((LocaleCompare(extension,"bz2") == 0) ||
1422 (LocaleCompare(extension,"gz") == 0) ||
1423 (LocaleCompare(extension,"svgz") == 0) ||
1424 (LocaleCompare(extension,"wmz") == 0) ||
1425 (LocaleCompare(extension,"Z") == 0))
1426 {
1427 if (IsBasenameSeparator(*p) != MagickFalse)
1428 p++;
1429 q=strrchr(p,'.');
1430 if ((q != (char *) NULL) && (q > p))
1431 *q='\0';
1432 }
1433 break;
1434 }
1435 case ExtensionPath:
1436 {
1437 if (IsBasenameSeparator(*p) != MagickFalse)
1438 (void) CopyMagickString(component,p+1,MagickPathExtent);
1439 if (*component != '\0')
1440 for (p=component+strlen(component)-1; p > component; p--)
1441 if (*p == '.')
1442 break;
1443 *component='\0';
1444 if (*p == '.')
1445 (void) CopyMagickString(component,p+1,MagickPathExtent);
1446 break;
1447 }
1448 case SubimagePath:
1449 {
1450 *component='\0';
1451 if ((subimage_length != 0) && (magick_length < subimage_offset))
1452 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1453 break;
1454 }
1455 case SubcanonicalPath:
1456 case CanonicalPath:
1457 case UndefinedPath:
1458 break;
1459 }
1460}
1461␌
1462/*
1463%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1464% %
1465% %
1466% %
1467% G e t P a t h C o m p o n e n t s %
1468% %
1469% %
1470% %
1471%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1472%
1473% GetPathComponents() returns a list of path components.
1474%
1475% The format of the GetPathComponents method is:
1476%
1477% char **GetPathComponents(const char *path,
1478% size_t *number_components)
1479%
1480% A description of each parameter follows:
1481%
1482% o path: Specifies the string to segment into a list.
1483%
1484% o number_components: return the number of components in the list
1485%
1486*/
1487MagickPrivate char **GetPathComponents(const char *path,
1488 size_t *number_components)
1489{
1490 char
1491 **components;
1492
1493 const char
1494 *p,
1495 *q;
1496
1497 ssize_t
1498 i;
1499
1500 if (path == (char *) NULL)
1501 return((char **) NULL);
1502 *number_components=1;
1503 for (p=path; *p != '\0'; p++)
1504 if (IsBasenameSeparator(*p))
1505 (*number_components)++;
1506 components=(char **) AcquireQuantumMemory((size_t) *number_components+1UL,
1507 sizeof(*components));
1508 if (components == (char **) NULL)
1509 return((char **) NULL);
1510 p=path;
1511 for (i=0; i < (ssize_t) *number_components; i++)
1512 {
1513 for (q=p; *q != '\0'; q++)
1514 if (IsBasenameSeparator(*q))
1515 break;
1516 components[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MagickPathExtent,
1517 sizeof(**components));
1518 if (components[i] == (char*)NULL)
1519 {
1520 ssize_t
1521 j;
1522
1523 for (j=0; j < i; j++)
1524 components[j]=DestroyString(components[j]);
1525 components=(char **) RelinquishMagickMemory(components);
1526 return((char **) NULL);
1527 }
1528 (void) CopyMagickString(components[i],p,(size_t) (q-p+1));
1529 p=q+1;
1530 }
1531 components[i]=(char *) NULL;
1532 return(components);
1533}
1534␌
1535/*
1536%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1537% %
1538% %
1539% %
1540% G e t P a t h T e m p l a t e %
1541% %
1542% %
1543% %
1544%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1545%
1546% GetPathTemplate() constructs a template for a temporary filename.
1547%
1548% The format of the GetPathComponent function is:
1549%
1550% MagickBooleanType GetPathComponent(const char *path)
1551%
1552% A description of each parameter follows:
1553%
1554% o path: Specifies a pointer to a character array populated with the
1555% file path template.
1556%
1557%
1558*/
1559MagickExport MagickBooleanType GetPathTemplate(char *path)
1560{
1561 char
1562 *directory,
1563 *value;
1564
1565 ExceptionInfo
1566 *exception;
1567
1568 MagickBooleanType
1569 status;
1570
1571 struct stat
1572 attributes;
1573
1574 (void) FormatLocaleString(path,MagickPathExtent,"magick-"
1575 MagickPathTemplate);
1576 exception=AcquireExceptionInfo();
1577 directory=(char *) GetImageRegistry(StringRegistryType,"temporary-path",
1578 exception);
1579 exception=DestroyExceptionInfo(exception);
1580 if (directory == (char *) NULL)
1581 directory=GetEnvironmentValue("MAGICK_TEMPORARY_PATH");
1582 if (directory == (char *) NULL)
1583 directory=GetEnvironmentValue("MAGICK_TMPDIR");
1584 if (directory == (char *) NULL)
1585 directory=GetEnvironmentValue("TMPDIR");
1586#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__OS2__) || defined(__CYGWIN__)
1587 if (directory == (char *) NULL)
1588 directory=GetEnvironmentValue("TMP");
1589 if (directory == (char *) NULL)
1590 directory=GetEnvironmentValue("TEMP");
1591#endif
1592#if defined(__VMS)
1593 if (directory == (char *) NULL)
1594 directory=GetEnvironmentValue("MTMPDIR");
1595#endif
1596#if defined(P_tmpdir)
1597 if (directory == (char *) NULL)
1598 directory=ConstantString(P_tmpdir);
1599#endif
1600 if (directory == (char *) NULL)
1601 return(MagickFalse);
1602 value=GetPolicyValue("resource:temporary-path");
1603 if (value != (char *) NULL)
1604 {
1605 (void) CloneString(&directory,value);
1606 value=DestroyString(value);
1607 }
1608 if (strlen(directory) > (MagickPathExtent-25))
1609 {
1610 directory=DestroyString(directory);
1611 return(MagickFalse);
1612 }
1613 status=GetPathAttributes(directory,&attributes);
1614 if ((status == MagickFalse) || !S_ISDIR(attributes.st_mode))
1615 {
1616 directory=DestroyString(directory);
1617 return(MagickFalse);
1618 }
1619 if (directory[strlen(directory)-1] == *DirectorySeparator)
1620 (void) FormatLocaleString(path,MagickPathExtent,"%smagick-"
1621 MagickPathTemplate,directory);
1622 else
1623 (void) FormatLocaleString(path,MagickPathExtent,
1624 "%s%smagick-" MagickPathTemplate,directory,DirectorySeparator);
1625 directory=DestroyString(directory);
1626#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1627 {
1628 char
1629 *p;
1630
1631 /*
1632 Ghostscript does not like backslashes so we need to replace them. The
1633 forward slash also works under Windows.
1634 */
1635 for (p=(path[1] == *DirectorySeparator ? path+2 : path); *p != '\0'; p++)
1636 if (*p == *DirectorySeparator)
1637 *p='/';
1638 }
1639#endif
1640 return(MagickTrue);
1641}
1642␌
1643/*
1644%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1645% %
1646% %
1647% %
1648% I s P a t h A c c e s s i b l e %
1649% %
1650% %
1651% %
1652%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1653%
1654% IsPathAccessible() returns MagickTrue if the file as defined by the path is
1655% accessible.
1656%
1657% The format of the IsPathAccessible method is:
1658%
1659% MagickBooleanType IsPathAccessible(const char *path)
1660%
1661% A description of each parameter follows.
1662%
1663% o path: Specifies a path to a file.
1664%
1665*/
1666MagickExport MagickBooleanType IsPathAccessible(const char *path)
1667{
1668 MagickBooleanType
1669 status;
1670
1671 struct stat
1672 attributes;
1673
1674 if ((path == (const char *) NULL) || (*path == '\0'))
1675 return(MagickFalse);
1676 if (LocaleCompare(path,"-") == 0)
1677 return(MagickTrue);
1678 status=GetPathAttributes(path,&attributes);
1679 if (status == MagickFalse)
1680 return(status);
1681 if (S_ISREG(attributes.st_mode) == 0)
1682 return(MagickFalse);
1683 if (access_utf8(path,F_OK) != 0)
1684 return(MagickFalse);
1685 return(MagickTrue);
1686}
1687␌
1688/*
1689%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1690% %
1691% %
1692% %
1693+ I s P a t h D i r e c t o r y %
1694% %
1695% %
1696% %
1697%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1698%
1699% IsPathDirectory() returns -1 if the directory does not exist, 1 is returned
1700% if the path represents a directory otherwise 0.
1701%
1702% The format of the IsPathDirectory method is:
1703%
1704% int IsPathDirectory(const char *path)
1705%
1706% A description of each parameter follows.
1707%
1708% o path: The directory path.
1709%
1710*/
1711static int IsPathDirectory(const char *path)
1712{
1713 MagickBooleanType
1714 status;
1715
1716 struct stat
1717 attributes;
1718
1719 if ((path == (const char *) NULL) || (*path == '\0'))
1720 return(MagickFalse);
1721 status=GetPathAttributes(path,&attributes);
1722 if (status == MagickFalse)
1723 return(-1);
1724 if (S_ISDIR(attributes.st_mode) == 0)
1725 return(0);
1726 return(1);
1727}
1728␌
1729/*
1730%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1731% %
1732% %
1733% %
1734% L i s t F i l e s %
1735% %
1736% %
1737% %
1738%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1739%
1740% ListFiles() reads the directory specified and returns a list of filenames
1741% contained in the directory sorted in ascending alphabetic order.
1742%
1743% The format of the ListFiles function is:
1744%
1745% char **ListFiles(const char *directory,const char *pattern,
1746% ssize_t *number_entries)
1747%
1748% A description of each parameter follows:
1749%
1750% o filelist: Method ListFiles returns a list of filenames contained
1751% in the directory. If the directory specified cannot be read or it is
1752% a file a NULL list is returned.
1753%
1754% o directory: Specifies a pointer to a text string containing a directory
1755% name.
1756%
1757% o pattern: Specifies a pointer to a text string containing a pattern.
1758%
1759% o number_entries: This integer returns the number of filenames in the
1760% list.
1761%
1762*/
1763
1764#if defined(__cplusplus) || defined(c_plusplus)
1765extern "C" {
1766#endif
1767
1768static int FileCompare(const void *x,const void *y)
1769{
1770 const char
1771 **p,
1772 **q;
1773
1774 p=(const char **) x;
1775 q=(const char **) y;
1776 return(LocaleCompare(*p,*q));
1777}
1778
1779#if defined(__cplusplus) || defined(c_plusplus)
1780}
1781#endif
1782
1783MagickPrivate char **ListFiles(const char *directory,const char *pattern,
1784 size_t *number_entries)
1785{
1786 char
1787 **filelist;
1788
1789 DIR
1790 *current_directory;
1791
1792 struct dirent
1793 *buffer,
1794 *entry;
1795
1796 size_t
1797 max_entries;
1798
1799 /*
1800 Open directory.
1801 */
1802 assert(directory != (const char *) NULL);
1803 assert(pattern != (const char *) NULL);
1804 assert(number_entries != (size_t *) NULL);
1805 if (IsEventLogging() != MagickFalse)
1806 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",directory);
1807 *number_entries=0;
1808 current_directory=opendir(directory);
1809 if (current_directory == (DIR *) NULL)
1810 return((char **) NULL);
1811 /*
1812 Allocate filelist.
1813 */
1814 max_entries=2048;
1815 filelist=(char **) AcquireQuantumMemory((size_t) max_entries,
1816 sizeof(*filelist));
1817 if (filelist == (char **) NULL)
1818 {
1819 (void) closedir(current_directory);
1820 return((char **) NULL);
1821 }
1822 /*
1823 Save the current and change to the new directory.
1824 */
1825 buffer=(struct dirent *) AcquireMagickMemory(sizeof(*buffer)+FILENAME_MAX+1);
1826 if (buffer == (struct dirent *) NULL)
1827 {
1828 filelist=(char **) RelinquishMagickMemory(filelist);
1829 (void) closedir(current_directory);
1830 return((char **) NULL);
1831 }
1832 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1833 (entry != (struct dirent *) NULL))
1834 {
1835 if ((LocaleCompare(entry->d_name,".") == 0) ||
1836 (LocaleCompare(entry->d_name,"..") == 0))
1837 continue;
1838 if ((IsPathDirectory(entry->d_name) > 0) ||
1839#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1840 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1841#else
1842 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1843#endif
1844 {
1845 if (*number_entries >= max_entries)
1846 {
1847 /*
1848 Extend the file list.
1849 */
1850 max_entries<<=1;
1851 filelist=(char **) ResizeQuantumMemory(filelist,(size_t)
1852 max_entries,sizeof(*filelist));
1853 if (filelist == (char **) NULL)
1854 break;
1855 }
1856#if defined(vms)
1857 {
1858 char
1859 *p;
1860
1861 p=strchr(entry->d_name,';');
1862 if (p)
1863 *p='\0';
1864 if (*number_entries > 0)
1865 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1866 continue;
1867 }
1868#endif
1869 filelist[*number_entries]=(char *) AcquireString(entry->d_name);
1870 (*number_entries)++;
1871 }
1872 }
1873 buffer=(struct dirent *) RelinquishMagickMemory(buffer);
1874 (void) closedir(current_directory);
1875 if (filelist == (char **) NULL)
1876 return((char **) NULL);
1877 /*
1878 Sort filelist in ascending order.
1879 */
1880 qsort((void *) filelist,(size_t) *number_entries,sizeof(*filelist),
1881 FileCompare);
1882 return(filelist);
1883}
1884␌
1885/*
1886%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1887% %
1888% %
1889% %
1890% M a g i c k D e l a y %
1891% %
1892% %
1893% %
1894%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1895%
1896% MagickDelay() suspends program execution for the number of milliseconds
1897% specified.
1898%
1899% The format of the Delay method is:
1900%
1901% void MagickDelay(const MagickSizeType milliseconds)
1902%
1903% A description of each parameter follows:
1904%
1905% o milliseconds: Specifies the number of milliseconds to delay before
1906% returning.
1907%
1908*/
1909MagickExport void MagickDelay(const MagickSizeType milliseconds)
1910{
1911 if (milliseconds == 0)
1912 return;
1913#if defined(MAGICKCORE_HAVE_NANOSLEEP)
1914 {
1915 struct timespec
1916 timer;
1917
1918 timer.tv_sec=(time_t) (milliseconds/1000);
1919 timer.tv_nsec=(time_t) ((milliseconds % 1000)*1000*1000);
1920 (void) nanosleep(&timer,(struct timespec *) NULL);
1921 }
1922#elif defined(MAGICKCORE_HAVE_USLEEP)
1923 usleep(1000*milliseconds);
1924#elif defined(MAGICKCORE_HAVE_SELECT)
1925 {
1926 struct timeval
1927 timer;
1928
1929 timer.tv_sec=(long) milliseconds/1000;
1930 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1931 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1932 }
1933#elif defined(MAGICKCORE_HAVE_POLL)
1934 (void) poll((struct pollfd *) NULL,0,(int) milliseconds);
1935#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1936 Sleep((long) milliseconds);
1937#elif defined(vms)
1938 {
1939 float
1940 timer;
1941
1942 timer=milliseconds/1000.0;
1943 lib$wait(&timer);
1944 }
1945#elif defined(__BEOS__)
1946 snooze(1000*milliseconds);
1947#else
1948 {
1949 clock_t
1950 time_end;
1951
1952 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1953 while (clock() < time_end)
1954 {
1955 }
1956 }
1957#endif
1958}
1959␌
1960/*
1961%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1962% %
1963% %
1964% %
1965% M u l t i l i n e C e n s u s %
1966% %
1967% %
1968% %
1969%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1970%
1971% MultilineCensus() returns the number of lines within a label. A line is
1972% represented by a \n character.
1973%
1974% The format of the MultilineCensus method is:
1975%
1976% size_t MultilineCensus(const char *label)
1977%
1978% A description of each parameter follows.
1979%
1980% o label: This character string is the label.
1981%
1982*/
1983MagickExport size_t MultilineCensus(const char *label)
1984{
1985 size_t
1986 number_lines;
1987
1988 /*
1989 Determine the number of lines within this label.
1990 */
1991 if (label == (char *) NULL)
1992 return(0);
1993 for (number_lines=1; *label != '\0'; label++)
1994 if (*label == '\n')
1995 number_lines++;
1996 return(number_lines);
1997}
1998␌
1999/*
2000%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2001% %
2002% %
2003% %
2004% S h r e d F i l e %
2005% %
2006% %
2007% %
2008%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2009%
2010% ShredFile() overwrites the specified file with random data. The overwrite is
2011% optional and is only required to help keep the contents of the file private.
2012%
2013% The format of the ShredFile method is:
2014%
2015% MagickBooleanType ShredFile(const char *path)
2016%
2017% A description of each parameter follows.
2018%
2019% o path: Specifies a path to a file.
2020%
2021*/
2022MagickPrivate MagickBooleanType ShredFile(const char *path)
2023{
2024 int
2025 file,
2026 status;
2027
2028 MagickSizeType
2029 length;
2030
2031 RandomInfo
2032 *random_info;
2033
2034 size_t
2035 quantum;
2036
2037 ssize_t
2038 i;
2039
2040 static ssize_t
2041 passes = -1;
2042
2043 StringInfo
2044 *key;
2045
2046 struct stat
2047 file_stats;
2048
2049 if ((path == (const char *) NULL) || (*path == '\0'))
2050 return(MagickFalse);
2051 if (passes == -1)
2052 passes=GetShredPasses();
2053 if (passes == 0)
2054 return(MagickTrue);
2055 /*
2056 Shred the file.
2057 */
2058 file=open_utf8(path,O_WRONLY | O_CLOEXEC | O_EXCL | O_BINARY,S_MODE);
2059 if (file == -1)
2060 return(MagickFalse);
2061 quantum=(size_t) MagickMinBufferExtent;
2062 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
2063 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
2064 length=(MagickSizeType) file_stats.st_size;
2065 random_info=AcquireRandomInfo();
2066 key=GetRandomKey(random_info,quantum);
2067 for (i=0; i < passes; i++)
2068 {
2069 MagickOffsetType
2070 j;
2071
2072 ssize_t
2073 count;
2074
2075 if (lseek(file,0,SEEK_SET) < 0)
2076 break;
2077 for (j=0; j < (MagickOffsetType) length; j+=count)
2078 {
2079 if (i != 0)
2080 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
2081 count=MagickWrite(file,GetStringInfoDatum(key),(size_t)
2082 MagickMin((MagickOffsetType) quantum,(MagickOffsetType) length-j));
2083 if (count <= 0)
2084 break;
2085 }
2086 if (j < (MagickOffsetType) length)
2087 break;
2088 }
2089 key=DestroyStringInfo(key);
2090 random_info=DestroyRandomInfo(random_info);
2091 status=close_utf8(file);
2092 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);
2093}