Add compare content

This commit is contained in:
vedri 2021-08-08 15:05:45 +02:00
parent 10cdbca4e3
commit 9cf6d57ceb
3 changed files with 96 additions and 0 deletions

10
2108-compare/Makefile Normal file
View File

@ -0,0 +1,10 @@
## Simple Makefile by botanicals
## Code available at https://git.disroot.org/botanicals/blog-files
# Compiler and flags
CC=gcc
CFLAGS=-Wall -Wextra -pedantic
LDLIBS=-lm # math.h library
# No recipes necessary.
# `make` builds using implicit rules.

41
2108-compare/README.md Normal file
View File

@ -0,0 +1,41 @@
# Compare SHA Sums
## Idea
I wanted to practice working with files in C programs and at the same time check if the SHA sums for a Manjaro ISO file were the same.
## Prerequisites
- [GNU make](https://gnu.org/software/make)
- [gcc](https://gnu.org/software/gcc)
## Usage
Place the `compare.c` and `Makefile` in the same directory. Build `compare`.
``` sh
make compare
```
Either download or create a text file. The first line of the text file should contain only the SHA sum provided by the developer.
Example of `sha-test.txt`:
``` txt
6ff0ebecb9813542e1fa81bbe897bb59039bb00d
```
Append to the file a SHA sum you generated yourself.
``` sh
sha1sum ISO >> sha-test.txt
```
Run `compare` and enter the name of the file with SHA sums when prompted.
``` sh
$ ./compare
Filename: sha-test.txt
# The program prints whether SHA sums match or not.
```

45
2108-compare/compare.c Normal file
View File

@ -0,0 +1,45 @@
/* Compare by botanicals
* Available at https://git.disroot.org/botanicals/blog-files
*
* This program compares two strings, each in its own line,
* of SHA sums read from a file.
*
* Read the projects README for more details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
// Declarations
FILE *sha;
char name[100];
char sha1[100];
char sha2[100];
char c[2];
// Collect the relevant filename
printf("Filename: ");
scanf("%s", name);
// Open file
if ((sha = fopen(name, "rt")) == NULL) {
printf("Cannot open file!\n");
exit(EXIT_FAILURE);
}
// Read the file content; run the compare function
fscanf(sha, "%[^\n]", sha1);
fscanf(sha, "%[\n]", c);
fscanf(sha, "%[^ ]", sha2);
if (strcmp(sha1, sha2) == 0)
printf("SHA sums are the same!\n");
else
printf("SHA sums are not the same! Do not use.\n");
// Close file; exit program
fclose(sha);
return 0;
}