Added files
This commit is contained in:
commit
b355e1400f
120
Cat.c
Executable file
120
Cat.c
Executable file
@ -0,0 +1,120 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
19
CatOutput.txt
Normal file
19
CatOutput.txt
Normal file
@ -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
|
19
CatOutputArguments.txt
Normal file
19
CatOutputArguments.txt
Normal file
@ -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
|
19
CatSystem.txt
Normal file
19
CatSystem.txt
Normal file
@ -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
|
19
CatSystemArguments.txt
Normal file
19
CatSystemArguments.txt
Normal file
@ -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
|
7
Test1.txt
Normal file
7
Test1.txt
Normal file
@ -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
|
4
Test2.txt
Normal file
4
Test2.txt
Normal file
@ -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
|
8
Test3.txt
Normal file
8
Test3.txt
Normal file
@ -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
|
14
TestCat.sh
Executable file
14
TestCat.sh
Executable file
@ -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."
|
Loading…
Reference in New Issue
Block a user