This source file includes following definitions.
- AcquireUniqueFilename
- AcquireUniqueSymbolicLink
- AppendImageFormat
- Base64Decode
- Base64Encode
- ChopPathComponents
- ExpandFilename
- ExpandFilenames
- GetExecutionPath
- GetMagickPageSize
- GetPathAttributes
- GetPathComponent
- GetPathComponents
- IsPathAccessible
- IsPathDirectory
- FileCompare
- MagickReadDirectory
- ListFiles
- MagickDelay
- MultilineCensus
- ShredFile
#include "magick/studio.h"
#include "magick/property.h"
#include "magick/blob.h"
#include "magick/color.h"
#include "magick/exception.h"
#include "magick/exception-private.h"
#include "magick/geometry.h"
#include "magick/image-private.h"
#include "magick/list.h"
#include "magick/log.h"
#include "magick/memory_.h"
#include "magick/nt-base-private.h"
#include "magick/option.h"
#include "magick/policy.h"
#include "magick/random_.h"
#include "magick/registry.h"
#include "magick/resource_.h"
#include "magick/semaphore.h"
#include "magick/signature-private.h"
#include "magick/statistic.h"
#include "magick/string_.h"
#include "magick/string-private.h"
#include "magick/token.h"
#include "magick/utility.h"
#include "magick/utility-private.h"
#if defined(MAGICKCORE_HAVE_PROCESS_H)
#include <process.h>
#endif
#if defined(MAGICKCORE_HAVE_MACH_O_DYLD_H)
#include <mach-o/dyld.h>
#endif
static const char
Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static int
IsPathDirectory(const char *);
MagickExport MagickBooleanType AcquireUniqueFilename(char *path)
{
int
file;
file=AcquireUniqueFileResource(path);
if (file == -1)
return(MagickFalse);
file=close(file)-1;
return(MagickTrue);
}
MagickExport MagickBooleanType AcquireUniqueSymbolicLink(const char *source,
char *destination)
{
int
destination_file,
source_file;
size_t
length,
quantum;
ssize_t
count;
struct stat
attributes;
unsigned char
*buffer;
assert(source != (const char *) NULL);
assert(destination != (char *) NULL);
#if defined(MAGICKCORE_HAVE_SYMLINK)
(void) AcquireUniqueFilename(destination);
(void) RelinquishUniqueFileResource(destination);
if (*source == *DirectorySeparator)
{
if (symlink(source,destination) == 0)
return(MagickTrue);
}
else
{
char
path[MaxTextExtent];
*path='\0';
if (getcwd(path,MaxTextExtent) == (char *) NULL)
return(MagickFalse);
(void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
(void) ConcatenateMagickString(path,source,MaxTextExtent);
if (symlink(path,destination) == 0)
return(MagickTrue);
}
#endif
destination_file=AcquireUniqueFileResource(destination);
if (destination_file == -1)
return(MagickFalse);
source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
if (source_file == -1)
{
(void) close(destination_file);
(void) RelinquishUniqueFileResource(destination);
return(MagickFalse);
}
quantum=(size_t) MagickMaxBufferExtent;
if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
quantum=(size_t) MagickMin(attributes.st_size,MagickMaxBufferExtent);
buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
if (buffer == (unsigned char *) NULL)
{
(void) close(source_file);
(void) close(destination_file);
(void) RelinquishUniqueFileResource(destination);
return(MagickFalse);
}
for (length=0; ; )
{
count=(ssize_t) read(source_file,buffer,quantum);
if (count <= 0)
break;
length=(size_t) count;
count=(ssize_t) write(destination_file,buffer,length);
if ((size_t) count != length)
{
(void) close(destination_file);
(void) close(source_file);
buffer=(unsigned char *) RelinquishMagickMemory(buffer);
(void) RelinquishUniqueFileResource(destination);
return(MagickFalse);
}
}
(void) close(destination_file);
(void) close(source_file);
buffer=(unsigned char *) RelinquishMagickMemory(buffer);
return(MagickTrue);
}
MagickExport void AppendImageFormat(const char *format,char *filename)
{
char
extension[MaxTextExtent],
root[MaxTextExtent];
assert(format != (char *) NULL);
assert(filename != (char *) NULL);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
if ((*format == '\0') || (*filename == '\0'))
return;
if (LocaleCompare(filename,"-") == 0)
{
char
message[MaxTextExtent];
(void) FormatLocaleString(message,MaxTextExtent,"%s:%s",format,filename);
(void) CopyMagickString(filename,message,MaxTextExtent);
return;
}
GetPathComponent(filename,ExtensionPath,extension);
if ((LocaleCompare(extension,"Z") == 0) ||
(LocaleCompare(extension,"bz2") == 0) ||
(LocaleCompare(extension,"gz") == 0) ||
(LocaleCompare(extension,"wmz") == 0) ||
(LocaleCompare(extension,"svgz") == 0))
{
GetPathComponent(filename,RootPath,root);
(void) CopyMagickString(filename,root,MaxTextExtent);
GetPathComponent(filename,RootPath,root);
(void) FormatLocaleString(filename,MaxTextExtent,"%s.%s.%s",root,format,
extension);
return;
}
GetPathComponent(filename,RootPath,root);
(void) FormatLocaleString(filename,MaxTextExtent,"%s.%s",root,format);
}
MagickExport unsigned char *Base64Decode(const char *source,size_t *length)
{
int
state;
register const char
*p,
*q;
register size_t
i;
unsigned char
*decode;
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
assert(source != (char *) NULL);
assert(length != (size_t *) NULL);
*length=0;
decode=(unsigned char *) AcquireQuantumMemory((strlen(source)+3)/4,
3*sizeof(*decode));
if (decode == (unsigned char *) NULL)
return((unsigned char *) NULL);
i=0;
state=0;
for (p=source; *p != '\0'; p++)
{
if (isspace((int) ((unsigned char) *p)) != 0)
continue;
if (*p == '=')
break;
q=strchr(Base64,*p);
if (q == (char *) NULL)
{
decode=(unsigned char *) RelinquishMagickMemory(decode);
return((unsigned char *) NULL);
}
switch (state)
{
case 0:
{
decode[i]=(q-Base64) << 2;
state++;
break;
}
case 1:
{
decode[i++]|=(q-Base64) >> 4;
decode[i]=((q-Base64) & 0x0f) << 4;
state++;
break;
}
case 2:
{
decode[i++]|=(q-Base64) >> 2;
decode[i]=((q-Base64) & 0x03) << 6;
state++;
break;
}
case 3:
{
decode[i++]|=(q-Base64);
state=0;
break;
}
}
}
if (*p != '=')
{
if (state != 0)
{
decode=(unsigned char *) RelinquishMagickMemory(decode);
return((unsigned char *) NULL);
}
}
else
{
p++;
switch (state)
{
case 0:
case 1:
{
decode=(unsigned char *) RelinquishMagickMemory(decode);
return((unsigned char *) NULL);
}
case 2:
{
for ( ; *p != '\0'; p++)
if (isspace((int) ((unsigned char) *p)) == 0)
break;
if (*p != '=')
{
decode=(unsigned char *) RelinquishMagickMemory(decode);
return((unsigned char *) NULL);
}
p++;
}
case 3:
{
for ( ; *p != '\0'; p++)
if (isspace((int) ((unsigned char) *p)) == 0)
{
decode=(unsigned char *) RelinquishMagickMemory(decode);
return((unsigned char *) NULL);
}
if ((int) decode[i] != 0)
{
decode=(unsigned char *) RelinquishMagickMemory(decode);
return((unsigned char *) NULL);
}
break;
}
}
}
*length=i;
return(decode);
}
MagickExport char *Base64Encode(const unsigned char *blob,
const size_t blob_length,size_t *encode_length)
{
char
*encode;
register const unsigned char
*p;
register size_t
i;
size_t
remainder;
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
assert(blob != (const unsigned char *) NULL);
assert(blob_length != 0);
assert(encode_length != (size_t *) NULL);
*encode_length=0;
encode=(char *) AcquireQuantumMemory(blob_length/3+4,4*sizeof(*encode));
if (encode == (char *) NULL)
return((char *) NULL);
i=0;
for (p=blob; p < (blob+blob_length-2); p+=3)
{
encode[i++]=Base64[(int) (*p >> 2)];
encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
}
remainder=blob_length % 3;
if (remainder != 0)
{
ssize_t
j;
unsigned char
code[3];
code[0]='\0';
code[1]='\0';
code[2]='\0';
for (j=0; j < (ssize_t) remainder; j++)
code[j]=(*p++);
encode[i++]=Base64[(int) (code[0] >> 2)];
encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
if (remainder == 1)
encode[i++]='=';
else
encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
encode[i++]='=';
}
*encode_length=i;
encode[i++]='\0';
return(encode);
}
MagickExport void ChopPathComponents(char *path,const size_t components)
{
register ssize_t
i;
for (i=0; i < (ssize_t) components; i++)
GetPathComponent(path,HeadPath,path);
}
MagickExport void ExpandFilename(char *path)
{
char
expand_path[MaxTextExtent];
if (path == (char *) NULL)
return;
if (*path != '~')
return;
(void) CopyMagickString(expand_path,path,MaxTextExtent);
if ((*(path+1) == *DirectorySeparator) || (*(path+1) == '\0'))
{
char
*home;
(void) CopyMagickString(expand_path,".",MaxTextExtent);
(void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
home=GetEnvironmentValue("HOME");
if (home == (char *) NULL)
home=GetEnvironmentValue("USERPROFILE");
if (home != (char *) NULL)
{
(void) CopyMagickString(expand_path,home,MaxTextExtent);
(void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
home=DestroyString(home);
}
}
else
{
#if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
char
username[MaxTextExtent];
register char
*p;
struct passwd
*entry;
(void) CopyMagickString(username,path+1,MaxTextExtent);
p=strchr(username,'/');
if (p != (char *) NULL)
*p='\0';
entry=getpwnam(username);
if (entry == (struct passwd *) NULL)
return;
(void) CopyMagickString(expand_path,entry->pw_dir,MaxTextExtent);
if (p != (char *) NULL)
{
(void) ConcatenateMagickString(expand_path,"/",MaxTextExtent);
(void) ConcatenateMagickString(expand_path,p+1,MaxTextExtent);
}
#endif
}
(void) CopyMagickString(path,expand_path,MaxTextExtent);
}
MagickExport MagickBooleanType ExpandFilenames(int *number_arguments,
char ***arguments)
{
char
home_directory[MaxTextExtent],
**vector;
register ssize_t
i,
j;
size_t
number_files;
ssize_t
count,
parameters;
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
assert(number_arguments != (int *) NULL);
assert(arguments != (char ***) NULL);
vector=(char **) AcquireQuantumMemory((size_t) (*number_arguments+1),
sizeof(*vector));
if (vector == (char **) NULL)
ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
*home_directory='\0';
count=0;
for (i=0; i < (ssize_t) *number_arguments; i++)
{
char
**filelist,
filename[MaxTextExtent],
magick[MaxTextExtent],
*option,
path[MaxTextExtent],
subimage[MaxTextExtent];
MagickBooleanType
destroy;
option=(*arguments)[i];
*magick='\0';
*path='\0';
*filename='\0';
*subimage='\0';
number_files=0;
vector[count++]=ConstantString(option);
destroy=MagickTrue;
parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
if (parameters > 0)
{
for (j=0; j < parameters; j++)
{
i++;
if (i == (ssize_t) *number_arguments)
break;
option=(*arguments)[i];
vector[count++]=ConstantString(option);
}
continue;
}
if ((*option == '"') || (*option == '\''))
continue;
GetPathComponent(option,TailPath,filename);
GetPathComponent(option,MagickPath,magick);
if ((LocaleCompare(magick,"CAPTION") == 0) ||
(LocaleCompare(magick,"LABEL") == 0) ||
(LocaleCompare(magick,"PANGO") == 0) ||
(LocaleCompare(magick,"VID") == 0))
continue;
if ((IsGlob(filename) == MagickFalse) && (*option != '@'))
continue;
if (*option != '@')
{
GetPathComponent(option,HeadPath,path);
GetPathComponent(option,SubimagePath,subimage);
ExpandFilename(path);
if (*home_directory == '\0')
getcwd_utf8(home_directory,MaxTextExtent-1);
filelist=ListFiles(*path == '\0' ? home_directory : path,filename,
&number_files);
}
else
{
char
*files;
ExceptionInfo
*exception;
int
length;
exception=AcquireExceptionInfo();
files=FileToString(option+1,~0UL,exception);
exception=DestroyExceptionInfo(exception);
if (files == (char *) NULL)
continue;
filelist=StringToArgv(files,&length);
if (filelist == (char **) NULL)
continue;
files=DestroyString(files);
filelist[0]=DestroyString(filelist[0]);
for (j=0; j < (ssize_t) (length-1); j++)
filelist[j]=filelist[j+1];
number_files=(size_t) length-1;
}
if (filelist == (char **) NULL)
continue;
for (j=0; j < (ssize_t) number_files; j++)
if (IsPathDirectory(filelist[j]) <= 0)
break;
if (j == (ssize_t) number_files)
{
for (j=0; j < (ssize_t) number_files; j++)
filelist[j]=DestroyString(filelist[j]);
filelist=(char **) RelinquishMagickMemory(filelist);
continue;
}
vector=(char **) ResizeQuantumMemory(vector,(size_t) *number_arguments+
count+number_files+1,sizeof(*vector));
if (vector == (char **) NULL)
{
for (j=0; j < (ssize_t) number_files; j++)
filelist[j]=DestroyString(filelist[j]);
filelist=(char **) RelinquishMagickMemory(filelist);
return(MagickFalse);
}
for (j=0; j < (ssize_t) number_files; j++)
{
option=filelist[j];
parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
if (parameters > 0)
{
ssize_t
k;
vector[count++]=ConstantString(option);
for (k=0; k < parameters; k++)
{
j++;
if (j == (ssize_t) number_files)
break;
option=filelist[j];
vector[count++]=ConstantString(option);
}
continue;
}
(void) CopyMagickString(filename,path,MaxTextExtent);
if (*path != '\0')
(void) ConcatenateMagickString(filename,DirectorySeparator,
MaxTextExtent);
if (filelist[j] != (char *) NULL)
(void) ConcatenateMagickString(filename,filelist[j],MaxTextExtent);
filelist[j]=DestroyString(filelist[j]);
if (strlen(filename) >= (MaxTextExtent-1))
ThrowFatalException(OptionFatalError,"FilenameTruncated");
if (IsPathDirectory(filename) <= 0)
{
char
path[MaxTextExtent];
*path='\0';
if (*magick != '\0')
{
(void) ConcatenateMagickString(path,magick,MaxTextExtent);
(void) ConcatenateMagickString(path,":",MaxTextExtent);
}
(void) ConcatenateMagickString(path,filename,MaxTextExtent);
if (*subimage != '\0')
{
(void) ConcatenateMagickString(path,"[",MaxTextExtent);
(void) ConcatenateMagickString(path,subimage,MaxTextExtent);
(void) ConcatenateMagickString(path,"]",MaxTextExtent);
}
if (strlen(path) >= (MaxTextExtent-1))
ThrowFatalException(OptionFatalError,"FilenameTruncated");
if (destroy != MagickFalse)
{
count--;
vector[count]=DestroyString(vector[count]);
destroy=MagickFalse;
}
vector[count++]=ConstantString(path);
}
}
filelist=(char **) RelinquishMagickMemory(filelist);
}
vector[count]=(char *) NULL;
if (IsEventLogging() != MagickFalse)
{
char
*command_line;
command_line=AcquireString(vector[0]);
for (i=1; i < count; i++)
{
(void) ConcatenateString(&command_line," {");
(void) ConcatenateString(&command_line,vector[i]);
(void) ConcatenateString(&command_line,"}");
}
(void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
"Command line: %s",command_line);
command_line=DestroyString(command_line);
}
*number_arguments=(int) count;
*arguments=vector;
return(MagickTrue);
}
MagickExport MagickBooleanType GetExecutionPath(char *path,const size_t extent)
{
char
*directory;
*path='\0';
directory=getcwd(path,(unsigned long) extent);
(void) directory;
#if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
{
char
link_path[MaxTextExtent],
execution_path[PATH_MAX+1];
ssize_t
count;
(void) FormatLocaleString(link_path,MaxTextExtent,"/proc/%.20g/exe",
(double) getpid());
count=readlink(link_path,execution_path,PATH_MAX);
if (count == -1)
{
(void) FormatLocaleString(link_path,MaxTextExtent,"/proc/%.20g/file",
(double) getpid());
count=readlink(link_path,execution_path,PATH_MAX);
}
if ((count > 0) && (count <= (ssize_t) PATH_MAX))
{
execution_path[count]='\0';
(void) CopyMagickString(path,execution_path,extent);
}
}
#endif
#if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
{
char
executable_path[PATH_MAX << 1],
execution_path[PATH_MAX+1];
uint32_t
length;
length=sizeof(executable_path);
if ((_NSGetExecutablePath(executable_path,&length) == 0) &&
(realpath(executable_path,execution_path) != (char *) NULL))
(void) CopyMagickString(path,execution_path,extent);
}
#endif
#if defined(MAGICKCORE_HAVE_GETEXECNAME)
{
const char
*execution_path;
execution_path=(const char *) getexecname();
if (execution_path != (const char *) NULL)
{
if (*execution_path != *DirectorySeparator)
(void) ConcatenateMagickString(path,DirectorySeparator,extent);
(void) ConcatenateMagickString(path,execution_path,extent);
}
}
#endif
#if defined(MAGICKCORE_WINDOWS_SUPPORT)
NTGetExecutionPath(path,extent);
#endif
#if defined(__GNU__)
{
char
*program_name,
*execution_path;
ssize_t
count;
count=0;
execution_path=(char *) NULL;
program_name=program_invocation_name;
if (*program_invocation_name != '/')
{
size_t
extent;
extent=strlen(directory)+strlen(program_name)+2;
program_name=AcquireQuantumMemory(extent,sizeof(*program_name));
if (program_name == (char *) NULL)
program_name=program_invocation_name;
else
count=FormatLocaleString(program_name,extent,"%s/%s",directory,
program_invocation_name);
}
if (count != -1)
{
execution_path=realpath(program_name,NULL);
if (execution_path != (char *) NULL)
(void) CopyMagickString(path,execution_path,extent);
}
if (program_name != program_invocation_name)
program_name=(char *) RelinquishMagickMemory(program_name);
execution_path=(char *) RelinquishMagickMemory(execution_path);
}
#endif
#if defined(__OpenBSD__)
{
extern char
*__progname;
(void) CopyMagickString(path,__progname,extent);
}
#endif
return(IsPathAccessible(path));
}
MagickExport ssize_t GetMagickPageSize(void)
{
static ssize_t
page_size = -1;
if (page_size > 0)
return(page_size);
#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
#else
#if defined(MAGICKCORE_HAVE_GETPAGESIZE)
page_size=(ssize_t) getpagesize();
#endif
#endif
if (page_size <= 0)
page_size=16384;
return(page_size);
}
MagickExport MagickBooleanType GetPathAttributes(const char *path,
void *attributes)
{
MagickBooleanType
status;
if (path == (const char *) NULL)
{
errno=EINVAL;
return(MagickFalse);
}
(void) ResetMagickMemory(attributes,0,sizeof(struct stat));
status=stat_utf8(path,(struct stat *) attributes) == 0 ? MagickTrue :
MagickFalse;
return(status);
}
MagickExport void GetPathComponent(const char *path,PathType type,
char *component)
{
char
magick[MaxTextExtent],
*q,
subimage[MaxTextExtent];
register char
*p;
assert(path != (const char *) NULL);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",path);
assert(component != (char *) NULL);
if (*path == '\0')
{
*component='\0';
return;
}
(void) CopyMagickString(component,path,MaxTextExtent);
*magick='\0';
#if defined(__OS2__)
if (path[1] != ":")
#endif
for (p=component; *p != '\0'; p++)
{
if ((*p == '%') && (*(p+1) == '['))
{
for (p++; (*p != ']') && (*p != '\0'); p++) ;
if (*p == '\0')
break;
}
if ((*p == ':') && (IsPathDirectory(path) < 0) &&
(IsPathAccessible(path) == MagickFalse))
{
(void) CopyMagickString(magick,component,(size_t) (p-component+1));
if (IsMagickConflict(magick) != MagickFalse)
*magick='\0';
else
for (q=component; *q != '\0'; q++)
*q=(*++p);
break;
}
}
*subimage='\0';
p=component;
if (*p != '\0')
p=component+strlen(component)-1;
if ((*p == ']') && (strchr(component,'[') != (char *) NULL) &&
(IsPathAccessible(path) == MagickFalse))
{
for (q=p-1; q > component; q--)
if (*q == '[')
break;
if (*q == '[')
{
(void) CopyMagickString(subimage,q+1,MaxTextExtent);
subimage[p-q-1]='\0';
if ((IsSceneGeometry(subimage,MagickFalse) == MagickFalse) &&
(IsGeometry(subimage) == MagickFalse))
*subimage='\0';
else
*q='\0';
}
}
p=component;
if (*p != '\0')
for (p=component+strlen(component)-1; p > component; p--)
if (IsBasenameSeparator(*p) != MagickFalse)
break;
switch (type)
{
case MagickPath:
{
(void) CopyMagickString(component,magick,MaxTextExtent);
break;
}
case RootPath:
{
for (p=component+(strlen(component)-1); p > component; p--)
{
if (IsBasenameSeparator(*p) != MagickFalse)
break;
if (*p == '.')
break;
}
if (*p == '.')
*p='\0';
break;
}
case HeadPath:
{
*p='\0';
break;
}
case TailPath:
{
if (IsBasenameSeparator(*p) != MagickFalse)
(void) CopyMagickMemory((unsigned char *) component,
(const unsigned char *) (p+1),strlen(p+1)+1);
break;
}
case BasePath:
{
if (IsBasenameSeparator(*p) != MagickFalse)
(void) CopyMagickString(component,p+1,MaxTextExtent);
for (p=component+(strlen(component)-1); p > component; p--)
if (*p == '.')
{
*p='\0';
break;
}
break;
}
case ExtensionPath:
{
if (IsBasenameSeparator(*p) != MagickFalse)
(void) CopyMagickString(component,p+1,MaxTextExtent);
p=component;
if (*p != '\0')
for (p=component+strlen(component)-1; p > component; p--)
if (*p == '.')
break;
*component='\0';
if (*p == '.')
(void) CopyMagickString(component,p+1,MaxTextExtent);
break;
}
case SubimagePath:
{
(void) CopyMagickString(component,subimage,MaxTextExtent);
break;
}
case CanonicalPath:
case UndefinedPath:
break;
}
}
MagickExport char **GetPathComponents(const char *path,
size_t *number_components)
{
char
**components;
register const char
*p,
*q;
register ssize_t
i;
if (path == (char *) NULL)
return((char **) NULL);
*number_components=1;
for (p=path; *p != '\0'; p++)
if (IsBasenameSeparator(*p))
(*number_components)++;
components=(char **) AcquireQuantumMemory((size_t) *number_components+1UL,
sizeof(*components));
if (components == (char **) NULL)
ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
p=path;
for (i=0; i < (ssize_t) *number_components; i++)
{
for (q=p; *q != '\0'; q++)
if (IsBasenameSeparator(*q))
break;
components[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
sizeof(**components));
if (components[i] == (char *) NULL)
ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
(void) CopyMagickString(components[i],p,(size_t) (q-p+1));
p=q+1;
}
components[i]=(char *) NULL;
return(components);
}
MagickExport MagickBooleanType IsPathAccessible(const char *path)
{
MagickBooleanType
status;
struct stat
attributes;
if ((path == (const char *) NULL) || (*path == '\0'))
return(MagickFalse);
if (strcmp(path,"-") == 0)
return(MagickTrue);
status=GetPathAttributes(path,&attributes);
if (status == MagickFalse)
return(status);
if (S_ISREG(attributes.st_mode) == 0)
return(MagickFalse);
if (access_utf8(path,F_OK) != 0)
return(MagickFalse);
return(MagickTrue);
}
static int IsPathDirectory(const char *path)
{
MagickBooleanType
status;
struct stat
attributes;
if ((path == (const char *) NULL) || (*path == '\0'))
return(MagickFalse);
status=GetPathAttributes(path,&attributes);
if (status == MagickFalse)
return(-1);
if (S_ISDIR(attributes.st_mode) == 0)
return(0);
return(1);
}
#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif
static int FileCompare(const void *x,const void *y)
{
register const char
**p,
**q;
p=(const char **) x;
q=(const char **) y;
return(LocaleCompare(*p,*q));
}
#if defined(__cplusplus) || defined(c_plusplus)
}
#endif
static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
struct dirent **result)
{
#if defined(MAGICKCORE_HAVE_READDIR_R)
return(readdir_r(directory,entry,result));
#else
(void) entry;
errno=0;
*result=readdir(directory);
return(errno);
#endif
}
MagickExport char **ListFiles(const char *directory,const char *pattern,
size_t *number_entries)
{
char
**filelist;
DIR
*current_directory;
struct dirent
*buffer,
*entry;
size_t
max_entries;
assert(directory != (const char *) NULL);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",directory);
assert(pattern != (const char *) NULL);
assert(number_entries != (size_t *) NULL);
*number_entries=0;
current_directory=opendir(directory);
if (current_directory == (DIR *) NULL)
return((char **) NULL);
max_entries=2048;
filelist=(char **) AcquireQuantumMemory((size_t) max_entries,
sizeof(*filelist));
if (filelist == (char **) NULL)
{
(void) closedir(current_directory);
return((char **) NULL);
}
buffer=(struct dirent *) AcquireMagickMemory(sizeof(*buffer)+FILENAME_MAX+1);
if (buffer == (struct dirent *) NULL)
ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
(entry != (struct dirent *) NULL))
{
if (*entry->d_name == '.')
continue;
if ((IsPathDirectory(entry->d_name) > 0) ||
#if defined(MAGICKCORE_WINDOWS_SUPPORT)
(GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
#else
(GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
#endif
{
if (*number_entries >= max_entries)
{
max_entries<<=1;
filelist=(char **) ResizeQuantumMemory(filelist,(size_t)
max_entries,sizeof(*filelist));
if (filelist == (char **) NULL)
break;
}
#if defined(vms)
{
register char
*p;
p=strchr(entry->d_name,';');
if (p)
*p='\0';
if (*number_entries > 0)
if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
continue;
}
#endif
filelist[*number_entries]=(char *) AcquireString(entry->d_name);
(*number_entries)++;
}
}
buffer=(struct dirent *) RelinquishMagickMemory(buffer);
(void) closedir(current_directory);
if (filelist == (char **) NULL)
return((char **) NULL);
qsort((void *) filelist,(size_t) *number_entries,sizeof(*filelist),
FileCompare);
return(filelist);
}
MagickExport void MagickDelay(const MagickSizeType milliseconds)
{
if (milliseconds == 0)
return;
#if defined(MAGICKCORE_HAVE_NANOSLEEP)
{
struct timespec
timer;
timer.tv_sec=(time_t) (milliseconds/1000);
timer.tv_nsec=(milliseconds % 1000)*1000*1000;
(void) nanosleep(&timer,(struct timespec *) NULL);
}
#elif defined(MAGICKCORE_HAVE_USLEEP)
usleep(1000*milliseconds);
#elif defined(MAGICKCORE_HAVE_SELECT)
{
struct timeval
timer;
timer.tv_sec=(long) milliseconds/1000;
timer.tv_usec=(long) (milliseconds % 1000)*1000;
(void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
}
#elif defined(MAGICKCORE_HAVE_POLL)
(void) poll((struct pollfd *) NULL,0,(int) milliseconds);
#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
Sleep((long) milliseconds);
#elif defined(vms)
{
float
timer;
timer=milliseconds/1000.0;
lib$wait(&timer);
}
#elif defined(__BEOS__)
snooze(1000*milliseconds);
#else
# error "Time delay method not defined."
#endif
}
MagickExport size_t MultilineCensus(const char *label)
{
size_t
number_lines;
if (label == (char *) NULL)
return(0);
for (number_lines=1; *label != '\0'; label++)
if (*label == '\n')
number_lines++;
return(number_lines);
}
MagickPrivate MagickBooleanType ShredFile(const char *path)
{
char
*passes;
int
file,
status;
MagickSizeType
length;
register ssize_t
i;
size_t
quantum;
struct stat
file_stats;
if ((path == (const char *) NULL) || (*path == '\0'))
return(MagickFalse);
passes=GetEnvironmentValue("MAGICK_SHRED_PASSES");
if (passes == (char *) NULL)
{
passes=DestroyString(passes);
status=remove_utf8(path);
if (status == -1)
{
(void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
"Failed to remove: %s",path);
return(MagickFalse);
}
return(MagickTrue);
}
file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
if (file == -1)
{
passes=DestroyString(passes);
status=remove_utf8(path);
if (status == -1)
(void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
"Failed to remove: %s",path);
return(MagickFalse);
}
quantum=(size_t) MagickMaxBufferExtent;
if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
length=(MagickSizeType) file_stats.st_size;
for (i=0; i < (ssize_t) StringToInteger(passes); i++)
{
RandomInfo
*random_info;
register MagickOffsetType
j;
ssize_t
count;
if (lseek(file,0,SEEK_SET) < 0)
break;
random_info=AcquireRandomInfo();
for (j=0; j < (MagickOffsetType) length; j+=count)
{
StringInfo
*key;
key=GetRandomKey(random_info,quantum);
if (i == 0)
ResetStringInfo(key);
count=write(file,GetStringInfoDatum(key),(size_t)
MagickMin(quantum,length-j));
key=DestroyStringInfo(key);
if (count <= 0)
{
count=0;
if (errno != EINTR)
break;
}
}
random_info=DestroyRandomInfo(random_info);
if (j < (MagickOffsetType) length)
break;
}
status=close(file);
status=remove_utf8(path);
if (status != -1)
status=StringToInteger(passes);
passes=DestroyString(passes);
return((status == -1 || i < (ssize_t) status) ? MagickFalse : MagickTrue);
}