Use const char* for timestamp strings which we don't plan to modify

Makes the intent a bit clearer.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-05-24 09:36:56 +02:00
parent d3d280242c
commit 4d9685be5f
5 changed files with 29 additions and 31 deletions

View file

@ -474,8 +474,9 @@ static int print_session_status_info(sd_bus *bus, const char *path, bool *new_li
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
char since2[FORMAT_TIMESTAMP_MAX], *s2;
char since1[FORMAT_TIMESTAMP_RELATIVE_MAX];
char since2[FORMAT_TIMESTAMP_MAX];
const char *s1, *s2;
SessionStatusInfo i = {};
int r;
@ -605,8 +606,9 @@ static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line)
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
char since2[FORMAT_TIMESTAMP_MAX], *s2;
char since1[FORMAT_TIMESTAMP_RELATIVE_MAX];
char since2[FORMAT_TIMESTAMP_MAX];
const char *s1, *s2;
_cleanup_(user_status_info_clear) UserStatusInfo i = {};
int r;

View file

@ -556,8 +556,9 @@ static void machine_status_info_clear(MachineStatusInfo *info) {
}
static void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) {
char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
char since2[FORMAT_TIMESTAMP_MAX], *s2;
char since1[FORMAT_TIMESTAMP_RELATIVE_MAX];
char since2[FORMAT_TIMESTAMP_MAX];
const char *s1, *s2;
int ifi = -1;
assert(bus);
@ -902,10 +903,11 @@ typedef struct ImageStatusInfo {
} ImageStatusInfo;
static void print_image_status_info(sd_bus *bus, ImageStatusInfo *i) {
char ts_relative[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
char ts_absolute[FORMAT_TIMESTAMP_MAX], *s2;
char bs[FORMAT_BYTES_MAX], *s3;
char bs_exclusive[FORMAT_BYTES_MAX], *s4;
char ts_relative[FORMAT_TIMESTAMP_RELATIVE_MAX];
char ts_absolute[FORMAT_TIMESTAMP_MAX];
char bs[FORMAT_BYTES_MAX];
char bs_exclusive[FORMAT_BYTES_MAX];
const char *s1, *s2, *s3, *s4;
assert(bus);
assert(i);

View file

@ -697,7 +697,8 @@ int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all
* should it turn out to not be sufficient */
if (endswith(name, "Timestamp") || STR_IN_SET(name, "NextElapseUSecRealtime", "LastTriggerUSec")) {
char timestamp[FORMAT_TIMESTAMP_MAX], *t;
char timestamp[FORMAT_TIMESTAMP_MAX];
const char *t;
t = format_timestamp(timestamp, sizeof(timestamp), u);
if (t || all)

View file

@ -3948,8 +3948,8 @@ static void print_status_info(
UnitStatusInfo *i,
bool *ellipsized) {
char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1, since2[FORMAT_TIMESTAMP_MAX], *s2;
const char *active_on, *active_off, *on, *off, *ss;
char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX];
const char *s1, *s2, *active_on, *active_off, *on, *off, *ss;
_cleanup_free_ char *formatted_path = NULL;
ExecStatusInfo *p;
usec_t timestamp;
@ -4077,7 +4077,7 @@ static void print_status_info(
if (endswith(i->id, ".timer")) {
char tstamp1[FORMAT_TIMESTAMP_RELATIVE_MAX],
tstamp2[FORMAT_TIMESTAMP_MAX];
char *next_rel_time, *next_time;
const char *next_rel_time, *next_time;
dual_timestamp nw, next = {i->next_elapse_real,
i->next_elapse_monotonic};
usec_t next_elapse;
@ -4086,12 +4086,8 @@ static void print_status_info(
dual_timestamp_get(&nw);
next_elapse = calc_next_elapse(&nw, &next);
next_rel_time = format_timestamp_relative(tstamp1,
sizeof(tstamp1),
next_elapse);
next_time = format_timestamp(tstamp2,
sizeof(tstamp2),
next_elapse);
next_rel_time = format_timestamp_relative(tstamp1, sizeof tstamp1, next_elapse);
next_time = format_timestamp(tstamp2, sizeof tstamp2, next_elapse);
if (next_time && next_rel_time)
printf("%s; %s\n", next_time, next_rel_time);

View file

@ -120,18 +120,16 @@ static void test_parse_nsec(void) {
}
static void test_format_timespan_one(usec_t x, usec_t accuracy) {
char *r;
char l[FORMAT_TIMESPAN_MAX];
const char *t;
usec_t y;
log_info(USEC_FMT" (at accuracy "USEC_FMT")", x, accuracy);
r = format_timespan(l, sizeof(l), x, accuracy);
assert_se(r);
assert_se(t = format_timespan(l, sizeof l, x, accuracy));
log_info(" = <%s>", t);
log_info(" = <%s>", l);
assert_se(parse_sec(l, &y) >= 0);
assert_se(parse_sec(t, &y) >= 0);
log_info(" = "USEC_FMT, y);
@ -271,13 +269,12 @@ static void test_format_timestamp(void) {
}
}
static void test_format_timestamp_utc_one(usec_t t, const char *result) {
static void test_format_timestamp_utc_one(usec_t val, const char *result) {
char buf[FORMAT_TIMESTAMP_MAX];
const char *t;
assert_se(!format_timestamp_utc(buf, sizeof(buf), t) == !result);
if (result)
assert_se(streq(result, buf));
t = format_timestamp_utc(buf, sizeof(buf), val);
assert_se(streq_ptr(t, result));
}
static void test_format_timestamp_utc(void) {