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