MagickCore  7.1.0
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-2021 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
22 % %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
25 % %
26 % https://imagemagick.org/script/license.php %
27 % %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
33 % %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 
39 /*
40  Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/property.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/color.h"
46 #include "MagickCore/exception.h"
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  const char
352  *p,
353  *q;
354 
355  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  const unsigned char
508  *p;
509 
510  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  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 #if defined(MAGICKCORE_HAVE_GETPWNAM_R)
651  buffer[MagickPathExtent],
652 #endif
653  username[MagickPathExtent];
654 
655  char
656  *p;
657 
658  struct passwd
659  *entry,
660  pwd;
661 
662  /*
663  Substitute ~ with home directory from password file.
664  */
665  (void) CopyMagickString(username,path+1,MagickPathExtent);
666  p=strchr(username,'/');
667  if (p != (char *) NULL)
668  *p='\0';
669 #if !defined(MAGICKCORE_HAVE_GETPWNAM_R)
670  entry=getpwnam(username);
671 #else
672  entry=(struct passwd *) NULL;
673  if (getpwnam_r(username,&pwd,buffer,sizeof(buffer),&entry) < 0)
674  return;
675 #endif
676  if (entry == (struct passwd *) NULL)
677  return;
678  (void) CopyMagickString(expand_path,entry->pw_dir,MagickPathExtent);
679  if (p != (char *) NULL)
680  {
681  (void) ConcatenateMagickString(expand_path,"/",MagickPathExtent);
682  (void) ConcatenateMagickString(expand_path,p+1,MagickPathExtent);
683  }
684 #endif
685  }
686  (void) CopyMagickString(path,expand_path,MagickPathExtent);
687 }
688 
689 /*
690 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
691 % %
692 % %
693 % %
694 % E x p a n d F i l e n a m e s %
695 % %
696 % %
697 % %
698 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
699 %
700 % ExpandFilenames() checks each argument of the given argument array, and
701 % expands it if they have a wildcard character.
702 %
703 % Any coder prefix (EG: 'coder:filename') or read modifier postfix (EG:
704 % 'filename[...]') are ignored during the file the expansion, but will be
705 % included in the final argument. If no filename matching the meta-character
706 % 'glob' is found the original argument is returned.
707 %
708 % For example, an argument of '*.gif[20x20]' will be replaced by the list
709 % 'abc.gif[20x20]', 'foobar.gif[20x20]', 'xyzzy.gif[20x20]'
710 % if such filenames exist, (in the current directory in this case).
711 %
712 % Meta-characters handled...
713 % @ read a list of filenames (no further expansion performed)
714 % ~ At start of filename expands to HOME environemtn variable
715 % * matches any string including an empty string
716 % ? matches by any single character
717 %
718 % WARNING: filenames starting with '.' (hidden files in a UNIX file system)
719 % will never be expanded. Attempting to epand '.*' will produce no change.
720 %
721 % Expansion is ignored for coders "label:" "caption:" "pango:" and "vid:".
722 % Which provide their own '@' meta-character handling.
723 %
724 % You can see the results of the expansion using "Configure" log events.
725 %
726 % The returned list should be freed using DestroyStringList().
727 %
728 % However the strings in the original pointed to argv are not
729 % freed (TO BE CHECKED). So a copy of the original pointer (and count)
730 % should be kept separate if they need to be freed later.
731 %
732 % The format of the ExpandFilenames function is:
733 %
734 % status=ExpandFilenames(int *number_arguments,char ***arguments)
735 %
736 % A description of each parameter follows:
737 %
738 % o number_arguments: Specifies a pointer to an integer describing the
739 % number of elements in the argument vector.
740 %
741 % o arguments: Specifies a pointer to a text array containing the command
742 % line arguments.
743 %
744 */
746  char ***arguments)
747 {
748  char
749  home_directory[MagickPathExtent],
750  **vector;
751 
752  ssize_t
753  i,
754  j;
755 
756  size_t
757  number_files;
758 
759  ssize_t
760  count,
761  parameters;
762 
763  /*
764  Allocate argument vector.
765  */
766  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
767  assert(number_arguments != (int *) NULL);
768  assert(arguments != (char ***) NULL);
769  vector=(char **) AcquireQuantumMemory((size_t) (*number_arguments+1),
770  sizeof(*vector));
771  if (vector == (char **) NULL)
772  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
773  /*
774  Expand any wildcard filenames.
775  */
776  *home_directory='\0';
777  count=0;
778  for (i=0; i < (ssize_t) *number_arguments; i++)
779  {
780  char
781  **filelist,
782  filename[MagickPathExtent],
783  magick[MagickPathExtent],
784  *option,
785  path[MagickPathExtent],
786  subimage[MagickPathExtent];
787 
789  destroy;
790 
791  option=(*arguments)[i];
792  *magick='\0';
793  *path='\0';
794  *filename='\0';
795  *subimage='\0';
796  number_files=0;
797  vector[count++]=ConstantString(option);
798  destroy=MagickTrue;
800  if (parameters > 0)
801  {
802  /*
803  Do not expand command option parameters.
804  */
805  for (j=0; j < parameters; j++)
806  {
807  i++;
808  if (i == (ssize_t) *number_arguments)
809  break;
810  option=(*arguments)[i];
811  vector[count++]=ConstantString(option);
812  }
813  continue;
814  }
815  if ((*option == '"') || (*option == '\''))
816  continue;
817  GetPathComponent(option,TailPath,filename);
818  GetPathComponent(option,MagickPath,magick);
819  if ((LocaleCompare(magick,"CAPTION") == 0) ||
820  (LocaleCompare(magick,"LABEL") == 0) ||
821  (LocaleCompare(magick,"PANGO") == 0) ||
822  (LocaleCompare(magick,"VID") == 0))
823  continue;
824  if ((IsGlob(filename) == MagickFalse) && (*option != '@'))
825  continue;
826  if ((*option != '@') && (IsPathAccessible(option) == MagickFalse))
827  {
828  /*
829  Generate file list from wildcard filename (e.g. *.jpg).
830  */
831  GetPathComponent(option,HeadPath,path);
832  GetPathComponent(option,SubimagePath,subimage);
833  ExpandFilename(path);
834  if (*home_directory == '\0')
835  getcwd_utf8(home_directory,MagickPathExtent-1);
836  filelist=ListFiles(*path == '\0' ? home_directory : path,filename,
837  &number_files);
838  }
839  else
840  {
841  char
842  *files;
843 
845  *exception;
846 
847  int
848  length;
849 
850  /*
851  Generate file list from file list (e.g. @filelist.txt).
852  */
853  exception=AcquireExceptionInfo();
854  files=FileToString(option+1,~0UL,exception);
855  exception=DestroyExceptionInfo(exception);
856  if (files == (char *) NULL)
857  continue;
858  filelist=StringToArgv(files,&length);
859  if (filelist == (char **) NULL)
860  continue;
861  files=DestroyString(files);
862  filelist[0]=DestroyString(filelist[0]);
863  for (j=0; j < (ssize_t) (length-1); j++)
864  filelist[j]=filelist[j+1];
865  number_files=(size_t) length-1;
866  }
867  if (filelist == (char **) NULL)
868  continue;
869  for (j=0; j < (ssize_t) number_files; j++)
870  if (IsPathDirectory(filelist[j]) <= 0)
871  break;
872  if (j == (ssize_t) number_files)
873  {
874  for (j=0; j < (ssize_t) number_files; j++)
875  filelist[j]=DestroyString(filelist[j]);
876  filelist=(char **) RelinquishMagickMemory(filelist);
877  continue;
878  }
879  /*
880  Transfer file list to argument vector.
881  */
882  vector=(char **) ResizeQuantumMemory(vector,(size_t) *number_arguments+
883  count+number_files+1,sizeof(*vector));
884  if (vector == (char **) NULL)
885  {
886  for (j=0; j < (ssize_t) number_files; j++)
887  filelist[j]=DestroyString(filelist[j]);
888  filelist=(char **) RelinquishMagickMemory(filelist);
889  return(MagickFalse);
890  }
891  for (j=0; j < (ssize_t) number_files; j++)
892  {
893  option=filelist[j];
895  if (parameters > 0)
896  {
897  ssize_t
898  k;
899 
900  /*
901  Do not expand command option parameters.
902  */
903  vector[count++]=ConstantString(option);
904  for (k=0; k < parameters; k++)
905  {
906  j++;
907  if (j == (ssize_t) number_files)
908  break;
909  option=filelist[j];
910  vector[count++]=ConstantString(option);
911  }
912  continue;
913  }
914  (void) CopyMagickString(filename,path,MagickPathExtent);
915  if (*path != '\0')
918  if (filelist[j] != (char *) NULL)
919  (void) ConcatenateMagickString(filename,filelist[j],MagickPathExtent);
920  filelist[j]=DestroyString(filelist[j]);
921  if (strlen(filename) >= (MagickPathExtent-1))
922  ThrowFatalException(OptionFatalError,"FilenameTruncated");
923  if (IsPathDirectory(filename) <= 0)
924  {
925  char
926  file_path[MagickPathExtent];
927 
928  *file_path='\0';
929  if (*magick != '\0')
930  {
931  (void) ConcatenateMagickString(file_path,magick,
933  (void) ConcatenateMagickString(file_path,":",MagickPathExtent);
934  }
935  (void) ConcatenateMagickString(file_path,filename,MagickPathExtent);
936  if (*subimage != '\0')
937  {
938  (void) ConcatenateMagickString(file_path,"[",MagickPathExtent);
939  (void) ConcatenateMagickString(file_path,subimage,
941  (void) ConcatenateMagickString(file_path,"]",MagickPathExtent);
942  }
943  if (strlen(file_path) >= (MagickPathExtent-1))
944  ThrowFatalException(OptionFatalError,"FilenameTruncated");
945  if (destroy != MagickFalse)
946  {
947  count--;
948  vector[count]=DestroyString(vector[count]);
949  destroy=MagickFalse;
950  }
951  vector[count++]=ConstantString(file_path);
952  }
953  }
954  filelist=(char **) RelinquishMagickMemory(filelist);
955  }
956  vector[count]=(char *) NULL;
957  if (IsEventLogging() != MagickFalse)
958  {
959  char
960  *command_line;
961 
962  command_line=AcquireString(vector[0]);
963  for (i=1; i < count; i++)
964  {
965  (void) ConcatenateString(&command_line," {");
966  (void) ConcatenateString(&command_line,vector[i]);
967  (void) ConcatenateString(&command_line,"}");
968  }
970  "Command line: %s",command_line);
971  command_line=DestroyString(command_line);
972  }
973  *number_arguments=(int) count;
974  *arguments=vector;
975  return(MagickTrue);
976 }
977 
978 /*
979 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
980 % %
981 % %
982 % %
983 % G e t E x e c u t i o n P a t h %
984 % %
985 % %
986 % %
987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
988 %
989 % GetExecutionPath() returns the pathname of the executable that started
990 % the process. On success MagickTrue is returned, otherwise MagickFalse.
991 %
992 % The format of the GetExecutionPath method is:
993 %
994 % MagickBooleanType GetExecutionPath(char *path,const size_t extent)
995 %
996 % A description of each parameter follows:
997 %
998 % o path: the pathname of the executable that started the process.
999 %
1000 % o extent: the maximum extent of the path.
1001 %
1002 */
1003 MagickPrivate MagickBooleanType GetExecutionPath(char *path,const size_t extent)
1004 {
1005  char
1006  *directory;
1007 
1008  *path='\0';
1009  directory=getcwd(path,(unsigned long) extent);
1010  (void) directory;
1011 #if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
1012  {
1013  char
1014  execution_path[PATH_MAX+1],
1015  link_path[MagickPathExtent];
1016 
1017  ssize_t
1018  count;
1019 
1020  (void) FormatLocaleString(link_path,MagickPathExtent,"/proc/%.20g/exe",
1021  (double) getpid());
1022  count=readlink(link_path,execution_path,PATH_MAX);
1023  if (count == -1)
1024  {
1025  (void) FormatLocaleString(link_path,MagickPathExtent,"/proc/%.20g/file",
1026  (double) getpid());
1027  count=readlink(link_path,execution_path,PATH_MAX);
1028  }
1029  if ((count > 0) && (count <= (ssize_t) PATH_MAX))
1030  {
1031  execution_path[count]='\0';
1032  (void) CopyMagickString(path,execution_path,extent);
1033  }
1034  }
1035 #endif
1036 #if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
1037  {
1038  char
1039  executable_path[PATH_MAX << 1],
1040  execution_path[PATH_MAX+1];
1041 
1042  uint32_t
1043  length;
1044 
1045  length=sizeof(executable_path);
1046  if ((_NSGetExecutablePath(executable_path,&length) == 0) &&
1047  (realpath(executable_path,execution_path) != (char *) NULL))
1048  (void) CopyMagickString(path,execution_path,extent);
1049  }
1050 #endif
1051 #if defined(MAGICKCORE_HAVE_GETEXECNAME)
1052  {
1053  const char
1054  *execution_path;
1055 
1056  execution_path=(const char *) getexecname();
1057  if (execution_path != (const char *) NULL)
1058  {
1059  if (*execution_path != *DirectorySeparator)
1060  (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1061  (void) ConcatenateMagickString(path,execution_path,extent);
1062  }
1063  }
1064 #endif
1065 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1066  NTGetExecutionPath(path,extent);
1067 #endif
1068 #if defined(__GNU__)
1069  {
1070  char
1071  *program_name;
1072 
1073  ssize_t
1074  count;
1075 
1076  count=0;
1077  program_name=program_invocation_name;
1078  if (*program_invocation_name != '/')
1079  {
1080  size_t
1081  extent;
1082 
1083  extent=strlen(directory)+strlen(program_name)+2;
1084  program_name=AcquireQuantumMemory(extent,sizeof(*program_name));
1085  if (program_name == (char *) NULL)
1086  program_name=program_invocation_name;
1087  else
1088  count=FormatLocaleString(program_name,extent,"%s/%s",directory,
1089  program_invocation_name);
1090  }
1091  if (count != -1)
1092  {
1093  char
1094  execution_path[PATH_MAX+1];
1095 
1096  if (realpath(program_name,execution_path) != (char *) NULL)
1097  (void) CopyMagickString(path,execution_path,extent);
1098  }
1099  if (program_name != program_invocation_name)
1100  program_name=(char *) RelinquishMagickMemory(program_name);
1101  }
1102 #endif
1103 #if defined(__OpenBSD__)
1104  {
1105  extern char
1106  *__progname;
1107 
1108  (void) CopyMagickString(path,__progname,extent);
1109  }
1110 #endif
1111  return(IsPathAccessible(path));
1112 }
1113 
1114 /*
1115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1116 % %
1117 % %
1118 % %
1119 % G e t M a g i c k P a g e S i z e %
1120 % %
1121 % %
1122 % %
1123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1124 %
1125 % GetMagickPageSize() returns the memory page size.
1126 %
1127 % The format of the GetMagickPageSize method is:
1128 %
1129 % ssize_t GetMagickPageSize()
1130 %
1131 */
1133 {
1134  static ssize_t
1135  page_size = -1;
1136 
1137  if (page_size > 0)
1138  return(page_size);
1139 #if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1140  page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1141 #elif defined(MAGICKCORE_HAVE_GETPAGESIZE)
1142  page_size=(ssize_t) getpagesize();
1143 #endif
1144  if (page_size <= 0)
1145  page_size=4096;
1146  return(page_size);
1147 }
1148 
1149 /*
1150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1151 % %
1152 % %
1153 % %
1154 % G e t P a t h A t t r i b u t e s %
1155 % %
1156 % %
1157 % %
1158 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1159 %
1160 % GetPathAttributes() returns attributes (e.g. size of file) about a path.
1161 %
1162 % The path of the GetPathAttributes method is:
1163 %
1164 % MagickBooleanType GetPathAttributes(const char *path,void *attributes)
1165 %
1166 % A description of each parameter follows.
1167 %
1168 % o path: the file path.
1169 %
1170 % o attributes: the path attributes are returned here.
1171 %
1172 */
1174  void *attributes)
1175 {
1177  status;
1178 
1179  if (path == (const char *) NULL)
1180  {
1181  errno=EINVAL;
1182  return(MagickFalse);
1183  }
1184  (void) memset(attributes,0,sizeof(struct stat));
1185  status=stat_utf8(path,(struct stat *) attributes) == 0 ? MagickTrue :
1186  MagickFalse;
1187  return(status);
1188 }
1189 
1190 /*
1191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1192 % %
1193 % %
1194 % %
1195 % G e t P a t h C o m p o n e n t %
1196 % %
1197 % %
1198 % %
1199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1200 %
1201 % GetPathComponent() returns the parent directory name, filename, basename, or
1202 % extension of a file path.
1203 %
1204 % The component string pointed to must have at least MagickPathExtent space
1205 % for the results to be stored.
1206 %
1207 % The format of the GetPathComponent function is:
1208 %
1209 % GetPathComponent(const char *path,PathType type,char *component)
1210 %
1211 % A description of each parameter follows:
1212 %
1213 % o path: Specifies a pointer to a character array that contains the
1214 % file path.
1215 %
1216 % o type: Specififies which file path component to return.
1217 %
1218 % o component: the selected file path component is returned here.
1219 %
1220 */
1221 MagickExport void GetPathComponent(const char *path,PathType type,
1222  char *component)
1223 {
1224  char
1225  *q;
1226 
1227  char
1228  *p;
1229 
1230  size_t
1231  magick_length,
1232  subimage_offset,
1233  subimage_length;
1234 
1235  assert(path != (const char *) NULL);
1236  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",path);
1237  assert(component != (char *) NULL);
1238  if (*path == '\0')
1239  {
1240  *component='\0';
1241  return;
1242  }
1243  (void) CopyMagickString(component,path,MagickPathExtent);
1244  subimage_length=0;
1245  subimage_offset=0;
1246  if (type != SubcanonicalPath)
1247  {
1248  p=component+strlen(component)-1;
1249  if ((strlen(component) > 2) && (*p == ']'))
1250  {
1251  q=strrchr(component,'[');
1252  if ((q != (char *) NULL) && ((q == component) || (*(q-1) != ']')) &&
1253  (IsPathAccessible(path) == MagickFalse))
1254  {
1255  /*
1256  Look for scene specification (e.g. img0001.pcd[4]).
1257  */
1258  *p='\0';
1259  if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1260  (IsGeometry(q+1) == MagickFalse))
1261  *p=']';
1262  else
1263  {
1264  subimage_length=(size_t) (p-q);
1265  subimage_offset=(size_t) (q-component+1);
1266  *q='\0';
1267  }
1268  }
1269  }
1270  }
1271  magick_length=0;
1272 #if defined(__OS2__)
1273  if (path[1] != ":")
1274 #endif
1275  for (p=component; *p != '\0'; p++)
1276  {
1277  if ((*p == '%') && (*(p+1) == '['))
1278  {
1279  /*
1280  Skip over %[...].
1281  */
1282  for (p++; (*p != ']') && (*p != '\0'); p++) ;
1283  if (*p == '\0')
1284  break;
1285  }
1286  if ((p != component) && (*p == ':') && (IsPathDirectory(component) < 0) &&
1287  (IsPathAccessible(component) == MagickFalse))
1288  {
1289  /*
1290  Look for image format specification (e.g. ps3:image).
1291  */
1292  *p='\0';
1293  if (IsMagickConflict(component) != MagickFalse)
1294  *p=':';
1295  else
1296  {
1297  magick_length=(size_t) (p-component+1);
1298  for (q=component; *(++p) != '\0'; q++)
1299  *q=(*p);
1300  *q='\0';
1301  }
1302  break;
1303  }
1304  }
1305  p=component;
1306  if (*p != '\0')
1307  for (p=component+strlen(component)-1; p > component; p--)
1308  if (IsBasenameSeparator(*p) != MagickFalse)
1309  break;
1310  switch (type)
1311  {
1312  case MagickPath:
1313  {
1314  if (magick_length != 0)
1315  (void) CopyMagickString(component,path,magick_length);
1316  else
1317  *component='\0';
1318  break;
1319  }
1320  case RootPath:
1321  {
1322  if (*component != '\0')
1323  {
1324  for (p=component+(strlen(component)-1); p > component; p--)
1325  {
1326  if (IsBasenameSeparator(*p) != MagickFalse)
1327  break;
1328  if (*p == '.')
1329  break;
1330  }
1331  if (*p == '.')
1332  *p='\0';
1333  break;
1334  }
1335  }
1336  case HeadPath:
1337  {
1338  *p='\0';
1339  break;
1340  }
1341  case TailPath:
1342  {
1343  if (IsBasenameSeparator(*p) != MagickFalse)
1344  (void) CopyMagickString(component,p+1,MagickPathExtent);
1345  break;
1346  }
1347  case BasePath:
1348  {
1349  if (IsBasenameSeparator(*p) != MagickFalse)
1350  (void) CopyMagickString(component,p+1,MagickPathExtent);
1351  if (*component != '\0')
1352  for (p=component+(strlen(component)-1); p > component; p--)
1353  if (*p == '.')
1354  {
1355  *p='\0';
1356  break;
1357  }
1358  break;
1359  }
1361  {
1362  char
1363  extension[MagickPathExtent];
1364 
1365  /*
1366  Base path sans any compression extension.
1367  */
1368  GetPathComponent(path,ExtensionPath,extension);
1369  if ((LocaleCompare(extension,"bz2") == 0) ||
1370  (LocaleCompare(extension,"gz") == 0) ||
1371  (LocaleCompare(extension,"svgz") == 0) ||
1372  (LocaleCompare(extension,"wmz") == 0) ||
1373  (LocaleCompare(extension,"Z") == 0))
1374  GetPathComponent(path,BasePath,component);
1375  break;
1376  }
1377  case ExtensionPath:
1378  {
1379  if (IsBasenameSeparator(*p) != MagickFalse)
1380  (void) CopyMagickString(component,p+1,MagickPathExtent);
1381  if (*component != '\0')
1382  for (p=component+strlen(component)-1; p > component; p--)
1383  if (*p == '.')
1384  break;
1385  *component='\0';
1386  if (*p == '.')
1387  (void) CopyMagickString(component,p+1,MagickPathExtent);
1388  break;
1389  }
1390  case SubimagePath:
1391  {
1392  *component='\0';
1393  if ((subimage_length != 0) && (magick_length < subimage_offset))
1394  (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1395  break;
1396  }
1397  case SubcanonicalPath:
1398  case CanonicalPath:
1399  case UndefinedPath:
1400  break;
1401  }
1402 }
1403 
1404 /*
1405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1406 % %
1407 % %
1408 % %
1409 % G e t P a t h C o m p o n e n t s %
1410 % %
1411 % %
1412 % %
1413 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1414 %
1415 % GetPathComponents() returns a list of path components.
1416 %
1417 % The format of the GetPathComponents method is:
1418 %
1419 % char **GetPathComponents(const char *path,
1420 % size_t *number_componenets)
1421 %
1422 % A description of each parameter follows:
1423 %
1424 % o path: Specifies the string to segment into a list.
1425 %
1426 % o number_components: return the number of components in the list
1427 %
1428 */
1429 MagickPrivate char **GetPathComponents(const char *path,
1430  size_t *number_components)
1431 {
1432  char
1433  **components;
1434 
1435  const char
1436  *p,
1437  *q;
1438 
1439  ssize_t
1440  i;
1441 
1442  if (path == (char *) NULL)
1443  return((char **) NULL);
1444  *number_components=1;
1445  for (p=path; *p != '\0'; p++)
1446  if (IsBasenameSeparator(*p))
1447  (*number_components)++;
1448  components=(char **) AcquireQuantumMemory((size_t) *number_components+1UL,
1449  sizeof(*components));
1450  if (components == (char **) NULL)
1451  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1452  p=path;
1453  for (i=0; i < (ssize_t) *number_components; i++)
1454  {
1455  for (q=p; *q != '\0'; q++)
1456  if (IsBasenameSeparator(*q))
1457  break;
1458  components[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MagickPathExtent,
1459  sizeof(**components));
1460  if (components[i] == (char *) NULL)
1461  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1462  (void) CopyMagickString(components[i],p,(size_t) (q-p+1));
1463  p=q+1;
1464  }
1465  components[i]=(char *) NULL;
1466  return(components);
1467 }
1468 
1469 /*
1470 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1471 % %
1472 % %
1473 % %
1474 % I s P a t h A c c e s s i b l e %
1475 % %
1476 % %
1477 % %
1478 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1479 %
1480 % IsPathAccessible() returns MagickTrue if the file as defined by the path is
1481 % accessible.
1482 %
1483 % The format of the IsPathAccessible method is:
1484 %
1485 % MagickBooleanType IsPathAccessible(const char *path)
1486 %
1487 % A description of each parameter follows.
1488 %
1489 % o path: Specifies a path to a file.
1490 %
1491 */
1493 {
1495  status;
1496 
1497  struct stat
1498  attributes;
1499 
1500  if ((path == (const char *) NULL) || (*path == '\0'))
1501  return(MagickFalse);
1502  if (LocaleCompare(path,"-") == 0)
1503  return(MagickTrue);
1504  status=GetPathAttributes(path,&attributes);
1505  if (status == MagickFalse)
1506  return(status);
1507  if (S_ISREG(attributes.st_mode) == 0)
1508  return(MagickFalse);
1509  if (access_utf8(path,F_OK) != 0)
1510  return(MagickFalse);
1511  return(MagickTrue);
1512 }
1513 
1514 /*
1515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1516 % %
1517 % %
1518 % %
1519 + I s P a t h D i r e c t o r y %
1520 % %
1521 % %
1522 % %
1523 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1524 %
1525 % IsPathDirectory() returns -1 if the directory does not exist, 1 is returned
1526 % if the path represents a directory otherwise 0.
1527 %
1528 % The format of the IsPathDirectory method is:
1529 %
1530 % int IsPathDirectory(const char *path)
1531 %
1532 % A description of each parameter follows.
1533 %
1534 % o path: The directory path.
1535 %
1536 */
1537 static int IsPathDirectory(const char *path)
1538 {
1540  status;
1541 
1542  struct stat
1543  attributes;
1544 
1545  if ((path == (const char *) NULL) || (*path == '\0'))
1546  return(MagickFalse);
1547  status=GetPathAttributes(path,&attributes);
1548  if (status == MagickFalse)
1549  return(-1);
1550  if (S_ISDIR(attributes.st_mode) == 0)
1551  return(0);
1552  return(1);
1553 }
1554 
1555 /*
1556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1557 % %
1558 % %
1559 % %
1560 % L i s t F i l e s %
1561 % %
1562 % %
1563 % %
1564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1565 %
1566 % ListFiles() reads the directory specified and returns a list of filenames
1567 % contained in the directory sorted in ascending alphabetic order.
1568 %
1569 % The format of the ListFiles function is:
1570 %
1571 % char **ListFiles(const char *directory,const char *pattern,
1572 % ssize_t *number_entries)
1573 %
1574 % A description of each parameter follows:
1575 %
1576 % o filelist: Method ListFiles returns a list of filenames contained
1577 % in the directory. If the directory specified cannot be read or it is
1578 % a file a NULL list is returned.
1579 %
1580 % o directory: Specifies a pointer to a text string containing a directory
1581 % name.
1582 %
1583 % o pattern: Specifies a pointer to a text string containing a pattern.
1584 %
1585 % o number_entries: This integer returns the number of filenames in the
1586 % list.
1587 %
1588 */
1589 
1590 #if defined(__cplusplus) || defined(c_plusplus)
1591 extern "C" {
1592 #endif
1593 
1594 static int FileCompare(const void *x,const void *y)
1595 {
1596  const char
1597  **p,
1598  **q;
1599 
1600  p=(const char **) x;
1601  q=(const char **) y;
1602  return(LocaleCompare(*p,*q));
1603 }
1604 
1605 #if defined(__cplusplus) || defined(c_plusplus)
1606 }
1607 #endif
1608 
1609 MagickPrivate char **ListFiles(const char *directory,const char *pattern,
1610  size_t *number_entries)
1611 {
1612  char
1613  **filelist;
1614 
1615  DIR
1616  *current_directory;
1617 
1618  struct dirent
1619  *buffer,
1620  *entry;
1621 
1622  size_t
1623  max_entries;
1624 
1625  /*
1626  Open directory.
1627  */
1628  assert(directory != (const char *) NULL);
1629  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",directory);
1630  assert(pattern != (const char *) NULL);
1631  assert(number_entries != (size_t *) NULL);
1632  *number_entries=0;
1633  current_directory=opendir(directory);
1634  if (current_directory == (DIR *) NULL)
1635  return((char **) NULL);
1636  /*
1637  Allocate filelist.
1638  */
1639  max_entries=2048;
1640  filelist=(char **) AcquireQuantumMemory((size_t) max_entries,
1641  sizeof(*filelist));
1642  if (filelist == (char **) NULL)
1643  {
1644  (void) closedir(current_directory);
1645  return((char **) NULL);
1646  }
1647  /*
1648  Save the current and change to the new directory.
1649  */
1650  buffer=(struct dirent *) AcquireMagickMemory(sizeof(*buffer)+FILENAME_MAX+1);
1651  if (buffer == (struct dirent *) NULL)
1652  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1653  while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1654  (entry != (struct dirent *) NULL))
1655  {
1656  if ((LocaleCompare(entry->d_name,".") == 0) ||
1657  (LocaleCompare(entry->d_name,"..") == 0))
1658  continue;
1659  if ((IsPathDirectory(entry->d_name) > 0) ||
1660 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1661  (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1662 #else
1663  (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1664 #endif
1665  {
1666  if (*number_entries >= max_entries)
1667  {
1668  /*
1669  Extend the file list.
1670  */
1671  max_entries<<=1;
1672  filelist=(char **) ResizeQuantumMemory(filelist,(size_t)
1673  max_entries,sizeof(*filelist));
1674  if (filelist == (char **) NULL)
1675  break;
1676  }
1677 #if defined(vms)
1678  {
1679  char
1680  *p;
1681 
1682  p=strchr(entry->d_name,';');
1683  if (p)
1684  *p='\0';
1685  if (*number_entries > 0)
1686  if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1687  continue;
1688  }
1689 #endif
1690  filelist[*number_entries]=(char *) AcquireString(entry->d_name);
1691  (*number_entries)++;
1692  }
1693  }
1694  buffer=(struct dirent *) RelinquishMagickMemory(buffer);
1695  (void) closedir(current_directory);
1696  if (filelist == (char **) NULL)
1697  return((char **) NULL);
1698  /*
1699  Sort filelist in ascending order.
1700  */
1701  qsort((void *) filelist,(size_t) *number_entries,sizeof(*filelist),
1702  FileCompare);
1703  return(filelist);
1704 }
1705 
1706 /*
1707 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1708 % %
1709 % %
1710 % %
1711 % M a g i c k D e l a y %
1712 % %
1713 % %
1714 % %
1715 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1716 %
1717 % MagickDelay() suspends program execution for the number of milliseconds
1718 % specified.
1719 %
1720 % The format of the Delay method is:
1721 %
1722 % void MagickDelay(const MagickSizeType milliseconds)
1723 %
1724 % A description of each parameter follows:
1725 %
1726 % o milliseconds: Specifies the number of milliseconds to delay before
1727 % returning.
1728 %
1729 */
1730 MagickExport void MagickDelay(const MagickSizeType milliseconds)
1731 {
1732  if (milliseconds == 0)
1733  return;
1734 #if defined(MAGICKCORE_HAVE_NANOSLEEP)
1735  {
1736  struct timespec
1737  timer;
1738 
1739  timer.tv_sec=(time_t) (milliseconds/1000);
1740  timer.tv_nsec=(milliseconds % 1000)*1000*1000;
1741  (void) nanosleep(&timer,(struct timespec *) NULL);
1742  }
1743 #elif defined(MAGICKCORE_HAVE_USLEEP)
1744  usleep(1000*milliseconds);
1745 #elif defined(MAGICKCORE_HAVE_SELECT)
1746  {
1747  struct timeval
1748  timer;
1749 
1750  timer.tv_sec=(long) milliseconds/1000;
1751  timer.tv_usec=(long) (milliseconds % 1000)*1000;
1752  (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1753  }
1754 #elif defined(MAGICKCORE_HAVE_POLL)
1755  (void) poll((struct pollfd *) NULL,0,(int) milliseconds);
1756 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1757  Sleep((long) milliseconds);
1758 #elif defined(vms)
1759  {
1760  float
1761  timer;
1762 
1763  timer=milliseconds/1000.0;
1764  lib$wait(&timer);
1765  }
1766 #elif defined(__BEOS__)
1767  snooze(1000*milliseconds);
1768 #else
1769  {
1770  clock_t
1771  time_end;
1772 
1773  time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1774  while (clock() < time_end)
1775  {
1776  }
1777  }
1778 #endif
1779 }
1780 
1781 /*
1782 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1783 % %
1784 % %
1785 % %
1786 % M u l t i l i n e C e n s u s %
1787 % %
1788 % %
1789 % %
1790 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1791 %
1792 % MultilineCensus() returns the number of lines within a label. A line is
1793 % represented by a \n character.
1794 %
1795 % The format of the MultilineCenus method is:
1796 %
1797 % size_t MultilineCensus(const char *label)
1798 %
1799 % A description of each parameter follows.
1800 %
1801 % o label: This character string is the label.
1802 %
1803 */
1804 MagickExport size_t MultilineCensus(const char *label)
1805 {
1806  size_t
1807  number_lines;
1808 
1809  /*
1810  Determine the number of lines within this label.
1811  */
1812  if (label == (char *) NULL)
1813  return(0);
1814  for (number_lines=1; *label != '\0'; label++)
1815  if (*label == '\n')
1816  number_lines++;
1817  return(number_lines);
1818 }
1819 
1820 /*
1821 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1822 % %
1823 % %
1824 % %
1825 % S h r e d F i l e %
1826 % %
1827 % %
1828 % %
1829 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1830 %
1831 % ShredFile() overwrites the specified file with zeros or random data and then
1832 % removes it. The overwrite is optional and is only required to help keep
1833 % the contents of the file private. On the first pass, the file is zeroed.
1834 % For subsequent passes, random data is written.
1835 %
1836 % The format of the ShredFile method is:
1837 %
1838 % MagickBooleanType ShredFile(const char *path)
1839 %
1840 % A description of each parameter follows.
1841 %
1842 % o path: Specifies a path to a file.
1843 %
1844 */
1846 {
1847  char
1848  *passes;
1849 
1850  int
1851  file,
1852  status;
1853 
1855  length;
1856 
1857  ssize_t
1858  i;
1859 
1860  size_t
1861  quantum;
1862 
1863  struct stat
1864  file_stats;
1865 
1866  if ((path == (const char *) NULL) || (*path == '\0'))
1867  return(MagickFalse);
1868  passes=GetPolicyValue("system:shred");
1869  if (passes == (char *) NULL)
1870  passes=GetEnvironmentValue("MAGICK_SHRED_PASSES");
1871  if (passes == (char *) NULL)
1872  {
1873  /*
1874  Don't shred the file, just remove it.
1875  */
1876  status=remove_utf8(path);
1877  if (status == -1)
1878  {
1880  "Failed to remove: %s",path);
1881  return(MagickFalse);
1882  }
1883  return(MagickTrue);
1884  }
1885  file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
1886  if (file == -1)
1887  {
1888  /*
1889  Don't shred the file, just remove it.
1890  */
1891  passes=DestroyString(passes);
1892  status=remove_utf8(path);
1893  if (status == -1)
1895  "Failed to remove: %s",path);
1896  return(MagickFalse);
1897  }
1898  /*
1899  Shred the file.
1900  */
1901  quantum=(size_t) MagickMaxBufferExtent;
1902  if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1903  quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
1904  length=(MagickSizeType) file_stats.st_size;
1905  for (i=0; i < (ssize_t) StringToInteger(passes); i++)
1906  {
1907  RandomInfo
1908  *random_info;
1909 
1911  j;
1912 
1913  ssize_t
1914  count;
1915 
1916  if (lseek(file,0,SEEK_SET) < 0)
1917  break;
1919  for (j=0; j < (MagickOffsetType) length; j+=count)
1920  {
1921  StringInfo
1922  *key;
1923 
1924  key=GetRandomKey(random_info,quantum);
1925  if (i == 0)
1926  ResetStringInfo(key); /* zero on first pass */
1927  count=write(file,GetStringInfoDatum(key),(size_t)
1928  MagickMin((MagickSizeType) quantum,length-j));
1929  key=DestroyStringInfo(key);
1930  if (count <= 0)
1931  {
1932  count=0;
1933  if (errno != EINTR)
1934  break;
1935  }
1936  }
1938  if (j < (MagickOffsetType) length)
1939  break;
1940  }
1941  status=close(file);
1942  status=remove_utf8(path);
1943  if (status != -1)
1944  status=StringToInteger(passes);
1945  passes=DestroyString(passes);
1946  return((status == -1 || i < (ssize_t) status) ? MagickFalse : MagickTrue);
1947 }
#define lib
Definition: vms.h:763
MagickExport MagickBooleanType GetPathAttributes(const char *path, void *attributes)
Definition: utility.c:1173
MagickPrivate MagickBooleanType ShredFile(const char *path)
Definition: utility.c:1845
MagickExport size_t ConcatenateMagickString(char *magick_restrict destination, const char *magick_restrict source, const size_t length)
Definition: string.c:392
static const char Base64[]
Definition: utility.c:80
char d_name[255]
Definition: vms.h:944
#define IsBasenameSeparator(c)
Definition: studio.h:268
MagickExport ssize_t ParseCommandOption(const CommandOption option, const MagickBooleanType list, const char *options)
Definition: option.c:3055
#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:1609
#define MagickMaxBufferExtent
Definition: blob.h:25
#define S_ISDIR(mode)
Definition: studio.h:199
MagickPrivate MagickBooleanType IsMagickConflict(const char *)
MagickExport ExceptionInfo * AcquireExceptionInfo(void)
Definition: exception.c:115
MagickExport ssize_t FormatLocaleString(char *magick_restrict string, const size_t length, const char *magick_restrict format,...)
Definition: locale.c:463
MagickExport size_t CopyMagickString(char *magick_restrict destination, const char *magick_restrict source, const size_t length)
Definition: string.c:731
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:1457
#define O_BINARY
Definition: studio.h:330
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:965
ssize_t MagickOffsetType
Definition: magick-type.h:133
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:1537
MagickExport unsigned char * GetStringInfoDatum(const StringInfo *string_info)
Definition: string.c:1176
MagickExport size_t MultilineCensus(const char *label)
Definition: utility.c:1804
MagickExport MagickBooleanType IsGeometry(const char *geometry)
Definition: geometry.c:612
MagickExport void GetPathComponent(const char *path, PathType type, char *component)
Definition: utility.c:1221
MagickBooleanType
Definition: magick-type.h:165
#define DirectorySeparator
Definition: studio.h:264
MagickExport char * AcquireString(const char *source)
Definition: string.c:94
MagickExport int AcquireUniqueFileResource(char *path)
Definition: resource.c:551
MagickExport MagickBooleanType GlobExpression(const char *magick_restrict expression, const char *magick_restrict pattern, const MagickBooleanType case_insensitive)
Definition: token.c:352
static int remove_utf8(const char *path)
MagickExport StringInfo * DestroyStringInfo(StringInfo *string_info)
Definition: string.c:815
MagickExport void ResetStringInfo(StringInfo *string_info)
Definition: string.c:1553
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:665
Definition: vms.h:950
MagickExport MagickBooleanType RelinquishUniqueFileResource(const char *path)
Definition: resource.c:1098
MagickExport char * Base64Encode(const unsigned char *blob, const size_t blob_length, size_t *encode_length)
Definition: utility.c:501
MagickPrivate MagickBooleanType IsGlob(const char *) magick_attribute((__pure__))
Definition: token.c:594
size_t MagickSizeType
Definition: magick-type.h:134
#define MagickPathExtent
MagickExport void MagickDelay(const MagickSizeType milliseconds)
Definition: utility.c:1730
MagickExport MagickBooleanType IsEventLogging(void)
Definition: log.c:725
PathType
Definition: utility.h:25
MagickPrivate MagickBooleanType GetExecutionPath(char *path, const size_t extent)
Definition: utility.c:1003
MagickExport MagickBooleanType LogMagickEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition: log.c:1660
MagickExport MagickBooleanType IsPathAccessible(const char *path)
Definition: utility.c:1492
static int open_utf8(const char *path, int flags, mode_t mode)
MagickExport RandomInfo * AcquireRandomInfo(void)
Definition: random.c:163
MagickExport MagickBooleanType AcquireUniqueSymbolicLink(const char *source, char *destination)
Definition: utility.c:153
MagickExport char * GetEnvironmentValue(const char *name)
Definition: string.c:1143
#define S_MODE
Definition: studio.h:244
MagickPrivate char ** GetPathComponents(const char *path, size_t *number_components)
Definition: utility.c:1429
#define PATH_MAX
Definition: studio.h:334
MagickExport void AppendImageFormat(const char *format, char *filename)
Definition: utility.c:281
MagickExport int LocaleCompare(const char *p, const char *q)
Definition: locale.c:1399
#define GetMagickModule()
Definition: log.h:28
void closedir(DIR *)
MagickExport MagickBooleanType ExpandFilenames(int *number_arguments, char ***arguments)
Definition: utility.c:745
MagickExport char * DestroyString(char *string)
Definition: string.c:788
MagickExport void * AcquireMagickMemory(const size_t size)
Definition: memory.c:552
MagickPrivate ssize_t GetMagickPageSize(void)
Definition: utility.c:1132
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:37
MagickExport char * GetPolicyValue(const char *name)
Definition: policy.c:531
MagickExport MagickBooleanType ConcatenateString(char **magick_restrict destination, const char *magick_restrict source)
Definition: string.c:458
static RandomInfo * random_info
Definition: resource.c:113
MagickExport void * RelinquishMagickMemory(void *memory)
Definition: memory.c:1162
MagickPrivate void ChopPathComponents(char *path, const size_t components)
Definition: utility.c:584
#define CLOCKS_PER_SEC
Definition: timer.c:59
static int FileCompare(const void *x, const void *y)
Definition: utility.c:1594
#define MagickPrivate
#define MagickExport
MagickExport StringInfo * GetRandomKey(RandomInfo *random_info, const size_t length)
Definition: random.c:682
MagickExport char ** StringToArgv(const char *text, int *argc)
Definition: string.c:2021
MagickExport MagickBooleanType IsSceneGeometry(const char *geometry, const MagickBooleanType pedantic)
Definition: geometry.c:653
MagickExport char * ConstantString(const char *source)
Definition: string.c:678
MagickExport ExceptionInfo * DestroyExceptionInfo(ExceptionInfo *exception)
Definition: exception.c:418
#define S_ISREG(mode)
Definition: studio.h:202