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

System Programming Lab 5

The document describes a systems programming lab report that implemented common file manipulation commands like cp, rm, and mv in C. It includes the code for each command, descriptions of the objectives, and before and after outputs showing the commands in use. The cp command copies a file, rm deletes files, and mv moves a file by copying and deleting. The code opens files, reads/writes their contents, and closes them to perform the corresponding operations.

Uploaded by

Hurair khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

System Programming Lab 5

The document describes a systems programming lab report that implemented common file manipulation commands like cp, rm, and mv in C. It includes the code for each command, descriptions of the objectives, and before and after outputs showing the commands in use. The cp command copies a file, rm deletes files, and mv moves a file by copying and deleting. The code opens files, reads/writes their contents, and closes them to perform the corresponding operations.

Uploaded by

Hurair khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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