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 | 25 ++++++++++++++----------- shlibs/uuid/src/gen_uuid.c | 6 +++--- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/misc-utils/uuidd.c b/misc-utils/uuidd.c index b7e97b7..80434d2 100644 --- a/misc-utils/uuidd.c +++ b/misc-utils/uuidd.c @@ -42,6 +42,9 @@ extern int optind; #define CODE_ATTR(x) #endif +/* length of textual representation of UUID, including trailing \0 */ +#define UUID_STR_LEN 37 + static void usage(const char *progname) { fprintf(stderr, _("Usage: %s [-d] [-p pidfile] [-s socketpath] " @@ -141,8 +144,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) * sizeof(uuid_t) > buflen-4) + *num = (buflen-4) / sizeof(uuid_t); } op_buf[0] = op; op_len = 1; @@ -176,8 +179,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) (sizeof(uuid_t) + sizeof(int))) + memcpy(buf + sizeof(uuid_t), num, sizeof(int)); else *num = -1; } @@ -203,7 +206,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; @@ -375,19 +378,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 * sizeof(uuid_t) > (int) (sizeof(reply_buf)-sizeof(num))) + num = (sizeof(reply_buf)-sizeof(num)) / sizeof(uuid_t); 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+=sizeof(uuid_t)) { uuid_unparse((unsigned char *)cp, str); printf("\t%s\n", str); } } - reply_len = (num*16) + sizeof(num); + reply_len = (num * sizeof(uuid_t)) + sizeof(num); memcpy(reply_buf, &num, sizeof(num)); break; default: @@ -408,7 +411,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; @@ -505,7 +508,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+=sizeof(uuid_t)) { uuid_unparse((unsigned char *) cp, str); printf("\t%s\n", str); } diff --git a/shlibs/uuid/src/gen_uuid.c b/shlibs/uuid/src/gen_uuid.c index a90bd8e..c71f186 100644 --- a/shlibs/uuid/src/gen_uuid.c +++ b/shlibs/uuid/src/gen_uuid.c @@ -483,7 +483,7 @@ static int get_uuid_via_daemon(int op, uuid_t out, int *num) int op_len; int s; ssize_t ret; - int32_t reply_len = 0, expected = 16; + int32_t reply_len = 0, expected = sizeof(uuid_t); struct sockaddr_un srv_addr; struct stat st; pid_t pid; @@ -541,9 +541,9 @@ static int get_uuid_via_daemon(int op, uuid_t out, int *num) ret = read_all(s, op_buf, reply_len); if (op == UUIDD_OP_BULK_TIME_UUID) - memcpy(op_buf+16, num, sizeof(int)); + memcpy(op_buf+sizeof(uuid_t), num, sizeof(int)); - memcpy(out, op_buf, 16); + memcpy(out, op_buf, sizeof(uuid_t)); close(s); return ((ret == expected) ? 0 : -1); -- 1.7.1 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html