11. Write C programs that simulate
the following unix commands:
a)mv b)cp (Use system calls)
A) Program to Implement Linux
“mv” command
Vi 11a.c
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<sys/stat.h>
int main(int argc,char **argv)
{
if(argc>3 || argc<3)
printf("please provide 2 arguments.\n");
else
{
int
r,fd1,fd2;
if(access(argv[1],F_OK)<0)
{
printf("%s not found.\n",argv[1]);
}
fd1=open(argv[1],O_RDONLY);
fd2=creat(argv[2],S_IWUSR);
r=rename(argv[1],argv[2]);
if(r==0)
{
printf("%s is moved or renamed to %s successfully.
\n",argv[1],argv[2]);
}
unlink(argv[1]);
}
return(0);
}
[student@localhost ~]$ vi 11a.c
[student@localhost ~]$ cc 11a.c
[student@localhost ~]$ cat >fi
C PROGRAM to demonstrate mv command
[student@localhost ~]$ cat fi
C PROGRAM to demonstrate mv command
[student@localhost ~]$ ./a.out
please provide 2 arguments.
[student@localhost ~]$ ./a.out of nf
of not found.
[student@localhost ~]$ ./a.out fi nf
fi is moved or renamed to nf successfully.
[student@localhost ~]$ cat fi
cat: fi: No such file or directory
[student@localhost ~]$ cat nf
C PROGRAM to demonstrate mv command
or
another solution
#include<fcntl.h>
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
int main(int argc,char **argv)
{
int fd1,fd2,i;
char *file1,*file2,buf[2];
file1=argv[1];
file2=argv[2];
printf("file1=%s file2=%s",file1,file2);
fd1=open(file1,O_RDONLY,0777);
fd2=creat(file2,0777);
while((i=read(fd1,buf,1)>0))
{
write(fd2,buf,1);
}
remove(file1);
close(fd1);
close(fd2);
}
b) Program to Implement Linux
“CP” copy command
This C program is a simulation of the Linux “cp” copy command. The
copy command can be used to make a copy of your files and directories, but here
in this implementation only the basic functionality i.e., copy the content from
one file to another is handled.The program for “cp” command takes in two arguments namely the source file and the destination file.
./cpcmd sourcefile destinationfile
1) First check if both source & the destination files are received from the command line argument and exit if argc counter is not equal to 3.
There is also a check to handle “–help” option to print the usage of cpcmd.
2) Open the source file with read only flag set.
3) Open the destination file with the respective flags & modes.
O_WRONLY -> Open the file in write only mode
O_TRUNC -> Truncates the contents of the existing file
O_CREAT -> Creates a new file if it doesn’t exist
S_IXUSR are file permissions for the current user (‘X’ can be R for read & W for write)
S_IXGRP are file permissions for the groups (‘X’ can be R for read & W for write)
S_IXOTH are file permissions for the groups (‘X’ can be R for read & W for write)
4) Start data transfer from source file to destination file till it reaches EOF (nbread == 0).
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
gedit 11b.c
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<errno.h>
#define BUFF_SIZE 1024
int main(int argc,char* argv[])
{
int srcFD,destFD,nbread,nbwrite;
char *buff[BUFF_SIZE];
/*Check if both src & dest
files are received or --help is received to get usage*/
if(argc!=3||argv[1]=="--help")
{
printf("\nUsage: cpcmd
source_file destination_file\n");
exit(EXIT_FAILURE);
}
/*Open source file*/
srcFD=open(argv[1],O_RDONLY);
if(srcFD==-1)
{
printf("\nError opening file
%s errno = %d\n",argv[1],errno);
exit(EXIT_FAILURE);
}
/*Open destination file with
respective flags & modes
O_CREAT & O_TRUNC is to truncate existing
file or create a new file
S_IXXXX are file permissions for the
user,groups & others*/
destFD=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
if(destFD==-1)
{
printf("\nError opening file
%s errno = %d\n",argv[2],errno);
exit(EXIT_FAILURE);
}
/*Start data transfer from src file
to dest file till it reaches EOF*/
while((nbread=read(srcFD,buff,BUFF_SIZE))>0)
{
if(write(destFD,buff,nbread)!=nbread)
printf("\nError in writing data
to %s\n",argv[2]);
}
if(nbread==-1)
printf("\nError in reading
data from %s\n",argv[1]);
if(close(srcFD)==-1)
printf("\nError in closing
file %s\n",argv[1]);
if(close(destFD)==-1)
printf("\nError in closing
file %s\n",argv[2]);
exit(EXIT_SUCCESS);
}
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
cat >f1
this cp command simulation
It is 11b exercise
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
cat f1
this cp command simulation
It is 11b exercise
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
cp f1 f2
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
cat f2
this cp command simulation
It is 11b exercise
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
cat f1
this cp command simulation
It is 11b exercise
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
gcc 11b.c
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
./a.out
Usage: cpcmd source_file
destination_file
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
./a.out f2 f3
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
cat f3
this cp command simulation
It is 11b exercise
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
cat f2
this cp command simulation
It is 11b exercise
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
gcc 11b.c -o 11b
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
./11b f2 f
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
cat f
this cp command simulation
It is 11b exercise
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
clear
12. Write
a C program that simulates ls Command
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
gedit 12exp.c
#include<fcntl.h>/* System
calls: open, read, etc. O_RDONLY, etc */
#include<unistd.h>/*
STDIN_FILENO, STDERR_FILENO, etc. */
#include<dirent.h>/* DIR,
dirent, etc. */
#include<string.h>/* String
functions (strcat,strlen) */
#include<stdio.h>
int main(int argc,char *argv[])
{
DIR *dir_to_read;/* Handle to directory
to be read */
struct dirent *dir_info;/* Details
of directory entries */
char *cur_work_dir=NULL;/* String
to hold current working directory name */
char success_msg[] =
"\nCommand executed successfully\n\n";
/* Get the current working directory
*/
if((cur_work_dir=((char*)get_current_dir_name()))==NULL)
{
printf("\nCould not retrieve
the current working directory\n");
}
/* Open the current working
directory */
if((dir_to_read=opendir(cur_work_dir))==NULL)
{
printf("\nCould not open the
current working directory\n\n");
}
/* Loop through current working
directory and display each file name
till all the entries are exhausted
*/
while((dir_info=readdir(dir_to_read))!=NULL)
{
write(STDOUT_FILENO,dir_info->d_name,strlen(dir_info->d_name));
write(STDOUT_FILENO,"\n",strlen("\n"));
}
write(STDOUT_FILENO,success_msg,strlen(success_msg));
return 1;
}
output:
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
gcc 12exp.c
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
./a.out
.
..
a.out
12exp.c
12exp.c~
11b.c
11b.c~
f
11b
f3
f2
f1
.mozilla
.ICEauthority
.local
.gconf
Videos
Pictures
Music
Documents
Public
Templates
Downloads
Desktop
.xsession-errors
.Xauthority
.kde
.cache
.config
.bashrc
.bash_logout
.profile
examples.desktop
Command executed successfully
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
13.9 Write a
shell script to accept student number, name, marks in 5 subjects. Find total,
average andgrade. Display the result of student and store in a file called
stu.dat Rules: avg>=80 then grade A Avg<80&&Avg>=70 then grade
B Avg<70&&Avg>=60 then grade C Avg<60&&Avg>=50 then
grade D Avg<50&&Avg>=40 then grade E Else grade F
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ gedit
13_9exp.sh
echo "Do you want to enter student data:"
read a
while [ $a = y -o $a = Y ]
do
echo "enter number"
read sno
echo "enter name"
read name
echo "enter 5 subject marks"
read m1
read m2
read m3
read m4
read m5
total=`expr $m1 + $m2 + $m3 + $m4 + $m5`
echo total is: $total
avg=`expr $total / 5`
echo average is: $avg
if [ $avg -ge 80 ]
then
grade="A"
elif [ $avg -lt 80 -a $avg -ge 70 ]
then
grade="B"
elif [ $avg -lt 70 -a $avg -ge 60 ]
then
grade="C"
elif [ $avg -lt 60 -a $avg -ge 50 ]
then
grade="D"
elif [ $avg -lt 50 -a $avg -ge 40 ]
then
grade="E"
else
grade="F"
fi
echo "enter filename"
read f
echo "$sno $name $total $avg $grade" >> $f
echo "Do you want to continue"
read a
done
First create the file with name stu.dat in the
following format.
cat > stu.dat
Student Information
is:
SNO
NAME
TOTAL
AVERAGE
GRADE
Press ctrl –d
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
cat >stu.dat
Student
Information is:
SNO NAME
TOTAL AVERAGE GRADE
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
cat stu.dat
Student
Information is:
SNO NAME TOTAL
AVERAGE GRADE
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
sh 13_9exp.sh
Do
you want to enter student data:
y
enter
number
12
enter
name
tanu
enter
5 subject marks
89
79
78
89
90
total
is: 425
average
is: 85
enter
filename
stu.dat
Do
you want to continue
y
enter
number
23
enter
name
rani
enter
5 subject marks
45
34
23
23
21
total
is: 146
average
is: 29
enter
filename
stu.dat
Do
you want to continue
n
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
cat stu.dat
Student
Information is:
SNO NAME TOTAL
AVERAGE GRADE
12
tanu 425 85 A
23
rani 146 29 F
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
1.
13.10 Write
a shell script to accept empno,empname, and basic. Find DA, HRA, TA, PF using
following rules.Display empno, empname, basic, DA,HRA,PF,TA,GROSS SAL and
NETSAL. Also store all details ina file called emp.dat Rules: HRA is 18% of
basic if basic > 5000 otherwise 550 DA is 35% of basic PF is 13% of basic IT
is 14% of basic TA is 10% of basic
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
gedit 13_10exp.sh
echo "Do you
want to enter employee data"
read a
while [ $a = y -o
$a = Y ]
do
echo "enter
employee number:"
read sno
echo "enter
employee name:"
read name
echo "enter
basic salary:"
read bsal
if [ $bsal -gt
5000 ]
then
k=`expr 18 \*
$bsal`
hra=`expr $k /
100`
else
hra=550
fi
k=`expr 15 \*
$bsal`
da=`expr $k / 100`
k=`expr 13 \*
$bsal`
pf=`expr $k / 100`
k=`expr 10 \*
$bsal`
ta=`expr $k / 100`
k=`expr 14 \*
$bsal`
it=`expr $k / 100`
gsal=`expr $hra +
$da + $ta + $it + $bsal + $pf`
nsal=`expr $hra +
$da + $ta + $bsal`
echo "enter
file name:"
read f
echo
"EMPLOYEE DETAILS ARE:">> $f
echo "EMPNO
is: $sno" >> $f
echo
"EMPLOYEE NAME IS: $name">> $f
echo "BASIC
SALARY IS: $bsal">>$f
echo "GROSS
SALARY IS : $gsal">> $f
echo "NETSALARY
IS: $nsal">>$f
echo "Do you
want to continue"
read a
done
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$
cat emp.dat
Employee
Information is:
Empno EmpName
Basic DA HRA
PF TA GROSS SAL
NETSAL
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ sh 13_10exp.sh
Do you want to enter employee data
y
enter employee number:
333
enter employee name:
yogi
enter basic salary:
8000
enter file name:
emp.dat
Do you want to continue
n
guest-nUtd1q@hadoop54-ThinkCentre-E73:~$ cat emp.dat
EMPLOYEE DETAILS
ARE:
EMPNO is: 333
EMPLOYEE NAME IS:
yogi
BASIC SALARY IS:
8000
GROSS SALARY IS :
13600
NETSALARY IS:
11440
No comments:
Post a Comment