aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2019-04-13 14:17:55 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2019-04-13 14:17:55 +0200
commit94356088049a29c81dc29e448e621d81ccd43bfc (patch)
tree9129832215a462549910be2cdc1ce33a362f8597
parentdc1b2d43565dee709229dd76d856736bf543792d (diff)
downloadbusybox-w32-94356088049a29c81dc29e448e621d81ccd43bfc.tar.gz
busybox-w32-94356088049a29c81dc29e448e621d81ccd43bfc.tar.bz2
busybox-w32-94356088049a29c81dc29e448e621d81ccd43bfc.zip
brctl: simplify str_to_jiffies()
function old new delta write_uint - 96 +96 brctl_main 721 678 -43 write_ulong 96 - -96 ------------------------------------------------------------------------------ (add/remove: 1/1 grow/shrink: 0/1 up/down: 96/-139) Total: -43 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--networking/brctl.c68
1 files changed, 20 insertions, 48 deletions
diff --git a/networking/brctl.c b/networking/brctl.c
index 804728e3c..0f2c10f22 100644
--- a/networking/brctl.c
+++ b/networking/brctl.c
@@ -66,9 +66,9 @@
66//usage: "\n setpathcost BRIDGE IFACE COST Set path cost" 66//usage: "\n setpathcost BRIDGE IFACE COST Set path cost"
67//usage: ) 67//usage: )
68// Not yet implemented: 68// Not yet implemented:
69// hairpin BRIDGE IFACE on|off Hairpin on/off 69// hairpin BRIDGE IFACE on|off Hairpin on/off
70// showmacs BRIDGE List mac addrs 70// showmacs BRIDGE List mac addrs
71// showstp BRIDGE Show stp info 71// showstp BRIDGE Show stp info
72 72
73#include "libbb.h" 73#include "libbb.h"
74#include "common_bufsiz.h" 74#include "common_bufsiz.h"
@@ -88,51 +88,23 @@
88# define SIOCBRDELIF BRCTL_DEL_IF 88# define SIOCBRDELIF BRCTL_DEL_IF
89#endif 89#endif
90 90
91/* Use internal number parsing and not the "exact" conversion. */
92/* #define BRCTL_USE_INTERNAL 0 */ /* use exact conversion */
93#define BRCTL_USE_INTERNAL 1
94
95#if ENABLE_FEATURE_BRCTL_FANCY 91#if ENABLE_FEATURE_BRCTL_FANCY
96/* FIXME: These 4 funcs are not really clean and could be improved */ 92static unsigned str_to_jiffies(const char *time_str)
97static ALWAYS_INLINE void bb_strtotimeval(struct timeval *tv,
98 const char *time_str)
99{ 93{
100 double secs; 94 double dd;
101# if BRCTL_USE_INTERNAL
102 char *endptr; 95 char *endptr;
103 secs = /*bb_*/strtod(time_str, &endptr); 96 dd = /*bb_*/strtod(time_str, &endptr);
104 if (endptr == time_str) 97 if (endptr == time_str || dd < 0)
105# else
106 if (sscanf(time_str, "%lf", &secs) != 1)
107# endif
108 bb_error_msg_and_die(bb_msg_invalid_arg_to, time_str, "timespec"); 98 bb_error_msg_and_die(bb_msg_invalid_arg_to, time_str, "timespec");
109 tv->tv_sec = secs;
110 tv->tv_usec = 1000000 * (secs - tv->tv_sec);
111}
112 99
113static ALWAYS_INLINE unsigned long tv_to_jiffies(const struct timeval *tv) 100 dd *= 100;
114{ 101 /* For purposes of brctl,
115 unsigned long long jif; 102 * capping SECONDS by ~20 million seconds is quite enough:
103 */
104 if (dd > INT_MAX)
105 dd = INT_MAX;
116 106
117 jif = 1000000ULL * tv->tv_sec + tv->tv_usec; 107 return dd;
118
119 return jif/10000;
120}
121# if 0
122static void jiffies_to_tv(struct timeval *tv, unsigned long jiffies)
123{
124 unsigned long long tvusec;
125
126 tvusec = 10000ULL*jiffies;
127 tv->tv_sec = tvusec/1000000;
128 tv->tv_usec = tvusec - 1000000 * tv->tv_sec;
129}
130# endif
131static unsigned long str_to_jiffies(const char *time_str)
132{
133 struct timeval tv;
134 bb_strtotimeval(&tv, time_str);
135 return tv_to_jiffies(&tv);
136} 108}
137#endif 109#endif
138 110
@@ -208,7 +180,7 @@ static int show_bridge(const char *name, int need_hdr)
208 return 0; 180 return 0;
209} 181}
210 182
211static void write_ulong(const char *name, const char *leaf, unsigned long val) 183static void write_uint(const char *name, const char *leaf, unsigned val)
212{ 184{
213 char pathbuf[IFNAMSIZ + sizeof("/bridge/bridge_id") + 32]; 185 char pathbuf[IFNAMSIZ + sizeof("/bridge/bridge_id") + 32];
214 int fd, n; 186 int fd, n;
@@ -219,7 +191,7 @@ static void write_ulong(const char *name, const char *leaf, unsigned long val)
219 sprintf(pathbuf, "%.*s/%s", (int)IFNAMSIZ, name, leaf); 191 sprintf(pathbuf, "%.*s/%s", (int)IFNAMSIZ, name, leaf);
220#endif 192#endif
221 fd = xopen(pathbuf, O_WRONLY); 193 fd = xopen(pathbuf, O_WRONLY);
222 n = sprintf(filedata, "%lu\n", val); 194 n = sprintf(filedata, "%u\n", val);
223 if (write(fd, filedata, n) < 0) 195 if (write(fd, filedata, n) < 0)
224 bb_simple_perror_msg_and_die(name); 196 bb_simple_perror_msg_and_die(name);
225 close(fd); 197 close(fd);
@@ -338,7 +310,7 @@ int brctl_main(int argc UNUSED_PARAM, char **argv)
338 if (onoff < 0) 310 if (onoff < 0)
339 bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name); 311 bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name);
340 onoff = (unsigned)onoff / 4; 312 onoff = (unsigned)onoff / 4;
341 write_ulong(br, "bridge/stp_state", onoff); 313 write_uint(br, "bridge/stp_state", onoff);
342 //goto done_next_argv; 314 //goto done_next_argv;
343 return EXIT_SUCCESS; 315 return EXIT_SUCCESS;
344 } 316 }
@@ -349,7 +321,7 @@ int brctl_main(int argc UNUSED_PARAM, char **argv)
349 * sethello BR N: "N*100\n" to /sys/class/net/BR/bridge/hello_time 321 * sethello BR N: "N*100\n" to /sys/class/net/BR/bridge/hello_time
350 * setmaxage BR N: "N*100\n" to /sys/class/net/BR/bridge/max_age 322 * setmaxage BR N: "N*100\n" to /sys/class/net/BR/bridge/max_age
351 */ 323 */
352 write_ulong(br, 324 write_uint(br,
353 nth_string( 325 nth_string(
354 "bridge/ageing_time" "\0" /* ARG_setageing */ 326 "bridge/ageing_time" "\0" /* ARG_setageing */
355 "bridge/forward_delay""\0" /* ARG_setfd */ 327 "bridge/forward_delay""\0" /* ARG_setfd */
@@ -364,7 +336,7 @@ int brctl_main(int argc UNUSED_PARAM, char **argv)
364 } 336 }
365 337
366 if (key == ARG_setbridgeprio) { 338 if (key == ARG_setbridgeprio) {
367 write_ulong(br, "bridge/priority", xatoi_positive(*argv)); 339 write_uint(br, "bridge/priority", xatoi_positive(*argv));
368 //goto done_next_argv; 340 //goto done_next_argv;
369 return EXIT_SUCCESS; 341 return EXIT_SUCCESS;
370 } 342 }
@@ -380,7 +352,7 @@ int brctl_main(int argc UNUSED_PARAM, char **argv)
380 * "setportprio BR PORT N" writes "N\n" to 352 * "setportprio BR PORT N" writes "N\n" to
381 * /sys/class/net/PORT/brport/priority 353 * /sys/class/net/PORT/brport/priority
382 */ 354 */
383 write_ulong(argv[0], 355 write_uint(argv[0],
384 nth_string( 356 nth_string(
385 "brport/path_cost" "\0" /* ARG_setpathcost */ 357 "brport/path_cost" "\0" /* ARG_setpathcost */
386 "brport/priority", /* ARG_setportprio */ 358 "brport/priority", /* ARG_setportprio */