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