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