Skip to content

Commit

Permalink
Merge pull request #1 from nuno-silva/ex1
Browse files Browse the repository at this point in the history
Exercise 1
  • Loading branch information
nuno-silva committed Oct 15, 2014
2 parents 7ef3c54 + bf4ad1a commit 4398ce8
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,18 @@
*.i*86
*.x86_64
*.hex

# Project Specific Files
*.txt

# temporary save files
*.swp

# c9.io folder
.c9/*

*.zip

# binaries
src/reader
src/writer
26 changes: 26 additions & 0 deletions src/Makefile
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
84 changes: 84 additions & 0 deletions src/reader.c
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;
}
13 changes: 13 additions & 0 deletions src/reader.h
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
14 changes: 14 additions & 0 deletions src/reader_main.c
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);
}
14 changes: 14 additions & 0 deletions src/shared_stuff.c
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"
};
17 changes: 17 additions & 0 deletions src/shared_stuff.h
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
30 changes: 30 additions & 0 deletions src/writer.c
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];
}
21 changes: 21 additions & 0 deletions src/writer.h
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__
19 changes: 19 additions & 0 deletions src/writer_main.c
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;
}

0 comments on commit 4398ce8

Please sign in to comment.