|
@@ -7,57 +7,56 @@
|
|
|
#include "test_helpers.h"
|
|
|
|
|
|
int main(void) {
|
|
|
- int pid, pip[2];
|
|
|
+ int pip[2];
|
|
|
char instring[20];
|
|
|
- char * outstring = "Hello World!";
|
|
|
+ char *outstring = "Hello World!";
|
|
|
|
|
|
- if (pipe(pip) < 0) {
|
|
|
- perror("pipe");
|
|
|
- exit(EXIT_FAILURE);
|
|
|
- }
|
|
|
+ int pipe_status = pipe(pip);
|
|
|
+ ERROR_IF(pipe, pipe_status, == -1);
|
|
|
+ UNEXP_IF(pipe, pipe_status, != 0);
|
|
|
|
|
|
- pid = fork();
|
|
|
- if (pid == 0) /* child : sends message to parent*/
|
|
|
- {
|
|
|
- /* close read end */
|
|
|
- close(pip[0]);
|
|
|
+ int pid = fork();
|
|
|
+ ERROR_IF(fork, pid, == -1);
|
|
|
|
|
|
- /* send 7 characters in the string, including end-of-string */
|
|
|
- int bytes = write(pip[1], outstring, strlen(outstring));
|
|
|
+ if (pid == 0) {
|
|
|
+ // child: sends message to parent
|
|
|
+ // close read end
|
|
|
+ int cr = close(pip[0]);
|
|
|
+ ERROR_IF(close, cr, == -1);
|
|
|
+ UNEXP_IF(close, cr, != 0);
|
|
|
|
|
|
- /* close write end */
|
|
|
- close(pip[1]);
|
|
|
+ // send 7 characters in the string, including end-of-string
|
|
|
+ int bytes = write(pip[1], outstring, strlen(outstring));
|
|
|
+ ERROR_IF(write, bytes, == -1);
|
|
|
|
|
|
- /* check result */
|
|
|
- if (bytes < 0) {
|
|
|
- perror("pipe write");
|
|
|
- exit(EXIT_FAILURE);
|
|
|
- } else if (bytes != strlen(outstring)) {
|
|
|
+ // check result
|
|
|
+ if (bytes != strlen(outstring)) {
|
|
|
fprintf(stderr, "pipe write: %d != %ld\n", bytes, strlen(outstring));
|
|
|
exit(EXIT_FAILURE);
|
|
|
}
|
|
|
|
|
|
+ // close write end
|
|
|
+ int cw = close(pip[1]);
|
|
|
+ ERROR_IF(close, cw, == -1);
|
|
|
+ UNEXP_IF(close, cw, != 0);
|
|
|
+
|
|
|
exit(EXIT_SUCCESS);
|
|
|
- }
|
|
|
- else /* parent : receives message from child */
|
|
|
- {
|
|
|
- /* close write end */
|
|
|
- close(pip[1]);
|
|
|
+ } else {
|
|
|
+ // parent: receives message from child
|
|
|
+ // close write end
|
|
|
+ int cw = close(pip[1]);
|
|
|
+ ERROR_IF(close, cw, == -1);
|
|
|
+ UNEXP_IF(close, cw, != 0);
|
|
|
|
|
|
- /* clear memory */
|
|
|
+ // clear memory
|
|
|
memset(instring, 0, sizeof(instring));
|
|
|
|
|
|
- /* read from the pipe */
|
|
|
+ // read from the pipe
|
|
|
int bytes = read(pip[0], instring, sizeof(instring) - 1);
|
|
|
+ ERROR_IF(read, bytes, == -1);
|
|
|
|
|
|
- /* close read end */
|
|
|
- close(pip[0]);
|
|
|
-
|
|
|
- /* check result */
|
|
|
- if (bytes < 0) {
|
|
|
- perror("pipe read");
|
|
|
- exit(EXIT_FAILURE);
|
|
|
- } else if (bytes != strlen(outstring)) {
|
|
|
+ // check result
|
|
|
+ if (bytes != strlen(outstring)) {
|
|
|
fprintf(stderr, "pipe read: %d != %ld\n", bytes, strlen(outstring));
|
|
|
exit(EXIT_FAILURE);
|
|
|
} else if (memcmp(instring, outstring, strlen(outstring)) != 0) {
|
|
@@ -67,6 +66,11 @@ int main(void) {
|
|
|
printf("%s\n", instring);
|
|
|
}
|
|
|
|
|
|
+ // close read end
|
|
|
+ int cr = close(pip[0]);
|
|
|
+ ERROR_IF(close, cr, == -1);
|
|
|
+ UNEXP_IF(close, cr, != 0);
|
|
|
+
|
|
|
exit(EXIT_SUCCESS);
|
|
|
}
|
|
|
}
|