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