Advertisement
alsagone

check_h

Oct 7th, 2020
1,723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. #ifndef CHECK_H
  2. #define CHECK_H
  3. /*
  4.  * This file provides a cool macro to check the return values
  5.  * without polluting your beautiful code. Few examples:
  6.  *
  7.  * CHECK((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) != -1);
  8.  * CHECK((pid = fork()) != -1);
  9.  * CHECK(wait(NULL) != -1);
  10.  *
  11.  * In your code, just add this to use it:
  12.  * #include "check.h"
  13.  * -Given to MI students by Cedric Bastoul
  14.  */
  15.  
  16. #include <stdio.h> // For fprintf, perror
  17. #include <stdlib.h> // For exit
  18.  
  19. #define CHECK(x) \
  20.   do { \
  21.     if (!(x)) { \
  22.       fprintf(stderr, "%s:%d: ", __func__, __LINE__); \
  23.       perror(#x); \
  24.       exit(EXIT_FAILURE); \
  25.     } \
  26.   } while (0)
  27.  
  28.  
  29. #endif /* __CHECK_H__ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement