OS Week9 Lab and Skill

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

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()
#define _POSIX_SOURCE
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#undef _POSIX_SOURCE
#include <stdio.h>
void main() {
char fn[]="./temp.file";
FILE *stream;
struct stat info;
if ((stream = fopen(fn, "w")) == NULL)
perror("fopen() error");
else {
fclose(stream);
stat(fn, &info);
printf("original owner was %d and group was %d\n", info.st_uid,
info.st_gid);
if (chown(fn, 25, 0) != 0)
perror("chown() error");
else {
stat(fn, &info);
printf("after chown(), owner is %d and group is %d\n",
info.st_uid, info.st_gid);
}
unlink(fn);
}
}

Output:
1. original owner was 1000 and group was 1000
after chown(), owner is 1025 and group is 1000

2. original owner was 1000 and group was 1000

chown() error: Operation not permitted (If ur not an admin)

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.

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FILENAME "umask-example.temp"
void printFilePerms(void)
{
FILE *poutput = popen("ls -l " FILENAME, "r");
char buff[512];
while(fgets(buff, sizeof(buff), poutput)!=NULL)
printf("%s", buff);
pclose(poutput);
}

void createFile(mode_t mode)


{
unlink(FILENAME);
int fd = open(FILENAME, O_CREAT, mode);
close(fd);
}

int main(void)
{
printf("Decimal %d and octal %d\n\n", 777, 0777);
printf("Trying to create a file with rwxrwxrwx permissions with open()\n");
createFile(0777);
printFilePerms();
printf("\n");

printf("Setting umask to 0000\n");


umask(0000);
printf("Trying to create a file with rwxrwxrwx permissions with open()\n");
createFile(0777);
printFilePerms();
printf("\n");

printf("Setting umask to 0000\n");


umask(0000);
printf("Trying to create a file with rwx------ permissions with open()\n");
createFile(0700);
printFilePerms();
printf("\n");
printf("Setting umask to 0077\n");
umask(0077);
printf("Trying to create a file with rwxrwxrwx permissions with open()\n");
createFile(0777);
printFilePerms();
printf("\n");

printf("Setting umask to 0077\n");


umask(0077);
printf("Trying to create a file with rw-rw-rw- permissions with open()\n");
createFile(0666);
printFilePerms();

umask(0321);
unlink(FILENAME);
return 0;
}

Output:
Decimal 777 and octal 511
Trying to create a file with rwxrwxrwx permissions with open()
-rwxr-xr-x 1 cse cse 0 Oct 23 10:22 umask-example.temp
Setting umask to 0000
Trying to create a file with rwxrwxrwx permissions with open()
-rwxrwxrwx 1 cse cse 0 Oct 23 10:22 umask-example.temp
Setting umask to 0000
Trying to create a file with rwx------ permissions with open()
-rwx------ 1 cse cse 0 Oct 23 10:22 umask-example.temp
Setting umask to 0077
Trying to create a file with rwxrwxrwx permissions with open()
-rwx------ 1 cse cse 0 Oct 23 10:22 umask-example.temp
Setting umask to 0077
Trying to create a file with rw-rw-rw- permissions with open()
-rw------- 1 cse cse 0 Oct 23 10:22 umask-example.temp

SKILL EXERCISE 9:
1. Write a system program that lists all in only directories - Uses S_ISDIR
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>

int scan_dir( char *_pathname );


int isdirectory(char *path);
int main(int argc, char **argv)
{
scan_dir( argv[1] );
return 0;
}

int scan_dir( char *path )


{
struct dirent *direntp;
DIR *dirp;
if ((dirp = opendir(path)) == NULL)
{
perror ("Failed to open directory");
return 1;
}
while ((direntp = readdir(dirp)) != NULL)
{
isdirectory(direntp->d_name);
printf("%s\n", direntp->d_name);
}
return 0;
}

int isdirectory(char *path) {


struct stat statbuf;

if (stat(path, &statbuf) == -1)


return 0;
else
return S_ISDIR(statbuf.st_mode);
}

Output:

List of Directory
.
hi
jk
hii
hello

2. Write a UNIX system program to demonstrate file locking using fcntl( )


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

int main()
{
struct flock fl;
int fd;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = getpid();
fd = open("jkk.txt", O_RDWR | O_EXCL);
fcntl(fd, F_SETLKW, &fl);
sleep(5);
printf(" release lock \n");
fl.l_type = F_UNLCK;
fcntl(fd, F_SETLK, &fl);
}

Output:
after 5 seconds
release lock

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy