Add input check to generator
This commit is contained in:
parent
9a652f4d4e
commit
833c560621
@ -27,13 +27,29 @@
|
|||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
#include "shared/shm.c"
|
#include "shared/shm.c"
|
||||||
|
#include <regex.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if(argc > 1) {
|
if(argc < 2) {
|
||||||
fprintf(stderr, "ERROR: Command takes no arguments");
|
fprintf(stderr, "ERROR: Command takes at least one argument.\n");
|
||||||
|
puts("Usage: \ngenerator EDGE1 EDGE2 ...");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
regex_t regex;
|
||||||
|
if(regcomp(®ex, "^[0-9]*-[0-9]*$", REG_EXTENDED|REG_NOSUB)) {
|
||||||
|
fprintf(stderr, "ERROR: Could not compile regex\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i=1; i <= argc - 1; i++) {
|
||||||
|
if(regexec(®ex, argv[i], 0, NULL, 0) == REG_NOMATCH) {
|
||||||
|
fprintf(stderr, "ERROR: Incorrect input found\n");
|
||||||
|
puts("Usage: \ngenerator EDGE1 EDGE2 ...");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void* terminate_shm = open_shm(TERMINATE_SHM, TERMINATE_SHM_SIZE);
|
void* terminate_shm = open_shm(TERMINATE_SHM, TERMINATE_SHM_SIZE);
|
||||||
|
|
||||||
write_to_shm(terminate_shm, "1", TERMINATE_SHM_SIZE);
|
write_to_shm(terminate_shm, "1", TERMINATE_SHM_SIZE);
|
||||||
|
@ -13,12 +13,12 @@
|
|||||||
void* open_shm(char *shm_name, size_t shm_size) {
|
void* open_shm(char *shm_name, size_t shm_size) {
|
||||||
int shmfd = shm_open(shm_name, O_RDWR | O_CREAT, 0600);
|
int shmfd = shm_open(shm_name, O_RDWR | O_CREAT, 0600);
|
||||||
if(shmfd == -1) {
|
if(shmfd == -1) {
|
||||||
fprintf(stderr, "ERROR: Failed creating shared memory object");
|
fprintf(stderr, "ERROR: Failed creating shared memory object\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ftruncate(shmfd, shm_size) < 0) {
|
if(ftruncate(shmfd, shm_size) < 0) {
|
||||||
fprintf(stderr, "ERROR: Failed truncating shared memory object");
|
fprintf(stderr, "ERROR: Failed truncating shared memory object\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ void* open_shm(char *shm_name, size_t shm_size) {
|
|||||||
shm = mmap(NULL, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0);
|
shm = mmap(NULL, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0);
|
||||||
|
|
||||||
if(shm == MAP_FAILED) {
|
if(shm == MAP_FAILED) {
|
||||||
fprintf(stderr, "ERROR: Failed mapping shared memory object");
|
fprintf(stderr, "ERROR: Failed mapping shared memory object\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,14 +39,14 @@ void write_to_shm(void* shm, char *message, size_t message_size) {
|
|||||||
|
|
||||||
void close_shm(void* shm, size_t shm_size) {
|
void close_shm(void* shm, size_t shm_size) {
|
||||||
if (munmap(shm, shm_size) == -1) {
|
if (munmap(shm, shm_size) == -1) {
|
||||||
fprintf(stderr, "ERROR: Failed unmapping shared memory object");
|
fprintf(stderr, "ERROR: Failed unmapping shared memory object\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void destroy_shm(char *shm_name) {
|
void destroy_shm(char *shm_name) {
|
||||||
if (shm_unlink(shm_name) == -1) {
|
if (shm_unlink(shm_name) == -1) {
|
||||||
fprintf(stderr, "ERROR: Failed unlinking shared memory object");
|
fprintf(stderr, "ERROR: Failed unlinking shared memory object\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user