aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorLRN <lrn1986@gmail.com>2013-03-04 08:28:19 +0000
committerLRN <lrn1986@gmail.com>2013-03-04 08:28:19 +0000
commit4775c35078e2a2223509089ccd5aec80e0008080 (patch)
tree5b0094eafdb9a7d439757f27baa1eef3dfccb840 /src/util
parent05f231d254655e988f0c111ab3b86df1266df7d8 (diff)
downloadgnunet-4775c35078e2a2223509089ccd5aec80e0008080.tar.gz
gnunet-4775c35078e2a2223509089ccd5aec80e0008080.zip
Accept time strings where number and unit are not separated by a space
Fixes #2806
Diffstat (limited to 'src/util')
-rw-r--r--src/util/strings.c36
-rw-r--r--src/util/test_strings.c9
2 files changed, 33 insertions, 12 deletions
diff --git a/src/util/strings.c b/src/util/strings.c
index 70986a978..4cef5b6fd 100644
--- a/src/util/strings.c
+++ b/src/util/strings.c
@@ -216,21 +216,33 @@ convert_with_table (const char *input,
216 in = GNUNET_strdup (input); 216 in = GNUNET_strdup (input);
217 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " ")) 217 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " "))
218 { 218 {
219 i = 0; 219 do
220 while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok)))
221 i++;
222 if (table[i].name != NULL)
223 last *= table[i].value;
224 else
225 { 220 {
226 ret += last; 221 i = 0;
227 last = 0; 222 while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok)))
228 if (1 != SSCANF (tok, "%llu", &last)) 223 i++;
224 if (table[i].name != NULL)
229 { 225 {
230 GNUNET_free (in); 226 last *= table[i].value;
231 return GNUNET_SYSERR; /* expected number */ 227 break; /* next tok */
232 } 228 }
233 } 229 else
230 {
231 char *endptr;
232 ret += last;
233 errno = 0;
234 last = strtoull (tok, &endptr, 10);
235 if ((0 != errno) || (endptr == tok))
236 {
237 GNUNET_free (in);
238 return GNUNET_SYSERR; /* expected number */
239 }
240 if ('\0' == endptr[0])
241 break; /* next tok */
242 else
243 tok = endptr; /* and re-check (handles times like "10s") */
244 }
245 } while (GNUNET_YES);
234 } 246 }
235 ret += last; 247 ret += last;
236 *output = ret; 248 *output = ret;
diff --git a/src/util/test_strings.c b/src/util/test_strings.c
index bc4169421..e5247ad91 100644
--- a/src/util/test_strings.c
+++ b/src/util/test_strings.c
@@ -39,6 +39,8 @@ main (int argc, char *argv[])
39 const char *bc; 39 const char *bc;
40 struct GNUNET_TIME_Absolute at; 40 struct GNUNET_TIME_Absolute at;
41 struct GNUNET_TIME_Absolute atx; 41 struct GNUNET_TIME_Absolute atx;
42 struct GNUNET_TIME_Relative rt;
43 struct GNUNET_TIME_Relative rtx;
42 const char *hdir; 44 const char *hdir;
43 45
44 GNUNET_log_setup ("test_strings", "ERROR", NULL); 46 GNUNET_log_setup ("test_strings", "ERROR", NULL);
@@ -109,6 +111,13 @@ main (int argc, char *argv[])
109 b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "unknown"); 111 b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "unknown");
110 GNUNET_log_skip (0, GNUNET_YES); 112 GNUNET_log_skip (0, GNUNET_YES);
111 WANT ("TEST", b); 113 WANT ("TEST", b);
114
115 GNUNET_assert (GNUNET_OK ==
116 GNUNET_STRINGS_fancy_time_to_relative ("15m", &rt));
117 GNUNET_assert (GNUNET_OK ==
118 GNUNET_STRINGS_fancy_time_to_relative ("15 m", &rtx));
119 GNUNET_assert (rt.rel_value == rtx.rel_value);
120
112 return 0; 121 return 0;
113} 122}
114 123