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