You are on page 1of 8

HOMEWORK No.

Course :Linux Programming Code : CSE207

Homework Title/No.:___Avijit Kumar__________ Course code: _______CSE 207_______

Course Instructor: ___Atul Malhotra________ Course Tutor (if applicable) :________________

Date of Allotment: ____________ Date of Submission: ______07/11/10____________

Student’s Roll No. : ___RF1902A20_______ Section No.: ________F1902________

Declaration:

I declare that this assignment is my individual work. I have not copied from any other student’s
work or from any other source except where due acknowledgement is made explicitly in the text,
nor has any part been written for me by another person.

Student’s Signature: ______Avijit Kumar_________

Evaluator’s comment:

__________________________________________________________________________

Marks obtained: __________________________ out of: __________________________

Content of Homework should start from this page only:

PART-A

Ques 1: Write a C program to create a file at least of 2 MB with command


ls -R / >file1.txt and move contents of the file to another file using

a) System call(open, read and buffer of 10 KB)


b) Library functions
1. fgets, fputs
2. fgetc, fputc
c) System call(with buffer of 20 bytes)
Also calculate and compare the time taken by each of the above and
which is better.
Ans: By system call and 2048 size buffer

#include<unistd.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<stdlib.h>

int main()

char bl[20480];

int in,out;

int nread;

in=open("file.in",O_RDONLY);

out=open("file.out",O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);

while((nread=read(in,block,sizeof(bl)))>0)

write(out,bl,nread);

exit(0);

By using library functions

#include<stdio.h>

#include<stdlib.h>

int main()

int c;

FILE *in, *out;


in=fopen("file.in","r");

out=fopen("file.out","w");

while((c=fgetc(in))!=EOF)

fputc(c,out);

exit(0);

#include<unistd.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<stdlib.h>

int main()

char bl[16348];

int in,out;

int nread;

in=open("file.in",O_RDONLY);

out=open("file.out",O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);

while((nread=read(in,block,sizeof(block)))>0)

write(out,block,nread);

exit(0);

2. WAP to read a ‘C’ program file and count the following in the program.

1. Total no. of statements

2. Total no. of included files

3. Total no. of blocks and brackets


Ans :2

a. to count number of statements

#include<unistd.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<stdlib.h>

#include<stdio.h>

int main()

char bl[20];

int count=0;

int in, out;

int nread;

in=open("file.in",O_RDONLY);

out=open("file.out",O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);

while((nread=read(in,bl,sizeof(bl)))>0)

write(out,bl,nread);

count ++;

printf("No. of statemnets is %d",count);

exit(0);

b. to count number of included files


3. Write a C program which takes directory name as input and delete
all files, within that directory.

Ans:3
rmdir -p --ignore-fail-on-non-empty ./sources/glibc-build/
rm -rf ow

Q4. Illustrate through example in which scenario would you prefer using perror ( ) and in
which stderror ( ).

Answer: The perror() function shall map the error number accessed through the symbol errno to
a language-dependent error message, which shall be written to the standard error stream as
follows:

• First (if s is not a null pointer and the character pointed to by s is not the null byte), the
string pointed to by s followed by a colon and a <space>.
• Then an error message string followed by a <newline>.

The perror() function shall not change the orientation of the standard error stream.

Printing an Error Message for a Function

The following example replaces bufptr with a buffer that is the necessary size. If an error occurs,
the perror() function prints a message and the program exits.

#include <stdio.h>

#include <stdlib.h>

...

char *bufptr;

size_t szbuf;

...

if ((bufptr = malloc(szbuf)) == NULL) {

perror("malloc"); exit(2);

}
...

EXAMPLE:

/* perror example */
#include <stdio.h>

int main ()
{
FILE * pFile;
pFile=fopen ("unexist.ent","rb");
if (pFile==NULL)
perror ("The following error occurred");
else
fclose (pFile);
return 0;
}

Q5.Write a C program to input file name and change its permissions to reading by all and
writing by owner only.

ANSWER:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

main()
{
pid_t pid;
char *parmList[] = {"/bin/chmod", "0700", "/home/op/biaoaai/bead",NULL};

if ((pid = fork()) ==-1) //fork failed


perror("fork error");
else if (pid == 0) { //child (pid==0 if child)
execvp("chmod", parmList);
printf("Return not expected. Must be an execve error.n");
} else { //parent (pid==child process)
//parent specific code goes here
}
}
Q6 To create a following tree structure in current directory.

Your Rollno

Previous
Next Rollno
Rollno

Answer:

1- Check which directory you are currently active in prior to using the "mkdir" command.
This will eliminate errors later if you are in a directory in which you don't have such
command privileges. You can check this by using the "pwd" command and pressing
"Enter."

2- Type "mkdir [directory]" at the command prompt to make the directory. Use the name of
your new directory in place of the [directory] command line operator. To create a
directory called "business," you would type "mkdir business." Be aware that this will
create the directory within the current working directory.

3- Use the "-p" command line parameter to create subdirectories within a parent directory.
For example, you would type "mkdir -p games/strategy/chess" to create a directory tree
including "strategy/chess" within the "games" directory.
4- Create a new directory in a directory other than the current working directory using the "-
p" command line parameter as you did to create a directory tree. In this manner however,
it is important to include the slash "/" prior to the first directory's name. In other words, if
you wanted to create a "chess" directory in a "games" directory that already exists you
would type "mkdir -p /games/chess."

5- Make multiple directories at once using the "mkdir" command. You can do this by
including a space in between each of the directories you intend to create. For example,
the command "mkdir -p games bin lib personal" would create a "games," "bin," "lib" and
"personal" directory all at once.

You might also like