0% found this document useful (0 votes)
5 views1 page

Lab 2. Exercises

The document outlines a series of programming exercises focused on file and directory manipulation in Linux. Tasks include reading and writing files, creating a simple echo and grep emulator, and implementing basic shell commands like 'ls' and 'cd'. It also suggests comparing performance between basic system calls and the standard C library for file operations.

Uploaded by

23021933
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Lab 2. Exercises

The document outlines a series of programming exercises focused on file and directory manipulation in Linux. Tasks include reading and writing files, creating a simple echo and grep emulator, and implementing basic shell commands like 'ls' and 'cd'. It also suggests comparing performance between basic system calls and the standard C library for file operations.

Uploaded by

23021933
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab 2: Programming I/O in Linux

Working with file


1. Given file Hello.txt with content: “Hello! What is your name?”. Write a program to:
a) Read the content of file Hello.txt and print on the screen.
b) Read the content of file Hello.txt starting from the 7th byte from the beginning
of the file and print on the screen. (Hint: using lseek system call)

2. Write a program that creates a file with two options:


Select 1: The contents of the file are entered from the keyboard.
Select 2: Read the contents of another file and write it to the file you just created.

Optional: Create two versions of above program: one version using basic system
call open(), read(), write(), close() and one version using standard C library.
Compare time needed to executing two versions of program? (You can use time
shell command) Which one is faster? Why?

3. Write a program to enter the name of a file and print out the size of the file, the
access mode information of the file. (Hint: you can use stat or fstat)

4. Write a program named linuxecho.c emulator:


echo [string] > example.txt.
(Printed string is placed in argv[1], filename is placed in argv[2])

5. Write a program called linuxgrep.c that emulates "grep" so that the filtered word
is placed in argv[1], and the path to the file is placed in argv[2].

6. (Optional) Write a program named grepdir.c that list all the files in the folder
containing the filtered word. The program receives 2 input parameters: filtered
words in argv[1] and links to folders in argv[2].
Hint: Browse the input directory, for each file in the directory, reuse the linuxgrep
program code in exercise 5 to filter the content of the file with the filtered word.
If the filtered word appears in the file, print the file name.

Working with directory


7. Write a program linuxls.c that emulates ls shell command. The program receives
1 input parameters: directory path in argv[1].

8. Write a program linuxcd.c that emulates cd shell command. The program receives
1 input parameters: directory path in argv[1]. (Hint: using chdir system call)

1 Advanced Programming Techniques (ELT 3296)

You might also like