[PATCH 2/2] Update error messages to use RNET_ERROR

Sergio Durigan Junior sergiodj en sergiodj.net
Mie Oct 22 02:57:09 UTC 2014


This commit updates the generation of error messages.  Instead of using
"fprintf (stderr,...)", they now use the macro RNET_ERROR.
---
 decfile.c    | 46 ++++++++++++++++++++--------------------------
 rnetclient.c | 36 ++++++++++++++++--------------------
 2 files changed, 36 insertions(+), 46 deletions(-)

diff --git a/decfile.c b/decfile.c
index 0baf5d2..e7a48c4 100644
--- a/decfile.c
+++ b/decfile.c
@@ -1,6 +1,7 @@
 /*
  *  Copyright (C) 2012-2014  Thadeu Lima de Souza Cascardo <cascardo em minaslivre.org>
  *  Copyright (C) 2014  Alexandre Oliva <lxoliva em fsfla.org>
+ *  Copyright (C) 2014  Sergio Durigan Junior <sergiodj em sergiodj.net>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -18,10 +19,9 @@
  */
 
 #define _GNU_SOURCE
+#include "rnet_defs.h"
 #include "decfile.h"
 #include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
 #include <errno.h>
 #include <unistd.h>
 #include <gcrypt.h>
@@ -211,7 +211,7 @@ static int parse_header_2014(struct pmhash *hash, char *buffer)
 	parse("hash", 10);
 
 	if (p - buffer != RNET_HEADER_HEAD_2014) {
-		fprintf(stderr, "RNET_HEADER_HEAD_2014 in decfile.h needs to be adjusted to %ti\n", p - buffer);
+		RNET_ERROR("RNET_HEADER_HEAD_2014 in decfile.h needs to be adjusted to %li\n", p - buffer);
 		goto out_val;
 	}
 
@@ -294,25 +294,22 @@ static int parse_header_2014(struct pmhash *hash, char *buffer)
 	parse("controle", 10);
 
 	if (*p++ != '\r') {
-		fprintf(stderr,
-			"missing CR at the %tith header character\n",
-			p - buffer);
+		RNET_ERROR("missing CR at the %lith header character\n",
+			   p - buffer);
 		goto out_val;
 	} else if (*p++ != '\n') {
-		fprintf(stderr,
-			"missing LF at the %tith header character\n",
-			p - buffer);
+		RNET_ERROR("missing LF at the %lith header character\n",
+			   p - buffer);
 		goto out_val;
 	} else if (*p != 0) {
-		fprintf(stderr,
-			"missing NUL at the %tith header character\n",
-			p - buffer);
+		RNET_ERROR("missing NUL at the %lith header character\n",
+			   p - buffer);
 		goto out_val;
 	} else if (p - buffer != RNET_HEADER_SIZE_2014) {
-		fprintf(stderr, "RNET_HEADER_SIZE_2014 in decfile.h needs to be adjusted to %ti,\nor parse_header in decfile.c needs updating\n", p - buffer);
+		RNET_ERROR("RNET_HEADER_SIZE_2014 in decfile.h needs to be adjusted to %li,\nor parse_header in decfile.c needs updating\n", p - buffer);
 		goto out_val;
 	} else if (p - tail != RNET_HEADER_TAIL_2014) {
-		fprintf(stderr, "RNET_HEADER_TAIL_2014 in decfile.h needs to be adjusted to %ti\n", p - tail);
+		RNET_ERROR("RNET_HEADER_TAIL_2014 in decfile.h needs to be adjusted to %li\n", p - tail);
 		goto out_val;
 	}
 
@@ -365,7 +362,7 @@ static int parse_header_2013(struct pmhash *hash, char *buffer)
 	parse("hash", 10);
 
 	if (p - buffer != RNET_HEADER_HEAD_2013) {
-		fprintf(stderr, "RNET_HEADER_HEAD_2013 in decfile.h needs to be adjusted to %ti\n", p - buffer);
+		RNET_ERROR("RNET_HEADER_HEAD_2013 in decfile.h needs to be adjusted to %li\n", p - buffer);
 		goto out_val;
 	}
 
@@ -443,25 +440,22 @@ static int parse_header_2013(struct pmhash *hash, char *buffer)
 	parse("controle", 10);
 
 	if (*p++ != '\r') {
-		fprintf(stderr,
-			"missing CR at the %tith header character\n",
-			p - buffer);
+		RNET_ERROR("missing CR at the %lith header character\n",
+			   p - buffer);
 		goto out_val;
 	} else if (*p++ != '\n') {
-		fprintf(stderr,
-			"missing LF at the %tith header character\n",
-			p - buffer);
+		RNET_ERROR("missing LF at the %lith header character\n",
+			   p - buffer);
 		goto out_val;
 	} else if (*p != 0) {
-		fprintf(stderr,
-			"missing NUL at the %tith header character\n",
-			p - buffer);
+		RNET_ERROR("missing NUL at the %lith header character\n",
+			   p - buffer);
 		goto out_val;
 	} else if (p - buffer != RNET_HEADER_SIZE_2013) {
-		fprintf(stderr, "RNET_HEADER_SIZE_2013 in decfile.h needs to be adjusted to %ti,\nor parse_header in decfile.c needs updating\n", p - buffer);
+		RNET_ERROR("RNET_HEADER_SIZE_2013 in decfile.h needs to be adjusted to %li,\nor parse_header in decfile.c needs updating\n", p - buffer);
 		goto out_val;
 	} else if (p - tail != RNET_HEADER_TAIL_2013) {
-		fprintf(stderr, "RNET_HEADER_TAIL_2013 in decfile.h needs to be adjusted to %ti\n", p - tail);
+		RNET_ERROR("RNET_HEADER_TAIL_2013 in decfile.h needs to be adjusted to %li\n", p - tail);
 		goto out_val;
 	}
 
diff --git a/rnetclient.c b/rnetclient.c
index e37a723..42e7464 100644
--- a/rnetclient.c
+++ b/rnetclient.c
@@ -18,10 +18,8 @@
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#define _GNU_SOURCE
+#include "rnet_defs.h"
 #include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -34,7 +32,6 @@
 #include <gnutls/gnutls.h>
 #include <zlib.h>
 #include <argp.h>
-#include "config.h"
 #include "decfile.h"
 #include "rnet_message.h"
 #include "rnet_encode.h"
@@ -358,11 +355,11 @@ static void save_rec_file(char *cpf, char *buffer, int len, const struct rnetcli
 	else {
 		struct stat st;
 		if (stat(args->output_dir, &st) < 0) {
-			fprintf(stderr, "Could not stat directory \"%s\": %s\n", args->output_dir, strerror(errno));
+			RNET_ERROR("Could not stat directory \"%d\": %s\n", args->output_dir, strerror(errno));
 			return;
 		}
 		if (!S_ISDIR(st.st_mode)) {
-			fprintf(stderr, "Error: \"%s\" is a not a directory.\n", args->output_dir);
+			RNET_ERROR("Error: \"%s\" is a not a directory.\n", args->output_dir);
 			return;
 		}
 		path = args->output_dir;
@@ -401,16 +398,16 @@ static void save_rec_file(char *cpf, char *buffer, int len, const struct rnetcli
 	/* Now, open the file and write.  */
 	fd = open(fname, O_CREAT | O_WRONLY | O_EXCL, S_IRUSR | S_IWUSR);
 	if (fd < 0) {
-		fprintf(stderr, "Could not create receipt file \"%s\": %s\n", fname, strerror(errno));
+		RNET_ERROR("Could not create receipt file \"%s\": %s\n", fname, strerror(errno));
 		return;
 	}
 	do {
 		r = write(fd, buffer, len);
 	} while (r < 0 && errno == EAGAIN);
 	if (r != len)
-		fprintf(stderr, "Could not write to receipt file: %s", strerror(errno));
+		RNET_ERROR("Could not write to receipt file: %s", strerror(errno));
 	else
-		fprintf(stderr, "Wrote the receipt file to %s.\n", fname);
+		RNET_ERROR("Wrote the receipt file to %s.\n", fname);
 	close(fd);
 }
 
@@ -419,7 +416,7 @@ static void handle_response_text_and_file(char *cpf, struct rnet_message *messag
 	char *value;
 	int vlen;
 	if (!rnet_message_parse(message, "texto", &value, &vlen))
-		fprintf(stderr, "%.*s\n", vlen, value);
+		RNET_ERROR("%.*s\n", vlen, value);
 	if (!rnet_message_parse(message, "arquivo", &value, &vlen))
 		save_rec_file(cpf, value, vlen, args);
 }
@@ -434,8 +431,8 @@ static void handle_response_error(struct rnet_message *message)
 	char *value;
 	int vlen;
 	if (!rnet_message_parse(message, "texto", &value, &vlen))
-		fprintf(stderr, "%.*s\n", vlen, value);
-	fprintf(stderr, "Error transmiting DEC file.\n");
+		RNET_ERROR("%.*s\n", vlen, value);
+	RNET_ERROR("Error transmiting DEC file.\n");
 }
 
 int main(int argc, char **argv)
@@ -461,11 +458,11 @@ int main(int argc, char **argv)
 	memset(&rnet_args, 0, sizeof (rnet_args));
 	err = argp_parse (&rnetclient_argp, argc, argv, 0, NULL, &rnet_args);
 	if (err != 0)
-		fprintf(stderr, "internal error while parsing command line arguments.");
+		RNET_ERROR("internal error while parsing command line arguments.");
 
 	decfile = rnet_decfile_open(rnet_args.input_file);
 	if (!decfile) {
-		fprintf(stderr, "could not parse file \"%s\": %s\n", rnet_args.input_file, strerror(errno));
+		RNET_ERROR("could not parse file \"%s\": %s\n", rnet_args.input_file, strerror(errno));
 		exit(1);
 	}
 
@@ -476,8 +473,8 @@ int main(int argc, char **argv)
 	session_new(&session);
 	r = connect_rnet(&c);
 	if (r) {
-		fprintf(stderr, "error connecting to server: %s\n",
-			r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
+		RNET_ERROR("error connecting to server: %s\n",
+			   r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
 		exit(1);
 	}
 	gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t)(intptr_t) c);
@@ -486,8 +483,7 @@ int main(int argc, char **argv)
 		exit(1);
 	}
 	if ((r = gnutls_handshake(session)) < 0)
-		fprintf(stderr, "error in handshake: %s\n",
-				gnutls_strerror(r));
+		RNET_ERROR("error in handshake: %s\n", gnutls_strerror(r));
 
 	rnet_encode(decfile, &message);
 	rnet_send(session, message->buffer, message->len, 1);
@@ -496,7 +492,7 @@ int main(int argc, char **argv)
 	message = NULL;
 	r = rnet_recv(session, &message);
 	if (r || !message || message->len == 0) {
-		fprintf(stderr, "error when receiving response\n");
+		RNET_ERROR("error when receiving response\n");
 		goto out;
 	}
 	switch (message->buffer[0]) {
@@ -528,7 +524,7 @@ int main(int argc, char **argv)
 	message = NULL;
 	r = rnet_recv(session, &message);
 	if (r || !message || message->len == 0) {
-		fprintf(stderr, "error when receiving response\n");
+		RNET_ERROR("error when receiving response\n");
 		goto out;
 	}
 	switch (message->buffer[0]) {
-- 
1.9.3



Más información sobre la lista de distribución Softwares-impostos