int stat(char *FileName,struct stat *StatusBuffer);
stat
call retrieves information about the
named file into the StatusBuffer
struct stat
is declared in
sys/stat.h
:
struct stat { dev_t st_dev; /* ID of device containing */ /* a directory entry for this file*/ ino_t st_ino; /* File inode number */ ushort st_mode; /* File mode */ short st_nlink; /* Number of links */ ushort st_uid; /* User ID of the file's owner */ ushort st_gid; /* Group ID of the file's group */ dev_t st_rdev; /* ID of device - only for */ /* character or block special */ off_t st_size; /* File size in bytes */ time_t st_atime; /* Time of last access */ time_t st_mtime; /* Time of last data modification */ time_t st_ctime; /* Time of last file status change*/ /* Time measured in seconds since */ /* 00:00:00 GMT, Jan. 1, 1970 */ };
int fstat(int Descriptor,struct stat *StatusBuffer);
fstat
performs the same function as
stat
on an open file whos file descriptor is known.
int mkdir(const char *Path, mode_t Mode);
mkdir
creates the directory Path
with permissions Mode
DIR * opendir(const char *Directory);
opendir
opens the Directory
and returns a pointer to a directory stream.
NULL
on error
struct dirent * readdir(DIR *DirectoryStream);
readdir
returns a pointer to the next
directory entry
struct dirent
is defined in
sys/dirent.h
:
struct dirent {
int d_fileno; /* file number of entry */
int d_reclen; /* length of this record */
int d_type; /* file type */
int d_namlen; /* length of string in d_name */
char d_name[255 + 1]; /* name must be no longer than this */
};
NULL
on eof or error
int closedir(DIR *DirectoryStream);
closedir
closes the directory stream
int utimes(char *Filename,struct timeval *Times);
utimes
sets the access and modification times
of a file
struct timeval
is defined in
sys/time.h
struct timeval {
long tv_sec; /* seconds since Jan. 1, 1970 */
long tv_usec; /* and microseconds */
};
Times
is NULL
then the current
time is used.
int chmod(char *Path,int Mode);
chmod
changes the permission, sticky and
set-ID bits of Path
Mode
is created from or'd permission bit masks
defined in sys/stat.h
:
#define S_IRWXU 0000700 /* RWX mask for owner */
#define S_IRUSR 0000400 /* R for owner */
#define S_IWUSR 0000200 /* W for owner */
#define S_IXUSR 0000100 /* X for owner */
#define S_IRWXG 0000070 /* RWX mask for group */
#define S_IRGRP 0000040 /* R for group */
#define S_IWGRP 0000020 /* W for group */
#define S_IXGRP 0000010 /* X for group */
#define S_IRWXO 0000007 /* RWX mask for other */
#define S_IROTH 0000004 /* R for other */
#define S_IWOTH 0000002 /* W for other */
#define S_IXOTH 0000001 /* X for other */
#define S_ISUID 0004000 /* set user id on execution */
#define S_ISGID 0002000 /* set group id on execution */
#define S_ISVTX 0001000 /* sticky bit */
int chown(char *Path,int Owner,int Group);
chown
changes the owner UID and GID of a file
int link(char *OldName,char *NewName);
link
creates a hard link between the existing
file OldName
and the NewName
int unlink(char *Pathname);
unlink
removes a directory entry.
Descriptor = open("temp_file",MODE);
unlink("temp_file");
. . . . .
/*----processing that may crash */
. . . . .
close(Descriptor);
int symlink(const char *Target, const char *LinkPath);
int symlink
creates a symbolic link from the
LinkPath
to the Target
.
prompt> mknod MyPipeName p
or (FreeBSD)
prompt> mkfifo MyPipeName
or write a program using the same named system calls.
FIFORead.c
- Program that reads from a named pipe
CSC322 STOPS HERE
sys/param.h
as BSIZE, and is
typically 512 or 1024 bytes.
sync
call can be used to
schedule the output of buffers.
void sync(void);
sync
does not actually write out the buffers, merely
requests the hardware to do so, and it will as soon as possible.
int open(char *Pathname,int OpenFlag[,int AccessPerms]);
open
returns a file descriptor, -1 on error
OpenFlag
indicates whether the file is to
opened for reading, writing or updating.
It is created by taking the inclusive-OR of a number of
possible values defined in
file /usr/include/fcntl.h
O_RDONLY
- open for reading
O_WRONLY
- open for writing
O_RDWR
- open for reading and writing
Exactly one of these must be used in combination with
any of the others.
O_NDELAY
- if the file is being opened as
a pipe then opening for reading will return immediately
and opening for writing will return an error if no process
has the file open for reading.
If the file is being opened as a communications line then
the file will open without waiting for a carrier.
O_APPEND
- sets the file pointer to the
end of the file when opening for writing.
O_SYNC
- sets synchronous write option
O_CREAT
- if the file already exists the
O_CREAT
has no effect, but if it does not
exist it is created with the access permissions being
passed in the optional 3rd parameter.
O_TRUNC
- if the file already exists then
its length is set back to 0.
O_EXCL
- if O_CREAT
and
O_EXCL
are both set then the open
call will fail if the file already exists. This prevents
copies of a file being overwritten.
int write(int Descriptor,char *Buffer,int NumberOfBytes);
write
writes NumberOfBytes
bytes to
the Descriptor
from the Buffer
int read(int Descriptor,char *Buffer,int NumberOfBytes);
read
reads NumberOfBytes
bytes from
the Descriptor
into the Buffer
lseek(int Descriptor,long Offset,int Origin);
lseek
moves the file pointer of the
Descriptor
to Offset
Origin
.
Origin
can be 0, 1 or 2 indicating that the
offset is relative to the beginning, current position or end of
the file respectively.
fseek
call handling is implemented
close(int Descriptor);
close
breaks the link between the
Descriptor
and the file.
fork
due to the data structure.
int pipe(int Descriptors[2]);
creates a bidirectional pipe
Descriptors[1]
and read from
Descriptors[0]
-u uid
: Only list files owned by uid
-g uid
: Only list files in the group gid
-m days
: Only list files which have been modified
in the last days
days.
-a days
: Only list files which have been accessed
in the last days
days.
-s size
: Only list files that are larger than
size
bytes.
-r
: List subdirectories recursively.
int read(int Descriptor,char *Buffer,int NumberOfBytes);and the write system call is declared as:
int write(int Descriptor,char *Buffer,int NumberOfBytes);Complete the internals of the following function, which must copy data from the ReadDescriptor to the WriteDescriptor until end-of-file on the ReadDescriptor, and then close both descriptors. Both descriptors are already open when passed into the function.
void CopyData(int ReadDescriptor,int WriteDescriptor) { }
int link(char *OldName,char *NewName);and the unlink system call is declared as:
int unlink(char *Pathname);Write a function that takes two filenames as string parameters, and renames the first named file to the second name.