MagickCore 7.1.1-43
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/script/license.php %
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,ReadPolicyRights |
187 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]=(q-Base64) << 2;
398 state++;
399 break;
400 }
401 case 1:
402 {
403 decode[i++]|=(q-Base64) >> 4;
404 decode[i]=((q-Base64) & 0x0f) << 4;
405 state++;
406 break;
407 }
408 case 2:
409 {
410 decode[i++]|=(q-Base64) >> 2;
411 decode[i]=((q-Base64) & 0x03) << 6;
412 state++;
413 break;
414 }
415 case 3:
416 {
417 decode[i++]|=(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*/
756MagickExport MagickBooleanType ExpandFilenames(int *number_arguments,
757 char ***arguments)
758{
759 char
760 home_directory[MagickPathExtent],
761 **vector;
762
763 ssize_t
764 i,
765 j;
766
767 size_t
768 number_files;
769
770 ssize_t
771 count,
772 parameters;
773
774 /*
775 Allocate argument vector.
776 */
777 assert(number_arguments != (int *) NULL);
778 assert(arguments != (char ***) NULL);
779 if (IsEventLogging() != MagickFalse)
780 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
781 vector=(char **) AcquireQuantumMemory((size_t) (*number_arguments+1),
782 sizeof(*vector));
783 if (vector == (char **) NULL)
784 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
785 /*
786 Expand any wildcard filenames.
787 */
788 *home_directory='\0';
789 count=0;
790 for (i=0; i < (ssize_t) *number_arguments; i++)
791 {
792 char
793 **filelist,
794 filename[MagickPathExtent],
795 magick[MagickPathExtent],
796 *option,
797 path[MagickPathExtent],
798 subimage[MagickPathExtent];
799
800 MagickBooleanType
801 destroy;
802
803 option=(*arguments)[i];
804 *magick='\0';
805 *path='\0';
806 *filename='\0';
807 *subimage='\0';
808 number_files=0;
809 vector[count++]=ConstantString(option);
810 destroy=MagickTrue;
811 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
812 if (parameters > 0)
813 {
814 /*
815 Do not expand command option parameters.
816 */
817 for (j=0; j < parameters; j++)
818 {
819 i++;
820 if (i == (ssize_t) *number_arguments)
821 break;
822 option=(*arguments)[i];
823 vector[count++]=ConstantString(option);
824 }
825 continue;
826 }
827 if ((*option == '"') || (*option == '\''))
828 continue;
829 GetPathComponent(option,TailPath,filename);
830 GetPathComponent(option,MagickPath,magick);
831 if ((LocaleCompare(magick,"CAPTION") == 0) ||
832 (LocaleCompare(magick,"LABEL") == 0) ||
833 (LocaleCompare(magick,"PANGO") == 0) ||
834 (LocaleCompare(magick,"VID") == 0))
835 continue;
836 if ((IsGlob(filename) == MagickFalse) && (*option != '@'))
837 continue;
838 if (IsPathAccessible(option) != MagickFalse)
839 continue;
840 if (*option != '@')
841 {
842 /*
843 Generate file list from wildcard filename (e.g. *.jpg).
844 */
845 GetPathComponent(option,HeadPath,path);
846 GetPathComponent(option,SubimagePath,subimage);
847 ExpandFilename(path);
848 if (*home_directory == '\0')
849 getcwd_utf8(home_directory,MagickPathExtent-1);
850 filelist=ListFiles(*path == '\0' ? home_directory : path,filename,
851 &number_files);
852 }
853 else
854 {
855 char
856 *files;
857
859 *exception;
860
861 int
862 length;
863
864 /*
865 Generate file list from file list (e.g. @filelist.txt).
866 */
867 exception=AcquireExceptionInfo();
868 files=FileToString(option,~0UL,exception);
869 exception=DestroyExceptionInfo(exception);
870 if (files == (char *) NULL)
871 continue;
872 filelist=StringToArgv(files,&length);
873 if (filelist == (char **) NULL)
874 continue;
875 files=DestroyString(files);
876 filelist[0]=DestroyString(filelist[0]);
877 for (j=0; j < (ssize_t) (length-1); j++)
878 filelist[j]=filelist[j+1];
879 number_files=(size_t) length-1;
880 }
881 if (filelist == (char **) NULL)
882 continue;
883 for (j=0; j < (ssize_t) number_files; j++)
884 if (IsPathDirectory(filelist[j]) <= 0)
885 break;
886 if (j == (ssize_t) number_files)
887 {
888 for (j=0; j < (ssize_t) number_files; j++)
889 filelist[j]=DestroyString(filelist[j]);
890 filelist=(char **) RelinquishMagickMemory(filelist);
891 continue;
892 }
893 /*
894 Transfer file list to argument vector.
895 */
896 vector=(char **) ResizeQuantumMemory(vector,(size_t) ((ssize_t)
897 *number_arguments+count+(ssize_t) number_files+1),sizeof(*vector));
898 if (vector == (char **) NULL)
899 {
900 for (j=0; j < (ssize_t) number_files; j++)
901 filelist[j]=DestroyString(filelist[j]);
902 filelist=(char **) RelinquishMagickMemory(filelist);
903 return(MagickFalse);
904 }
905 for (j=0; j < (ssize_t) number_files; j++)
906 {
907 option=filelist[j];
908 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
909 if (parameters > 0)
910 {
911 ssize_t
912 k;
913
914 /*
915 Do not expand command option parameters.
916 */
917 vector[count++]=ConstantString(option);
918 for (k=0; k < parameters; k++)
919 {
920 j++;
921 if (j == (ssize_t) number_files)
922 break;
923 option=filelist[j];
924 vector[count++]=ConstantString(option);
925 }
926 continue;
927 }
928 (void) CopyMagickString(filename,path,MagickPathExtent);
929 if (*path != '\0')
930 (void) ConcatenateMagickString(filename,DirectorySeparator,
931 MagickPathExtent);
932 if (filelist[j] != (char *) NULL)
933 (void) ConcatenateMagickString(filename,filelist[j],MagickPathExtent);
934 filelist[j]=DestroyString(filelist[j]);
935 if (strlen(filename) >= (MagickPathExtent-1))
936 ThrowFatalException(OptionFatalError,"FilenameTruncated");
937 if (IsPathDirectory(filename) <= 0)
938 {
939 char
940 file_path[MagickPathExtent];
941
942 *file_path='\0';
943 if (*magick != '\0')
944 {
945 (void) ConcatenateMagickString(file_path,magick,
946 MagickPathExtent);
947 (void) ConcatenateMagickString(file_path,":",MagickPathExtent);
948 }
949 (void) ConcatenateMagickString(file_path,filename,MagickPathExtent);
950 if (*subimage != '\0')
951 {
952 (void) ConcatenateMagickString(file_path,"[",MagickPathExtent);
953 (void) ConcatenateMagickString(file_path,subimage,
954 MagickPathExtent);
955 (void) ConcatenateMagickString(file_path,"]",MagickPathExtent);
956 }
957 if (strlen(file_path) >= (MagickPathExtent-1))
958 ThrowFatalException(OptionFatalError,"FilenameTruncated");
959 if (destroy != MagickFalse)
960 {
961 count--;
962 vector[count]=DestroyString(vector[count]);
963 destroy=MagickFalse;
964 }
965 vector[count++]=ConstantString(file_path);
966 }
967 }
968 filelist=(char **) RelinquishMagickMemory(filelist);
969 }
970 vector[count]=(char *) NULL;
971 if (IsEventLogging() != MagickFalse)
972 {
973 char
974 *command_line;
975
976 command_line=AcquireString(vector[0]);
977 for (i=1; i < count; i++)
978 {
979 (void) ConcatenateString(&command_line," {");
980 (void) ConcatenateString(&command_line,vector[i]);
981 (void) ConcatenateString(&command_line,"}");
982 }
983 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
984 "Command line: %s",command_line);
985 command_line=DestroyString(command_line);
986 }
987 *number_arguments=(int) count;
988 *arguments=vector;
989 return(MagickTrue);
990}
991
992/*
993%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
994% %
995% %
996% %
997% G e t E x e c u t i o n P a t h %
998% %
999% %
1000% %
1001%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1002%
1003% GetExecutionPath() returns the pathname of the executable that started
1004% the process. On success MagickTrue is returned, otherwise MagickFalse.
1005%
1006% The format of the GetExecutionPath method is:
1007%
1008% MagickBooleanType GetExecutionPath(char *path,const size_t extent)
1009%
1010% A description of each parameter follows:
1011%
1012% o path: the pathname of the executable that started the process.
1013%
1014% o extent: the maximum extent of the path.
1015%
1016*/
1017MagickPrivate MagickBooleanType GetExecutionPath(char *path,const size_t extent)
1018{
1019 char
1020 *directory;
1021
1022 *path='\0';
1023 directory=getcwd(path,(unsigned long) extent);
1024 (void) directory;
1025#if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
1026 {
1027 char
1028 execution_path[PATH_MAX+1],
1029 link_path[MagickPathExtent];
1030
1031 ssize_t
1032 count;
1033
1034 (void) FormatLocaleString(link_path,MagickPathExtent,"/proc/%.20g/exe",
1035 (double) getpid());
1036 count=readlink(link_path,execution_path,PATH_MAX);
1037 if (count == -1)
1038 {
1039 (void) FormatLocaleString(link_path,MagickPathExtent,"/proc/%.20g/file",
1040 (double) getpid());
1041 count=readlink(link_path,execution_path,PATH_MAX);
1042 }
1043 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
1044 {
1045 execution_path[count]='\0';
1046 (void) CopyMagickString(path,execution_path,extent);
1047 }
1048 }
1049#endif
1050#if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
1051 {
1052 char
1053 executable_path[PATH_MAX << 1];
1054
1055 uint32_t
1056 length;
1057
1058 length=sizeof(executable_path);
1059 if (_NSGetExecutablePath(executable_path,&length) == 0)
1060 {
1061 char
1062 *real_path = realpath_utf8(executable_path);
1063
1064 if (real_path != (char *) NULL)
1065 {
1066 (void) CopyMagickString(path,real_path,extent);
1067 real_path=DestroyString(real_path);
1068 }
1069 }
1070 }
1071#endif
1072#if defined(MAGICKCORE_HAVE_GETEXECNAME)
1073 {
1074 const char
1075 *execution_path;
1076
1077 execution_path=(const char *) getexecname();
1078 if (execution_path != (const char *) NULL)
1079 {
1080 if (*execution_path != *DirectorySeparator)
1081 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1082 (void) ConcatenateMagickString(path,execution_path,extent);
1083 }
1084 }
1085#endif
1086#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1087 NTGetExecutionPath(path,extent);
1088#endif
1089#if defined(__GNU__)
1090 {
1091 char
1092 *program_name;
1093
1094 ssize_t
1095 count;
1096
1097 count=0;
1098 program_name=program_invocation_name;
1099 if (*program_invocation_name != '/')
1100 {
1101 size_t
1102 extent;
1103
1104 extent=strlen(directory)+strlen(program_name)+2;
1105 program_name=AcquireQuantumMemory(extent,sizeof(*program_name));
1106 if (program_name == (char *) NULL)
1107 program_name=program_invocation_name;
1108 else
1109 count=FormatLocaleString(program_name,extent,"%s/%s",directory,
1110 program_invocation_name);
1111 }
1112 if (count != -1)
1113 {
1114 char
1115 *real_path = realpath_utf8(program_name);
1116
1117 if (real_path != (char *) NULL)
1118 {
1119 (void) CopyMagickString(path,real_path,extent);
1120 real_path=DestroyString(real_path);
1121 }
1122 }
1123 if (program_name != program_invocation_name)
1124 program_name=(char *) RelinquishMagickMemory(program_name);
1125 }
1126#endif
1127#if defined(__OpenBSD__)
1128 {
1129 extern char
1130 *__progname;
1131
1132 (void) CopyMagickString(path,__progname,extent);
1133 }
1134#endif
1135 return(IsPathAccessible(path));
1136}
1137
1138/*
1139%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1140% %
1141% %
1142% %
1143% G e t M a g i c k P a g e S i z e %
1144% %
1145% %
1146% %
1147%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1148%
1149% GetMagickPageSize() returns the memory page size.
1150%
1151% The format of the GetMagickPageSize method is:
1152%
1153% ssize_t GetMagickPageSize()
1154%
1155*/
1156MagickPrivate ssize_t GetMagickPageSize(void)
1157{
1158 static ssize_t
1159 page_size = -1;
1160
1161 if (page_size > 0)
1162 return(page_size);
1163#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1164 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1165#elif defined(MAGICKCORE_HAVE_GETPAGESIZE)
1166 page_size=(ssize_t) getpagesize();
1167#endif
1168 if (page_size <= 0)
1169 page_size=4096;
1170 return(page_size);
1171}
1172
1173/*
1174%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1175% %
1176% %
1177% %
1178% G e t P a t h A t t r i b u t e s %
1179% %
1180% %
1181% %
1182%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1183%
1184% GetPathAttributes() returns attributes (e.g. size of file) about a path.
1185%
1186% The path of the GetPathAttributes method is:
1187%
1188% MagickBooleanType GetPathAttributes(const char *path,void *attributes)
1189%
1190% A description of each parameter follows.
1191%
1192% o path: the file path.
1193%
1194% o attributes: the path attributes are returned here.
1195%
1196*/
1197MagickExport MagickBooleanType GetPathAttributes(const char *path,
1198 void *attributes)
1199{
1200 MagickBooleanType
1201 status;
1202
1203 if (path == (const char *) NULL)
1204 {
1205 errno=EINVAL;
1206 return(MagickFalse);
1207 }
1208 (void) memset(attributes,0,sizeof(struct stat));
1209 status=stat_utf8(path,(struct stat *) attributes) == 0 ? MagickTrue :
1210 MagickFalse;
1211 return(status);
1212}
1213
1214/*
1215%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1216% %
1217% %
1218% %
1219% G e t P a t h C o m p o n e n t %
1220% %
1221% %
1222% %
1223%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1224%
1225% GetPathComponent() returns the parent directory name, filename, basename, or
1226% extension of a file path.
1227%
1228% The component string pointed to must have at least MagickPathExtent space
1229% for the results to be stored.
1230%
1231% The format of the GetPathComponent function is:
1232%
1233% GetPathComponent(const char *path,PathType type,char *component)
1234%
1235% A description of each parameter follows:
1236%
1237% o path: Specifies a pointer to a character array that contains the
1238% file path.
1239%
1240% o type: Specifies which file path component to return.
1241%
1242% o component: the selected file path component is returned here.
1243%
1244*/
1245MagickExport void GetPathComponent(const char *path,PathType type,
1246 char *component)
1247{
1248 char
1249 *q;
1250
1251 char
1252 *p;
1253
1254 size_t
1255 magick_length,
1256 subimage_offset,
1257 subimage_length;
1258
1259 assert(path != (const char *) NULL);
1260 assert(component != (char *) NULL);
1261 if (IsEventLogging() != MagickFalse)
1262 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",path);
1263 if (*path == '\0')
1264 {
1265 *component='\0';
1266 return;
1267 }
1268 (void) CopyMagickString(component,path,MagickPathExtent);
1269 subimage_length=0;
1270 subimage_offset=0;
1271 if (type != SubcanonicalPath)
1272 {
1273 p=component+strlen(component)-1;
1274 if ((strlen(component) > 2) && (*p == ']'))
1275 {
1276 q=strrchr(component,'[');
1277 if ((q != (char *) NULL) && ((q == component) || (*(q-1) != ']')) &&
1278 (IsPathAccessible(path) == MagickFalse))
1279 {
1280 /*
1281 Look for scene specification (e.g. img0001.pcd[4]).
1282 */
1283 *p='\0';
1284 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1285 (IsGeometry(q+1) == MagickFalse))
1286 *p=']';
1287 else
1288 {
1289 subimage_length=(size_t) (p-q);
1290 subimage_offset=(size_t) (q-component+1);
1291 *q='\0';
1292 }
1293 }
1294 }
1295 }
1296 magick_length=0;
1297#if defined(__OS2__)
1298 if (path[1] != ":")
1299#endif
1300 for (p=component; *p != '\0'; p++)
1301 {
1302 if ((*p == '%') && (*(p+1) == '['))
1303 {
1304 /*
1305 Skip over %[...].
1306 */
1307 for (p++; (*p != ']') && (*p != '\0'); p++) ;
1308 if (*p == '\0')
1309 break;
1310 }
1311 if ((p != component) && (*p == ':') && (IsPathDirectory(component) < 0) &&
1312 (IsPathAccessible(component) == MagickFalse))
1313 {
1314 /*
1315 Look for image format specification (e.g. ps3:image).
1316 */
1317 *p='\0';
1318 if (IsMagickConflict(component) != MagickFalse)
1319 *p=':';
1320 else
1321 {
1322 magick_length=(size_t) (p-component+1);
1323 for (q=component; *(++p) != '\0'; q++)
1324 *q=(*p);
1325 *q='\0';
1326 }
1327 break;
1328 }
1329 }
1330 p=component;
1331 if (*p != '\0')
1332 for (p=component+strlen(component)-1; p > component; p--)
1333 if (IsBasenameSeparator(*p) != MagickFalse)
1334 break;
1335 switch (type)
1336 {
1337 case MagickPath:
1338 {
1339 if (magick_length != 0)
1340 (void) CopyMagickString(component,path,magick_length);
1341 else
1342 *component='\0';
1343 break;
1344 }
1345 case RootPath:
1346 {
1347 if (*component != '\0')
1348 {
1349 for (p=component+(strlen(component)-1); p > component; p--)
1350 {
1351 if (IsBasenameSeparator(*p) != MagickFalse)
1352 break;
1353 if (*p == '.')
1354 break;
1355 }
1356 if (*p == '.')
1357 *p='\0';
1358 break;
1359 }
1360 magick_fallthrough;
1361 }
1362 case HeadPath:
1363 {
1364 *p='\0';
1365 break;
1366 }
1367 case TailPath:
1368 {
1369 if (IsBasenameSeparator(*p) != MagickFalse)
1370 (void) CopyMagickString(component,p+1,MagickPathExtent);
1371 break;
1372 }
1373 case BasePath:
1374 {
1375 if (IsBasenameSeparator(*p) != MagickFalse)
1376 (void) CopyMagickString(component,p+1,MagickPathExtent);
1377 if (*component != '\0')
1378 for (p=component+(strlen(component)-1); p > component; p--)
1379 if (*p == '.')
1380 {
1381 *p='\0';
1382 break;
1383 }
1384 break;
1385 }
1386 case BasePathSansCompressExtension:
1387 {
1388 char
1389 extension[MagickPathExtent];
1390
1391 /*
1392 Base path sans any compression extension.
1393 */
1394 GetPathComponent(path,ExtensionPath,extension);
1395 if ((LocaleCompare(extension,"bz2") == 0) ||
1396 (LocaleCompare(extension,"gz") == 0) ||
1397 (LocaleCompare(extension,"svgz") == 0) ||
1398 (LocaleCompare(extension,"wmz") == 0) ||
1399 (LocaleCompare(extension,"Z") == 0))
1400 GetPathComponent(path,BasePath,component);
1401 break;
1402 }
1403 case ExtensionPath:
1404 {
1405 if (IsBasenameSeparator(*p) != MagickFalse)
1406 (void) CopyMagickString(component,p+1,MagickPathExtent);
1407 if (*component != '\0')
1408 for (p=component+strlen(component)-1; p > component; p--)
1409 if (*p == '.')
1410 break;
1411 *component='\0';
1412 if (*p == '.')
1413 (void) CopyMagickString(component,p+1,MagickPathExtent);
1414 break;
1415 }
1416 case SubimagePath:
1417 {
1418 *component='\0';
1419 if ((subimage_length != 0) && (magick_length < subimage_offset))
1420 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1421 break;
1422 }
1423 case SubcanonicalPath:
1424 case CanonicalPath:
1425 case UndefinedPath:
1426 break;
1427 }
1428}
1429
1430/*
1431%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1432% %
1433% %
1434% %
1435% G e t P a t h C o m p o n e n t s %
1436% %
1437% %
1438% %
1439%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1440%
1441% GetPathComponents() returns a list of path components.
1442%
1443% The format of the GetPathComponents method is:
1444%
1445% char **GetPathComponents(const char *path,
1446% size_t *number_components)
1447%
1448% A description of each parameter follows:
1449%
1450% o path: Specifies the string to segment into a list.
1451%
1452% o number_components: return the number of components in the list
1453%
1454*/
1455MagickPrivate char **GetPathComponents(const char *path,
1456 size_t *number_components)
1457{
1458 char
1459 **components;
1460
1461 const char
1462 *p,
1463 *q;
1464
1465 ssize_t
1466 i;
1467
1468 if (path == (char *) NULL)
1469 return((char **) NULL);
1470 *number_components=1;
1471 for (p=path; *p != '\0'; p++)
1472 if (IsBasenameSeparator(*p))
1473 (*number_components)++;
1474 components=(char **) AcquireQuantumMemory((size_t) *number_components+1UL,
1475 sizeof(*components));
1476 if (components == (char **) NULL)
1477 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1478 p=path;
1479 for (i=0; i < (ssize_t) *number_components; i++)
1480 {
1481 for (q=p; *q != '\0'; q++)
1482 if (IsBasenameSeparator(*q))
1483 break;
1484 components[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MagickPathExtent,
1485 sizeof(**components));
1486 if (components[i] == (char *) NULL)
1487 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1488 (void) CopyMagickString(components[i],p,(size_t) (q-p+1));
1489 p=q+1;
1490 }
1491 components[i]=(char *) NULL;
1492 return(components);
1493}
1494
1495/*
1496%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1497% %
1498% %
1499% %
1500% I s P a t h A c c e s s i b l e %
1501% %
1502% %
1503% %
1504%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1505%
1506% IsPathAccessible() returns MagickTrue if the file as defined by the path is
1507% accessible.
1508%
1509% The format of the IsPathAccessible method is:
1510%
1511% MagickBooleanType IsPathAccessible(const char *path)
1512%
1513% A description of each parameter follows.
1514%
1515% o path: Specifies a path to a file.
1516%
1517*/
1518MagickExport MagickBooleanType IsPathAccessible(const char *path)
1519{
1520 MagickBooleanType
1521 status;
1522
1523 struct stat
1524 attributes;
1525
1526 if ((path == (const char *) NULL) || (*path == '\0'))
1527 return(MagickFalse);
1528 if (LocaleCompare(path,"-") == 0)
1529 return(MagickTrue);
1530 status=GetPathAttributes(path,&attributes);
1531 if (status == MagickFalse)
1532 return(status);
1533 if (S_ISREG(attributes.st_mode) == 0)
1534 return(MagickFalse);
1535 if (access_utf8(path,F_OK) != 0)
1536 return(MagickFalse);
1537 return(MagickTrue);
1538}
1539
1540/*
1541%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1542% %
1543% %
1544% %
1545+ I s P a t h D i r e c t o r y %
1546% %
1547% %
1548% %
1549%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1550%
1551% IsPathDirectory() returns -1 if the directory does not exist, 1 is returned
1552% if the path represents a directory otherwise 0.
1553%
1554% The format of the IsPathDirectory method is:
1555%
1556% int IsPathDirectory(const char *path)
1557%
1558% A description of each parameter follows.
1559%
1560% o path: The directory path.
1561%
1562*/
1563static int IsPathDirectory(const char *path)
1564{
1565 MagickBooleanType
1566 status;
1567
1568 struct stat
1569 attributes;
1570
1571 if ((path == (const char *) NULL) || (*path == '\0'))
1572 return(MagickFalse);
1573 status=GetPathAttributes(path,&attributes);
1574 if (status == MagickFalse)
1575 return(-1);
1576 if (S_ISDIR(attributes.st_mode) == 0)
1577 return(0);
1578 return(1);
1579}
1580
1581/*
1582%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1583% %
1584% %
1585% %
1586% L i s t F i l e s %
1587% %
1588% %
1589% %
1590%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1591%
1592% ListFiles() reads the directory specified and returns a list of filenames
1593% contained in the directory sorted in ascending alphabetic order.
1594%
1595% The format of the ListFiles function is:
1596%
1597% char **ListFiles(const char *directory,const char *pattern,
1598% ssize_t *number_entries)
1599%
1600% A description of each parameter follows:
1601%
1602% o filelist: Method ListFiles returns a list of filenames contained
1603% in the directory. If the directory specified cannot be read or it is
1604% a file a NULL list is returned.
1605%
1606% o directory: Specifies a pointer to a text string containing a directory
1607% name.
1608%
1609% o pattern: Specifies a pointer to a text string containing a pattern.
1610%
1611% o number_entries: This integer returns the number of filenames in the
1612% list.
1613%
1614*/
1615
1616#if defined(__cplusplus) || defined(c_plusplus)
1617extern "C" {
1618#endif
1619
1620static int FileCompare(const void *x,const void *y)
1621{
1622 const char
1623 **p,
1624 **q;
1625
1626 p=(const char **) x;
1627 q=(const char **) y;
1628 return(LocaleCompare(*p,*q));
1629}
1630
1631#if defined(__cplusplus) || defined(c_plusplus)
1632}
1633#endif
1634
1635MagickPrivate char **ListFiles(const char *directory,const char *pattern,
1636 size_t *number_entries)
1637{
1638 char
1639 **filelist;
1640
1641 DIR
1642 *current_directory;
1643
1644 struct dirent
1645 *buffer,
1646 *entry;
1647
1648 size_t
1649 max_entries;
1650
1651 /*
1652 Open directory.
1653 */
1654 assert(directory != (const char *) NULL);
1655 assert(pattern != (const char *) NULL);
1656 assert(number_entries != (size_t *) NULL);
1657 if (IsEventLogging() != MagickFalse)
1658 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",directory);
1659 *number_entries=0;
1660 current_directory=opendir(directory);
1661 if (current_directory == (DIR *) NULL)
1662 return((char **) NULL);
1663 /*
1664 Allocate filelist.
1665 */
1666 max_entries=2048;
1667 filelist=(char **) AcquireQuantumMemory((size_t) max_entries,
1668 sizeof(*filelist));
1669 if (filelist == (char **) NULL)
1670 {
1671 (void) closedir(current_directory);
1672 return((char **) NULL);
1673 }
1674 /*
1675 Save the current and change to the new directory.
1676 */
1677 buffer=(struct dirent *) AcquireMagickMemory(sizeof(*buffer)+FILENAME_MAX+1);
1678 if (buffer == (struct dirent *) NULL)
1679 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1680 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1681 (entry != (struct dirent *) NULL))
1682 {
1683 if ((LocaleCompare(entry->d_name,".") == 0) ||
1684 (LocaleCompare(entry->d_name,"..") == 0))
1685 continue;
1686 if ((IsPathDirectory(entry->d_name) > 0) ||
1687#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1688 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1689#else
1690 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1691#endif
1692 {
1693 if (*number_entries >= max_entries)
1694 {
1695 /*
1696 Extend the file list.
1697 */
1698 max_entries<<=1;
1699 filelist=(char **) ResizeQuantumMemory(filelist,(size_t)
1700 max_entries,sizeof(*filelist));
1701 if (filelist == (char **) NULL)
1702 break;
1703 }
1704#if defined(vms)
1705 {
1706 char
1707 *p;
1708
1709 p=strchr(entry->d_name,';');
1710 if (p)
1711 *p='\0';
1712 if (*number_entries > 0)
1713 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1714 continue;
1715 }
1716#endif
1717 filelist[*number_entries]=(char *) AcquireString(entry->d_name);
1718 (*number_entries)++;
1719 }
1720 }
1721 buffer=(struct dirent *) RelinquishMagickMemory(buffer);
1722 (void) closedir(current_directory);
1723 if (filelist == (char **) NULL)
1724 return((char **) NULL);
1725 /*
1726 Sort filelist in ascending order.
1727 */
1728 qsort((void *) filelist,(size_t) *number_entries,sizeof(*filelist),
1729 FileCompare);
1730 return(filelist);
1731}
1732
1733/*
1734%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1735% %
1736% %
1737% %
1738% M a g i c k D e l a y %
1739% %
1740% %
1741% %
1742%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1743%
1744% MagickDelay() suspends program execution for the number of milliseconds
1745% specified.
1746%
1747% The format of the Delay method is:
1748%
1749% void MagickDelay(const MagickSizeType milliseconds)
1750%
1751% A description of each parameter follows:
1752%
1753% o milliseconds: Specifies the number of milliseconds to delay before
1754% returning.
1755%
1756*/
1757MagickExport void MagickDelay(const MagickSizeType milliseconds)
1758{
1759 if (milliseconds == 0)
1760 return;
1761#if defined(MAGICKCORE_HAVE_NANOSLEEP)
1762 {
1763 struct timespec
1764 timer;
1765
1766 timer.tv_sec=(time_t) (milliseconds/1000);
1767 timer.tv_nsec=(time_t) ((milliseconds % 1000)*1000*1000);
1768 (void) nanosleep(&timer,(struct timespec *) NULL);
1769 }
1770#elif defined(MAGICKCORE_HAVE_USLEEP)
1771 usleep(1000*milliseconds);
1772#elif defined(MAGICKCORE_HAVE_SELECT)
1773 {
1774 struct timeval
1775 timer;
1776
1777 timer.tv_sec=(long) milliseconds/1000;
1778 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1779 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1780 }
1781#elif defined(MAGICKCORE_HAVE_POLL)
1782 (void) poll((struct pollfd *) NULL,0,(int) milliseconds);
1783#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1784 Sleep((long) milliseconds);
1785#elif defined(vms)
1786 {
1787 float
1788 timer;
1789
1790 timer=milliseconds/1000.0;
1791 lib$wait(&timer);
1792 }
1793#elif defined(__BEOS__)
1794 snooze(1000*milliseconds);
1795#else
1796 {
1797 clock_t
1798 time_end;
1799
1800 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1801 while (clock() < time_end)
1802 {
1803 }
1804 }
1805#endif
1806}
1807
1808/*
1809%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1810% %
1811% %
1812% %
1813% M u l t i l i n e C e n s u s %
1814% %
1815% %
1816% %
1817%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1818%
1819% MultilineCensus() returns the number of lines within a label. A line is
1820% represented by a \n character.
1821%
1822% The format of the MultilineCensus method is:
1823%
1824% size_t MultilineCensus(const char *label)
1825%
1826% A description of each parameter follows.
1827%
1828% o label: This character string is the label.
1829%
1830*/
1831MagickExport size_t MultilineCensus(const char *label)
1832{
1833 size_t
1834 number_lines;
1835
1836 /*
1837 Determine the number of lines within this label.
1838 */
1839 if (label == (char *) NULL)
1840 return(0);
1841 for (number_lines=1; *label != '\0'; label++)
1842 if (*label == '\n')
1843 number_lines++;
1844 return(number_lines);
1845}
1846
1847/*
1848%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1849% %
1850% %
1851% %
1852% S h r e d F i l e %
1853% %
1854% %
1855% %
1856%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1857%
1858% ShredFile() overwrites the specified file with random data. The overwrite is
1859% optional and is only required to help keep the contents of the file private.
1860%
1861% The format of the ShredFile method is:
1862%
1863% MagickBooleanType ShredFile(const char *path)
1864%
1865% A description of each parameter follows.
1866%
1867% o path: Specifies a path to a file.
1868%
1869*/
1870MagickPrivate MagickBooleanType ShredFile(const char *path)
1871{
1872 int
1873 file,
1874 status;
1875
1876 MagickSizeType
1877 length;
1878
1880 *random_info;
1881
1882 size_t
1883 quantum;
1884
1885 ssize_t
1886 i;
1887
1888 static ssize_t
1889 passes = -1;
1890
1892 *key;
1893
1894 struct stat
1895 file_stats;
1896
1897 if ((path == (const char *) NULL) || (*path == '\0'))
1898 return(MagickFalse);
1899 if (passes == -1)
1900 {
1901 char
1902 *property;
1903
1904 passes=0;
1905 property=GetEnvironmentValue("MAGICK_SHRED_PASSES");
1906 if (property != (char *) NULL)
1907 {
1908 passes=(ssize_t) StringToInteger(property);
1909 property=DestroyString(property);
1910 }
1911 property=GetPolicyValue("system:shred");
1912 if (property != (char *) NULL)
1913 {
1914 passes=(ssize_t) StringToInteger(property);
1915 property=DestroyString(property);
1916 }
1917 }
1918 if (passes == 0)
1919 return(MagickTrue);
1920 /*
1921 Shred the file.
1922 */
1923 file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
1924 if (file == -1)
1925 return(MagickFalse);
1926 quantum=(size_t) MagickMinBufferExtent;
1927 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1928 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
1929 length=(MagickSizeType) file_stats.st_size;
1930 random_info=AcquireRandomInfo();
1931 key=GetRandomKey(random_info,quantum);
1932 for (i=0; i < passes; i++)
1933 {
1934 MagickOffsetType
1935 j;
1936
1937 ssize_t
1938 count;
1939
1940 if (lseek(file,0,SEEK_SET) < 0)
1941 break;
1942 for (j=0; j < (MagickOffsetType) length; j+=count)
1943 {
1944 if (i != 0)
1945 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
1946 count=write(file,GetStringInfoDatum(key),(size_t)
1947 MagickMin((MagickOffsetType) quantum,(MagickOffsetType) length-j));
1948 if (count <= 0)
1949 {
1950 count=0;
1951 if (errno != EINTR)
1952 break;
1953 }
1954 }
1955 if (j < (MagickOffsetType) length)
1956 break;
1957 }
1958 key=DestroyStringInfo(key);
1959 random_info=DestroyRandomInfo(random_info);
1960 status=close_utf8(file);
1961 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);
1962}