📄 fal.c
字号:
/***************************************************************************
* Copyright (c) 1993 - 2001 Accelerated Technology, Inc.
*
* PROPRIETARY RIGHTS of Accelerated Technology are involved in the subject
* matter of this material. All manufacturing, reproduction, use and sales
* rights pertaining to this subject matter are governed by the license
* agreement. The recipient of this software implicity accepts the terms
* of the license.
*
****************************************************************************/
/****************************************************************************
*
* FILENAME VERSION
*
* FAL 1.5
*
* DESCRIPTION
*
* This file contains the File Abstraction Layer functions.
* The FAL currently supports Nucleus File, Nucleus File 2,
* Windows NT File System, the In-Memory File System, or you can add your
* own file system. All Nucleus Net protocols which require a file system
* are dependent on the File Abstraction Layer. The user can choose which
* file system to use by modifying the macros in FAL.H to include the
* preferred file system. The time functions have been implemented for the
* NT File System only. The user will have to write their own time functions
* for Nucleus File, the In-Memory File System or their own file system.
*
* DATA STRUCTURES
*
* None
*
* FUNCTIONS
*
* FAL_Open
* FAL_Fprintf
* FAL_Fclose
* FAL_Seek
* FAL_Find_Next
* FAL_Find_First
* FAL_Remove
* FAL_Find_Close
* FAL_Fwrite
* FAL_Fread
* FAL_Mk_Dir
* FAL_Rmv_Dir
* FAL_Is_Dir
* FAL_Set_Curr_Dir
* FAL_Current_Dir
* FAL_Rename_File
* FAL_Time
* FAL_Ctime
* FAL_Local_Time
* FAL_Store_Name
* FAL_Sys_Init
* FAL_Object_File_Length
* FAL_Handle_File_Length
* FAL_Disk_Space
* FAL_Get_Curr_Drive
* FAL_Set_Curr_Drive
* FAL_Open_Disk
* FAL_Close_Disk
* FAL_Release_File_User
* FAL_Become_File_User
* FAL_Get_Attribute
* FAL_Get_Last_Error
* FAL_Object_Mod_Time
*
* DEPENDENCIES
*
* fal.h
* nucleus.h
*
***************************************************************************/
/* File abstraction layer functions. */
#include "fal/inc/fal.h"
extern STATUS ramdisk_init(UINT8 *path);
INT Default_Drive;
#if NUCLEUS_FILE2_INCLUDED
extern STATUS file_init(VOID);
#endif
CHAR FAL_Month[12][5] = {"Jan ", "Feb ", "Mar ", "Apr ", "May ", "Jun ", "Jul ",
"Aug ", "Sep ", "Oct ", "Nov ", "Dec "};
/* Supports STDIO open, Nucleus File NU_Open and Nucleus IMF_Open */
/************************************************************************
*
* FUNCTION
*
* FAL_Open
*
* DESCRIPTION
*
* File open functions. This function is used to open a file and
* return the file descriptor.
*
* SUPPORTED BY
*
* Nucleus File
* NT File System
* In-Memory File System
*
* INPUTS
*
* *path Name and path of the file to open.
* flag File access flags.
* PO_RDONLY
* PO_WRONLY
* PO_RDWR
* PO_APPEND
* PO_CREAT
* PO_TRUNC
* PO_EXCL
* PO_TEXT
* PO_BINARY
* PO_NOSHAREANY
* PO_NOSHAREWRITE
* mode The mode: either read or write.
* FAL_IWRITE
* FAL_IREAD
*
* OUTPUTS
*
* Number of Bytes Written
*
************************************************************************/
FAL_FILE FAL_Open(CHAR * path, UINT16 flag, UINT16 mode)
{
/* Remove Warnings */
#if IMF_INCLUDED
UNUSED_PARAMETER(mode);
#endif
#if NUCLEUS_FILE_INCLUDED
return (NU_Open(path, flag, mode));
#endif
#if NUCLEUS_FILE2_INCLUDED
return (NU_Open(path, flag, mode));
#endif
#if NT_FILE_SYSTEM
return (_open((CONST CHAR*)path, flag, mode));
#endif
#if IMF_INCLUDED
return (IMF_Open(path, (INT16)flag));
#endif
}
/************************************************************************
*
* FUNCTION
*
* FAL_Fprintf
*
* DESCRIPTION
*
* Writes a string to the file. A sprintf function must be
* on the file before calling the function.
*
* SUPPORTED BY
*
* Nucleus File
* NT File System
* In-Memory File System
*
* INPUTS
*
* file The file descriptor associated with the file to
* write to.
* *string A pointer to the buffer of data to write.
*
* OUTPUTS
*
* The Number of Bytes Written
*
************************************************************************/
INT32 FAL_Fprintf(FAL_FILE file, CHAR *string)
{
INT length;
/* Determine the length of the data to be written, then write that
* amount of data
*/
#if NUCLEUS_FILE_INCLUDED
length = strlen (string);
return ((INT32)NU_Write (file, (UINT8 *)string, (UINT16)length));
#endif
#if NUCLEUS_FILE2_INCLUDED
length = strlen (string);
return (NU_Write (file, string, (INT32)length));
#endif
#if NT_FILE_SYSTEM
length = strlen (string);
return (_write (file, (CONST VOID *)string, (UINT32)length));
#endif
#if IMF_INCLUDED
length = strlen (string);
return (IMF_Write ((CHAR *)string, length, file));
#endif
}
/************************************************************************
*
* FUNCTION
*
* FAL_Fclose
*
* DESCRIPTION
*
* Closes a file with a particular file descriptor. The iotype
* parameter is used to write an EOF(-1) to the end of the file.
* The iotype should be set a 1 when writing an EOF and a 0 when the
* file is to be only closed. Note only set iotype to a 1 when you
* are writing data to a function.
*
* SUPPORTED BY
*
* Nucleus File
* NT File System
* In-Memory File System
*
* INPUTS
*
* file File descriptor associated with the file to be
* closed.
* iotype Flag to indicate whether you want to write an EOF
* at the end of the file - only supported by
* Nucleus File. 1 indicates to write an EOF.
* 0 indicates do not write an EOF.
*
* OUTPUTS
*
* If Successful - NU_SUCCESS
* If Unsuccessful - -1
*
************************************************************************/
INT FAL_Fclose(FAL_FILE file, INT iotype)
{
UINT8 msg = 0;
/* Remove Warnings */
#if (NT_FILE_SYSTEM || IMF_INCLUDED)
UNUSED_PARAMETER(msg);
UNUSED_PARAMETER(iotype);
#endif
#if NUCLEUS_FILE_INCLUDED
if (iotype)
NU_Write (file, &msg, 1);
return (NU_Close (file));
#endif
#if NUCLEUS_FILE2_INCLUDED
if (iotype)
NU_Write (file, (CHAR *)&msg, 1);
if (NU_Close (file) != NU_SUCCESS)
return (-1);
else
return (NU_SUCCESS);
#endif
#if NT_FILE_SYSTEM
return (_close (file));
#endif
#if IMF_INCLUDED
return (IMF_Close (file));
#endif
}
/************************************************************************
*
* FUNCTION
*
* FAL_Seek
*
* DESCRIPTION
*
* This function will move the file pointer offset bytes from the
* origin specified.
* Upon successful completion of this service, the return value will
* be the new offset. If the service was unsuccessful then a -1
* will be returned.
*
* SUPPORTED BY
*
* Nucleus File
* NT File System
* In-Memory File System
*
* INPUTS
*
* file File descriptor of the file to seek into.
* offset Byte offset to move the pointer.
* origin Initial position from which the offset should be
* calculated.
* PSEEK_SET Beginning of File
* PSEEK_CUR Current Position of File Pointer
* PSEEK_END End of File
*
* OUTPUTS
*
* If Successful - The new byte offset
* If Unsuccessful - -1
*
************************************************************************/
INT32 FAL_Seek(FAL_FILE file, INT32 offset, INT16 origin)
{
#if (NUCLEUS_FILE_INCLUDED || NUCLEUS_FILE2_INCLUDED)
return (NU_Seek (file, offset, origin));
#endif
#if NT_FILE_SYSTEM
return (_lseek (file, offset, origin));
#endif
#if IMF_INCLUDED
return (IMF_Seek (file, offset, origin));
#endif
}
/************************************************************************
*
* FUNCTION
*
* FAL_Find_Next
*
* DESCRIPTION
*
* Function is used to find a particular file of a specific sequence
* in a subdirectory. Upon successful completion, the statobj is
* filled in with the 'next' file's information.
*
* SUPPORTED BY
*
* Nucleus File
* NT File System
*
* INPUTS
*
* *statobj Pointer to the data structure that will be filled
* in with the next file's information.
* hFile Search handle returned by a previous call to
* FAL_Find_First.
*
* OUTPUTS
*
* If Successful - NU_SUCCESS
* If Unsuccessful - -1
*
************************************************************************/
INT FAL_Find_Next(FAL_DIR *statobj, INT32 hFile)
{
/* Remove Warnings */
#if (IMF_INCLUDED || NUCLEUS_FILE_INCLUDED || NUCLEUS_FILE2_INCLUDED)
UNUSED_PARAMETER(hFile);
#if IMF_INCLUDED
UNUSED_PARAMETER(statobj);
#endif
#endif
#if NUCLEUS_FILE_INCLUDED
if (NU_Get_Next (statobj) == 0)
return (-1);
else
return (NU_SUCCESS);
#endif
#if NUCLEUS_FILE2_INCLUDED
if (NU_Get_Next (statobj) != NU_SUCCESS)
return (-1);
else
return (NU_SUCCESS);
#endif
#if NT_FILE_SYSTEM
return ((INT32)_findnext ((INT32)hFile, statobj));
#endif
#if IMF_INCLUDED
p_errno = NU_UNAVAILABLE;
return (-1);
#endif
}
/************************************************************************
*
* FUNCTION
*
* FAL_Find_First
*
* DESCRIPTION
*
* Finds the first file in a subdirectory of a particular sequence.
* Upon succesful completion, statobj is filled in with the file's
* information.
*
* SUPPORTED BY
*
* Nucleus File
* NT File System
*
* INPUTS
*
* *path The path and sequence to search.
* *statobj Pointer to the data structure that will be filled
* in with the file's information.
* *attrib Search handle that can be used in subsequent
* calls to FAL_Find_Next or FAL_Find_Close.
* OUTPUTS
*
* If Successful - NU_SUCCESS
* If Unsuccessful - -1
*
************************************************************************/
INT FAL_Find_First(CHAR *path, FAL_DIR *statobj, INT32 *attrib)
{
/* Remove Warnings */
#if (IMF_INCLUDED || NUCLEUS_FILE_INCLUDED || NUCLEUS_FILE2_INCLUDED)
UNUSED_PARAMETER(attrib);
#if IMF_INCLUDED
UNUSED_PARAMETER(statobj);
UNUSED_PARAMETER(path);
#endif
#endif
#if NUCLEUS_FILE_INCLUDED
if (NU_Get_First ((DSTAT *)statobj, path) == 0)
return (-1);
else
return (NU_SUCCESS);
#endif
#if NUCLEUS_FILE2_INCLUDED
if (NU_Get_First (statobj, path) != NU_SUCCESS)
return (-1);
else
{
if (statobj->fattribute & AVOLUME)
{
if (FAL_Find_Next(statobj, 0) != NU_SUCCESS)
return (-1);
}
return (NU_SUCCESS);
}
#endif
#if NT_FILE_SYSTEM
if ((*attrib = _findfirst ((CHAR*)path, (FAL_DIR*)statobj)) < 0)
return (-1);
else
return (NU_SUCCESS);
#endif
#if IMF_INCLUDED
p_errno = NU_UNAVAILABLE;
return (-1);
#endif
}
/************************************************************************
*
* FUNCTION
*
* FAL_Remove
*
* DESCRIPTION
*
* Removes the file that is referenced in the given path.
*
* SUPPORTED BY
*
* Nucleus File
* NT File System
* In-Memory File System
*
* INPUTS
*
* *name Name of the file to remove.
*
* OUTPUTS
*
* If Successful - NU_SUCCESS
* If Unsuccessful - -1
*
************************************************************************/
INT FAL_Remove(CHAR *name)
{
#if NUCLEUS_FILE_INCLUDED
if (NU_Delete(name) == 0)
return (-1);
else
return (NU_SUCCESS);
#endif
#if NUCLEUS_FILE2_INCLUDED
if (NU_Delete(name) != NU_SUCCESS)
return (-1);
else
return (NU_SUCCESS);
#endif
#if NT_FILE_SYSTEM
return (remove ((CONST CHAR*)name));
#endif
#if IMF_INCLUDED
return (IMF_Delete ((CHAR *)name));
#endif
}
/************************************************************************
*
* FUNCTION
*
* FAL_Find_Close
*
* DESCRIPTION
*
* Function used to free all external and internal resources used
* with the FAL_findfirst and FAL_findnext functions.
*
* SUPPORTED BY
*
* Nucleus File
* NT File System
*
* INPUTS
*
* hFile Search handle returned by a previous call to
* FAL_Find_First.
* *statobj The file structure set up by a previous call to
* FAL_Find_First.
*
* OUTPUTS
*
* If Successful - NU_SUCCESS
* If Unsuccessful - -1
*
************************************************************************/
INT FAL_Find_Close(INT32 hfile, FAL_DIR *statobj)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -