Let's create a cat utility
Let's create a cat utility in C. Create a file named cat.c and write the following code.
#include <stdio.h> int main(int argc, char* argv[]) { printf("hello, world\n"); return 0; }
Run the program and confirm the setup.
$ gcc cat.c -o cat && ./cat hello, world
Our cat utility requires one or more arguments as filenames that we're interested in reading. If we don't pass any arguments, we need to guide the user with a help message. We can get the argument count from argc and the argument values from argv. The name of the executable is the first argument. So, argc needs to be greater than 1.
#include <stdio.h> int main(int argc, char* argv[]) { if (argc == 1) { printf("Usage: cat [filename...]\n"); return 1; } return 0; }
Users can read one or more files by passing the names or paths of the files as arguments. So, we need to loop through the argv values.
#include <stdio.h> int main(int argc, char* argv[]) { if (argc == 1) { printf("Usage: cat [filename...]\n"); return 1; } for (int i = 1; i < argc; i++) { // ... } return 0; }
We can open a file using the fopen() function in read mode if the file/path exists and can be opened.
#include <stdio.h> int main(int argc, char* argv[]) { if (argc == 1) { printf("Usage: cat [filename...]\n"); return 1; } for (int i = 1; i < argc; i++) { const char* path = argv[i]; FILE *fp = fopen(path, "r"); } return 0; }
We are ready to read the content of the file. But before that, let's add error-handling code in case we don't get the file pointer for the file we're interested in reading.
#include <stdio.h> int main(int argc, char* argv[]) { if (argc == 1) { printf("Usage: cat [filename...]\n"); return 1; } for (int i = 1; i < argc; i++) { const char* path = argv[i]; FILE *fp = fopen(path, "r"); if (!fp) { perror(path); return 1; } } return 0; }
Now, we can loop through each character using a while loop until we reach the end of the file, or EOF. We can get each character from the file using the fgetc() function and print it on the screen using the putchar() function.
#include <stdio.h> int main(int argc, char* argv[]) { if (argc == 1) { printf("Usage: cat [filename...]\n"); return 1; } for (int i = 1; i < argc; i++) { const char* path = argv[i]; FILE *fp = fopen(path, "r"); if (!fp) { perror(path); return 1; } int c; while ((c = fgetc(fp)) != EOF) { putchar(c); } } return 0; }
Finally, let's close the opened file after we're done with the operation.
#include <stdio.h> int main(int argc, char* argv[]) { if (argc == 1) { printf("Usage: cat [filename...]\n"); return 1; } for (int i = 1; i < argc; i++) { const char* path = argv[i]; FILE *fp = fopen(path, "r"); if (!fp) { perror(path); return 1; } int c; while ((c = fgetc(fp)) != EOF) { putchar(c); } fclose(fp); } return 0; }
Let's run the executable and confirm that it works.
$ gcc cat.c -o cat $ ./cat Usage: cat [filename...] $ ./cat cat.c [content of cat.c will be printed] $ ./cat cat.c touch.c [content of cat.c and touch.c will be printed]
Comments
Post a Comment