You are on page 1of 7

OCTOBER 29, 2019

SYSTEMS PROGRAMMING LAB


ENGR. MADIHA SHER

LAB REPORT 05

IMAD RASHID
DEPARTMENT OF COMPUTER SYSTEM ENGINEERING
17 PWCSE 1541

NAVEED AHMED
DEPARTMENT OF COMPUTER SYSTEM ENGINEERING
17 PWCSE 1507
PRE LAB NOTES

OBJECTIVES

IMPLEMENTING USER DEFINED COMMANDS/CALLS:


Most of the time computer users take simple bash commands and operation like move, copy, paste
or delete etc. for granted this lab has been designed to provide students with an in-depth overview
of how these functions are handled by a computer and what actually happens inside a computer
when we use them,
After dealing with lab students will be enabled to understand the core idea of systems
Programming and shall get an understanding of how an operating system is written in reality.

TASK : 01
Implement the cp command.

CODE

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include <sys/stat.h>
#include<string.h>

int main(int argc, char *argv[])


{
if(argc!=3)
{
printf("Usage Error: You must have to files as Arguments \n");
}
int fd1= open(argv[1],O_RDONLY );
int fd2 = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IROTH );
char buf[10000];

if(read(fd1,buf,10000) != -1);

if(write(fd2,buf,strlen(buf))!=-1);

printf("File Copied Successfully \n");


close(fd1);
close(fd2);

return 0;

1
OUTPUT

BEFORE

AFTER

2
TASK : 02

Implement rm command.

CODE

#include<stdio.h>
#include<unistd.h>

#include<fcntl.h>

#include <sys/stat.h>
#include<string.h>

int main(int argc, char *argv[])


{

for(int i=0;i<argc;i++)
{
unlink(argv[i]);
}
printf("Files Deleted Succesfully\n");

return 0;

OUTPUT

BEFORE

3
AFTER

4
TASK : 03

Implement the mov command.

CODE

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include <sys/stat.h>
#include<string.h>

int main(int argc, char *argv[])


{
if(argc!=3)
{
printf("Usage Error: You must have to files as Arguments \n");

}
int fd1= open(argv[1],O_RDONLY , S_IROTH );

int fd2 = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IROTH );


char buf[10000];
read(fd1,buf,10000);

if(read(fd1,buf,10000) != -1);

if(write(fd2,buf,strlen(buf))!=-1);

printf("File Copied Successfully \n");

unlink(argv[1]);

close(fd2);

return 0;

5
OUTPUT

BEFORE

AFTER

You might also like