From b355e1400f2710eb52128d42407bcec1312c6c8a Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Thu, 30 Mar 2023 22:02:58 -0500 Subject: [PATCH] Added files --- Cat | Bin 0 -> 16568 bytes Cat.c | 120 +++++++++++++++++++++++++++++++++++++++++ CatOutput.txt | 19 +++++++ CatOutputArguments.txt | 19 +++++++ CatSystem.txt | 19 +++++++ CatSystemArguments.txt | 19 +++++++ Test1.txt | 7 +++ Test2.txt | 4 ++ Test3.txt | 8 +++ TestCat.sh | 14 +++++ 10 files changed, 229 insertions(+) create mode 100755 Cat create mode 100755 Cat.c create mode 100644 CatOutput.txt create mode 100644 CatOutputArguments.txt create mode 100644 CatSystem.txt create mode 100644 CatSystemArguments.txt create mode 100644 Test1.txt create mode 100644 Test2.txt create mode 100644 Test3.txt create mode 100755 TestCat.sh diff --git a/Cat b/Cat new file mode 100755 index 0000000000000000000000000000000000000000..649b52748f0b118ed45228363b05451a81339993 GIT binary patch literal 16568 zcmeHOdu$xV8J~0F#DpZag99OjFUdet*-8M%dDMiE?eCkNZ@ss@ zYowMxTIt5x{pS09kD2*qW<7Je^NrxTHAOz3;51HrQ4kj>w~>GnjIK}_kbqbrrrP$mbx!iEJ?t|3vf%a^KD0Zc`itx0yplvTW! zHc~^7n6lg{Pm^LHJamR?A5*s-xy)4BcA1)N`aNa0Pf@lbmhAeJU7xaJI!qNpV#@KP zn9#LC#nVfx$Owt4*RECBd1-@fCYVwgl(rl-BmYTxo0OfdNjJhvBQ`9Ua(OqxPIZye z`1gdnd9x}nUq572{F$ooibZX)re#YOwM8o1V)1lmW#@`zmCKg+QwjfKSuoX0!$o6i z?S^dvZu$vRVH8Cfn`eI{t59;%KQZA8Kk41`?m%PDhld0Ixcpb6BabIYhw4o>q(h1P z@em=N@>6h;j*N%zo5+bG!lvRn6n(ROFHk8TY$wM9`vOM4=&>S)-iITg|1B8<0j_!}?p<0MzC zFgqST$YRLyyt^zG!;mZ%DTP%?e!SSB`1H)!MKrpmlEuA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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."