How To Write System Calls in C Program

To write a ‘C’ program to implement system calls.
Step by Step Procedure Algorithm
1. Start the program.
2. Read source filename from the user.
3. Read destination filename from the user.
4. Open source file name in read mode.
5. Open destination file name in write mode.
6. Read a block of data from source file and store it in the buffer.
7. If the destination file does not exist, create the source file and then open.
8. Write the block of data to the destination from buffer.
9. Repeat steps 6 and 8 until the block of data in source file becomes zero.
10. Close source file and destination file.
11. End.
Source Code Programming Algorithm
#include
#include
#define SIZE 500
main()
{
system("clear");
char buf[SIZE];
char *d;
char dn[20],sn[20];
int fd,fd1,n;
system("echo Enter the source file name");
scanf("%s",&sn);
system("echo Enter the destination file name");
scanf("%s",&dn);
fd=open(sn,0);
fd1=create(dn,0755);
fd1=open(dn,1);
while((n=read(fd,buf,512))>0)
write(fd1,buf,n);
close(fd);
close(fd1);
}
Output
"system.c" 22L, 391C written
[staff@linux-router staff]$ gcc system.c
[staff@linux-router staff]$ ./a.out

Enter the source file name
jas
Enter the source file name
jeg
[staff@linux-router staff]$ cat jas
hi
u r
always
welcome
[staff@linux-router staff]$ cat jeg
hi
u r
always
welcome
[staff@linux-router staff]$
Result : Thus a ‘C’ program to implement system calls was written and executed.

Related post