-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from nuno-silva/ex1
Exercise 1
- Loading branch information
Showing
10 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
CC = gcc | ||
CFLAGS = -g | ||
|
||
all: writer reader | ||
|
||
|
||
writer: writer_main.o writer.o shared_stuff.o | ||
|
||
writer_main.o: writer_main.c writer.h | ||
writer.o: writer.c writer.h shared_stuff.h | ||
|
||
|
||
|
||
reader: reader_main.o reader.o shared_stuff.o | ||
|
||
reader_main.o: reader_main.c reader.h | ||
reader.o: reader.c reader.h shared_stuff.h | ||
|
||
shared_stuff.o: shared_stuff.c shared_stuff.h | ||
|
||
clean: | ||
rm -rf writer reader *.o *.txt | ||
|
||
zip: | ||
rm -f grupo-46.zip | ||
zip grupo-46.zip *.c *.h Makefile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
|
||
#include "shared_stuff.h" | ||
#include "reader.h" | ||
|
||
const static int reader_f_flags = O_RDONLY; | ||
|
||
|
||
int reader(int file_num){ | ||
char file_name[64]; | ||
int fd, file_value; | ||
sprintf(file_name, FILENAME, file_num); // place file_num in FILENAME | ||
fd = open(file_name, reader_f_flags); | ||
|
||
if (fd == -1){ // error opening the file (does not exist?) | ||
return FILE_IS_INVALID; | ||
} | ||
|
||
if ( file_contents_are_valid(fd, WRITER_STRING_LEN, LINES_PER_FILE) == TRUE ){ | ||
file_value = FILE_IS_VALID; | ||
}else{ | ||
file_value = FILE_IS_INVALID; | ||
} | ||
|
||
close(fd); | ||
return file_value; | ||
} | ||
|
||
|
||
int file_contents_are_valid(int fd, int line_length, int line_count) | ||
{ | ||
// number of bytes to read at a time from the file | ||
int line_size = line_length * sizeof(char); | ||
|
||
char *first_line = (char*) malloc(line_size); | ||
|
||
// try to read and validate the first line of the file | ||
if ( read(fd, first_line, line_size) != line_size ) { | ||
// the first line doesn't even have the correct number of characters | ||
free(first_line); | ||
return FALSE; // file is invalid | ||
} | ||
if ( !known_writer_string(first_line, line_size) ){ | ||
free(first_line); | ||
return FALSE; // file is invalid | ||
} | ||
|
||
// the first line is valid, so we can compare it with all other lines | ||
char *line_buffer = (char*) malloc(line_size); | ||
|
||
// compare all lines with the first line | ||
int i; | ||
for ( i = 1 ; read(fd, line_buffer, line_size) == line_size; i++ ) | ||
{ | ||
if ( strncmp(line_buffer, first_line, line_size) != 0 || i >= line_count){ | ||
free(first_line); | ||
free(line_buffer); | ||
return FALSE; // file is invalid | ||
} | ||
} | ||
|
||
free(first_line); | ||
free(line_buffer); | ||
|
||
if (i != line_count) | ||
return FALSE; | ||
|
||
return TRUE; // file is valid | ||
} | ||
|
||
int known_writer_string(char *str, int str_size){ | ||
int i; | ||
|
||
for ( i = 0; i < WRITER_STRING_COUNT; i++ ){ | ||
if ( strncmp(str, writer_strings[i], str_size) == 0 ){ | ||
return TRUE; | ||
} | ||
} | ||
|
||
return FALSE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef __READER_H__ | ||
#define __READER_H__ | ||
|
||
|
||
#define FILE_IS_VALID 0 | ||
#define FILE_IS_INVALID -1 | ||
|
||
int reader(int file_num); | ||
int file_contents_are_valid(int fd, int line_length, int line_count); | ||
int known_writer_string(char *str, int str_size); | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "shared_stuff.h" | ||
#include "reader.h" | ||
#include <time.h> | ||
#include <stdlib.h> | ||
|
||
int main () { | ||
int file_num; | ||
|
||
srand (time(NULL)); | ||
|
||
file_num = RANDOM_RANGE(0, 4); | ||
|
||
return reader(file_num); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "shared_stuff.h" | ||
|
||
char writer_strings [ WRITER_STRING_COUNT ][ WRITER_STRING_LEN + 1 ] = { | ||
"aaaaaaaaa\n", | ||
"bbbbbbbbb\n", | ||
"ccccccccc\n", | ||
"ddddddddd\n", | ||
"eeeeeeeee\n", | ||
"fffffffff\n", | ||
"ggggggggg\n", | ||
"hhhhhhhhh\n", | ||
"iiiiiiiii\n", | ||
"jjjjjjjjj\n" | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef __SHARED_STUFF_H__ | ||
#define __SHARED_STUFF_H__ | ||
|
||
|
||
#define WRITER_STRING_LEN 10 | ||
#define WRITER_STRING_COUNT 10 | ||
#define LINES_PER_FILE 1024 | ||
|
||
#define FILENAME "SO2014-%d.txt" | ||
|
||
#define RANDOM_RANGE(MIN, MAX) (( rand() % ( MAX + 1 - MIN ) ) + MIN ) | ||
|
||
extern char writer_strings [ WRITER_STRING_COUNT ][ WRITER_STRING_LEN + 1 ]; | ||
|
||
#define TRUE 1 | ||
#define FALSE 0 | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <stdio.h> | ||
#include <fcntl.h> | ||
|
||
|
||
#include "writer.h" | ||
#include "shared_stuff.h" | ||
|
||
|
||
const static int writer_f_flags = O_WRONLY | O_CREAT | O_TRUNC; | ||
const static int writer_f_mode = S_IRUSR | S_IWUSR | S_IROTH; | ||
|
||
void writer(int file_num, char *txt, int txt_len){ | ||
char file_name[64]; | ||
int fd, i, txt_size; | ||
|
||
sprintf(file_name, FILENAME, file_num); // place file_num in FILENAME | ||
fd = open(file_name, writer_f_flags, writer_f_mode); | ||
|
||
txt_len *= sizeof(char); | ||
for (i = 0; i < LINES_PER_FILE; i++){ | ||
write(fd, txt, txt_len); | ||
} | ||
|
||
close(fd); | ||
} | ||
|
||
|
||
char *get_writer_string(int index){ | ||
return (char*) writer_strings[index]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef __WRITER_H__ | ||
#define __WRITER_H__ | ||
|
||
#define CYCLE_COUNT 5120 | ||
|
||
|
||
/** | ||
* @brief Opens the file specified by file_num and writes to it. | ||
* @param file_nume the number of the file | ||
* @param txt pointer to the string to write | ||
* @param txt_size number of characters in txt to write | ||
*/ | ||
void writer(int file_num, char *txt, int txt_len); | ||
|
||
|
||
/** | ||
* @brief Get a writer string by its index | ||
*/ | ||
char * get_writer_string( int index ); | ||
|
||
#endif //__WRITER_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "writer.h" | ||
#include <time.h> | ||
#include "shared_stuff.h" | ||
#include <stdlib.h> | ||
|
||
int main (int argc, char *argv[]){ | ||
int i, file_num; | ||
char *rand_str; | ||
|
||
srand (time(NULL)); | ||
|
||
for ( i = 0; i < CYCLE_COUNT; i++ ){ | ||
file_num = RANDOM_RANGE(0, 4); | ||
rand_str = get_writer_string( RANDOM_RANGE(0, WRITER_STRING_COUNT - 1) ); | ||
writer(file_num, rand_str, WRITER_STRING_LEN); | ||
} | ||
|
||
return 0; | ||
} |