aboutsummaryrefslogtreecommitdiff
path: root/toys/posix
diff options
context:
space:
mode:
Diffstat (limited to 'toys/posix')
-rw-r--r--toys/posix/date.c160
-rw-r--r--toys/posix/du.c14
-rw-r--r--toys/posix/grep.c98
3 files changed, 87 insertions, 185 deletions
diff --git a/toys/posix/date.c b/toys/posix/date.c
index a4e5e27c..e1ba636d 100644
--- a/toys/posix/date.c
+++ b/toys/posix/date.c
@@ -7,39 +7,21 @@
* Note: setting a 2 year date is 50 years back/forward from today,
* not posix's hardwired magic dates.
-USE_DATE(NEWTOY(date, "d:s:r:u", TOYFLAG_BIN))
+USE_DATE(NEWTOY(date, "r:u", TOYFLAG_BIN))
config DATE
bool "date"
default y
help
- usage: date [-u] [-r FILE] [-d DATE] [+DISPLAY_FORMAT] [-s SET_FORMAT] [SET]
+ usage: date [-u] [-r FILE] [+FORMAT] | mmddhhmm[[cc]yy[.ss]]
- Set/get the current date/time. With no SET shows the current date.
+ Set/get the current date/time.
- Default SET format is "MMDDhhmm[[CC]YY][.ss]", that's (2 digits each)
- month, day, hour (0-23), and minute. Optionally century, year, and second.
+ Setting the date requires month, day, hour (0-23), and minute, each
+ two digits. It can optionally include year, century, and .seconds.
- -d Show DATE instead of current time (convert date format)
- -r Use modification time of FILE instead of current date
- -s +FORMAT for SET or -d (instead of MMDDhhmm[[CC]YY][.ss])
- -u Use UTC instead of current timezone
-
- +FORMAT specifies display format string using these escapes:
-
- %% literal % %n newline %t tab
- %S seconds (00-60) %M minute (00-59) %m month (01-12)
- %H hour (0-23) %I hour (01-12) %p AM/PM
- %y short year (00-99) %Y year %C century
- %a short weekday name %A weekday name %u day of week (1-7, 1=mon)
- %b short month name %B month name %Z timezone name
- %j day of year (001-366) %d day of month (01-31) %e day of month ( 1-31)
-
- %U Week of year (0-53 start sunday) %W Week of year (0-53 start monday)
- %V Week of year (1-53 start monday, week < 4 days not part of this year)
-
- %D = "%m/%d/%y" %r = "%I : %M : %S %p" %T = "%H:%M:%S" %h = "%b"
- %x locale date %X locale time %c locale date/time
+ -u Use UTC timezone instead of current
+ -r Use date from FILE instead of current date
*/
#define FOR_date
@@ -47,95 +29,72 @@ config DATE
GLOBALS(
char *file;
- char *setfmt;
- char *showdate;
)
-// Handle default posix date format: mmddhhmm[[cc]yy]
-// returns 0 success, nonzero for error
-int parse_posixdate(char *str, struct tm *tm)
-{
- struct timeval tv;
- int len;
-
- len = 0;
- sscanf(str, "%2u%2u%2u%2u%n", &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
- &tm.tm_min, &len);
- if (len != 8) goto bad_date;
- str += len;
- tm.tm_mon--;
-
- // If year specified, overwrite one we fetched earlier
- if (*str && *str != '.') {
- unsigned year, r1 = tm.tm_year % 100, r2 = (tm.tm_year + 50) % 100,
- century = tm.tm_year - r1;
-
- len = 0;
- sscanf(str, "%u%n", &year, &len);
- if (len == 4) year -= 1900;
- else if (len != 2) goto bad_date;
- str += len;
-
- // 2 digit years, next 50 years are "future", last 50 years are "past".
- // A "future" date in past is a century ahead.
- // A non-future date in the future is a century behind.
- if ((r1 < r2) ? (r1 < year && year < r2) : (year < r1 || year > r2)) {
- if (year < r1) year += 100;
- } else if (year > r1) year -= 100;
- tm.tm_year = year + century;
- }
- if (*str == '.') {
- len = 0;
- sscanf(str, ".%u%n", &tm.tm_sec, &len);
- str += len;
- }
-
- return *str;
-}
-
-
void date_main(void)
{
- char *setdate = *toys.optargs, *format_string = "%a %b %e %H:%M:%S %Z %Y",
- *tz;
+ const char *format_string = "%a %b %e %H:%M:%S %Z %Y";
time_t now = time(NULL);
struct tm tm;
- // We can't just pass a timezone to mktime because posix.
- if (toys.optflags & FLAG_u) {
- tz = CFG_TOYBOX_FREE ? getenv("TZ") : 0;
- setenv("TZ", "UTC", 1);
- tzset();
- }
-
if (TT.file) {
struct stat st;
xstat(TT.file, &st);
now = st.st_mtim.tv_sec;
- } else if (TT.showdate) {
- if (TT.setfmt) {
- char *s = strptime(TT.showdate, TT.setfmt, &tm);
-
- if (!s || !*s) goto bad_date;
- } else if (parse_posixdate(TT.showdate, &tm)) goto bad_date;
- } else localtime_r(&now, &tm);
+ }
+ ((toys.optflags & FLAG_u) ? gmtime_r : localtime_r)(&now, &tm);
- // Fall through if no arguments
- if (!setdate);
// Display the date?
- else if (*setdate == '+') {
- format_string = toys.optargs[0]+1;
- setdate = toys.optargs[1];
+ if (!toys.optargs[0] || toys.optargs[0][0] == '+') {
+ if (toys.optargs[0]) format_string = toys.optargs[0]+1;
+ if (!strftime(toybuf, sizeof(toybuf), format_string, &tm)) goto bad_format;
+
+ puts(toybuf);
// Set the date
- } else if (setdate) {
- if (parse_posixdate(setdate, tm)) goto bad_date;
+ } else {
+ struct timeval tv;
+ char *s = *toys.optargs;
+ int len;
+
+ // Date format: mmddhhmm[[cc]yy]
+ len = 0;
+ sscanf(s, "%2u%2u%2u%2u%n", &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
+ &tm.tm_min, &len);
+ if (len != 8) goto bad_date;
+ s += len;
+ tm.tm_mon--;
+
+ // If year specified, overwrite one we fetched earlier
+ if (*s && *s != '.') {
+ unsigned year, r1 = tm.tm_year % 100, r2 = (tm.tm_year + 50) % 100,
+ century = tm.tm_year - r1;
+
+ len = 0;
+ sscanf(s, "%u%n", &year, &len);
+ if (len == 4) year -= 1900;
+ else if (len != 2) goto bad_date;
+ s += len;
+
+ // 2 digit years, next 50 years are "future", last 50 years are "past".
+ // A "future" date in past is a century ahead.
+ // A non-future date in the future is a century behind.
+ if ((r1 < r2) ? (r1 < year && year < r2) : (year < r1 || year > r2)) {
+ if (year < r1) year += 100;
+ } else if (year > r1) year -= 100;
+ tm.tm_year = year + century;
+ }
+ if (*s == '.') {
+ len = 0;
+ sscanf(s, ".%u%n", &tm.tm_sec, &len);
+ s += len;
+ }
+ if (*s) goto bad_date;
if (toys.optflags & FLAG_u) {
- // We can't just pass a timezone to mktime because posix.
+ // Get the UTC version of a struct tm
char *tz = CFG_TOYBOX_FREE ? getenv("TZ") : 0;
-
setenv("TZ", "UTC", 1);
tzset();
tv.tv_sec = mktime(&tm);
@@ -148,18 +107,11 @@ void date_main(void)
if (tv.tv_sec == (time_t)-1) goto bad_date;
tv.tv_usec = 0;
+ if (!strftime(toybuf, sizeof(toybuf), format_string, &tm)) goto bad_format;
+ puts(toybuf);
if (settimeofday(&tv, NULL) < 0) perror_msg("cannot set date");
}
- if (toys.optflags & FLAG_u) {
- if (tz) setenv("TZ", tz, 1);
- else unsetenv("TZ");
- tzset();
- }
-
- if (!strftime(toybuf, sizeof(toybuf), format_string, &tm)) goto bad_format;
- puts(toybuf);
-
return;
bad_date:
diff --git a/toys/posix/du.c b/toys/posix/du.c
index 64ba0517..96922bca 100644
--- a/toys/posix/du.c
+++ b/toys/posix/du.c
@@ -54,8 +54,18 @@ static void print(long long size, struct dirtree *node)
if (TT.maxdepth && TT.depth > TT.maxdepth) return;
- if (toys.optflags & FLAG_h) printf("%s", human_readable(size));
- else {
+ if (toys.optflags & FLAG_h) {
+ char buf[32];
+ int index, sz;
+
+ for (index = 0; 1024 < size>>(10*index); index++);
+ sz = size>>(10*index);
+ if (sz < 10) {
+ sprintf(buf, "%llu", size>>(10*(index-1)));
+ printf("%c.%c", buf[0], buf[1]);
+ } else printf("%d", sz);
+ if (index) printf("%c", " KMGTPE"[index]);
+ } else {
int bits = 10;
if (toys.optflags & FLAG_K) bits = 9;
diff --git a/toys/posix/grep.c b/toys/posix/grep.c
index 2837cc24..aba70878 100644
--- a/toys/posix/grep.c
+++ b/toys/posix/grep.c
@@ -4,7 +4,7 @@
*
* See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html
-USE_GREP(NEWTOY(grep, "A#B#C#ZzEFHabhinorsvwclqe*f*m#x[!wx][!EFw]", TOYFLAG_BIN))
+USE_GREP(NEWTOY(grep, "ZzEFHabhinorsvwclqe*f*m#x[!wx][!EFw]", TOYFLAG_BIN))
USE_GREP(OLDTOY(egrep, grep, OPTSTR_grep, TOYFLAG_BIN))
USE_GREP(OLDTOY(fgrep, grep, OPTSTR_grep, TOYFLAG_BIN))
@@ -46,67 +46,40 @@ GLOBALS(
long m;
struct arg_list *f;
struct arg_list *e;
- long C;
- long B;
- long A;
struct arg_list *regex;
- struct double_list *blist;
)
-struct dlist_off {
- char *next, *prev;
- long offset;
- char *data;
-};
-
static void do_grep(int fd, char *name)
{
FILE *file = fdopen(fd, "r");
long offset = 0;
- int lcount = 0, mcount = 0, which = toys.optflags & FLAG_w ? 2 : 0,
- blines = 0, alines = 0, dash = 0;
+ int lcount = 0, mcount = 0, which = toys.optflags & FLAG_w ? 2 : 0;
char indelim = '\n' * !(toys.optflags&FLAG_z),
outdelim = '\n' * !(toys.optflags&FLAG_Z);
if (!fd) name = "(standard input)";
-fprintf(stderr, "boo\n");
if (!file) {
perror_msg("%s", name);
return;
}
- // Loop through lines of input
for (;;) {
- char *oline = 0, *line = 0, *start;
+ char *line = 0, *start;
regmatch_t matches[3];
size_t unused;
long len;
int mmatch = 0;
- // Read next line of input
lcount++;
if (0 > (len = getdelim(&line, &unused, indelim, file))) break;
if (line[len-1] == indelim) line[len-1] = 0;
-fprintf(stderr, "line=%s\n", line);
- // Unconditionally add line to blist so output can always just dump blist.
- dlist_add(&TT.blist, line);
-fprintf(stderr, "added=%s\n", TT.blist->data);
-fprintf(stderr, "prev=%s\n", TT.blist->prev->data);
- if (blines <= TT.B) blines++;
- else {
- struct double_list *temp = dlist_pop(&TT.blist);
-fprintf(stderr, "bird=%s\n", temp->data);
- free(temp->data);
- free(temp);
- }
start = line;
- // Loop to match multiple times within the same line (if necessary)
- for (;;) {
-fprintf(stderr, "match?\n");
+ for (;;)
+ {
int rc = 0, skip = 0;
if (toys.optflags & FLAG_F) {
@@ -159,51 +132,30 @@ fprintf(stderr, "match?\n");
}
matches[which].rm_so = 0;
} else if (rc) break;
-fprintf(stderr, "got match %s\n", line);
- // We got a match, figure out how to display it
+
mmatch++;
toys.exitval = 0;
if (toys.optflags & FLAG_q) xexit();
if (toys.optflags & FLAG_l) {
printf("%s%c", name, outdelim);
- goto finish;
+ free(line);
+ fclose(file);
+ return;
}
-
- line = 0;
-fprintf(stderr, "here=%s\n", TT.blist->prev->data);
- // Yes, -o sometimes counts things as a match (-c) but doesn't display it
if (toys.optflags & FLAG_o)
if (matches[which].rm_eo == matches[which].rm_so)
break;
-// List of lines that DIDN'T match, print backlog?
-// Except this includes the one we just matched...?
- while (TT.blist) {
- struct double_list *dlist = dlist_pop(&TT.blist);
- char *ll = dlist->data;
-fprintf(stderr, "popped %s\n", ll);
- if (dash) printf("--%c", outdelim);
- dash = 0;
-
- if (!(toys.optflags & FLAG_c)) {
- if (toys.optflags & FLAG_H) printf("%s:", name);
- if (toys.optflags & FLAG_n) printf("%d:", lcount);
- if (toys.optflags & FLAG_b)
- printf("%ld:", offset + (start - dlist->data) +
- ((toys.optflags & FLAG_o) ? matches[which].rm_so : 0));
- if (!(toys.optflags & FLAG_o)) xprintf("%s%c", dlist->data, outdelim);
- else if (!TT.blist) {
-
-// TODO: FLAG_o prints multiple times, can't free it yet?
- xprintf("%.*s%c", matches[which].rm_eo - matches[which].rm_so,
- start + matches[which].rm_so, outdelim);
- line = dlist->data;
- }
- }
- if (oline && !TT.blist) TT.blist = dlist;
+ if (!(toys.optflags & FLAG_c)) {
+ if (toys.optflags & FLAG_H) printf("%s:", name);
+ if (toys.optflags & FLAG_n) printf("%d:", lcount);
+ if (toys.optflags & FLAG_b)
+ printf("%ld:", offset + (start-line) +
+ ((toys.optflags & FLAG_o) ? matches[which].rm_so : 0));
+ if (!(toys.optflags & FLAG_o)) xprintf("%s%c", line, outdelim);
else {
- free(dlist->data);
- free(dlist);
+ xprintf("%.*s%c", matches[which].rm_eo - matches[which].rm_so,
+ start + matches[which].rm_so, outdelim);
}
}
@@ -211,7 +163,7 @@ fprintf(stderr, "popped %s\n", ll);
if (!(toys.optflags & FLAG_o) || !*start) break;
}
offset += len;
-fprintf(stderr, "Spacious skies\n");
+
free(line);
if (mmatch) mcount++;
@@ -223,14 +175,6 @@ fprintf(stderr, "Spacious skies\n");
xprintf("%d%c", mcount, outdelim);
}
-finish:
- while (CFG_TOYBOX_FREE && TT.blist) {
- struct double_list *dlist = dlist_pop(&TT.blist);
-
- free(dlist->data);
- free(dlist);
- }
-
// loopfiles will also close the fd, but this frees an (opaque) struct.
fclose(file);
}
@@ -329,10 +273,6 @@ void grep_main(void)
toys.optc--;
}
- if (!TT.A) TT.A = TT.C;
- if (!TT.B) TT.B = TT.C;
- if (toys.optflags & (FLAG_l|FLAG_o)) TT.B = 0; // avoid memory leak
-
parse_regex();
if (!(toys.optflags & FLAG_h) && toys.optc>1) toys.optflags |= FLAG_H;