Browse Source

Base

master
zmiguel.alves@gmail.com 2 years ago
parent
commit
f065c60ff7
  1. BIN
      SO-1920-EnunciadoTP-v.9i.pdf
  2. 1
      dec.h
  3. 27
      makefile
  4. 48
      server.c
  5. 23
      util.h
  6. 87
      verificador.c

BIN
SO-1920-EnunciadoTP-v.9i.pdf

Binary file not shown.

1
dec.h

@ -0,0 +1 @@
int obtem_rand(int min, int max);

27
makefile

@ -0,0 +1,27 @@
# Usage:
# make # compile all binary
# make clean # remove ALL binaries and objects
.PHONY = all clean
# compiler to use
CC = gcc
LINKERFLAG = -lm
SRCS := $(wildcard *.c)
BINS := $(SRCS:%.c=%)
all: ${BINS}
%: %.o
@echo "Checking.."
${CC} ${LINKERFLAG} $< -o $@
%.o: %.c
@echo "Creating object.."
${CC} -c $<
clean:
@echo "Cleaning up..."
rm -rvf *.o ${BINS}

48
server.c

@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <ncurses.h>
//declaraao de funcoes
#include "dec.h"
//estructuras e outras coisas uteis
#include "util.h"
int main(int argc, char *argv[]){
int fd_servidor;
/* VERIFICAR SE EXISTE "CP" DO SERVIDOR (access) -- APENAS UM!!!*/
if(access("CPservidor", F_OK)==0){
printf("[SERVIDOR] Ja existe um servidor!\n");
exit(1);
}
/* CRIAR "CP" DO SERVIDOR - MINHA (mkfifo) */
mkfifo("CPservidor", 0600);
/* ABRIR "CP" DO SERVIDOR - MINHA (open - O_RDONLY) */
fd_servidor = open("CPservidor", O_RDWR);
printf("[SERVER] Servidor Iniciado!\n");
/* Fazer coisas aqui! */
do{
}while(true);
printf("[SERVIDOR] SERVIDOR DESLIGADO\n");
/* FECHAR "CP" DO SERVIDOR - MINHA (close) */
close(fd_servidor);
/* REMOVER "CP" DO SERVIDOR- MINHA (UNLINK) */
unlink("CPservidor");
exit(0);
}
int obtem_rand(int min, int max){
int random;
random = min + (rand() % (max - min + 1));
return random;
}

23
util.h

@ -0,0 +1,23 @@
#define TAM_TITULO 128
#define TAM_CORPO 1001
#define TAM_NOME 100
typedef struct msgdef
{
int id;
char titulo[TAM_TITULO];
char corpo[TAM_CORPO];
int duracao;
int time_added;
struct topicdef * topico;
struct msgdef * prox;
}msg;
typedef struct topicdef
{
int id;
char nome[TAM_NOME];
struct msgdef * mensg;
struct topicdef * prox;
}topic;

87
verificador.c

@ -0,0 +1,87 @@
/* verificador.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
/* maximum word def */
#define MAXNW 50
/* maximum word length including \0 */
#define MAXWL 19
/* This function does what its name tells */
void exitNow(int s) {
exit(0);
}
/* reads forbidden word list from file */
/* assume all words have at most 19 chars */
/* file must not have more than one word per line */
/* return number of word read */
int readWordFile(FILE * f, char wdef[][MAXWL], int maxw) {
int numw = 0;
while (!feof(f) && numw < maxw)
if (fscanf(f,"%s", wdef[numw])>0)
numw++;
return numw;
}
/* checks word agains forbidden word dictionary */
/* word must match exactly - partials do not count */
/* returns 0 or 1*/
int checkWord(char * word, char wdef[][MAXWL], int maxw) {
int i;
for (i=0; i<maxw;i++)
if (!strcmp(word,wdef[i]))
return 1;
return 0;
}
/* The main fuction. It's kind of useful to exist */
int main(int argc, char** argv) {
char forbwords[MAXNW][MAXWL];
char word[MAXWL];
int numw; /* number of words */
int numhits; /* hits in this message */
int justprinted;
FILE * wordsf;
if (argc<2) {
printf("ERROR-1\n");
return 1;
}
wordsf = fopen(argv[1], "r");
if (wordsf == NULL) {
printf("ERROR-2\n");
return 2;
}
numw = readWordFile(wordsf, forbwords, MAXNW);
fclose(wordsf);
if (numw < 1) {
printf("ERROR-3\n");
return 3;
}
signal(SIGUSR2, exitNow);
/* preps done. start text processing */
numhits = 0;
justprinted = 0;
while (!feof(stdin)) {
if (scanf("%s", word)<0)
continue;
if (!strcmp(word,"##MSGEND##")) {
printf("%d\n",numhits);
fflush(stdout);
numhits = 0; /* restart counting */
justprinted = 1;
}
else {
numhits += checkWord(word, forbwords, numw);
justprinted = 0;
}
}
if (!justprinted)
printf("%d\n",numhits);
return 0;
}
Loading…
Cancel
Save