[PATCH] uuidd: use UUID_STR_LEN and UUID_LEN

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Use named constants for lentgh of UUID's binary and textual
representation, instead of magic numbers.

Signed-off-by: Petr Uzel <petr.uzel@xxxxxxx>
---
 misc-utils/uuidd.c |   28 +++++++++++++++++-----------
 1 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/misc-utils/uuidd.c b/misc-utils/uuidd.c
index af74172..5ed944a 100644
--- a/misc-utils/uuidd.c
+++ b/misc-utils/uuidd.c
@@ -41,6 +41,12 @@ extern int optind;
 #define CODE_ATTR(x)
 #endif
 
+/* length of textual representation of UUID, including trailing \0 */
+#define UUID_STR_LEN 37
+
+/* length of binary representation of UUID */
+#define UUID_LEN 16
+
 static void usage(const char *progname)
 {
 	fprintf(stderr, _("Usage: %s [-d] [-p pidfile] [-s socketpath] "
@@ -172,8 +178,8 @@ static int call_daemon(const char *socket_path, int op, char *buf,
 	}
 
 	if (op == UUIDD_OP_BULK_RANDOM_UUID) {
-		if ((*num)*16 > buflen-4)
-			*num = (buflen-4) / 16;
+		if ((*num)*UUID_LEN > buflen-4)
+			*num = (buflen-4) / UUID_LEN;
 	}
 	op_buf[0] = op;
 	op_len = 1;
@@ -207,8 +213,8 @@ static int call_daemon(const char *socket_path, int op, char *buf,
 	ret = read_all(s, (char *) buf, reply_len);
 
 	if ((ret > 0) && (op == UUIDD_OP_BULK_TIME_UUID)) {
-		if (reply_len >= (int) (16+sizeof(int)))
-			memcpy(buf+16, num, sizeof(int));
+		if (reply_len >= (int) (UUID_LEN + sizeof(int)))
+			memcpy(buf + UUID_LEN, num, sizeof(int));
 		else
 			*num = -1;
 	}
@@ -234,7 +240,7 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
 	uuid_t			uu;
 	mode_t			save_umask;
 	char			reply_buf[1024], *cp;
-	char			op, str[37];
+	char			op, str[UUID_STR_LEN];
 	int			i, s, ns, len, num;
 	int			fd_pidfile, ret;
 
@@ -406,19 +412,19 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
 				num = 1;
 			if (num > 1000)
 				num = 1000;
-			if (num*16 > (int) (sizeof(reply_buf)-sizeof(num)))
-				num = (sizeof(reply_buf)-sizeof(num)) / 16;
+			if (num * UUID_LEN > (int) (sizeof(reply_buf)-sizeof(num)))
+				num = (sizeof(reply_buf)-sizeof(num)) / UUID_LEN;
 			uuid__generate_random((unsigned char *) reply_buf +
 					      sizeof(num), &num);
 			if (debug) {
 				printf(_("Generated %d UUIDs:\n"), num);
 				for (i=0, cp=reply_buf+sizeof(num);
-				     i < num; i++, cp+=16) {
+				     i < num; i++, cp+=UUID_LEN) {
 					uuid_unparse((unsigned char *)cp, str);
 					printf("\t%s\n", str);
 				}
 			}
-			reply_len = (num*16) + sizeof(num);
+			reply_len = (num * UUID_LEN) + sizeof(num);
 			memcpy(reply_buf, &num, sizeof(num));
 			break;
 		default:
@@ -439,7 +445,7 @@ int main(int argc, char **argv)
 	const char	*pidfile_path = UUIDD_PIDFILE_PATH;
 	const char	*err_context;
 	char		buf[1024], *cp;
-	char		str[37], *tmp;
+	char		str[UUID_STR_LEN], *tmp;
 	uuid_t		uu;
 	uid_t		uid;
 	gid_t		gid;
@@ -536,7 +542,7 @@ int main(int argc, char **argv)
 			cp = buf + 4;
 			if (ret != (int) (sizeof(num) + num*sizeof(uu)))
 				goto unexpected_size;
-			for (i=0; i < num; i++, cp+=16) {
+			for (i=0; i < num; i++, cp+=UUID_LEN) {
 				uuid_unparse((unsigned char *) cp, str);
 				printf("\t%s\n", str);
 			}
-- 
1.7.1


Petr

--
Petr Uzel
IRC: ptr_uzl @ freenode

Attachment: pgpgygcyEmfPO.pgp
Description: PGP signature


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux