diff options
author | Rob Landley <rob@landley.net> | 2012-07-17 23:11:06 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2012-07-17 23:11:06 -0500 |
commit | 18b14e52a577754db66fe46a6ece5f2a06a7a83a (patch) | |
tree | 98f15451e276b450f216a66d1db8b254a742d974 | |
parent | 756f794beece13a27b4ab38947144fa356609d4c (diff) | |
download | toybox-18b14e52a577754db66fe46a6ece5f2a06a7a83a.tar.gz |
Largely cosmetic code cleanups.
-rw-r--r-- | toys/date.c | 47 | ||||
-rw-r--r-- | toys/taskset.c | 45 |
2 files changed, 33 insertions, 59 deletions
diff --git a/toys/date.c b/toys/date.c index ebcdeb1f..7881f464 100644 --- a/toys/date.c +++ b/toys/date.c @@ -6,7 +6,7 @@ * * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html -USE_DATE(NEWTOY(date, "u", TOYFLAG_BIN)) +USE_DATE(NEWTOY(date, "r:u", TOYFLAG_BIN)) config DATE bool "date" @@ -23,8 +23,7 @@ config DATE static int fromdec(const char *buf, int len) { int result = 0; - while (len--) - result=result * 10 + (*buf++ - '0'); + while (len--) result=result * 10 + (*buf++ - '0'); return result; } @@ -36,16 +35,13 @@ void date_main(void) if (!toys.optargs[0] || toys.optargs[0][0] == '+') { time_t now = time(NULL); struct tm *tm; - if (toys.optargs[0]) - format_string = &toys.optargs[0][1]; - if (toys.optflags) - tm = gmtime(&now); - else - tm = localtime(&now); - if (!tm) - perror_msg("Unable to retrieve current time"); + + if (toys.optargs[0]) format_string = toys.optargs[0]+1; + if (toys.optflags) tm = gmtime(&now); + else tm = localtime(&now); + if (!tm) perror_msg("Unable to retrieve current time"); if (!strftime(toybuf, sizeof(toybuf), format_string, tm)) - perror_msg("invalid format string `%s'", format_string); + perror_msg("bad format `%s'", format_string); puts(toybuf); } else { int len = strlen(toys.optargs[0]); @@ -53,30 +49,26 @@ void date_main(void) struct timeval tv; if (len < 8 || len > 12 || len & 1) - error_msg("invalid date `%s'", toys.optargs[0]); + error_msg("bad date `%s'", toys.optargs[0]); memset(&tm, 0, sizeof(tm)); /* Date format: mmddhhmm[[cc]yy] */ tm.tm_mon = fromdec(toys.optargs[0], 2) - 1; tm.tm_mday = fromdec(&toys.optargs[0][2], 2); tm.tm_hour = fromdec(&toys.optargs[0][4], 2); tm.tm_min = fromdec(&toys.optargs[0][6], 2); - if (len == 12) - tm.tm_year = fromdec(&toys.optargs[0][8], 4) - 1900; + if (len == 12) tm.tm_year = fromdec(&toys.optargs[0][8], 4) - 1900; else if (len == 10) { tm.tm_year = fromdec(&toys.optargs[0][8], 2); /* 69-99 = 1969-1999, 0 - 68 = 2000-2068 */ - if (tm.tm_year < 69) - tm.tm_year += 100; + if (tm.tm_year < 69) tm.tm_year += 100; } else { /* Year not specified, so retrieve current year */ time_t now = time(NULL); struct tm *now_tm = localtime(&now); - if (!now_tm) - perror_msg("Unable to retrieve current time"); + if (!now_tm) perror_msg("Unable to retrieve current time"); tm.tm_year = now_tm->tm_year; } - if (!toys.optflags) - tv.tv_sec = mktime(&tm); + if (!toys.optflags) tv.tv_sec = mktime(&tm); else { /* Get the UTC version of a struct tm */ char *tz = NULL; @@ -84,20 +76,17 @@ void date_main(void) setenv("TZ", "", 1); tzset(); tv.tv_sec = mktime(&tm); - if (tz) - setenv("TZ", tz, 1); - else - unsetenv("TZ"); + if (tz) setenv("TZ", tz, 1); + else unsetenv("TZ"); tzset(); } if (tv.tv_sec == (time_t)-1) - error_msg("invalid date `%s'", toys.optargs[0]); + error_msg("bad `%s'", toys.optargs[0]); tv.tv_usec = 0; if (!strftime(toybuf, sizeof(toybuf), format_string, &tm)) - perror_msg("invalid format string `%s'", format_string); + perror_msg("bad format `%s'", format_string); puts(toybuf); - if (settimeofday(&tv, NULL) < 0) - perror_msg("cannot set date"); + if (settimeofday(&tv, NULL) < 0) perror_msg("cannot set date"); } } diff --git a/toys/taskset.c b/toys/taskset.c index c877264a..6a962241 100644 --- a/toys/taskset.c +++ b/toys/taskset.c @@ -17,7 +17,7 @@ config TASKSET When mask is present the CPU affinity mask of a given PID will be set to this mask. When a mask is not given, the mask will be printed. A mask is a hexadecimal string where the bit position - ..matches the cpu number. + matches the cpu number. -a Set/get the affinity of all tasks of a PID. */ @@ -31,18 +31,14 @@ static int str_to_cpu_set(char * mask, cpu_set_t *set) int cpu = 0; CPU_ZERO(set); - if (size > 1 && mask[0] == '0' && mask[1] == 'x') - mask += 2; + if (size > 1 && mask[0] == '0' && mask[1] == 'x') mask += 2; while(ptr >= mask) { char val = 0; - if ( *ptr >= '0' && *ptr <= '9') - val = *ptr - '0'; - else if (*ptr >= 'a' && *ptr <= 'f') - val = 10 + (*ptr - 'a'); - else - return -1; + if ( *ptr >= '0' && *ptr <= '9') val = *ptr - '0'; + else if (*ptr >= 'a' && *ptr <= 'f') val = 10 + (*ptr - 'a'); + else return -1; if (val & 1) CPU_SET(cpu, set); if (val & 2) CPU_SET(cpu + 1, set); @@ -68,10 +64,8 @@ static char * cpu_set_to_str(cpu_set_t *set) if (CPU_ISSET(cpu + 3, set)) val |= 8; if (ptr != toybuf || val != 0) { - if (val < 10) - *ptr = '0' + val; - else - *ptr = 'a' + (val - 10); + if (val < 10) *ptr = '0' + val; + else *ptr = 'a' + (val - 10); ptr++; } } @@ -82,6 +76,8 @@ static char * cpu_set_to_str(cpu_set_t *set) static void do_taskset(pid_t pid) { cpu_set_t mask; + + if (!pid) return; if (sched_getaffinity(pid, sizeof(mask), &mask)) perror_exit("failed to get %d's affinity", pid); @@ -90,7 +86,7 @@ static void do_taskset(pid_t pid) if (toys.optc == 2) { if (str_to_cpu_set(toys.optargs[0], &mask)) - perror_exit("failed to parse CPU mask: %s", toys.optargs[0]); + perror_exit("bad mask: %s", toys.optargs[0]); if (sched_setaffinity(pid, sizeof(mask), &mask)) perror_exit("failed to set %d's affinity", pid); @@ -104,15 +100,10 @@ static void do_taskset(pid_t pid) static int task_cb(struct dirtree *new) { - if (S_ISDIR(new->st.st_mode)) - { - if (strstr(new->name,"/task/\0") != NULL) - return DIRTREE_RECURSE; + if (!new->parent) return DIRTREE_RECURSE; + if (S_ISDIR(new->st.st_mode) && *new->name != '.') + do_taskset(atoi(new->name)); - pid_t tid = atoi(new->name); - if (tid) - do_taskset(tid); - } return 0; } @@ -120,15 +111,9 @@ void taskset_main(void) { char * pidstr = (toys.optc==1)?toys.optargs[0]:toys.optargs[1]; - if ( toys.optflags & 0x1 ) + if (toys.optflags & 0x1) { sprintf(toybuf, "/proc/%s/task/", pidstr); dirtree_read(toybuf, task_cb); - } - else - { - pid_t tid = atoi(pidstr); - if (tid) - do_taskset(tid); - } + } else do_taskset(atoi(pidstr)); } |