Watcom C Library : fstat, ftell, ftime, _fullpath, fwrite
Watcom C Reference/D - E - F 2021. 6. 27. 18:14
Watcom C Library Reference : fstat, ftell, ftime, _fullpath, fwrite
fstat
Synopsis : #include <sys\types.h>
#include <sys\stat.h>
int fstat( int handle, struct stat *buf );
Description : The fstat function obtains information about an open file whose file handle is handle. This information is placed in the structure located at the address indicated by buf.
The file <sys\stat.h> contains definitions for the structure stat.
struct stat {
dev_t st_dev; /* disk drive file resides on */
ino_t st_ino; /* this inode's number */
unsigned short st_mode; /* file mode */
short st_nlink; /* # of hard links */
short st_uid; /* user-id, always 'root' */
short st-gid; /* group-id, always 'root' */
dev_t st_rdev; /* drive #, same as st_dev */
off_t st_size; /* total file size */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
At least the following macros are defined in the <sys\stat.h> header file.
Macro | Meaning |
S_ISFIFO(m) | Test for FIFO. |
S_ISCHR(m) | Test for character special file. |
S_ISDIR(m) | Test for directory file. |
S_ISBLK(m) | Test for block special file. |
S_ISREG(m) | Test for regular file. |
The value m supplied to the macros is the value of the st_mode field of a stat structure. The macro evaluates to a non-zero value if the test is true and zero if the test is false.
The following bits are encoded within the st_mode field of a stat structure.
Mask | Owner Permissions |
S_IRWXU | Read, write, search (if a directory), or execute (otherwise) |
S_IRUSR | Read permission bit |
S_IWUSR | Write permission bit |
S_IXUSR | Search/execute permission bit |
S_IREAD | == S_IRUSR (for Microsoft compatibility) |
S_IWRITE | == S_IWUSR (for Microsoft compatibility) |
S_IEXEC | == S_IXUSR (for Microsoft compatibility) |
S_IRWXU is the bitwise inclusive OR of S_IRUSR, S_IWUSR, and S_IXUSR.
Mask | Group Permissions (same as owner's on DOS, OS/2 or Window NT) |
S_IRWXG | Read, write, search (if a directory), or execute (otherwise) |
S_IRGRP | Read permission bit |
S_IWGRP | Write permission bit |
S_IXGRP | Search/execute permission bit |
S_IRWXG is the bitwise inclusive OR of S_IRGRP, S_IWGRP, and S_IXGRP.
Mask | Other Permissions (same as owner's on DOS, OS/2 or Windows NT) |
S_IRWXO | Read, write, search (if a directory), or execute (otherwise) |
S_IROTH | Read permission bit |
S_IWOTH | Write permission bit |
S_IXOTH | Search/execute permission bit |
S_IRWXO is the bitwise inclusive OR of S_IROTH, S_IWOTH, and S_IXOTH.
Mask | Meaning |
S_ISUID | (Not supported by DOS, OS/2 or Windows NT) Set user ID on execution. The process's effective user ID shall be set to that of the owner of the file when the file is run as a program. On a regular file, this bit should be cleared on any write. |
S_ISGID | (Not supported by DOS, OS/2 or Windows NT) Set group ID on execution. Set effective group ID on the process to the file's group when the file is run as a program. On a regular file, this bit should be cleared on any write. |
Returns : The fstat function returns zero when the information is successfully obtained. Otherwise, -1 is returned.
Errors : When an error has occurred, errno contains a value indicating the type of error that has been detected.
Constant | Meaning |
EBADF | The handle argument is not a valid file handle. |
See Also : creat, dup, dup2, open, sopen, stat
Example :
#include <stdio.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <fcntl.h>
#include <io.h>
void main( )
{
int handle, rc;
struct stat buf;
handle = open( "file", O_RDONLY );
if( handle != -1 ) {
rc = fstat ( handle, &buf );
if( rc != -1 )
printf( "File size = %d\n", buf.st_size );
close( handle );
}
}
Classification : POSIX 1003.1
Systems : All
ftell
Synopsis : #include <stdio.h>
long int ftell( FILE *fp );
Description : The ftell function returns the current read/write position of the file specified by fp. This position defines the character that will be read or written by the next I/O operation on the file. The value returned by ftell can be used in a subsequent call to fseek to set the file to the same position.
Returns : The ftell function returns the current read/write position of the file specified by fp. When an error is detected, -1L is returned. When an error has occurred, errno contains a value indicating the type of error that has been detected.
See Also : fgetpos, fopen, fsetpos, fseek
Example :
#include <stdio.h>
long int filesize( FILE *fp )
{
long int save_pos, size_of_file;
save_pos = ftell( fp );
fseek( fp, 0L, SEEK_END );
size_of_file = ftell( fp );
fseek ( fp, save_pos, SEEK_SET );
return( size_of_file );
}
void main( )
{
FILE *fp;
fp = fopen( "file", "r" );
if( fp != NULL) {
printf( "File size=%d\n", filesize( fp ) ) ;
fclose( fp );
}
}
Classification : ANSI
Systems : All
ftime
Synopsis : #include <sys\timeb.h>
int ftime( struct timeb *timeptr );
struct timeb {
time_t time; /* time in seconds since Jan 1, 1970 UTC */
unsigned short millitm; /* milliseconds */
short timezone; /* difference in minutes from UTC */
short dstflag; /* nonzero if in daylight savings time */
};
Description : The ftime function gets the current time and stores it in the structure pointed to by timeptr.
Returns : The ftime function fills in the fields of the structure pointed to by timeptr. The ftime function returns -1 if not successful, and a non-zero value otherwise.
See Also : asctime, clock, ctime, difftime, gmtime, localtime, mktime, strftime, time, tzset
Example :
#include <stdio.h>
#include <time.h>
#include <sys\timeb.h>
void main( )
{
struct timeb timebuf;
char *tod;
ftime( &timebuf );
tod = ctime ( &timebuf.time );
printf( "The time is %19s %hu %s", tod, timebuf.millitm, &tod[20] );
}
produces the following :
The time is Tue Dec 25 15:58:42.870 1999
Classification : WATCOM
Systems : All
_fullpath
Synopsis : #include <stdlib.h>
char *_fullpath( char *buffer, const char *path, size_t size );
Description : The _fullpath function returns the full pathname of the file specification in path in the specified buffer buffer of length size.
The maximum size that might be required for buffer is _MAX_PATH. If the buffer provide is too small, NULL is returned and errno is set.
If buffer is NULL then a buffer of size _MAX_PATH is allocated using malloc. This buffer may be freed using the free function.
If path is NULL or points to a null string ("") then the current working directory is returned in buffer.
Returns : The _fullpath function returns a pointer to the full path specification if no error occurred. Otherwise, NULL is returned.
Errors : When an error has occurred, errno contains a value indicating the type of error that has been detected.
Constant | Meaning |
ENOENT | The current working directory could not be obtained. |
ENOMEM | The buffer could not be allocated. |
ERANGE | The buffer passed was too small. |
See Also : _makepath, _splitpath
Example :
#include <stdio.h>
#include <stdlib.h>
void main( int argc, char *argv[ ] )
{
int i;
char buff[ PATH_MAX ];
for ( i = 1; i < argc; ++i ) {
puts ( argv[i] );
if( _fullpath( buff, argv[i], PATH_MAX ) ) {
puts ( buff );
} else {
puts ( "FAIL!" );
}
}
}
Classification : WATCOM
Systems : All
fwrite
Synopsis : #include <stdio.h>
size_t fwrite( const void *buf, size_t elsize, size_t nelem, FILE *fp );
Description : The fwrite function writes nelem elements of elsize bytes each to the file specified by fp.
Returns : The fwrite function returns the number of complete elements successfully written. This value will be less than the requested number of elements only if a write error occurs. When an error has occurred, errno contains a value indicating the type of error that has been detected.
See Also : ferror, fopen
Example :
#include <stdio.h>
struct student_data {
int student_id;
unsigned char marks[10]; };
};
void main( )
{
int i;
FILE *fp;
struct student_data std;
fp = fopen( "file", "w" );
if( fp != NULL ) {
std.student_id = 1001;
for( i = 0; i < 10; i++) {
std.marks[ i ] = ( unsigned char) (85 + i);
}
/* write student record with marks */
i = fwrite( &std, sizeof( std ), 1, fp );
printf( "%d record written\n", i );
fclose(fp);
}
}
Classification : ANSI
Systems : All
This manual describes the WATCOM C library for DOS, Windows, and OS/2, It includes the Standard C Library (as defined in the ANSI C Standard).
WATCOM C Language Reference manual describes the ANSI C Programming language and extensions to it which are supported by WATCOM C/C++ (32bit)