Operating System Design: Name: G Likhith Chowdary ID NO: 190030562

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

OPERATING SYSTEM DESIGN

NAME: G LIKHITH CHOWDARY

ID NO: 190030562
IN LAB
1. Uses lstat call and struct stat to display file attributes

attributes.c
#include <stdio.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
void quit(char *message, int exit_status)
{
printf(" %s",message);
exit(exit_status);
}
void arg_check (long int args, long int argc, char *message, long int exit_status)
{
if (argc != args)
{
printf("%s", message);
exit(exit_status);
}
}
int main(long int argc, char **argv)
{
struct stat statbuf; /* We’ll use lstat to populate this */
arg_check(2, argc, "Single filename required\n", 1) ;
if(lstat(argv[1], &statbuf) == -1)
quit("Couldn’t stat file", 1);
printf("File: %s\n", argv[1]);
printf("Inode number: %ld \n", statbuf.st_ino);
printf("UID: %ld ", statbuf.st_uid);
printf("GID: %ld\n", statbuf.st_gid);
printf("Type and Permissions: %o\n",statbuf.st_mode);
printf("Number of links: %ld \n", statbuf.st_nlink);
printf("Size in bytes: %ld\n", statbuf.st_size);
printf("Blocks allocated: %ld\n", statbuf.st_blocks);
printf("Last Modification Time: %s", ctime(&statbuf.st_mtime));
printf("Last Access Time: %s\n", ctime(&statbuf.st_atime));
exit(0);
}
Steps:

1.create a file having c code in it


2.compile the file with gcc command
3.and execute the file ,here while executing the file we need to give one more file name which we
created previously ,here(test.txt)
Eg: ./a.out test.txt

4.we get the final output.


2. Lists only directories - Uses S_IFMT and S_ISDIR macros

lsdir.c

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
void quit (char *message, int exit_status)
{
printf(" %s",message);
exit(exit_status);
}
void arg_check(int args, int argc, char *message, int exit_status)
{
if(argc != args)
{
printf("%s", message);
exit(exit_status);
}
}
int main(int argc, char *argv[])
{
DIR *dir;
struct dirent *direntry;
struct stat statbuf;
mode_t file_type,file_perm;
arg_check(2, argc, "Directory not specified\n", 1) ;
if((dir = opendir(argv[1])) == NULL)
quit("Couldn’t open directory", 1);
If((chdir(argv[1]) == -1))
quit("chdir", 2); /* you starting reading its entries */
while((direntry = readdir(dir)) != NULL)
{
if(lstat(direntry->d_name, &statbuf) < 0)
{
perror("lstat");
continue;
}
if(S_ISDIR(statbuf.st_mode))
{
file_type = statbuf.st_mode & S_IFMT;
file_perm = statbuf.st_mode & ~S_IFMT;
printf("%o %4o %s\n",file_type,file_perm,direntry->d_name);
}
}
exit(0);
}

Steps:
1.create a file having c code in it.
2.now compile it using gcc command
3.as it is having command line arguments we need to give the directory over there. After writing the
execution command, then we get the final output.
POST LAB
2. changed the permission flags of the file.

Steps:
1.create a file having some content in it.
2.now check all the permissions by using ls command
3. by using chmod command change the permissions of the file.
4.once check them whether the permissions are changed or not by using ls command.
1. change the group of the file.
Steps:
As we are going to change the group of file we need to add a user , so we should come from
home to the root directory in the terminal by pressing sudo su command.

1.create a file having some content in it.

2.check by using ls command .

3.now add a user

4.now add password for that user.


Now by using chown command change the group

5.once again check by using ls command


Finally the group will be changed.

You might also like