From: brarcher Date: Wed, 25 Dec 2013 03:04:59 +0000 (+0000) Subject: use only FILE* API for file reading/writing X-Git-Tag: 0.10.0~252 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ca05d6367326ea4d8c9d7ba8c0e93ad0a4374a55;p=check use only FILE* API for file reading/writing MSVC does not like switching between read()/write() API and FILE* API using fileno(). The fileno() call seems to return something that does not work with write(). Keeping the API used consistent removes this problem. git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@964 64e312b2-a51f-0410-8e61-82d0ca0eb02a --- diff --git a/src/check_msg.c b/src/check_msg.c index de262cc..318e03b 100644 --- a/src/check_msg.c +++ b/src/check_msg.c @@ -83,34 +83,30 @@ static FILE * get_pipe(void) void send_failure_info(const char *msg) { FailMsg fmsg; - int pipe_fileno = fileno(get_pipe()); fmsg.msg = (char *) msg; - ppack(pipe_fileno, CK_MSG_FAIL, (CheckMsg *) &fmsg); + ppack(get_pipe(), CK_MSG_FAIL, (CheckMsg *) &fmsg); } void send_duration_info(int duration) { DurationMsg dmsg; - int pipe_fileno = fileno(get_pipe()); dmsg.duration = duration; - ppack(pipe_fileno, CK_MSG_DURATION, (CheckMsg *) &dmsg); + ppack(get_pipe(), CK_MSG_DURATION, (CheckMsg *) &dmsg); } void send_loc_info(const char * file, int line) { LocMsg lmsg; - int pipe_fileno = fileno(get_pipe()); lmsg.file = (char *) file; lmsg.line = line; - ppack(pipe_fileno, CK_MSG_LOC, (CheckMsg *) &lmsg); + ppack(get_pipe(), CK_MSG_LOC, (CheckMsg *) &lmsg); } void send_ctx_info(enum ck_result_ctx ctx) { CtxMsg cmsg; - int pipe_fileno = fileno(get_pipe()); cmsg.ctx = ctx; - ppack(pipe_fileno, CK_MSG_CTX, (CheckMsg *) &cmsg); + ppack(get_pipe(), CK_MSG_CTX, (CheckMsg *) &cmsg); } TestResult *receive_test_result (int waserror) @@ -118,7 +114,6 @@ TestResult *receive_test_result (int waserror) FILE *fp; RcvMsg *rmsg; TestResult *result; - int pipe_fileno; fp = get_pipe(); if (fp == NULL) @@ -127,9 +122,7 @@ TestResult *receive_test_result (int waserror) } rewind(fp); - - pipe_fileno = fileno(fp); - rmsg = punpack (pipe_fileno); + rmsg = punpack (fp); if(rmsg == NULL) { diff --git a/src/check_msg.h b/src/check_msg.h index 2ea76ba..aeae548 100644 --- a/src/check_msg.h +++ b/src/check_msg.h @@ -34,4 +34,6 @@ TestResult *receive_test_result(int waserror); void setup_messaging(void); void teardown_messaging(void); +FILE * open_tmp_file (char ** name); + #endif /*CHECK_MSG_NEW_H */ diff --git a/src/check_pack.c b/src/check_pack.c index 6bed735..12a1b85 100644 --- a/src/check_pack.c +++ b/src/check_pack.c @@ -71,7 +71,7 @@ static void check_type (int type, const char *file, int line); static enum ck_msg_type upack_type (char **buf); static void pack_type (char **buf, enum ck_msg_type type); -static int read_buf (int fdes, int size, char *buf); +static int read_buf (FILE* fdes, int size, char *buf); static int get_result (char *buf, RcvMsg *rmsg); static void rcvmsg_update_ctx (RcvMsg *rmsg, enum ck_result_ctx ctx); static void rcvmsg_update_loc (RcvMsg *rmsg, const char *file, int line); @@ -290,7 +290,7 @@ static void ppack_cleanup( void *mutex ) } #endif -void ppack (int fdes, enum ck_msg_type type, CheckMsg *msg) +void ppack (FILE* fdes, enum ck_msg_type type, CheckMsg *msg) { char *buf; int n; @@ -303,22 +303,25 @@ void ppack (int fdes, enum ck_msg_type type, CheckMsg *msg) pthread_cleanup_push( ppack_cleanup, &ck_mutex_lock ); pthread_mutex_lock(&ck_mutex_lock); - r = write (fdes, buf, n); + r = fwrite(buf, 1, n, fdes); + fflush(fdes); pthread_mutex_unlock(&ck_mutex_lock); pthread_cleanup_pop(0); - if (r == -1) - eprintf ("Error in call to write:", __FILE__, __LINE__ - 2); + if (r != n) + eprintf ("Error in call to fwrite:", __FILE__, __LINE__ - 2); free (buf); } -static int read_buf (int fdes, int size, char *buf) +static int read_buf (FILE* fdes, int size, char *buf) { int n; + n = fread(buf, 1, size, fdes); - n = read (fdes, buf, size); - if (n == -1) - eprintf ("Error in call to read:", __FILE__, __LINE__ - 4); + if (ferror(fdes)) + { + eprintf ("Error in call to fread:", __FILE__, __LINE__ - 4); + } return n; } @@ -421,7 +424,7 @@ static void rcvmsg_update_loc (RcvMsg *rmsg, const char *file, int line) } } -RcvMsg *punpack (int fdes) +RcvMsg *punpack (FILE* fdes) { int nread, nparse, n; char *buf; diff --git a/src/check_pack.h b/src/check_pack.h index f7be4aa..a0eb294 100644 --- a/src/check_pack.h +++ b/src/check_pack.h @@ -76,7 +76,7 @@ void rcvmsg_free (RcvMsg *rmsg); int pack (enum ck_msg_type type, char **buf, CheckMsg *msg); int upack (char *buf, CheckMsg *msg, enum ck_msg_type *type); -void ppack (int fdes, enum ck_msg_type type, CheckMsg *msg); -RcvMsg *punpack (int fdes); +void ppack (FILE* fdes, enum ck_msg_type type, CheckMsg *msg); +RcvMsg *punpack (FILE* fdes); #endif /*CHECK_PACK_H */ diff --git a/tests/check_check_pack.c b/tests/check_check_pack.c index d6f931b..dad75c3 100644 --- a/tests/check_check_pack.c +++ b/tests/check_check_pack.c @@ -8,6 +8,7 @@ #include "check_pack.h" #include "check_error.h" #include "check_check.h" +#include "check_msg.h" static char errm[512]; @@ -191,24 +192,25 @@ END_TEST #if defined(HAVE_FORK) && HAVE_FORK==1 START_TEST(test_ppack) { - int filedes[2]; + FILE * result_file; + char * result_file_name = NULL; CtxMsg cmsg; LocMsg lmsg; FailMsg fmsg; RcvMsg *rmsg; - int pipe_result; cmsg.ctx = CK_CTX_TEST; lmsg.file = (char *) "abc123.c"; lmsg.line = 10; fmsg.msg = (char *) "oops"; - pipe_result = pipe (filedes); - ck_assert_msg (pipe_result == 0, "Failed to create pipe"); - ppack (filedes[1], CK_MSG_CTX, (CheckMsg *) &cmsg); - ppack (filedes[1], CK_MSG_LOC, (CheckMsg *) &lmsg); - ppack (filedes[1], CK_MSG_FAIL, (CheckMsg *) &fmsg); - close (filedes[1]); - rmsg = punpack (filedes[0]); + result_file = open_tmp_file(&result_file_name); + free(result_file_name); + ppack (result_file, CK_MSG_CTX, (CheckMsg *) &cmsg); + ppack (result_file, CK_MSG_LOC, (CheckMsg *) &lmsg); + ppack (result_file, CK_MSG_FAIL, (CheckMsg *) &fmsg); + + rewind(result_file); + rmsg = punpack (result_file); ck_assert_msg (rmsg != NULL, "Return value from ppack should always be malloc'ed"); @@ -231,21 +233,22 @@ END_TEST START_TEST(test_ppack_noctx) { - int filedes[2]; + FILE * result_file; + char * result_file_name = NULL; LocMsg lmsg; FailMsg fmsg; RcvMsg *rmsg; - int pipe_result; lmsg.file = (char *) "abc123.c"; lmsg.line = 10; fmsg.msg = (char *) "oops"; - pipe_result = pipe (filedes); - ck_assert_msg (pipe_result == 0, "Failed to create pipe"); - ppack (filedes[1], CK_MSG_LOC, (CheckMsg *) &lmsg); - ppack (filedes[1], CK_MSG_FAIL, (CheckMsg *) &fmsg); - close (filedes[1]); - rmsg = punpack (filedes[0]); + result_file = open_tmp_file(&result_file_name); + free(result_file_name); + ppack (result_file, CK_MSG_LOC, (CheckMsg *) &lmsg); + ppack (result_file, CK_MSG_FAIL, (CheckMsg *) &fmsg); + + rewind(result_file); + rmsg = punpack (result_file); ck_assert_msg (rmsg == NULL, "Result should be NULL with no CTX"); @@ -257,17 +260,17 @@ END_TEST START_TEST(test_ppack_onlyctx) { - int filedes[2]; + FILE * result_file; + char * result_file_name = NULL; CtxMsg cmsg; RcvMsg *rmsg; - int pipe_result; cmsg.ctx = CK_CTX_SETUP; - pipe_result = pipe (filedes); - ck_assert_msg (pipe_result == 0, "Failed to create pipe"); - ppack (filedes[1], CK_MSG_CTX, (CheckMsg *) &cmsg); - close (filedes[1]); - rmsg = punpack (filedes[0]); + result_file = open_tmp_file(&result_file_name); + free(result_file_name); + ppack (result_file, CK_MSG_CTX, (CheckMsg *) &cmsg); + rewind(result_file); + rmsg = punpack (result_file); ck_assert_msg (rmsg != NULL && rmsg->msg == NULL, "Result message should be NULL with only CTX"); @@ -283,26 +286,26 @@ END_TEST START_TEST(test_ppack_multictx) { - int filedes[2]; + FILE * result_file; + char * result_file_name = NULL; CtxMsg cmsg; LocMsg lmsg; RcvMsg *rmsg; - int pipe_result; cmsg.ctx = CK_CTX_SETUP; lmsg.line = 5; lmsg.file = (char *) "abc123.c"; - pipe_result = pipe (filedes); - ck_assert_msg (pipe_result == 0, "Failed to create pipe"); - ppack (filedes[1], CK_MSG_CTX, (CheckMsg *) &cmsg); - ppack (filedes[1], CK_MSG_LOC, (CheckMsg *) &lmsg); + result_file = open_tmp_file(&result_file_name); + free(result_file_name); + ppack (result_file, CK_MSG_CTX, (CheckMsg *) &cmsg); + ppack (result_file, CK_MSG_LOC, (CheckMsg *) &lmsg); cmsg.ctx = CK_CTX_TEST; - ppack (filedes[1], CK_MSG_CTX, (CheckMsg *) &cmsg); - ppack (filedes[1], CK_MSG_LOC, (CheckMsg *) &lmsg); + ppack (result_file, CK_MSG_CTX, (CheckMsg *) &cmsg); + ppack (result_file, CK_MSG_LOC, (CheckMsg *) &lmsg); cmsg.ctx = CK_CTX_TEARDOWN; - ppack (filedes[1], CK_MSG_CTX, (CheckMsg *) &cmsg); - close (filedes[1]); - rmsg = punpack (filedes[0]); + ppack (result_file, CK_MSG_CTX, (CheckMsg *) &cmsg); + rewind(result_file); + rmsg = punpack (result_file); ck_assert_msg (rmsg != NULL && rmsg->test_line == 5, "Test loc not being preserved on CTX change"); @@ -316,21 +319,21 @@ END_TEST START_TEST(test_ppack_nofail) { - int filedes[2]; + FILE * result_file; + char * result_file_name = NULL; CtxMsg cmsg; LocMsg lmsg; RcvMsg *rmsg; - int pipe_result; lmsg.file = (char *) "abc123.c"; lmsg.line = 10; cmsg.ctx = CK_CTX_SETUP; - pipe_result = pipe (filedes); - ck_assert_msg (pipe_result == 0, "Failed to create pipe"); - ppack (filedes[1], CK_MSG_CTX, (CheckMsg *) &cmsg); - ppack (filedes[1], CK_MSG_LOC, (CheckMsg *) &lmsg); - close (filedes[1]); - rmsg = punpack (filedes[0]); + result_file = open_tmp_file(&result_file_name); + free(result_file_name); + ppack (result_file, CK_MSG_CTX, (CheckMsg *) &cmsg); + ppack (result_file, CK_MSG_LOC, (CheckMsg *) &lmsg); + rewind (result_file); + rmsg = punpack (result_file); ck_assert_msg (rmsg != NULL && rmsg->msg == NULL, "Failure result should be NULL with no failure message"); @@ -343,12 +346,12 @@ END_TEST START_TEST(test_ppack_big) { - int filedes[2]; + FILE * result_file; + char * result_file_name = NULL; CtxMsg cmsg; LocMsg lmsg; FailMsg fmsg; RcvMsg *rmsg; - int pipe_result; cmsg.ctx = CK_CTX_TEST; lmsg.file = emalloc (BIG_MSG_LEN); @@ -358,13 +361,13 @@ START_TEST(test_ppack_big) fmsg.msg = emalloc (BIG_MSG_LEN); memset (fmsg.msg, 'a', BIG_MSG_LEN - 1); fmsg.msg[BIG_MSG_LEN - 1] = '\0'; - pipe_result = pipe (filedes); - ck_assert_msg (pipe_result == 0, "Failed to create pipe"); - ppack (filedes[1], CK_MSG_CTX, (CheckMsg *) &cmsg); - ppack (filedes[1], CK_MSG_LOC, (CheckMsg *) &lmsg); - ppack (filedes[1], CK_MSG_FAIL, (CheckMsg *) &fmsg); - close (filedes[1]); - rmsg = punpack (filedes[0]); + result_file = open_tmp_file(&result_file_name); + free(result_file_name); + ppack (result_file, CK_MSG_CTX, (CheckMsg *) &cmsg); + ppack (result_file, CK_MSG_LOC, (CheckMsg *) &lmsg); + ppack (result_file, CK_MSG_FAIL, (CheckMsg *) &fmsg); + rewind (result_file); + rmsg = punpack (result_file); ck_assert_msg (rmsg != NULL, "Return value from ppack should always be malloc'ed");