Unit 3
Unit 3
By
Kala.C.L
Lecturer, Dept of ISE
SJBIT, Bengaluru
UNIT-3
Unix file API’s
General file APIs
open Opens a file for data access
read Reads data from a file
write Writes data to a file
lseek Allows random access of
data in a file
close Terminates connection to a
file
stat, fstat Queries attributes of a
file
chmod Changes access
permissions of a file
chown Changes UID and/or GID of a
file
utime Changes last modification
time and access time
stamps of a file
link creates a hard link to a file
unlink Deletes a hard link of a file
umask Sets default file creation
mask
Open
The function establishes connection
between process and a file
The prototype of the function
#include <sys/types.h>
#include <fcntl.h>
int open (const char *pathname, int access
mode , mode_t permission);
Pathname : It can be absolute path
name or a relative path
name
Access_mode : An integer which
specifies how file is to be
accessed by calling
process
Access mode flag Use
only
O_RDWR Opens file for read
& write
Access modifier flag
O_APPEND
O_CREAT
O_EXCL
O_TRUNC
O_NONBLOCK
O_NOCTTY
O_APPEND : appends data to end of file
O_TRUNC : if the file already exists,
discards its contents and
sets file size to zero
O_CREAT : creates the file if it does not
exist
O_EXCL : used with O_CREAT only.
This flag causes open to
fail if the file exists
O_NONBLOCK : specifies that any
subsequent read or write
on the file should be non
blocking
control terminal
Umask
It specifies some access rights to be masked
off
Prototype:
#include <sys/types.h>
#include <unistd.h>
Int creat (const char* pathname, mode_t mode)
Read
This function fetches a fixed size block of
data from a file referenced by a given file
descriptor.
#include <sys/types.h>
#include <unistd.h>
ssize_t read (int fdesc ,void* buf, size_t size);
Write
#include <sys/types.h>
#include <unistd.h>
ssize_t write (int fdesc ,void* buf, size_t size);
Close
Disconnects a file from a process
#include <unistd.h>
int close (int fdesc);
Close function will de allocate system resources.
#include <sys/types.h>
#include <unistd.h>
Off_t lseek (int fdesc , off_t pos, int whence)
Pos :
specifies a byte offset to be added to a
reference location in deriving the new file
offset value
Whence location reference
SEEK_CUR current file pointer
address
SEEK_SET the beginning of a
file
SEEK_END the end of a file
link
#include <unistd.h>
int link (const char* cur_link ,const char*
new_link)
unlink
#include <unistd.h>
int unlink (const char* cur_link );
#include <sys/types.h>
#include <unistd.h>
int stat (const char* path_name,struct
stat* statv)
int fstat (const int fdesc,struct stat* statv)
Struct stat
{ dev_t st_dev;
ino_t st_ino;
mode_t st_mode;
nlink_t st_nlink;
uid_t st_uid;
gid_t st_gid;
dev_t st_rdev;
off_t st_size;
time_t st_atime;
time_t st_mtime
time_t st_ctime
};
If pathname specified in stat is a symbolic
link then the attributes of the non-
symbolic file is obtained
To avoid this lstat system call is used
It is used to obtain attributes of the
symbolic link file
statv);
/* Program to emulate the UNIX ls -l
command */
#include <iostream.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
static char xtbl[10] = "rwxrwxrwx";
#ifndef MAJOR
#define MINOR_BITS 8
#define MAJOR(dev) ((unsigned)dev >>
MINOR_BITS)
#define MINOR(dev) ( dev &
MINOR_BITS)
#endif
{
switch (st_mode &S_IFMT)
{
case S_IFDIR: ofs << 'd'; return;
/* directory file
*/
case S_IFCHR: ofs << 'c'; return;
/* character device file */
case S_IFBLK: ofs << 'b'; return;
/* block device file */
case S_IFREG: ofs << '-'; return;
/* regular file */
case S_IFLNK: ofs << 'l'; return;
/* symbolic link file */
case S_IFIFO: ofs << 'p'; return;
/* FIFO file */
}
}
/* Show access permission for owner, group,
others, and any special flags */
static void display_access_perm ( ostream&
ofs, int st_mode )
{
char amode[10];
for (int i=0, j= (1 << 8); i < 9; i++, j>>=1)
amode[i] = (st_mode&j) ? xtbl[i] : '-';
ofs << ' ' << pw_p->pw_name << ' ' <<
gr_p->gr_name << ' ';
if ((statv.st_mode&S_IFMT) == S_IFCHR ||
(statv.st_mode&S_IFMT)==S_IFBLK)
ofs << MAJOR(statv.st_rdev) << ','
<< MINOR(statv.st_rdev);
else ofs << statv.st_size;
/* show file size or major/minor no. */
ofs << ' ' << ctime (&statv.st_mtime);
/* print last modification time */
ofs << ' ' << path_name << endl;
/* show file name */
}
/* Main loop to display file attributes one file
at a time */
int main (int argc, char* argv[])
{
if (argc==1)
cerr << "usage: " << argv[0] << " <file
path name> ...\n";
else while (--argc >= 1) long_list( cout,
*++argv);
return 0;
}
Access
#include <unistd.h>
int access (const char* path_name, int flag);
The flag contains one or more bit flags
Bit flags USE
#include <unistd.h>
#include <sys/types.h>
#include <utime.h>
int utime (const char* path_name,
struct utimbuf* times);
Struct utimbuf
{
time_t actime;
time_t modtime;
};
#include<utime.h>
#include<time.h>
struct flock
{
short l_type;
short l_whence;
off_t l_start;
off_t l_len;
pid_t l_pid;
};
l_type and l_whence fields of flock
Rewinddir :
void rewinddir (DIR* dir_fdesc);
Used to reset the file pointer associated
with a dir_fdesc.
Device file APIs
Device files are used to interface physical
devices (ex: console, modem) with
application programs.
Device files may be character-based or
block-based
The only differences between device files
and regular files are the ways in which
device files are created and the fact that
lseek is not applicable for character
device files.
To create:
int mknod(const char* path_name, mode_t
mode,int device_id);
The mode argument specifies the access
char* buf[256],tname[256];
strcpy(tname,sym_link);
while(readlink(tname,buf,sizeof(buf))>0)
strcpy(tname,buf);
cout<<sym_link<<"=>"<<buf;
}
/*Program to Create a hard link or a symbolic link
depending upon the option given */
#ifndef PIPEFILE_H
#define PIPEFILE_H
#include"filebse.h"
class pipefile :public filebase
{
public:
pipefile(const char* fn,int flags,int
prot):filebase(fn,flags,prot)
{};
int create(const char* fn,mode_t prot)
{return mkfifo(fn,prot);};
streampos tellg() {return(streampos)-1;};
};
#endif
#include "pipefile.h"
#include "symfile.h"
int main()
{ char buf[256];
symfile nsym;
nsym.setlink("/usr/file/chap10","/usr/xyz/sym.lnk");
nsym.open(ios::in);
while(nsym.getline(buf,256))
cout<<buf;
cout<<nsym.ref_path();
nsym.close();
}
/*File Listing Program*/
#include "filebase.h"
#include "symfile.h"
#include "dirfile.h"
void show_list(ostream& ofs,const char* fname,int deep);
extern void long_list(ostream& ofs,char* fn);
void show_dir(ostream& ofs,const char* fname)
{
dirfile dirObj(fname);
char buf[256];
ofs<<"directory:"<<fname;
while(dirObj.read(buf,256))
show_list(ofs,buf,0);
dirObj.seekg(0);
while(dirObj.read(buf,256)){
filebase fObj(buf,ios::in,0755);
if(fObj.file_type==DIR_FILE)
show_dir(ofs,buf);
fObj.close();
}
dirObj.close();
}
void show_list(ostream& ofs,const char* fname,int deep)
{
long_list(ofs,fname);
filebase fobj(fname,ios::in,0755);
if(fobj.file_type()==SYM_FILE) {
symfile *symObj={symfile*)fobj;
ofs<<symObj-.ref_path()<<endl;
}
else if(fobj.file_type()==DIR_FILE && deep)
show_dir(ofs,fname);
}
int main(int argc,char* argv[])
{
while(--argc>0)
show_list(cout,*++argv,1);
return 0;
}
QUESTIONS
Explain the access mode flags and
access modifier flags. Also explain how
the permission value specified in an
‘Open’ call is modified by its calling
process ‘unmask, value. Illustrate with
an example (10)
Explain the use of following APIs (10)
i) fcntl ii) lseek iii) write iv) close
With suitable examples explain various
directory file APIs (10)
Write a C program to illustrate the use of
mkfifo ,open ,read & close APIs for a FIFO
file (10)
Differentiate between hard link and
symbolic link files with an example (5)
Describe FIFO and device file classes (5)
Explain process of changing user and
group ID of files (5)
What are named pipes? Explain with an
example the use of lseek, link, access with
their prototypes and argument values (12)
Explain how fcntl API can be used for file
record locking (8)
Describe the UNIX kernel support for a
process . Show the related data
structures (10)
Give and explain the APIs used for the
following (10)
1. To create a block device file called
SCS15 with major and minor device
number 15 and 3 respectively and
access rights read-write-execute for
everyone
2. To create FIFO file called FIF05 with
access permission of read-write-execute
for everyone