Skip to content

Instantly share code, notes, and snippets.

@tanerochris
Created March 15, 2015 15:06
Show Gist options
  • Save tanerochris/c5e2a30dcf5612a17ced to your computer and use it in GitHub Desktop.
Save tanerochris/c5e2a30dcf5612a17ced to your computer and use it in GitHub Desktop.
An incomplete C implementation of Linux cat command
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<stdlib.h>
#include <stdio.h>
#define SIZE 50
int main(int argc,char* argv[]){
int inputFile;//input file descriptor
int outputFile;//output file descriptor
char ch[SIZE];//buffer to store read characters
if(argc==1)
{
perror("No input file");
exit(EXIT_FAILURE);
}
inputFile=open(argv[1],O_RDONLY);
if(argc==3){
outputFile=open(argv[2],O_CREAT|O_RDWR|O_APPEND,S_IWUSR);
}
else if(argc==2)
outputFile=1;
while(read(inputFile,ch,1)!=0)
{ write(outputFile,ch,1);
}
close(inputFile);
close(outputFile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment