Assignment_9
Assignment_9
• If the file does not exist, create a new file named sample.txt.
Source Code:
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
int fd,openFlags;
mode_t filePerms;
fd=open(argv[1],O_RDONLY);
if(fd<0)
fd=open(argv[1],openFlags,filePerms);
close(fd);
Shubhashish Das Roy (23CS8013)
else
printf("File opened!");
printf("\n");
return 0;
Output :
Shubhashish Das Roy (23CS8013)
Q2. Write a C program to open an existing file named sample.txt and write the text “Welcome
to NIT Durgapur” into the file. Then, close the file.
Source Code:
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
int fd,openFlags;
char buf[50];
mode_t filePerms;
fd=open(argv[1],openFlags,filePerms);
write(fd,buf,strlen(buf));
close(fd);
printf("\n");
return 0;
Shubhashish Das Roy (23CS8013)
Output :
Shubhashish Das Roy (23CS8013)
Q3.Write a C program to open an existing file named sample.txt, read its content, and display
it on the screen. Then, close the file.
Source Code:
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#define BUFSIZE 50
int fd,openFlags;
char buf[BUFSIZE];
mode_t filePerms;
ssize_t numread;
fd=open(argv[1],O_RDONLY);
else
while((numread=read(fd,buf,BUFSIZE))>0)
write(1,buf,numread);
Shubhashish Das Roy (23CS8013)
close(fd);
printf("\n");
return 0;
Output :
Q4. Write a C program that accepts a filename as a command-line argument, opens the file,
and prints its content.
Source Code:
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#define BUFSIZE 50
{
Shubhashish Das Roy (23CS8013)
int fd,openFlags;
char buf[BUFSIZE];
mode_t filePerms;
ssize_t numread;
fd=open(argv[1],O_RDONLY);
else
while((numread=read(fd,buf,BUFSIZE))>0)
write(1,buf,numread);
close(fd);
printf("\n");
return 0;
OUTPUT:
Shubhashish Das Roy (23CS8013)
Q5. Write a C program that accepts two filenames as command-line arguments (a source file
and a destination file), reads the content from the source file, and writes it to the destination
file.
Source Code:
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#define BUFSIZE 50
int fd,openFlags,fd2;
char buf[BUFSIZE];
mode_t filePerms;
ssize_t numread;
fd=open(argv[1],O_RDONLY);
else
fd2=open(argv[2],openFlags,filePerms);
while((numread=read(fd,buf,BUFSIZE))>0)
{
Shubhashish Das Roy (23CS8013)
write(fd2,buf,numread);
close(fd);
close(fd2);
printf("\n");
return 0;
OUTPUT:
Source Code:
#include <stdio.h>
#include <unistd.h>
Shubhashish Das Roy (23CS8013)
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
if (argc != 3) {
return 1;
if (fd < 0) {
return 1;
char buf[BUFSIZE];
ssize_t numread;
int line_number = 1;
int found = 0;
char temp[BUFSIZE];
int temp_index = 0;
Shubhashish Das Roy (23CS8013)
temp[temp_index] = '\0';
if (strstr(temp, word))
found = 1;
line_number++;
temp_index = 0;
else
temp[temp_index++] = buf[i];
if (!found) {
close(fd);
Shubhashish Das Roy (23CS8013)
return 0;
OUTPUT:
Shubhashish Das Roy (23CS8013)
• Accepts two filenames as command-line arguments (source file and destination file).
Source Code:
#include <stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
int fd1,fd2,op;
char buf[100],arr[100];
mode_t fp;
ssize_t numread;
fd1=open(argv[1],O_RDONLY);
op=O_CREAT|O_WRONLY|O_TRUNC;
fp=S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
fd2=open(argv[2],op,fp);
if(fd1<0)
exit(0);
else
int k=0;
arr[k++]=buf[i];
write(fd2,arr,strlen(arr));
close(fd1);
close(fd2);
printf("Done...\n");
return 0;
}
Shubhashish Das Roy (23CS8013)
OUTPUT:
Shubhashish Das Roy (23CS8013)
• Prints "File exists" if it does, otherwise prints "File does not exist".
Source Code:
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
int fd,openFlags;
mode_t filePerms;
fd=open(argv[1],O_RDONLY);
if(fd<0)
else
printf("File exist!");
close(fd);
printf("\n");
Shubhashish Das Roy (23CS8013)
return 0;
OUTPUT:
Shubhashish Das Roy (23CS8013)
Source Code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
if (argc != 2) {
return 1;
if (fd < 0) {
return 1;
Shubhashish Das Roy (23CS8013)
char buf[BUFSIZE];
ssize_t numread;
int line_number = 0;
char temp[BUFSIZE];
int temp_index = 0;
temp[temp_index] = '\0';
line_number++;
temp_index = 0;
else
temp[temp_index++] = buf[i];
close(fd);
return 0;
Shubhashish Das Roy (23CS8013)
OUTPUT:
Source Code:
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
int fd,openFlags;
char buf[50];
mode_t filePerms;
Shubhashish Das Roy (23CS8013)
fd=open(argv[1],openFlags,filePerms);
lseek(fd,0,SEEK_END);
write(fd,buf,strlen(buf));
close(fd);
printf("\n");
return 0;
OUTPUT:
Shubhashish Das Roy (23CS8013)
Source Code:
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
int fd,openFlags;
mode_t filePerms;
fd=open(argv[1],O_RDONLY);
if(fd<0)
else
close(fd);
printf("File Deleted!");
printf("\n");
Shubhashish Das Roy (23CS8013)
return 0;
OUTPUT:
Shubhashish Das Roy (23CS8013)
Miscellaneous Question:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <utime.h>
if (argc != 3)
return 1;
sync_files(argv[1], argv[2]);
return 0;
if (src_dir == NULL)
delete_item(dst_path);
return;
if (dst_dir == NULL)
{
Shubhashish Das Roy (23CS8013)
copy_dir(src_path, dst_path);
return;
continue;
char src_item_path[MAX_PATH];
char dst_item_path[MAX_PATH];
perror("stat src");
continue;
if (S_ISREG(src_stat.st_mode))
copy_file(src_item_path, dst_item_path);
} else if (S_ISDIR(src_stat.st_mode))
copy_dir(src_item_path, dst_item_path);
continue;
if (S_ISREG(src_stat.st_mode))
if (S_ISREG(dst_stat.st_mode))
if (compare_file_metadata(src_item_path, dst_item_path) != 0)
copy_file(src_item_path, dst_item_path);
} else
delete_item(dst_item_path);
copy_file(src_item_path, dst_item_path);
} else if (S_ISDIR(src_stat.st_mode))
{
Shubhashish Das Roy (23CS8013)
if (S_ISDIR(dst_stat.st_mode))
sync_files(src_item_path, dst_item_path);
} else
delete_item(dst_item_path);
copy_dir(src_item_path, dst_item_path);
rewinddir(dst_dir);
continue;
char src_item_path[MAX_PATH];
char dst_item_path[MAX_PATH];
delete_item(dst_item_path);
closedir(src_dir);
closedir(dst_dir);
update_metadata(src_path, dst_path);
perror("copy_file");
return;
}
Shubhashish Das Roy (23CS8013)
char buffer[BUFFER_SIZE];
ssize_t bytes_read;
close(src_fd);
close(dst_fd);
printf("[o]%s\n", dst);
mkdir(dst, 0777);
printf("[+]%s\n", dst);
sync_files(src, dst);
if (S_ISREG(statbuf.st_mode))
{
Shubhashish Das Roy (23CS8013)
unlink(path);
printf("[-]%s\n", path);
} else if (S_ISDIR(statbuf.st_mode))
continue;
char item_path[MAX_PATH];
delete_item(item_path);
closedir(dir);
rmdir(path);
printf("[-]%s\n", path);
}
Shubhashish Das Roy (23CS8013)
return -1;
return 0;
times.actime = src_stat.st_atime;
times.modtime = src_stat.st_mtime;
if (src_stat.st_mtime != dst_stat.st_mtime)
utime(dst, ×);
printf("[t]%s\n", dst);
}
Shubhashish Das Roy (23CS8013)
if (src_stat.st_mode != dst_stat.st_mode)
chmod(dst, src_stat.st_mode);
printf("[p]%s\n", dst);
OUTPUT: