100% found this document useful (2 votes)
3K views2 pages

Implement in C The Cat Unix Command Using System Calls

The documents provide code implementations of the cat, ls, and mv Unix commands using C and system calls. The cat implementation opens and reads a file and prints the contents to standard output. The ls implementation uses scandir() to get a list of files in a directory and prints them. The mv implementation renames a source file to a destination by opening both files, renaming with rename(), and unlinking the source file.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
100% found this document useful (2 votes)
3K views2 pages

Implement in C The Cat Unix Command Using System Calls

The documents provide code implementations of the cat, ls, and mv Unix commands using C and system calls. The cat implementation opens and reads a file and prints the contents to standard output. The ls implementation uses scandir() to get a list of files in a directory and prints them. The mv implementation renames a source file to a destination by opening both files, renaming with rename(), and unlinking the source file.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 2

Implement in C the cat Unix command using system calls

#include<fcntl.h>
#include<sys/stat.h>
#define BUFSIZE 1
int main(int argc, char **argv)
{
int fd1;
int n;
char buf;
fd1=open(argv[1],O_RDONLY);
printf("SuhritSolutions Printing Files\n");
while((n=read(fd1,&buf,1))>0)
{
printf("%c",buf);
/* or
write(1,&buf,1); */

}
return (0);
}

--------------------------------------------------------

Implement in C the following ls Unix


command using system calls

#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>
#include <stdio.h>

#define FALSE 0
#define TRUE 1

extern int alphasort();

char pathname[MAXPATHLEN];

main() {
int count,i;
struct dirent **files;
int file_select();

if (getwd(pathname) == NULL )
{ printf("Error getting pathn");
exit(0);
}
printf("Current Working Directory = %sn",pathname);
count = scandir(pathname, &files, file_select, alphasort);

if (count <= 0)
{ printf("No files in this directoryn");
exit(0);
}
printf("Number of files = %dn",count);
for (i=1;i<count+1;++i)
printf("%s \n",files[i-1]->d_name);

int file_select(struct direct *entry)

{
if ((strcmp(entry->d_name, ".") == 0) ||(strcmp(entry->d_name, "..") == 0))
return (FALSE);
else
return (TRUE);
}

----------------------------------------------------------------------------------
--

Implement in C the Unix command mv using system calls

#include<fcntl.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
int main(int argc, char **argv)
{
int fd1,fd2;
int n,count=0;
fd1=open(argv[1],O_RDONLY);
fd2=creat(argv[2],S_IWUSR);
rename(fd1,fd2);
unlink(argv[1]);
return (0);
}

You might also like