0% found this document useful (0 votes)
5 views4 pages

C Program for File Copy and Listing

The document describes two C programs: one for copying a file using standard I/O and system calls, and another for emulating the Unix 'ls -l' command to list files and directories with detailed information. The first program uses functions like fopen, fread, and fwrite, while the second program utilizes opendir, readdir, and stat to gather file details. Both programs include error handling and demonstrate basic file operations in C.

Uploaded by

bdayncse
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 views4 pages

C Program for File Copy and Listing

The document describes two C programs: one for copying a file using standard I/O and system calls, and another for emulating the Unix 'ls -l' command to list files and directories with detailed information. The first program uses functions like fopen, fread, and fwrite, while the second program utilizes opendir, readdir, and stat to gather file details. Both programs include error handling and demonstrate basic file operations in C.

Uploaded by

bdayncse
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

M.

Jayachandra Siddhartha N200237

LAB-7
Aim: To implement a C program that makes a copy of a file using standard i/o and system
calls.
a) Description: This program reads the contents of a source file and write them to a
destination file. It use system calls, like open, read, write and close to handle file operations,
ensuring that destination file is an exact copy of source file.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
void copy_file_stdio(const char *source_file, const char *destination_file) {
FILE *src = fopen(source_file, "rb");
FILE *dest = fopen(destination_file, "wb");
if (src == NULL || dest == NULL) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
char buffer[1024];
size_t bytes;
while ((bytes = fread(buffer, 1, sizeof(buffer), src)) > 0) {
fwrite(buffer, 1, bytes, dest);
}
fclose(src);
fclose(dest);
}
void copy_file_syscalls(const char *source_file, const char *destination_file) {
int src = open(source_file, O_RDONLY);
int dest = open(destination_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
M. Jayachandra Siddhartha N200237

if (src < 0 || dest < 0) {


perror("Error opening file");
exit(EXIT_FAILURE);
}
char buffer[1024];
ssize_t bytes;
while ((bytes = read(src, buffer, sizeof(buffer))) > 0) {
write(dest, buffer, bytes);
}
close(src);
close(dest);
}
int main() {
const char *source = "[Link]";
const char *destination_stdio = "destination_stdio.txt";
const char *destination_syscalls = "destination_syscalls.txt";
copy_file_stdio(source, destination_stdio);
printf("File copied using standard I/O functions to %s.\n", destination_stdio);
copy_file_syscalls(source, destination_syscalls);
printf("File copied using system calls to %s.\n", destination_syscalls);
return 0;
}
Output:

Aim: To implement a C program that emulates the Unix ls-l command.


Description: This program lists the files and directories in a specified directory, displaying
detailed information for each including file permissions, no of links, owners, group size, last
modification time and name.
Program:
#include <stdio.h>
M. Jayachandra Siddhartha N200237

#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
void print_permissions(mode_t mode) {
printf((S_ISDIR(mode)) ? "d" : "-");
printf((mode & S_IRUSR) ? "r" : "-");
printf((mode & S_IWUSR) ? "w" : "-");
printf((mode & S_IXUSR) ? "x" : "-");
printf((mode & S_IRGRP) ? "r" : "-");
printf((mode & S_IWGRP) ? "w" : "-");
printf((mode & S_IXGRP) ? "x" : "-");
printf((mode & S_IROTH) ? "r" : "-");
printf((mode & S_IWOTH) ? "w" : "-");
printf((mode & S_IXOTH) ? "x" : "-");
}
void list_directory(const char *path) {
struct dirent *entry;
struct stat file_stat;
DIR *dp = opendir(path);
if (dp == NULL) {
perror("opendir");
return;
}
while ((entry = readdir(dp)) != NULL) {
if (entry->d_name[0] == '.' && (entry->d_name[1] == '\0' || (entry->d_name[1] == '.' &&
entry->d_name[2] == '\0')))
M. Jayachandra Siddhartha N200237

continue;
char file_path[1024];
snprintf(file_path, sizeof(file_path), "%s/%s", path, entry->d_name);
if (stat(file_path, &file_stat) == -1) {
perror("stat");
continue;
}
print_permissions(file_stat.st_mode);
printf(" %lu", file_stat.st_nlink);
struct passwd *pwd = getpwuid(file_stat.st_uid);
printf(" %s", pwd ? pwd->pw_name : "UNKNOWN");
struct group *grp = getgrgid(file_stat.st_gid);
printf(" %s", grp ? grp->gr_name : "UNKNOWN");
printf(" %ld", file_stat.st_size);
char timebuf[64];
strftime(timebuf, sizeof(timebuf), "%b %d %Y %H:%M", localtime(&file_stat.st_mtime));
printf(" %s", timebuf);
printf(" %s\n", entry->d_name);
}
closedir(dp);
}
int main() {
list_directory(".");
}
Output:

You might also like