/* AC 17/18 * TPC4 Ex1 */ #include "hw.h" #include "driver.h" #define LSR 0x3fd #define RBR 0x3f8 #define RXBIT 1 #define ERRBIT 2 /* baseado no exemplo da aula teorica 16 */ unsigned char recvByte( void ) { int status; while ( ((status = in(LSR)) & RXBIT)==0 || (status & ERRBIT)!=0 ) ; // espera enquanto não chega byte ou ha’ erro return in( RBR ); } /*********************************************************/ /* AC 17/18 * TPC4 Ex2 */ #include #include #include #define BUFSZ 1024 #define MAX(X, Y) (X > Y ? X : Y) // maximo entre X e Y // quem nao conhece strstr() necessita desta funcao para procurar uma // string dentro de outra: char *procuraStr(char *linha, char *str) { // str tem de ter pelo menos um char int i = 0; // seria mais compacto usando apenas pointers do { while (linha[i] != '\0' && linha[i] != str[0]) // enquanto diferentes i++; if (linha[i] == '\0') return NULL; // nao existe int j = 0; while (linha[i+j] != '\0' && linha[i+j] == str[j]) // enquanto iguais j++; if (str[j] == '\0') return linha+i; // encontrou uma na posicao i i++; } while (linha[i] != '\0'); return NULL; // nao existe } int main(int argc, char *argv[]) { if (argc != 5) { fprintf(stderr, "Wrong number of arguments.\n"); return 1; } char *from = argv[1]; char *to = argv[2]; int sz = MAX(strlen(from), BUFSZ); char *buf = malloc(sz + 1); FILE *infile = fopen(argv[3], "r"); FILE *outfile = fopen(argv[4], "w"); if (infile == NULL || outfile == NULL) { fprintf(stderr, "Problem with files.\n"); return 2; } while (fgets(buf, sz, infile) != NULL) { char *ptr = buf; char *str; while ((str = strstr(ptr, from)) != NULL) { // ou usando procuraStr() fwrite(ptr, 1, str - ptr, outfile); fwrite(to, 1, strlen(to), outfile); ptr = str + strlen(from); } if (strlen(ptr) > 0) fwrite(ptr, 1, strlen(ptr), outfile); } return 0; }