File Apisunix
File Apisunix
Arguments:
Fdesc: is a file descriptor that refers to an opened file
Return: 0 if the call succeeds
-1 if the call fails
fcntl
Objective: fcntl function helps a user to query or set access control flags and the close on exec flag of any
file descriptor
Users can also use fcntl to assign multiple file descriptors to reference the same file
Prototype: #include<sys/types.h>
#include<fcntl.h>
int fcntl(int fdesc, int cmd, );
Arguments:
Fdesc: is a file descriptor that refers to an opened file
Cmd:it specifies which operation to perform on a file referenced by fdesc.
3rd argument depends on the 2nd argument
CMD:
F_GETFL: returns the access control flags
F_SETFL: sets or clears access control flags
F_GETFD: returns the close on exec flag (0-off/nonzero-on)
F_SETFD: set or clears the close-on exec flag(0-off/nonzero-on)
F_DUPFD: duplicates the file descriptor fdesc with another file descriptor.
Return: 0 if the call succeeds
-1 if the call fails
Lseek
Objective:lseek system call can be used to change the file offset to a different value
Prototype: #include<sys/types.h>
#include<fcntl.h>
off_t lseek( int fdesc, off_t pos, int whence)
Arguments:
Fdesc: is a file descriptor that refers to an opened file
Pos: byte offset to be added to a reference location in deriving the new file offset value
Whence: reference location
Whence value
SEEK_CUR, SEEK_SET, SEEK_END
Return: New file offset value if the call succeeds
-1 if the call fails
Link
Objective: link function creates a new link for an existing file.
Prototype: #include<sys/types.h>
#include<fcntl.h>
int link( const char * cur_link, const char * new_link);
Arguments:
Cur_link is a pathname of an existing file
New_link is a new pathname to be assigned to the same file
Return: hard link count of the file will be increased by 1 if the call succeeds
unLink
Objective: unlink function deletes an existing link for an existing file.
Prototype: #include<sys/types.h>
#include<fcntl.h>
int unlink(const char * cur_link);
Arguments:
Cur_link is a pathname of an existing file
Return: 0 if the call succeeds
-1 if the call fails
Rename
int rename(const char* old_path_name, const char* new_path_name);
Stat,fstat
Objective:stat & fstat functions retrieve the file attributes of a given file
Prototype: #include<sys/types.h>
#include<unistd.h>
#include<sys/stat.h>
int stat ( const char *path_name, struct stat * statv);
int fstat(const int fdesc, struct stat * statv);
Arguments:
First argument of stat is a file pathname and
First argument of fstat is a file descriptor
Second argument of stat and fstat is an address of a buffer where the retrieved file
attributes will be stored
Return: 0 if the call succeeds
-1 if the call fails
Access
Objective: access function check the existence of a named file or checks the access
permission of user to a named file.
Prototype: #include<unistd.h>
int access( const char * path_name, int flag)
Arguments:
Path_name is the pathname of a file
Flag contains
F_OK: checks whether a named file exists
R_OK: checks whether a calling process has read permission
W_OK : checks whether a calling process has write permission
X_OK : checks whether a calling process has exec permission
Return: 0 if the call succeeds
-1 if the call fails
Chmod,fchmod
Objective:funtions change file access permissions for owner group and others
Prototype: #include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
int chmod(const char *path_name, mode_t flag);
int fchmod(int fdesc, mode_t flag);
Arguments:
First argument of chmod is a pathname of a file and for fchmod its a file descriptor
Flag value contains new access permissions
Return: 0 if the call succeeds
-1 if the call fails
Chown,fchown,lchown
Objective: funtions changes the userID and groupID of files
Prototype: #include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
int chown(const char *path_name, uid_t uid,gid_t gid);
int fchown(int fdesc, uid_t uid,gid_t gid);
int lchown(const char *path_name, uid_t uid,gid_t gid);
Arguments:
First argument of chown and lchown is a pathname of a file and for fchown its a file
descriptor
Uid is a new user_id
Gid is a new group_id
Return: 0 if the call succeeds
-1 if the call fails
Utime
Objective: utime function modifies the access and modification time stamps of a file
Prototype: #include<unistd.h>
#include<sys/types.h>
#include<utime.h>
int utime (const char * path_name, struct utimbuf *times);
Arguments:
Pathname is the pathname of a file
Times argument specifies the new access time and modification time for the file
Return: 0 if the call succeeds
-1 if the call fails