This shows you the differences between two versions of the page.
|
so:laboratoare:resurse:die [2015/03/24 00:42] marius.ungureanu93 Removed DIE_CB and DIE_BLOCK |
so:laboratoare:resurse:die [2018/03/02 23:36] (current) mihai.popescu1212 [Macro-ul DIE] |
||
|---|---|---|---|
| Line 14: | Line 14: | ||
| <code c> | <code c> | ||
| + | #include <errno.h> | ||
| #define DIE(assertion, call_description) \ | #define DIE(assertion, call_description) \ | ||
| do { \ | do { \ | ||
| Line 20: | Line 21: | ||
| __FILE__, __LINE__); \ | __FILE__, __LINE__); \ | ||
| perror(call_description); \ | perror(call_description); \ | ||
| - | exit(EXIT_FAILURE); \ | + | exit(errno); \ |
| } \ | } \ | ||
| - | } while(0) | + | } while (0) |
| </code> | </code> | ||
| Line 45: | Line 46: | ||
| int main(void) | int main(void) | ||
| { | { | ||
| - | int fd1, fd2; | + | int fd1, fd2; |
| - | fd1 = open("file1", O_CREAT | O_RDWR, 0644); | + | fd1 = open("file1", O_CREAT | O_RDWR, 0644); |
| - | if (fd1 < 0) { | + | if (fd1 < 0) { |
| - | perror("open file1 failed"); | + | perror("open file1 failed"); |
| - | goto exit_1; | + | goto exit_1; |
| - | } | + | } |
| - | fd2 = open("file2", O_CREAT | O_RDWR, 0644); | + | fd2 = open("file2", O_CREAT | O_RDWR, 0644); |
| - | if (fd2 < 0) { | + | if (fd2 < 0) { |
| - | perror("open file2 failed"); | + | perror("open file2 failed"); |
| - | goto exit_2; | + | goto exit_2; |
| - | } | + | } |
| /* ... rest of the code ...*/ | /* ... rest of the code ...*/ | ||
| - | return 0; | + | return 0; |
| exit_2: | exit_2: | ||
| - | close(fd1); | + | close(fd1); |
| exit_1: | exit_1: | ||
| - | return -1; | + | return -1; |
| } | } | ||
| </code> | </code> | ||
| ==== Concluzie === | ==== Concluzie === | ||
| - | Pentru că aplicațiile din laborator urmăresc în primul rând formarea deprinderii de a verifica **mereu** codul de eroare întors de apelurile de sistem, puteți folosi macro-ul DIE în rezolvarea task-urilor din laborator. | + | Pentru că aplicațiile din laborator și teme urmăresc în primul rând formarea deprinderii de a verifica **mereu** codul de eroare întors de apelurile de sistem, puteți folosi macro-ul DIE în rezolvarea task-urilor din laborator. |
| + | |||