0% found this document useful (0 votes)
16 views

File Apisunix

file apis for unix and linux

Uploaded by

maruthi631
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

File Apisunix

file apis for unix and linux

Uploaded by

maruthi631
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Open

Creat File APIs


Read
Write
Close
Fcntl
Lseek
Link
Unlink
Stat, fstat
Access
Chmod fchown lchown
utime
Open
Objective: open function establishes a connection between a process and a file, it can be
used to create a brand new file
Prototype: #include<sys/types.h>
#include<fcntl.h>
int open(const char *path_name, int access_mode, mode_t permission);
Arguments:
Path_name: is the pathname of the file
access_mode: is an integer value that specifies how the file is to be accessed by the calling
process
O_RDONLY, O_WRONLY, O_RDWR
Modifier flags can be specified by bitwise oring them with above access mode flags to alter
the access mechanism of the file.
O_APPEND,O_CREAT,O_EXCL etc
Permission argument is required only with O_CREAT flag is set in access_mode argument
which specifies the access permission of the file for its owner group and all others.
Return: file descriptor to the file
creat
Objective:Creat system call is used to create new regular files
Prototype: #include<sys/types.h>
#include<fcntl.h>
int creat(const char *path_name, mode_t mode);
Arguments:
Path_name: is the pathname of the file
Mode argument is same as access_mode in open API
Return: 0/-1
read
Objective: read system call fetches a fixed size block of data from a file referenced by
a given file descriptor
Prototype: #include<sys/types.h>
#include<fcntl.h>
ssize_t read( int fdesc, void *buf, size_t size);
Arguments:
Fdesc: is a file descriptor that refers to an opened file
Buf: address of the buffer holding any data read from file
Size: how many bytes of data are to be read from the file.
Return: returns the number of bytes of data successfully read
returns 0 if the file is empty
write
Objective: write system call puts a fixed size block of data to a file referenced by a file
descriptor.
Prototype: #include<sys/types.h>
#include<fcntl.h>
ssize_t write( int fdesc, const void *buf, size_t size);
Arguments:
Fdesc: is a file descriptor that refers to an opened file
Buf: address of the buffer which contains data to be written to the file.
Size: how many bytes of data are in the buf argument
Return: returns the number of bytes of data successfully written to a file
close
Objective: close system call disconnects a file from a process
Prototype: #include<sys/types.h>
#include<fcntl.h>
int close ( in fdesc);

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

You might also like