bssolver/view.c

62 lines
1.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "error.h"
#include "shr_mem.h"
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAX_SIZE 300
#define MAX_AMOUNT_SIZE 10
int main(int argc, char * argv[]) {
// sleep(2);
char amount[MAX_AMOUNT_SIZE];
switch (argc) {
case 1:
fgets(amount, MAX_AMOUNT_SIZE, stdin);
amount[strcspn(amount, "\n")] = '\0';
break;
//read(STDIN_FILENO, readAddr, MAX_SIZE); break;
case 2:
strcpy(amount, argv[1]);
break;
default:
printError("View must receive amount of files by argument or stdin");
}
int files = atoi(amount);
printf("%d\n", files);
int fd;
char * shmPointer;
if ((fd = shm_open("BottlerSHM", O_RDONLY, S_IRUSR)) < 0)
printSystemError("shm_open() -- ERROR");
struct stat statBuf;
fstat(fd, &statBuf);
if ((shmPointer = mmap(0, statBuf.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED){
printSystemError("mmap() -- ERROR");
}
void * shmStart;
shmStart = shmPointer;
char buf[MAX_SIZE];
while (files-- > 0) {
shmPointer += readShm(shmPointer, buf, MAX_SIZE);
printf("%s\n", buf);
}
if (munmap(shmStart, statBuf.st_size) == -1)
printSystemError("munmap() -- ERROR");
close(fd);
return EXIT_SUCCESS;
}