diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/libmdsserver/macro-bits.h | 159 | ||||
-rw-r--r-- | src/libmdsserver/macros.h | 1 | ||||
-rw-r--r-- | src/libmdsserver/mds-message.c | 2 | ||||
-rw-r--r-- | src/libmdsserver/util.c | 5 | ||||
-rw-r--r-- | src/mds-base.c | 2 | ||||
-rw-r--r-- | src/mds-clipboard.c | 5 | ||||
-rw-r--r-- | src/mds-registry/registry.c | 2 | ||||
-rw-r--r-- | src/mds-registry/slave.c | 1 | ||||
-rw-r--r-- | src/mds-respawn.c | 2 | ||||
-rw-r--r-- | src/mds-server/mds-server.c | 2 | ||||
-rw-r--r-- | src/mds-server/receiving.c | 4 | ||||
-rw-r--r-- | src/mds-server/sending.c | 2 |
12 files changed, 174 insertions, 13 deletions
diff --git a/src/libmdsserver/macro-bits.h b/src/libmdsserver/macro-bits.h new file mode 100644 index 0000000..f1feddd --- /dev/null +++ b/src/libmdsserver/macro-bits.h @@ -0,0 +1,159 @@ +/** + * mds — A micro-display server + * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef MDS_LIBMDSSERVER_MACRO_BITS_H +#define MDS_LIBMDSSERVER_MACRO_BITS_H + + +#include <stdint.h> +#include <stddef.h> + + +#ifndef __WORDSIZE +# if defined(__x86_64__) && !defined(__ILP32__) +# define __WORDSIZE 64 +# else +# define __WORDSIZE 32 +# endif +#endif + + +/** + * Convert the beginning of a `const char*` to a `size_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +#define atoz(str) ((size_t)atol(str)) + +/** + * Convert the beginning of a `const char*` to an `ssize_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +#define atosz(str) ((ssize_t)atol(str)) + +/** + * Convert the beginning of a `const char*` to a `short int` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +#define atoh(str) ((short)atol(str)) + +/** + * Convert the beginning of a `const char*` to an `unsigned short int` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +#define atouh(str) ((unsigned short)atol(str)) + +/** + * Convert the beginning of a `const char*` to an `unsigned int` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +#define atou(str) ((unsigned int)atoi(str)) + +/** + * Convert the beginning of a `const char*` to an `unsigned long int` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +#define atoul(str) ((unsigned long)atol(str)) + +/** + * Convert the beginning of a `const char*` to an `unsigned long long int` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +#define atoull(str) ((unsigned long long)atoll(str)) + +#if __WORDSIZE == 64 +/** + * Convert the beginning of a `const char*` to an `int32_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +# define ato32(str) ((int32_t)atoi(str)) + +/** + * Convert the beginning of a `const char*` to an `uint32_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +# define atou32(str) ((uint32_t)atou(str)) + +/** + * Convert the beginning of a `const char*` to an `int64_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +# define ato64(str) ((int64_t)atol(str)) + +/** + * Convert the beginning of a `const char*` to an `uint64_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +# define atou64(str) ((uint64_t)atoul(str)) +#else +/** + * Convert the beginning of a `const char*` to an `int32_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +# define ato32(str) ((int32_t)atol(str)) + +/** + * Convert the beginning of a `const char*` to an `uint32_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +# define atou32(str) ((uint32_t)atoul(str)) + +/** + * Convert the beginning of a `const char*` to an `int64_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +# define ato64(str) ((int64_t)atoll(str)) + +/** + * Convert the beginning of a `const char*` to an `uint64_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +# define atou64(str) ((uint64_t)atoull(str)) +#endif + + +#endif + diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h index b7eb1d8..2d3f7a3 100644 --- a/src/libmdsserver/macros.h +++ b/src/libmdsserver/macros.h @@ -20,6 +20,7 @@ #include "config.h" +#include "macro-bits.h" #include <stdio.h> #include <errno.h> diff --git a/src/libmdsserver/mds-message.c b/src/libmdsserver/mds-message.c index 5726b7a..8a3590c 100644 --- a/src/libmdsserver/mds-message.c +++ b/src/libmdsserver/mds-message.c @@ -163,7 +163,7 @@ static int get_payload_length(mds_message_t* restrict this) { /* Store the message length. */ header = this->headers[i] + strlen("Length: "); - this->payload_size = (size_t)atoll(header); + this->payload_size = atoz(header); /* Do not except a length that is not correctly formated. */ for (; *header; header++) diff --git a/src/libmdsserver/util.c b/src/libmdsserver/util.c index 469b616..7047d9b 100644 --- a/src/libmdsserver/util.c +++ b/src/libmdsserver/util.c @@ -57,9 +57,8 @@ uint64_t parse_client_id(const char* str) strcpy(client_high = client_words, str); client_low = rawmemchr(client_words, ':'); *client_low++ = '\0'; - client = (uint64_t)atoll(client_high); - client <<= 32; - client |= (uint64_t)atoll(client_low); + client = atou64(client_high) << 32; + client |= atou64(client_low); return client; } diff --git a/src/mds-base.c b/src/mds-base.c index 572202e..be4efbb 100644 --- a/src/mds-base.c +++ b/src/mds-base.c @@ -128,7 +128,7 @@ int __attribute__((weak)) parse_cmdline(void) else if (strequals(arg, "--re-exec")) /* Re-exec state-marshal. */ is_reexec = 1; else if (startswith(arg, "--alarm=")) /* Schedule an alarm signal for forced abort. */ - alarm((unsigned)min(atoi(arg + strlen("--alarm=")), 60)); /* At most 1 minute. */ + alarm(min(atou(arg + strlen("--alarm=")), 60)); /* At most 1 minute. */ else if (strequals(arg, "--on-init-fork")) /* Fork process when initialised. */ on_init_fork = 1; else if (startswith(arg, "--on-init-sh=")) /* Run a command when initialised. */ diff --git a/src/mds-clipboard.c b/src/mds-clipboard.c index 21a2671..6302ceb 100644 --- a/src/mds-clipboard.c +++ b/src/mds-clipboard.c @@ -516,7 +516,7 @@ int handle_message(void) return clipboard_add(level, recv_time_to_live, recv_client_id); } else if (strequals(recv_message_id, "read")) - return clipboard_read(level, (size_t)atoll(recv_index), recv_client_id, recv_message_id); + return clipboard_read(level, atoz(recv_index), recv_client_id, recv_message_id); else if (strequals(recv_message_id, "clear")) return clipboard_clear(level); else if (strequals(recv_message_id, "set-size")) @@ -526,7 +526,7 @@ int handle_message(void) eprint("received request for clipboard resizing without a new size, ignoring."); return 0; } - return clipboard_set_size(level, (size_t)atoll(recv_size)); + return clipboard_set_size(level, atoz(recv_size)); } else if (strequals(recv_message_id, "get-size")) return clipboard_get_size(level, recv_client_id, recv_message_id); @@ -689,6 +689,7 @@ int clipboard_add(int level, const char* time_to_live, const char* recv_client_i struct timespec dethklok; fail_if (monotone(&dethklok)); dethklok.tv_sec += (time_t)atoll(time_to_live); + /* It should really be `atol`, but we want to be future proof. */ new_clip.dethklok = dethklok; } else diff --git a/src/mds-registry/registry.c b/src/mds-registry/registry.c index c8610ca..d2a9c10 100644 --- a/src/mds-registry/registry.c +++ b/src/mds-registry/registry.c @@ -462,7 +462,7 @@ static int handle_register_message(void) /* Get message length, and make sure the action is defined. */ if (recv_length != NULL) - length = (size_t)atoll(recv_length); + length = atoz(recv_length); if (recv_action != NULL) recv_action = "add"; diff --git a/src/mds-registry/slave.c b/src/mds-registry/slave.c index 1be9322..5e714d4 100644 --- a/src/mds-registry/slave.c +++ b/src/mds-registry/slave.c @@ -185,6 +185,7 @@ int start_slave(hash_table_t* restrict wait_set, const char* restrict recv_clien if (monotone(&(slave->dethklok))) goto pfail_in_mutex; slave->dethklok.tv_sec += (time_t)atoll(ttl); + /* It should really be `atol`, but we want to be future proof. */ break; } diff --git a/src/mds-respawn.c b/src/mds-respawn.c index ebe8411..02b466c 100644 --- a/src/mds-respawn.c +++ b/src/mds-respawn.c @@ -105,7 +105,7 @@ int parse_cmdline(void) { char* arg = argv[i]; if (startswith(arg, "--alarm=")) /* Schedule an alarm signal for forced abort. */ - alarm((unsigned)min(atoi(arg + strlen("--alarm=")), 60)); /* At most 1 minute. */ + alarm((unsigned)min(atou(arg + strlen("--alarm=")), 60)); /* At most 1 minute. */ else if (startswith(arg, "--interval=")) interval = min(atoi(arg + strlen("--interval=")), 60); /* At most 1 minute. */ else if (strequals(arg, "--re-exec")) /* Re-exec state-marshal. */ diff --git a/src/mds-server/mds-server.c b/src/mds-server/mds-server.c index b217f2b..85e7c5c 100644 --- a/src/mds-server/mds-server.c +++ b/src/mds-server/mds-server.c @@ -104,7 +104,7 @@ int preinitialise_server(void) eprintf("invalid value for %s: %s.", "--socket-fd", arg);); } else if (startswith(arg, "--alarm=")) /* Schedule an alarm signal for forced abort. */ - alarm((unsigned)min(atoi(arg + strlen("--alarm=")), 60)); /* At most 1 minute. */ + alarm((unsigned)min(atou(arg + strlen("--alarm=")), 60)); /* At most 1 minute. */ else if (!strequals(arg, "--initial-spawn") && !strequals(arg, "--respawn")) /* Not recognised, it is probably for another server. */ diff --git a/src/mds-server/receiving.c b/src/mds-server/receiving.c index 127540b..15ebb4d 100644 --- a/src/mds-server/receiving.c +++ b/src/mds-server/receiving.c @@ -263,8 +263,8 @@ int message_received(client_t* client) else if (strequals(h, "Modifying: yes")) modifying = 1; else if (strequals(h, "Stop: yes")) stop = 1; else if (startswith(h, "Message ID: ")) message_id = strstr(h, ": ") + 2; - else if (startswith(h, "Priority: ")) priority = (int64_t)atoll(strstr(h, ": ") + 2); - else if (startswith(h, "Modify ID: ")) modify_id = (uint64_t)atoll(strstr(h, ": ") + 2); + else if (startswith(h, "Priority: ")) priority = ato64(strstr(h, ": ") + 2); + else if (startswith(h, "Modify ID: ")) modify_id = atou64(strstr(h, ": ") + 2); } diff --git a/src/mds-server/sending.c b/src/mds-server/sending.c index 3b78e54..bc78ef6 100644 --- a/src/mds-server/sending.c +++ b/src/mds-server/sending.c @@ -136,7 +136,7 @@ void multicast_message(multicast_t* multicast) char* value = multicast->message + n; char* lf = strchr(value, '\n'); *lf = '\0'; - modify_id = (uint64_t)atoll(value); + modify_id = atou64(value); *lf = '\n'; } |