commit b355e1400f2710eb52128d42407bcec1312c6c8a Author: TriantaTV Date: Thu Mar 30 22:02:58 2023 -0500 Added files diff --git a/Cat b/Cat new file mode 100755 index 0000000..649b527 Binary files /dev/null and b/Cat differ diff --git a/Cat.c b/Cat.c new file mode 100755 index 0000000..87bd47a --- /dev/null +++ b/Cat.c @@ -0,0 +1,120 @@ +#include +#include + +bool CheckArgumentSet(int argc, char* argv[]); +void EchoBox(); +void FileHandler(int argc, char* argv[]); +bool IsFileReadable(FILE* currentFile, char* fileName); +void PrintChosenFile(FILE* currentFile); +void PrintChosenFileNumbered(FILE* currentFile); +int lineNumber = 1; + +int main(int argc, char* argv[]) +{ + if (argc == 1) + EchoBox(); + if (argc > 1) + FileHandler(argc, argv); + return 0; +} + +// Check if line number argument is set +bool CheckArgumentSet(int argc, char* argv[]) +{ + char argumentHyphen = '-'; + char argumentLetter = 'b'; + for (int i = 1; i < argc; i++) + { + if ((*(argv[i]) == argumentHyphen) && (*(argv[i]+1) == argumentLetter)) + { + return 1; + } + } + return 0; +} + +// Handling Cat without arguments +void EchoBox() +{ + char inputText[100]; + while (true) + { + scanf("%s", inputText); + printf("%s\n", inputText); + } +} + +// Base function for passing work to other functions +void FileHandler(int argc, char* argv[]) +{ + FILE* currentFile; + bool argumentEnabled = CheckArgumentSet(argc, argv); + for (int i = 1; i < argc; i++) + { + if ((currentFile = fopen(argv[i], "r")) == NULL) + { + if (!argumentEnabled) + fprintf(stderr, "cat: %s: No such file or directory\n", argv[i]); + continue; + } + if (argumentEnabled) + PrintChosenFileNumbered(currentFile); + if (!argumentEnabled) + PrintChosenFile(currentFile); + fclose(currentFile); + } +} + +// Print text of a chosen file without line numbers +void PrintChosenFile(FILE* currentFile) +{ + char buffer[4]; + while (fgets(buffer, sizeof(buffer), currentFile)) + { + printf("%s", buffer); + } +} + +// Print text of a chosen file with line numbers +void PrintChosenFileNumbered(FILE* currentFile) +{ + bool isNewLine = 0; + char buffer[2]; + printf(" %2d ", lineNumber); + lineNumber = lineNumber + 1; + while (fgets(buffer, sizeof(buffer), currentFile)) + { + if (isNewLine && (buffer[0] == '\n')) + { + printf("%s", buffer); + continue; + } + if (isNewLine && (buffer[0] != '\n')) + { + printf(" %2d %s", lineNumber, buffer); + lineNumber = lineNumber + 1; + isNewLine = 0; + continue; + } + if (!isNewLine && (buffer[0] == '\n')) + { + isNewLine = 1; + printf("%s", buffer); + continue; + } + printf("%s", buffer); + } +} + +// Check if file is readable +// Return true if readable, and false if not readable +// Todo: Fix function, breaks code if used +bool IsFileReadable(FILE* currentFile, char* fileName) +{ + if((currentFile = fopen(fileName, "r")) == NULL) + { + fprintf(stderr, "cat: %s: No such file or directory\n", fileName); + return 0; + } + return 1; +} diff --git a/CatOutput.txt b/CatOutput.txt new file mode 100644 index 0000000..ac387d6 --- /dev/null +++ b/CatOutput.txt @@ -0,0 +1,19 @@ +reowiajroewaj orijewaoi jreowaj rioejwa oirjeawioj rejoia rewa + + +rewijoaroiejwao ijrewaioj reoiwaj roiewja oirjewaoi jrewaoij rewajoirewa + +r eiwjaoirjewoaijr eoiwjaio rejwaoi jrewaoij reoiwaj roeiwja +rewaoijrewoaijreowia jioj reiwoja riojewaio rjewaoijr ewa +rroiewjroiejwoi joirjew oirjewoijrewoij ewiojr ewio +oirjewoijreiow iojr oewij roiewj rioewjrwe +oirejwoirjew iorj woeijr ewoij riowej roeiwj +rioewjoirjewj ireowri jewiojr ewijo rewioj +reowij roiewjaori jewior jewioj rewiojr ewoijr iojwe + +roiewjoirejw iorewioj ewiojr wieojr ioewjr iojewi + + + +r oewijiorejwri oewior jewior jewiojr ewio +r oiewjroi ewjifoj ewijo rewjroiewji orjewiorew diff --git a/CatOutputArguments.txt b/CatOutputArguments.txt new file mode 100644 index 0000000..1ea2b7b --- /dev/null +++ b/CatOutputArguments.txt @@ -0,0 +1,19 @@ + 1 reowiajroewaj orijewaoi jreowaj rioejwa oirjeawioj rejoia rewa + + + 2 rewijoaroiejwao ijrewaioj reoiwaj roiewja oirjewaoi jrewaoij rewajoirewa + + 3 r eiwjaoirjewoaijr eoiwjaio rejwaoi jrewaoij reoiwaj roeiwja + 4 rewaoijrewoaijreowia jioj reiwoja riojewaio rjewaoijr ewa + 5 rroiewjroiejwoi joirjew oirjewoijrewoij ewiojr ewio + 6 oirjewoijreiow iojr oewij roiewj rioewjrwe + 7 oirejwoirjew iorj woeijr ewoij riowej roeiwj + 8 rioewjoirjewj ireowri jewiojr ewijo rewioj + 9 reowij roiewjaori jewior jewioj rewiojr ewoijr iojwe + + 10 roiewjoirejw iorewioj ewiojr wieojr ioewjr iojewi + + + + 11 r oewijiorejwri oewior jewior jewiojr ewio + 12 r oiewjroi ewjifoj ewijo rewjroiewji orjewiorew diff --git a/CatSystem.txt b/CatSystem.txt new file mode 100644 index 0000000..ac387d6 --- /dev/null +++ b/CatSystem.txt @@ -0,0 +1,19 @@ +reowiajroewaj orijewaoi jreowaj rioejwa oirjeawioj rejoia rewa + + +rewijoaroiejwao ijrewaioj reoiwaj roiewja oirjewaoi jrewaoij rewajoirewa + +r eiwjaoirjewoaijr eoiwjaio rejwaoi jrewaoij reoiwaj roeiwja +rewaoijrewoaijreowia jioj reiwoja riojewaio rjewaoijr ewa +rroiewjroiejwoi joirjew oirjewoijrewoij ewiojr ewio +oirjewoijreiow iojr oewij roiewj rioewjrwe +oirejwoirjew iorj woeijr ewoij riowej roeiwj +rioewjoirjewj ireowri jewiojr ewijo rewioj +reowij roiewjaori jewior jewioj rewiojr ewoijr iojwe + +roiewjoirejw iorewioj ewiojr wieojr ioewjr iojewi + + + +r oewijiorejwri oewior jewior jewiojr ewio +r oiewjroi ewjifoj ewijo rewjroiewji orjewiorew diff --git a/CatSystemArguments.txt b/CatSystemArguments.txt new file mode 100644 index 0000000..658c236 --- /dev/null +++ b/CatSystemArguments.txt @@ -0,0 +1,19 @@ + 1 reowiajroewaj orijewaoi jreowaj rioejwa oirjeawioj rejoia rewa + + + 2 rewijoaroiejwao ijrewaioj reoiwaj roiewja oirjewaoi jrewaoij rewajoirewa + + 3 r eiwjaoirjewoaijr eoiwjaio rejwaoi jrewaoij reoiwaj roeiwja + 4 rewaoijrewoaijreowia jioj reiwoja riojewaio rjewaoijr ewa + 5 rroiewjroiejwoi joirjew oirjewoijrewoij ewiojr ewio + 6 oirjewoijreiow iojr oewij roiewj rioewjrwe + 7 oirejwoirjew iorj woeijr ewoij riowej roeiwj + 8 rioewjoirjewj ireowri jewiojr ewijo rewioj + 9 reowij roiewjaori jewior jewioj rewiojr ewoijr iojwe + + 10 roiewjoirejw iorewioj ewiojr wieojr ioewjr iojewi + + + + 11 r oewijiorejwri oewior jewior jewiojr ewio + 12 r oiewjroi ewjifoj ewijo rewjroiewji orjewiorew diff --git a/Test1.txt b/Test1.txt new file mode 100644 index 0000000..8a08076 --- /dev/null +++ b/Test1.txt @@ -0,0 +1,7 @@ +reowiajroewaj orijewaoi jreowaj rioejwa oirjeawioj rejoia rewa + + +rewijoaroiejwao ijrewaioj reoiwaj roiewja oirjewaoi jrewaoij rewajoirewa + +r eiwjaoirjewoaijr eoiwjaio rejwaoi jrewaoij reoiwaj roeiwja +rewaoijrewoaijreowia jioj reiwoja riojewaio rjewaoijr ewa diff --git a/Test2.txt b/Test2.txt new file mode 100644 index 0000000..e74e4c6 --- /dev/null +++ b/Test2.txt @@ -0,0 +1,4 @@ +rroiewjroiejwoi joirjew oirjewoijrewoij ewiojr ewio +oirjewoijreiow iojr oewij roiewj rioewjrwe +oirejwoirjew iorj woeijr ewoij riowej roeiwj +rioewjoirjewj ireowri jewiojr ewijo rewioj diff --git a/Test3.txt b/Test3.txt new file mode 100644 index 0000000..96365e1 --- /dev/null +++ b/Test3.txt @@ -0,0 +1,8 @@ +reowij roiewjaori jewior jewioj rewiojr ewoijr iojwe + +roiewjoirejw iorewioj ewiojr wieojr ioewjr iojewi + + + +r oewijiorejwri oewior jewior jewiojr ewio +r oiewjroi ewjifoj ewijo rewjroiewji orjewiorew diff --git a/TestCat.sh b/TestCat.sh new file mode 100755 index 0000000..a217f07 --- /dev/null +++ b/TestCat.sh @@ -0,0 +1,14 @@ +#!/bin/bash +echo "Compiling..." +gcc -o Cat Cat.c +echo "Running difference tests..." +./Cat Test1.txt Test2.txt Test3.txt > CatOutput.txt +./Cat -b Test1.txt Test2.txt Test3.txt > CatOutputArguments.txt +cat Test1.txt Test2.txt Test3.txt > CatSystem.txt +cat -b Test1.txt Test2.txt Test3.txt > CatSystemArguments.txt +diff -w CatOutput.txt CatSystem.txt +diff -w CatOutputArguments.txt CatSystemArguments.txt +echo "Testing opening fake files..." +./Cat waoirjewa.txt +cat waoirjewa.txt +echo "If above lines match, successfully failed opening fake files."