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