Add input check to generator

This commit is contained in:
Ivaylo Ivanov 2019-01-11 17:23:54 +01:00
parent 9a652f4d4e
commit 833c560621
2 changed files with 23 additions and 7 deletions

View File

@ -27,13 +27,29 @@
*
**/
#include "shared/shm.c"
#include <regex.h>
int main(int argc, char *argv[]) {
if(argc > 1) {
fprintf(stderr, "ERROR: Command takes no arguments");
if(argc < 2) {
fprintf(stderr, "ERROR: Command takes at least one argument.\n");
puts("Usage: \ngenerator EDGE1 EDGE2 ...");
exit(EXIT_FAILURE);
}
regex_t regex;
if(regcomp(&regex, "^[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(&regex, 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);
write_to_shm(terminate_shm, "1", TERMINATE_SHM_SIZE);

View File

@ -13,12 +13,12 @@
void* open_shm(char *shm_name, size_t shm_size) {
int shmfd = shm_open(shm_name, O_RDWR | O_CREAT, 0600);
if(shmfd == -1) {
fprintf(stderr, "ERROR: Failed creating shared memory object");
fprintf(stderr, "ERROR: Failed creating shared memory object\n");
exit(EXIT_FAILURE);
}
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);
}
@ -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);
if(shm == MAP_FAILED) {
fprintf(stderr, "ERROR: Failed mapping shared memory object");
fprintf(stderr, "ERROR: Failed mapping shared memory object\n");
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) {
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);
}
}
void destroy_shm(char *shm_name) {
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);
}
}