From f736b24935d66733e9a53798fbc87dbc8aff9fca Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 16 Sep 2012 11:27:08 +0000 Subject: -fixing #2546 --- src/arm/gnunet-arm.c | 7 +------ src/chat/gnunet-chat.c | 7 +++---- src/core/gnunet-core.c | 11 ++++------- src/dht/gnunet-dht-monitor.c | 2 +- src/fs/gnunet-download.c | 11 ++++++----- src/fs/gnunet-publish.c | 6 +++--- src/fs/gnunet-unindex.c | 5 ++--- src/fs/test_fs_directory.c | 6 +++--- src/include/gnunet_strings_lib.h | 12 +++++++++--- src/mesh/mesh_api.c | 6 ++++-- src/namestore/gnunet-namestore.c | 5 ++--- src/statistics/gnunet-statistics.c | 4 ++-- src/testing_old/testing.c | 32 +++++++++++++++--------------- src/transport/gnunet-transport.c | 8 +++----- src/util/common_logging.c | 5 ++--- src/util/container_meta_data.c | 3 +-- src/util/strings.c | 40 ++++++++++++++++++++++++++------------ src/util/test_strings.c | 39 ++++++++++++++++++------------------- src/util/time.c | 24 +++-------------------- 19 files changed, 112 insertions(+), 121 deletions(-) diff --git a/src/arm/gnunet-arm.c b/src/arm/gnunet-arm.c index 84d91355a..d84de1c7a 100644 --- a/src/arm/gnunet-arm.c +++ b/src/arm/gnunet-arm.c @@ -412,8 +412,6 @@ cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) int main (int argc, char *const *argv) { - static unsigned long long temp_timeout_ms; - static const struct GNUNET_GETOPT_CommandLineOption options[] = { {'e', "end", NULL, gettext_noop ("stop all GNUnet services"), GNUNET_NO, &GNUNET_GETOPT_set_one, &end}, @@ -433,7 +431,7 @@ main (int argc, char *const *argv) GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet}, {'T', "timeout", NULL, gettext_noop ("timeout for completing current operation"), - GNUNET_YES, &GNUNET_GETOPT_set_ulong, &temp_timeout_ms}, + GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &timeout}, {'I', "info", NULL, gettext_noop ("List currently running services"), GNUNET_NO, &GNUNET_GETOPT_set_one, &list}, {'O', "no-stdout", NULL, gettext_noop ("Don't let gnunet-service-arm inherit standard output"), @@ -443,9 +441,6 @@ main (int argc, char *const *argv) GNUNET_GETOPT_OPTION_END }; - if (temp_timeout_ms > 0) - timeout.rel_value = temp_timeout_ms; - if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) return 2; diff --git a/src/chat/gnunet-chat.c b/src/chat/gnunet-chat.c index d41d59062..8e6ba9bc2 100644 --- a/src/chat/gnunet-chat.c +++ b/src/chat/gnunet-chat.c @@ -118,7 +118,7 @@ receive_cb (void *cls, struct GNUNET_CHAT_Room *room, char *non_unique_nick; char *nick; int nick_is_a_dup; - char *time; + const char *timestr; const char *fmt; if (NULL == sender) @@ -176,10 +176,9 @@ receive_cb (void *cls, struct GNUNET_CHAT_Room *room, fmt = _("(%s) <%s> said using an unknown message type: %s\n"); break; } - time = GNUNET_STRINGS_absolute_time_to_string (timestamp); - FPRINTF (stdout, fmt, time, nick, message); + timestr = GNUNET_STRINGS_absolute_time_to_string (timestamp); + FPRINTF (stdout, fmt, timestr, nick, message); GNUNET_free (nick); - GNUNET_free (time); return GNUNET_OK; } diff --git a/src/core/gnunet-core.c b/src/core/gnunet-core.c index ebdd8aab3..571a2a53d 100644 --- a/src/core/gnunet-core.c +++ b/src/core/gnunet-core.c @@ -110,7 +110,8 @@ monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer, const struct GNUNET_ATS_Information *ats, uint32_t ats_count) { struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get(); - char *now_str; + const char *now_str; + if (0 != memcmp (&my_id, peer, sizeof (my_id))) { monitor_connections_counter ++; @@ -120,8 +121,6 @@ monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer, _("Connected to"), GNUNET_i2s (peer), monitor_connections_counter); - - GNUNET_free (now_str); } } @@ -137,21 +136,19 @@ static void monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) { struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get(); - char *now_str; + const char *now_str; if (0 != memcmp (&my_id, peer, sizeof (my_id))) { now_str = GNUNET_STRINGS_absolute_time_to_string (now); GNUNET_assert (monitor_connections_counter > 0); - monitor_connections_counter --; - + monitor_connections_counter--; FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"), now_str, _("Disconnected from"), GNUNET_i2s (peer), monitor_connections_counter); - GNUNET_free (now_str); } } diff --git a/src/dht/gnunet-dht-monitor.c b/src/dht/gnunet-dht-monitor.c index e0e087786..b03c7c7c0 100644 --- a/src/dht/gnunet-dht-monitor.c +++ b/src/dht/gnunet-dht-monitor.c @@ -242,7 +242,7 @@ run (void *cls, char *const *args, const char *cfgfile, if (verbose) FPRINTF (stderr, "Monitoring for %s\n", - GNUNET_TIME_relative_to_string (timeout_request)); + GNUNET_STRINGS_relative_time_to_string (timeout_request, GNUNET_NO)); GNUNET_SCHEDULER_add_delayed (timeout_request, &cleanup_task, NULL); monitor_handle = GNUNET_DHT_monitor_start (dht_handle, block_type, diff --git a/src/fs/gnunet-download.c b/src/fs/gnunet-download.c index 2293cedd7..093dd7bdc 100644 --- a/src/fs/gnunet-download.c +++ b/src/fs/gnunet-download.c @@ -127,7 +127,7 @@ static void * progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info) { char *s; - char *s2; + const char *s2; char *t; switch (info->status) @@ -140,13 +140,15 @@ progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info) case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS: if (verbose) { - s = GNUNET_STRINGS_relative_time_to_string (info->value.download.eta); + s = GNUNET_strdup (GNUNET_STRINGS_relative_time_to_string (info->value.download.eta, + GNUNET_YES)); if (info->value.download.specifics.progress.block_download_duration.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value) - s2 = GNUNET_strdup (_("")); + s2 = _(""); else s2 = GNUNET_STRINGS_relative_time_to_string ( - info->value.download.specifics.progress.block_download_duration); + info->value.download.specifics.progress.block_download_duration, + GNUNET_YES); t = GNUNET_STRINGS_byte_size_fancy (info->value.download.completed * 1000LL / (info->value.download. @@ -157,7 +159,6 @@ progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info) (unsigned long long) info->value.download.completed, (unsigned long long) info->value.download.size, s, t, s2); GNUNET_free (s); - GNUNET_free (s2); GNUNET_free (t); } else diff --git a/src/fs/gnunet-publish.c b/src/fs/gnunet-publish.c index 96dcfbf69..3fdc1a9fa 100644 --- a/src/fs/gnunet-publish.c +++ b/src/fs/gnunet-publish.c @@ -134,7 +134,7 @@ stop_scanner_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) static void * progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info) { - char *s; + const char *s; switch (info->status) { @@ -143,12 +143,12 @@ progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info) case GNUNET_FS_STATUS_PUBLISH_PROGRESS: if (verbose) { - s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.eta); + s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.eta, + GNUNET_YES); FPRINTF (stdout, _("Publishing `%s' at %llu/%llu (%s remaining)\n"), info->value.publish.filename, (unsigned long long) info->value.publish.completed, (unsigned long long) info->value.publish.size, s); - GNUNET_free (s); } break; case GNUNET_FS_STATUS_PUBLISH_ERROR: diff --git a/src/fs/gnunet-unindex.c b/src/fs/gnunet-unindex.c index f4e352737..be16e9100 100644 --- a/src/fs/gnunet-unindex.c +++ b/src/fs/gnunet-unindex.c @@ -76,7 +76,7 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) static void * progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info) { - char *s; + const char *s; switch (info->status) { @@ -85,11 +85,10 @@ progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info) case GNUNET_FS_STATUS_UNINDEX_PROGRESS: if (verbose) { - s = GNUNET_STRINGS_relative_time_to_string (info->value.unindex.eta); + s = GNUNET_STRINGS_relative_time_to_string (info->value.unindex.eta, GNUNET_YES); FPRINTF (stdout, _("Unindexing at %llu/%llu (%s remaining)\n"), (unsigned long long) info->value.unindex.completed, (unsigned long long) info->value.unindex.size, s); - GNUNET_free (s); } break; case GNUNET_FS_STATUS_UNINDEX_ERROR: diff --git a/src/fs/test_fs_directory.c b/src/fs/test_fs_directory.c index 96ad29cb4..95225d7c0 100644 --- a/src/fs/test_fs_directory.c +++ b/src/fs/test_fs_directory.c @@ -79,7 +79,7 @@ testDirectory (unsigned int i) char txt[128]; int ret = 0; struct GNUNET_TIME_Absolute start; - char *s; + const char *s; cls.max = i; uris = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri *) * i); @@ -130,11 +130,11 @@ testDirectory (unsigned int i) GNUNET_FS_directory_builder_add (db, uris[p], mds[p], NULL); GNUNET_FS_directory_builder_finish (db, &dlen, (void **) &data); s = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration - (start)); + (start), + GNUNET_YES); FPRINTF (stdout, "Creating directory with %u entires and total size %llu took %s\n", i, (unsigned long long) dlen, s); - GNUNET_free (s); if (i < 100) { cls.pos = 0; diff --git a/src/include/gnunet_strings_lib.h b/src/include/gnunet_strings_lib.h index 4073b0cfa..64dbd1ef3 100644 --- a/src/include/gnunet_strings_lib.h +++ b/src/include/gnunet_strings_lib.h @@ -224,22 +224,28 @@ GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size, /** * "asctime", except for GNUnet time. + * This is one of the very few calls in the entire API that is + * NOT reentrant! * * @param t the absolute time to convert * @return timestamp in human-readable form */ -char * +const char * GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t); /** * Give relative time in human-readable fancy format. + * This is one of the very few calls in the entire API that is + * NOT reentrant! * * @param delta time in milli seconds + * @param do_round are we allowed to round a bit? * @return string in human-readable form */ -char * -GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta); +const char * +GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta, + int do_round); /** diff --git a/src/mesh/mesh_api.c b/src/mesh/mesh_api.c index 5a8c1bc48..347b68aec 100644 --- a/src/mesh/mesh_api.c +++ b/src/mesh/mesh_api.c @@ -828,8 +828,10 @@ do_reconnect (struct GNUNET_MESH_Handle *h) GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS, GNUNET_TIME_relative_multiply (h->reconnect_time, 2)); - LOG (GNUNET_ERROR_TYPE_DEBUG, " Next retry in %sms\n", - GNUNET_TIME_relative_to_string (h->reconnect_time)); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Next retry in %s\n", + GNUNET_STRINGS_relative_time_to_string (h->reconnect_time, + GNUNET_NO)); GNUNET_break (0); return GNUNET_NO; } diff --git a/src/namestore/gnunet-namestore.c b/src/namestore/gnunet-namestore.c index 7fc3ad45f..fbb242569 100644 --- a/src/namestore/gnunet-namestore.c +++ b/src/namestore/gnunet-namestore.c @@ -277,7 +277,7 @@ display_record (void *cls, const char *typestring; char *s; unsigned int i; - char *etime; + const char *etime; struct GNUNET_TIME_Absolute aex; struct GNUNET_TIME_Relative rex; @@ -308,7 +308,7 @@ display_record (void *cls, if (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION)) { rex.rel_value = rd[i].expiration_time; - etime = GNUNET_STRINGS_relative_time_to_string (rex); + etime = GNUNET_STRINGS_relative_time_to_string (rex, GNUNET_YES); } else { @@ -320,7 +320,6 @@ display_record (void *cls, ? _(/* what follows is relative expiration */ "for at least") : _(/* what follows is absolute expiration */ "until"), etime); - GNUNET_free (etime); GNUNET_free (s); } FPRINTF (stdout, "%s", "\n"); diff --git a/src/statistics/gnunet-statistics.c b/src/statistics/gnunet-statistics.c index 671fd35be..15dedaec1 100644 --- a/src/statistics/gnunet-statistics.c +++ b/src/statistics/gnunet-statistics.c @@ -79,7 +79,8 @@ printer (void *cls, const char *subsystem, const char *name, uint64_t value, int is_persistent) { struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get(); - char * now_str; + const char * now_str; + if (quiet == GNUNET_NO) { if (GNUNET_YES == watch) @@ -89,7 +90,6 @@ printer (void *cls, const char *subsystem, const char *name, uint64_t value, now_str, is_persistent ? "!" : " ", subsystem, _(name), (unsigned long long) value); - GNUNET_free (now_str); } else { diff --git a/src/testing_old/testing.c b/src/testing_old/testing.c index 1b164aa0f..b09afca0a 100644 --- a/src/testing_old/testing.c +++ b/src/testing_old/testing.c @@ -417,9 +417,9 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, "gnunet-arm", "gnunet-arm", "-c", d->cfgfile, "-s", "-q", "-T", - GNUNET_TIME_relative_to_string + GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining - (d->max_timeout)), NULL); + (d->max_timeout), GNUNET_NO), NULL); } else { @@ -438,9 +438,9 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) "-q", dst, "gnunet-arm", "-c", d->cfgfile, "-s", "-q", "-T", - GNUNET_TIME_relative_to_string + GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining - (d->max_timeout)), NULL); + (d->max_timeout), GNUNET_NO), NULL); } else { @@ -451,9 +451,9 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) "-q", dst, "gnunet-arm", "-c", d->cfgfile, "-s", "-q", "-T", - GNUNET_TIME_relative_to_string + GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining - (d->max_timeout)), NULL); + (d->max_timeout), GNUNET_NO), NULL); } GNUNET_free (dst); } @@ -897,7 +897,7 @@ GNUNET_TESTING_daemon_start_stopped_service (struct GNUNET_TESTING_Daemon *d, arg, "gnunet-arm", "-c", d->cfgfile, "-i", service, "-q", "-T", - GNUNET_TIME_relative_to_string (timeout), + GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO), NULL); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q\n", @@ -911,7 +911,7 @@ GNUNET_TESTING_daemon_start_stopped_service (struct GNUNET_TESTING_Daemon *d, d->proc_arm_srv_start = GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, "gnunet-arm", "gnunet-arm", "-c", d->cfgfile, "-i", service, "-q", "-T", - GNUNET_TIME_relative_to_string (timeout), + GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO), NULL); } @@ -965,12 +965,12 @@ GNUNET_TESTING_daemon_start_service (struct GNUNET_TESTING_Daemon *d, arg, "gnunet-arm", "-c", d->cfgfile, "-i", service, "-q", "-T", - GNUNET_TIME_relative_to_string (timeout), + GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO), NULL); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q -T %s\n", arg, "gnunet-arm", d->cfgfile, service, - GNUNET_TIME_relative_to_string (timeout)); + GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO)); GNUNET_free (arg); } else @@ -980,12 +980,12 @@ GNUNET_TESTING_daemon_start_service (struct GNUNET_TESTING_Daemon *d, d->proc_arm_srv_start = GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, "gnunet-arm", "gnunet-arm", "-c", d->cfgfile, "-i", service, "-q", "-T", - GNUNET_TIME_relative_to_string (timeout), + GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO), NULL); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting gnunet-arm with command %s -c %s -i %s -q -T %s\n", "gnunet-arm", d->cfgfile, service, - GNUNET_TIME_relative_to_string (timeout)); + GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO)); } d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout); @@ -1382,7 +1382,7 @@ GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d, arg, "gnunet-arm", "-c", d->cfgfile, "-k", service, "-q", "-T", - GNUNET_TIME_relative_to_string (timeout), + GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO), NULL); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -k %s -q\n", @@ -1396,7 +1396,7 @@ GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d, d->proc_arm_srv_stop = GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, "gnunet-arm", "gnunet-arm", "-c", d->cfgfile, "-k", service, "-q", "-T", - GNUNET_TIME_relative_to_string (timeout), + GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO), NULL); } @@ -1550,7 +1550,7 @@ GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d, "-q", arg, "gnunet-arm", "-c", d->cfgfile, "-e", "-q", "-T", - GNUNET_TIME_relative_to_string (timeout), + GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO), del_arg, NULL); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n", @@ -1564,7 +1564,7 @@ GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d, "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile); d->proc_arm_stop = GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, "gnunet-arm", "gnunet-arm", "-c", d->cfgfile, "-e", "-q", "-T", - GNUNET_TIME_relative_to_string (timeout), + GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO), del_arg, NULL); GNUNET_assert (NULL != d->proc_arm_stop); } diff --git a/src/transport/gnunet-transport.c b/src/transport/gnunet-transport.c index 36c02014e..1c865ad94 100644 --- a/src/transport/gnunet-transport.c +++ b/src/transport/gnunet-transport.c @@ -447,14 +447,13 @@ monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer, { monitor_connections_counter ++; struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get(); - char *now_str = GNUNET_STRINGS_absolute_time_to_string (now); + const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now); + FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"), now_str, _("Connected to"), GNUNET_i2s (peer), monitor_connections_counter); - - GNUNET_free (now_str); } @@ -469,7 +468,7 @@ static void monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) { struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get(); - char *now_str = GNUNET_STRINGS_absolute_time_to_string (now); + const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now); GNUNET_assert (monitor_connections_counter > 0); monitor_connections_counter --; @@ -479,7 +478,6 @@ monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) _("Disconnected from"), GNUNET_i2s (peer), monitor_connections_counter); - GNUNET_free (now_str); } diff --git a/src/util/common_logging.c b/src/util/common_logging.c index 873420460..fbc0de50c 100644 --- a/src/util/common_logging.c +++ b/src/util/common_logging.c @@ -798,7 +798,7 @@ flush_bulk (const char *datestr) char msg[DATE_STR_SIZE + BULK_TRACK_SIZE + 256]; int rev; char *last; - char *ft; + const char *ft; if ((last_bulk_time.abs_value == 0) || (last_bulk_repeat == 0)) return; @@ -814,11 +814,10 @@ flush_bulk (const char *datestr) last[0] = '\0'; } ft = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration - (last_bulk_time)); + (last_bulk_time), GNUNET_YES); snprintf (msg, sizeof (msg), _("Message `%.*s' repeated %u times in the last %s\n"), BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft); - GNUNET_free (ft); if (rev == 1) last[0] = '\n'; output_message (last_bulk_kind, last_bulk_comp, datestr, msg); diff --git a/src/util/container_meta_data.c b/src/util/container_meta_data.c index e725486b7..9e60a746a 100644 --- a/src/util/container_meta_data.c +++ b/src/util/container_meta_data.c @@ -427,7 +427,7 @@ void GNUNET_CONTAINER_meta_data_add_publication_date (struct GNUNET_CONTAINER_MetaData *md) { - char *dat; + const char *dat; struct GNUNET_TIME_Absolute t; t = GNUNET_TIME_absolute_get (); @@ -438,7 +438,6 @@ GNUNET_CONTAINER_meta_data_add_publication_date (struct EXTRACTOR_METATYPE_PUBLICATION_DATE, EXTRACTOR_METAFORMAT_UTF8, "text/plain", dat, strlen (dat) + 1); - GNUNET_free (dat); } diff --git a/src/util/strings.c b/src/util/strings.c index ef7658697..efa6029b4 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -616,33 +616,46 @@ GNUNET_STRINGS_filename_expand (const char *fil) /** * Give relative time in human-readable fancy format. + * This is one of the very few calls in the entire API that is + * NOT reentrant! * * @param delta time in milli seconds + * @param do_round are we allowed to round a bit? * @return time as human-readable string */ -char * -GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta) +const char * +GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta, + int do_round) { + static char buf[128]; const char *unit = _( /* time unit */ "ms"); char *ret; uint64_t dval = delta.rel_value; if (delta.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value) - return GNUNET_strdup (_("forever")); - if ( (dval > 5 * 1000) || (0 == (dval % 1000) )) + return _("forever"); + if ( ( (GNUNET_YES == do_round) && + (dval > 5 * 1000) ) || + (0 == (dval % 1000) )) { dval = dval / 1000; unit = _( /* time unit */ "s"); - if ( (dval > 5 * 60) || (0 == (dval % 60) ) ) + if ( ( (GNUNET_YES == do_round) && + (dval > 5 * 60) ) || + (0 == (dval % 60) ) ) { dval = dval / 60; unit = _( /* time unit */ "m"); - if ( (dval > 5 * 60) || (0 == (dval % 60) )) + if ( ( (GNUNET_YES == do_round) && + (dval > 5 * 60) ) || + (0 == (dval % 60) )) { dval = dval / 60; unit = _( /* time unit */ "h"); - if ( (dval > 5 * 24) || (0 == (dval % 24)) ) - { + if ( ( (GNUNET_YES == do_round) && + (dval > 5 * 24) ) || + (0 == (dval % 24)) ) + { dval = dval / 24; if (1 == dval) unit = _( /* time unit */ "day"); @@ -652,23 +665,26 @@ GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta) } } } - GNUNET_asprintf (&ret, "%llu %s", dval, unit); - return ret; + GNUNET_snprintf (buf, sizeof (buf), + "%llu %s", dval, unit); + return buf; } /** * "asctime", except for GNUnet time. + * This is one of the very few calls in the entire API that is + * NOT reentrant! * * @param t time to convert * @return absolute time in human-readable format */ -char * +const char * GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t) { + static char buf[255]; time_t tt; struct tm *tp; - char buf[255]; if (t.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value) return GNUNET_strdup (_("end of time")); diff --git a/src/util/test_strings.c b/src/util/test_strings.c index 749650589..a77acaa59 100644 --- a/src/util/test_strings.c +++ b/src/util/test_strings.c @@ -27,6 +27,7 @@ #define WANT(a,b) if (0 != strcmp(a,b)) { fprintf(stderr, "Got `%s', wanted `%s'\n", b, a); GNUNET_free(b); GNUNET_break(0); return 1;} else { GNUNET_free (b); } +#define WANTNF(a,b) do { if (0 != strcmp(a,b)) { fprintf(stderr, "Got `%s', wanted `%s'\n", b, a); GNUNET_break(0); return 1;} } while (0) #define WANTB(a,b,l) if (0 != memcmp(a,b,l)) { GNUNET_break(0); return 1;} else { } int @@ -35,6 +36,7 @@ main (int argc, char *argv[]) char buf[128]; char *r; char *b; + const char *bc; struct GNUNET_TIME_Absolute at; struct GNUNET_TIME_Absolute atx; const char *hdir; @@ -50,20 +52,20 @@ main (int argc, char *argv[]) b = GNUNET_STRINGS_byte_size_fancy (10240LL * 1024LL * 1024LL * 1024LL); WANT (buf, b); sprintf (buf, "4 %s", _( /* time unit */ "ms")); - b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_MILLISECONDS, - 4)); - WANT (buf, b); + bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_MILLISECONDS, + 4), GNUNET_YES); + WANTNF (buf, bc); sprintf (buf, "7 %s", _( /* time unit */ "s")); - b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_MILLISECONDS, - 7 * 1000)); - WANT (buf, b); + bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_MILLISECONDS, + 7 * 1000), GNUNET_YES); + WANTNF (buf, bc); sprintf (buf, "7 %s", _( /* time unit */ "h")); - b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply + bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, - 7 * 60 * 60 * 1000)); - WANT (buf, b); + 7 * 60 * 60 * 1000), GNUNET_YES); + WANTNF (buf, bc); #ifndef MINGW hdir = getenv ("HOME"); #else @@ -84,27 +86,24 @@ main (int argc, char *argv[]) if (0 != GNUNET_STRINGS_buffer_tokenize (buf, 2, 2, &r, &b)) return 1; at.abs_value = 5000; - r = GNUNET_STRINGS_absolute_time_to_string (at); - /* r should be something like "Wed Dec 31 17:00:05 1969" + bc = GNUNET_STRINGS_absolute_time_to_string (at); + /* bc should be something like "Wed Dec 31 17:00:05 1969" * where the details of the day and hour depend on the timezone; * however, the "0:05 19" should always be there; hence: */ - if (NULL == strstr (r, "0:05 19")) + if (NULL == strstr (bc, "0:05 19")) { - FPRINTF (stderr, "Got %s\n", r); + FPRINTF (stderr, "Got %s\n", bc); GNUNET_break (0); - GNUNET_free (r); return 1; } - GNUNET_free (r); b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "ASCII"); WANT ("TEST", b); at = GNUNET_TIME_UNIT_FOREVER_ABS; - b = GNUNET_STRINGS_absolute_time_to_string (at); + bc = GNUNET_STRINGS_absolute_time_to_string (at); GNUNET_assert (GNUNET_OK == - GNUNET_STRINGS_fancy_time_to_absolute (b, &atx)); + GNUNET_STRINGS_fancy_time_to_absolute (bc, &atx)); GNUNET_assert (atx.abs_value == at.abs_value); - GNUNET_free (b); #if ENABLE_NLS && HAVE_ICONV GNUNET_log_skip (2, GNUNET_NO); diff --git a/src/util/time.c b/src/util/time.c index 7467b4497..afb0c00c1 100644 --- a/src/util/time.c +++ b/src/util/time.c @@ -484,6 +484,7 @@ GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a) return ret; } + /** * Convert relative time from network byte order. * @@ -500,6 +501,7 @@ GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a) } + /** * Convert absolute time to network byte order. * @@ -515,6 +517,7 @@ GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a) return ret; } + /** * Convert absolute time from network byte order. * @@ -531,26 +534,5 @@ GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a) } -/** - * Convert a relative time to a string. - * This is one of the very few calls in the entire API that is - * NOT reentrant! - * - * @param time the time to print - * - * @return string form of the time (as milliseconds) - */ -const char * -GNUNET_TIME_relative_to_string (struct GNUNET_TIME_Relative time) -{ - static char time_string[21]; - - memset (time_string, 0, sizeof (time_string)); - - sprintf (time_string, "%llu", (unsigned long long) time.rel_value); - return (const char *) time_string; -} - - /* end of time.c */ -- cgit v1.2.3