Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

WEEK-9 (DIRECTORIES AND FILES)

Pre-Lab Task:
1. Write the function prototypes for the following calls
1) opendir( ) :-
11) ftruncate( ):-
2) readdir( ):-
12) ioctl( ) :-
3) closedir( ):- 13) link( ):-
4) stat( ):- 14) mknod( ):-
5) lstat( ):-
15) sync( ):-
6) chown( ):-
16) truncate( ):-
7) chmod( ):-
17) mount( ):-
8) fchown( ):-
18) getcwd( ):-
9) fchmod( ):-
19) chdir( ):-
10) fcntl( ):-

2. Write a program to print list of files and folders in column format (dir.c) and demonstrate directory
navigation with chdir and getcwd ?
i) //Print list of files and folders ii) //Demonstrate directory navigation
#include<stdio.h> with chdir and getcwd
#include<unistd.h> #include <stdio.h>
int main() #include <dirent.h>
{ int main(void)
char s[100]; {
printf("%s\n", getcwd(s, 100)); struct dirent *de;
chdir(".."); DIR *dr = opendir(".");
printf("%s\n", getcwd(s, 100)); if (dr == NULL)
return 0; {
} printf("Could not open current directory" );
Output: return 0;
C:\Users\KLH\Documents }
C:\Users\KLH while ((de = readdir(dr)) != NULL)
printf("%s\n", de->d_name);
closedir(dr);
return 0;
}
Output:
My Music
My Pictures
ReadThisFirst.pdf
s.c
sg.cpp
1
In Lab Task:
Steps of the Program:
• Open the Terminal.
• Then, you can see the logged in user with the $ symbol next to the username.
• $ means you are logged in as a regular user and the # means you are root user.

Problem Description:
1. Write a system program that demonstrates chmod( ), chown( ) System calls for changing file permissions.
i) //chmod()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>

int main(int argc, char **argv)


{
char mode[] = "0777";
char buf[100] = "hi.txt";
int i;
i = strtol(mode, 0, 8);
if (chmod (buf,i) < 0)
{
fprintf(stderr, "%s: error in chmod(%s, %s) - %d (%s)\n",
argv[0], buf, mode, errno, strerror(errno));
exit(1);
}
else
printf("Permission changed %s",mode);
return(0);
}
Output:
Permission changed 0777

ii) //chown()

2. umask returns the previous value of the mask. Unlike the shell’s umask statement, however,the umask system
call can’t display the current value of the mask without changing it. To use a workaround, store the current
mask by changing it to some arbitrary value, and then display it before restoring it. Write a Program that
Changes umask twice and checks effect on permissions.

SKILL EXERCISE 9:
1. Write a system program that lists only directories - Uses S_IFMT and S_ISDIR macros.

2. Write a UNIX system program to demonstrate file locking. Implementation of Sequence Number increment
problem with locking using fcntl( )

You might also like