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