📄 sflfile.c
字号:
strcpy (dest, src);
result = dest;
}
else
if (*ext == '.')
result = fixed_extension (dest, src, ext);
else
result = default_extension (dest, src, ext);
return (result);
}
/* ---------------------------------------------------------------------[<]-
Function: strip_file_path
Synopsis: Removes the leading path from the filename, if any path was
present. Returns name. The path can be specified using the local
operating system syntax; under MS-DOS, / and \ are interchangeable.
---------------------------------------------------------------------[>]-*/
char
*strip_file_path (
char *name)
{
char *path_end;
ASSERT (name);
path_end = strrchr (name, PATHEND); /* Find end of path, if any */
#if (defined (MSDOS_FILESYSTEM))
if (path_end == NULL)
path_end = strrchr (name, '/');
#endif
if (path_end != NULL)
memmove (name, path_end + 1, strlen (path_end));
return (name);
}
/* ---------------------------------------------------------------------[<]-
Function: strip_file_name
Synopsis: Returns the path for a fully-qualified filename. The path is
cleaned-up and resolved. The returned string is held in a static area
that should be copied directly after calling this function. The returned
path does not end in '/' unless that is the entire path. If the supplied
name contains no path, the returned path is ".".
---------------------------------------------------------------------[>]-*/
char
*strip_file_name (
char *name)
{
char *path_end;
ASSERT (name);
ASSERT (strlen (name) <= LINE_MAX);
strcpy (work_name, name);
path_end = strrchr (work_name, PATHEND);
#if (defined (MSDOS_FILESYSTEM))
if (path_end == NULL)
path_end = strrchr (work_name, '/');
#endif
if (path_end == NULL)
return (".");
else
{
path_end [1] = '\0';
return (clean_path (work_name));
}
}
/* ---------------------------------------------------------------------[<]-
Function: get_new_filename
Synopsis: Appends a numeric suffix (_001, _002,...) to the filename until
it is unique. Returns a freshly-allocated string containing the new
filename.
---------------------------------------------------------------------[>]-*/
char *
get_new_filename (
const char *filename)
{
char
suffix [8],
*new_name;
int
counter;
for (counter = 0; ; counter++)
{
sprintf (suffix, "_%03d", counter);
new_name = xstrcpy (NULL, filename, suffix, NULL);
if (!file_exists (new_name))
return (new_name);
else
mem_free (new_name);
}
return (NULL);
}
/* ---------------------------------------------------------------------[<]-
Function: file_is_readable
Synopsis: Returns TRUE if the current process can read the specified
file or directory. The filename may end in a slash (/ or \) only if
it is a directory.
---------------------------------------------------------------------[>]-*/
Bool
file_is_readable (
const char *filename)
{
ASSERT (filename);
if (file_is_directory (filename))
return ((file_mode (clean_path (filename)) & S_IREAD) != 0);
else
if (strlast (filename) == '/')
return (FALSE);
else
return ((file_mode (filename) & S_IREAD) != 0);
}
/* ---------------------------------------------------------------------[<]-
Function: file_is_writeable
Synopsis: Returns TRUE if the current process can write the specified
file or directory. The filename may end in a slash (/ or \) only if
it is a directory.
---------------------------------------------------------------------[>]-*/
Bool
file_is_writeable (
const char *filename)
{
ASSERT (filename);
if (file_is_directory (filename))
return ((file_mode (clean_path (filename)) & S_IWRITE) != 0);
else
if (strlast (filename) == '/')
return (FALSE);
else
return ((file_mode (filename) & S_IREAD) != 0);
}
/* ---------------------------------------------------------------------[<]-
Function: file_is_executable
Synopsis: Returns TRUE if the current process can execute the specified
file. Directories are _not_ considered to be executable.
Under DOS, Windows, appends ".com", ".exe", and ".bat" to the filename,
in that order, to build a possible executable filename. If this fails,
opens the file (if it exists) and examines the first few bytes of the
file: if these are "#!", or '/'*! or "MZ" then the file is assumed to
be executable. #! is a standard mechanism under Unix for indicating
executable files. Note that process_create() uses a compatible
mechanism to launch the correct interpreter for such 'executable'
scripts. NOTE: '/'*! is provided for REXX. [XXX]
Under OS/2 appends ".exe" and ".cmd" to the filename, in that order,
to be a possible executable filename. If this fails, it opens the
file (if it exists) and examines the first few bytes of the file: if
these are "#!" then the file is assumed to be executable. NOTE:
REXX scripts MUST be in files named script.cmd in order to be found.
BAT files are not considered, nor are COM files, since at present
process_create does not support launching DOS processes.
Under VMS, appends .exe and .com, in that order to build a possible
executable filename.
Does not search the PATH symbol; the filename must be specified with a
path if necessary.
---------------------------------------------------------------------[>]-*/
Bool
file_is_executable (
const char *filename)
{
#if (defined (__UNIX__))
ASSERT (filename);
return ((file_mode (filename) & S_IEXEC) != 0
&& (file_mode (filename) & S_IFDIR) == 0);
#elif (defined (MSDOS_FILESYSTEM))
Bool
executable; /* Return code */
FILE
*stream; /* Opened file stream */
char
input_char = 0, /* First and second bytes of file */
*extension; /* File extension, if any */
ASSERT (filename);
/* Find file extension; if not found, set extension to empty string */
extension = strrchr (filename, '.');
if (extension == NULL
|| strchr (extension, '/') /* If last '.' is part of the path */
|| strchr (extension, '\\')) /* then the filename has no ext. */
extension = "";
/* Windows: If extension is .exe/.com/.bat, the file is an executable */
/* OS/2: If the extension is .exe/.cmd, the file is an executable */
#if (defined ( __OS2__))
if (lexcmp (extension, ".exe") == 0
|| lexcmp (extension, ".cmd") == 0)
#else /* DOS, WINDOWS */
if (lexcmp (extension, ".com") == 0
|| lexcmp (extension, ".exe") == 0
|| lexcmp (extension, ".bat") == 0)
#endif
executable = file_exists (filename);
else
/* Windows: If the extension is empty, try .com, .exe, .bat */
/* OS/2: If the extension is empty, try .exe, .cmd */
if (strnull (extension)
#if (defined( __OS2__))
&& (file_exists (default_extension (work_name, filename, "exe"))
|| file_exists (default_extension (work_name, filename, "cmd"))))
#else /* DOS, WINDOWS */
&& (file_exists (default_extension (work_name, filename, "com"))
|| file_exists (default_extension (work_name, filename, "exe"))
|| file_exists (default_extension (work_name, filename, "bat"))))
#endif
executable = TRUE; /* Executable file found */
else
{
/* Look for magic header at start of file */
stream = file_open (filename, 'r');
if (stream)
{
input_char = fgetc (stream);
executable = ((input_char == '#' && fgetc (stream) == '!')
# if (defined (__WINDOWS__))
|| (input_char == '/' && fgetc (stream) == '*'
&& fgetc (stream) == '!')
|| (input_char == 'M' && fgetc (stream) == 'Z')
# endif
);
file_close (stream);
}
else
executable = FALSE;
}
return (executable);
#elif (defined (__VMS__))
Bool
executable; /* Return code */
char
*extension; /* File extension, if any */
ASSERT (filename);
/* Find file extension, if any */
extension = strrchr (filename, '.');
if ((file_mode (filename) & S_IEXEC) != 0)
executable = TRUE;
else
/* If the extension is empty, try .exe and .com */
if (!extension)
{
default_extension (work_name, filename, "exe");
if ((file_mode (work_name) & S_IEXEC) != 0)
executable = TRUE;
else
{
default_extension (work_name, filename, "com");
if ((file_mode (work_name) & S_IEXEC) != 0)
executable = TRUE;
else
executable = FALSE;
}
}
else
executable = FALSE;
return (executable);
#else
return (FALSE); /* Not supported on this system */
#endif
}
/* ---------------------------------------------------------------------[<]-
Function: file_is_program
Synopsis: Returns TRUE if the specified filename is an executable
program on the PATH.
Under DOS, and Windows, appends ".exe", ".com" to the file, in that
order, to build an executable filename, then searches the PATH
definition for the executable filename. Under OS/2, appends ".exe"
to the file to build an executable filename, then searches the PATH
definition for the executable filename. If the filename already has
a path specifier, will not use the PATH definition. Under VMS,
appends "exe" and "com" to the file, in that order, to build an
executable filename. Searches the PATH if necessary.
---------------------------------------------------------------------[>]-*/
Bool
file_is_program (
const char *filename)
{
Bool
executable = FALSE; /* Return code */
#if (defined (__UNIX__))
char
*found_file;
ASSERT (filename);
found_file = file_where ('r', "PATH", filename, "");
if (found_file && (file_mode (found_file) & S_IEXEC))
executable = TRUE; /* Executable file found */
#elif (defined (__VMS__))
char
*found_file;
ASSERT (filename);
found_file = file_where ('r', "PATH", filename, "");
if (!found_file)
found_file = file_where ('r', "PATH", filename, ".exe");
if (!found_file)
found_file = file_where ('r', "PATH", filename, ".com");
if (found_file && (file_mode (found_file) & S_IEXEC))
executable = TRUE; /* Executable file found */
#elif (defined (MSDOS_FILESYSTEM))
char
*path; /* What path do we search? */
ASSERT (filename);
/* If the filename already contains a path, don't look at PATH */
if (strchr (filename, '/') || strchr (filename, '\\'))
path = NULL;
else
path = "PATH";
# if (defined (__WINDOWS__))
if (file_where ('r', path, filename, ".exe")
|| file_where ('r', path, filename, ".com")
|| file_where ('r', path, filename, ".bat"))
executable = TRUE; /* Executable file found */
# else /* OS/2 */
if (file_where ('r', path, filename, ".exe"))
executable = TRUE;
# endif
#endif
return (executable);
}
/* ---------------------------------------------------------------------[<]-
Function: file_is_directory
Synopsis: Returns TRUE if the specified file is a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -