Silly shell tricks (and patch)...

David Edwards busybox at dpe.lusars.net
Wed Sep 17 04:14:38 UTC 2008


Useless trivia:

awk 'BEGIN { print "\x1B\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" }' | \
nc -u -w 1 $NTP_SERVER 123 | od -t x1 -j 32 -N 4 | \
awk 'NR == 1 { bar = "0x" $2 $3 $4 $5; print strtonum(bar) - 2208988800 }'

will fetch the time (in seconds from 1 JAN 1970) from your NTP server, and 
print it. Works swimmingly on the SVN version of busybox.

With GNU date, you can use this to set your system time (approximately) by 
specifying the time in the '@SECONDS' format. Unfortunately, busybox date 
doesn't recognize the '@SECONDS' format.

So I wrote a patch. (Constructive) criticism happily accepted. If 
there's interest, I'll work up a patch to 'rdate' to directly set time 
from a NTP server.


Index: busybox/coreutils/date.c
===================================================================
--- busybox/coreutils/date.c	(revision 23413)
+++ busybox/coreutils/date.c	(working copy)
@@ -146,6 +146,14 @@
  						end = '\0';
  					/* else end != NUL and we error out */
  				}
+			} else if (date_str[0] == '@') {
+				/* The format '@SECONDS' is a poorly-documented GNU-ism. */
+				/* The 'SECONDS' counts from 1 Jan 1970 - just like time_t. */
+				int seconds; /* Avoiding casting pointers */
+				if (sscanf(date_str, "@%d%c", &seconds, &end) < 1)
+					bb_error_msg_and_die(bb_msg_invalid_date, date_str);
+				tm = seconds;
+				memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
  			} else {
  				if (sscanf(date_str, "%2u%2u%2u%2u%u%c", &tm_time.tm_mon,
  							&tm_time.tm_mday, &tm_time.tm_hour, &tm_time.tm_min,




$ ./scripts/bloat-o-meter busybox_unstripped.old busybox_unstripped
function                                             old     new   delta
date_main                                           1201    1282     +81
.rodata                                           120143  120144      +1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 82/0)               Total: 82 bytes




More information about the busybox mailing list