-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcat.c
58 lines (45 loc) · 1.41 KB
/
cat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// I pledge my honor that I have abided by the Stevens Honor System. - Aparajita Rana
// a simplified version of the “cat” shell command,
// which reads all contents from the input file and prints the data to the standard output
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
//cat shell command -> reads all contents from the input file and prints data to standard output
void cat(char *name){
FILE *file=fopen(name, "r");
if(file==NULL){
printf("failed to open the input file\n");
exit(1);
}
//first, let's determine the size of the file
//trace to the end of the file
fseek(file, 0L, SEEK_END);
//now that we are at the end, we can determine the size
long size;
size=ftell(file);
//reset
rewind(file);
//Next, we are asked to:
//use dynamically allocated memory as destination/source of stream read/write
char *vals;
vals=malloc(size);
fread(vals, 1, size, file);
fwrite(vals, size, 1, stdout);
//we are told to also use free -> frees the memory
free(vals);
//we done
fclose(file);
}
//main method for the application of the cat.c files
int main(int argc, char ** argv)
{
//the program should only be taking one argument, the path
//this if statement address the case there are too many/too little arguments
if(argc != 2) {
printf("Error regarding arguments");
exit(1);
}
//correct amount of arguments, lets try running cat
cat(argv[1]);
return(0);
}