uuidd: use UUID_STR_LEN and UUID_LEN

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

[kzak@redhat.com: - use sizeof(uuid_t) rather than hardcoded size]

Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Petr Uzel 2011-02-02 14:04:35 +01:00 committed by Karel Zak
parent 783140784a
commit a8f131986c
1 changed files with 17 additions and 11 deletions

View File

@ -41,6 +41,12 @@ extern int optind;
#define CODE_ATTR(x) #define CODE_ATTR(x)
#endif #endif
/* length of textual representation of UUID, including trailing \0 */
#define UUID_STR_LEN 37
/* length of binary representation of UUID */
#define UUID_LEN (sizeof(uuid_t))
static void usage(const char *progname) static void usage(const char *progname)
{ {
fprintf(stderr, _("Usage: %s [-d] [-p pidfile] [-s socketpath] " fprintf(stderr, _("Usage: %s [-d] [-p pidfile] [-s socketpath] "
@ -174,8 +180,8 @@ static int call_daemon(const char *socket_path, int op, char *buf,
} }
if (op == UUIDD_OP_BULK_RANDOM_UUID) { if (op == UUIDD_OP_BULK_RANDOM_UUID) {
if ((*num)*16 > buflen-4) if ((*num)*UUID_LEN > buflen-4)
*num = (buflen-4) / 16; *num = (buflen-4) / UUID_LEN;
} }
op_buf[0] = op; op_buf[0] = op;
op_len = 1; op_len = 1;
@ -209,8 +215,8 @@ static int call_daemon(const char *socket_path, int op, char *buf,
ret = read_all(s, (char *) buf, reply_len); ret = read_all(s, (char *) buf, reply_len);
if ((ret > 0) && (op == UUIDD_OP_BULK_TIME_UUID)) { if ((ret > 0) && (op == UUIDD_OP_BULK_TIME_UUID)) {
if (reply_len >= (int) (16+sizeof(int))) if (reply_len >= (int) (UUID_LEN + sizeof(int)))
memcpy(buf+16, num, sizeof(int)); memcpy(buf + UUID_LEN, num, sizeof(int));
else else
*num = -1; *num = -1;
} }
@ -236,7 +242,7 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
uuid_t uu; uuid_t uu;
mode_t save_umask; mode_t save_umask;
char reply_buf[1024], *cp; char reply_buf[1024], *cp;
char op, str[37]; char op, str[UUID_STR_LEN];
int i, s, ns, len, num; int i, s, ns, len, num;
int fd_pidfile, ret; int fd_pidfile, ret;
@ -410,19 +416,19 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
num = 1; num = 1;
if (num > 1000) if (num > 1000)
num = 1000; num = 1000;
if (num*16 > (int) (sizeof(reply_buf)-sizeof(num))) if (num * UUID_LEN > (int) (sizeof(reply_buf)-sizeof(num)))
num = (sizeof(reply_buf)-sizeof(num)) / 16; num = (sizeof(reply_buf)-sizeof(num)) / UUID_LEN;
uuid__generate_random((unsigned char *) reply_buf + uuid__generate_random((unsigned char *) reply_buf +
sizeof(num), &num); sizeof(num), &num);
if (debug) { if (debug) {
printf(_("Generated %d UUIDs:\n"), num); printf(_("Generated %d UUIDs:\n"), num);
for (i=0, cp=reply_buf+sizeof(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); uuid_unparse((unsigned char *)cp, str);
printf("\t%s\n", 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)); memcpy(reply_buf, &num, sizeof(num));
break; break;
default: default:
@ -443,7 +449,7 @@ int main(int argc, char **argv)
const char *pidfile_path = UUIDD_PIDFILE_PATH; const char *pidfile_path = UUIDD_PIDFILE_PATH;
const char *err_context; const char *err_context;
char buf[1024], *cp; char buf[1024], *cp;
char str[37], *tmp; char str[UUID_STR_LEN], *tmp;
uuid_t uu; uuid_t uu;
uid_t uid; uid_t uid;
gid_t gid; gid_t gid;
@ -540,7 +546,7 @@ int main(int argc, char **argv)
cp = buf + 4; cp = buf + 4;
if (ret != (int) (sizeof(num) + num*sizeof(uu))) if (ret != (int) (sizeof(num) + num*sizeof(uu)))
goto unexpected_size; 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); uuid_unparse((unsigned char *) cp, str);
printf("\t%s\n", str); printf("\t%s\n", str);
} }