diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-05-25 01:14:14 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-05-25 01:14:14 +0000 |
| commit | 30f892a0ee32ec556467e9f7bc7c46d4ed42f12e (patch) | |
| tree | b0cade901552b736374fc60e975e902fe61b9a5f /miscutils | |
| parent | 69dc3253972cbf6169abcaaef5fc7bc787a40075 (diff) | |
| download | busybox-w32-30f892a0ee32ec556467e9f7bc7c46d4ed42f12e.tar.gz busybox-w32-30f892a0ee32ec556467e9f7bc7c46d4ed42f12e.tar.bz2 busybox-w32-30f892a0ee32ec556467e9f7bc7c46d4ed42f12e.zip | |
last: code shrink
function old new delta
show_entry 319 311 -8
last_main 917 907 -10
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-18) Total: -18 bytes
Diffstat (limited to 'miscutils')
| -rw-r--r-- | miscutils/last.c | 5 | ||||
| -rw-r--r-- | miscutils/last_fancy.c | 34 |
2 files changed, 20 insertions, 19 deletions
diff --git a/miscutils/last.c b/miscutils/last.c index f25411881..a84e77644 100644 --- a/miscutils/last.c +++ b/miscutils/last.c | |||
| @@ -118,9 +118,10 @@ int last_main(int argc, char **argv ATTRIBUTE_UNUSED) | |||
| 118 | printf("%-10s %-14s %-18s %-12.12s\n", | 118 | printf("%-10s %-14s %-18s %-12.12s\n", |
| 119 | ut.ut_user, ut.ut_line, ut.ut_host, ctime(&t_tmp) + 4); | 119 | ut.ut_user, ut.ut_line, ut.ut_host, ctime(&t_tmp) + 4); |
| 120 | next: | 120 | next: |
| 121 | if (!pos) | 121 | pos -= sizeof(ut); |
| 122 | if (pos <= 0) | ||
| 122 | break; /* done. */ | 123 | break; /* done. */ |
| 123 | pos = lseek(file, pos - sizeof(ut), SEEK_SET); | 124 | xlseek(file, pos, SEEK_SET); |
| 124 | } | 125 | } |
| 125 | 126 | ||
| 126 | fflush_stdout_and_exit(EXIT_SUCCESS); | 127 | fflush_stdout_and_exit(EXIT_SUCCESS); |
diff --git a/miscutils/last_fancy.c b/miscutils/last_fancy.c index 91315d338..d234a48bf 100644 --- a/miscutils/last_fancy.c +++ b/miscutils/last_fancy.c | |||
| @@ -57,11 +57,11 @@ static void show_entry(struct utmp *ut, int state, time_t dur_secs) | |||
| 57 | hours = mins / 60; | 57 | hours = mins / 60; |
| 58 | mins = mins % 60; | 58 | mins = mins % 60; |
| 59 | 59 | ||
| 60 | if (days) { | 60 | // if (days) { |
| 61 | sprintf(duration, "(%u+%02u:%02u)", days, hours, mins); | 61 | sprintf(duration, "(%u+%02u:%02u)", days, hours, mins); |
| 62 | } else { | 62 | // } else { |
| 63 | sprintf(duration, " (%02u:%02u)", hours, mins); | 63 | // sprintf(duration, " (%02u:%02u)", hours, mins); |
| 64 | } | 64 | // } |
| 65 | 65 | ||
| 66 | logout_str = logout_time; | 66 | logout_str = logout_time; |
| 67 | duration_str = duration; | 67 | duration_str = duration; |
| @@ -100,13 +100,13 @@ static void show_entry(struct utmp *ut, int state, time_t dur_secs) | |||
| 100 | static int get_ut_type(struct utmp *ut) | 100 | static int get_ut_type(struct utmp *ut) |
| 101 | { | 101 | { |
| 102 | if (ut->ut_line[0] == '~') { | 102 | if (ut->ut_line[0] == '~') { |
| 103 | if (strncmp(ut->ut_user, "shutdown", sizeof("shutdown")-1) == 0) { | 103 | if (strcmp(ut->ut_user, "shutdown") == 0) { |
| 104 | return SHUTDOWN_TIME; | 104 | return SHUTDOWN_TIME; |
| 105 | } | 105 | } |
| 106 | if (strncmp(ut->ut_user, "reboot", sizeof("reboot")-1) == 0) { | 106 | if (strcmp(ut->ut_user, "reboot") == 0) { |
| 107 | return BOOT_TIME; | 107 | return BOOT_TIME; |
| 108 | } | 108 | } |
| 109 | if (strncmp(ut->ut_user, "runlevel", sizeof("runlevel")-1) == 0) { | 109 | if (strcmp(ut->ut_user, "runlevel") == 0) { |
| 110 | return RUN_LVL; | 110 | return RUN_LVL; |
| 111 | } | 111 | } |
| 112 | return ut->ut_type; | 112 | return ut->ut_type; |
| @@ -172,31 +172,32 @@ int last_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
| 172 | #endif | 172 | #endif |
| 173 | 173 | ||
| 174 | file = xopen(filename, O_RDONLY); | 174 | file = xopen(filename, O_RDONLY); |
| 175 | if (full_read(file, &ut, sizeof(ut)) != sizeof(ut)) { | 175 | { |
| 176 | /* in case the file is empty... */ | ||
| 176 | struct stat st; | 177 | struct stat st; |
| 177 | fstat(file, &st); | 178 | fstat(file, &st); |
| 178 | start_time = st.st_ctime; | 179 | start_time = st.st_ctime; |
| 179 | goto exit; | ||
| 180 | } | 180 | } |
| 181 | start_time = ut.ut_time; | ||
| 182 | 181 | ||
| 183 | time(&down_time); | 182 | time(&down_time); |
| 184 | going_down = 0; | 183 | going_down = 0; |
| 185 | boot_down = NORMAL; /* 0 */ | 184 | boot_down = NORMAL; /* 0 */ |
| 186 | zlist = NULL; | 185 | zlist = NULL; |
| 187 | boot_time = 0; | 186 | boot_time = 0; |
| 188 | pos = 0; | 187 | /* get file size, rounding down to last full record */ |
| 188 | pos = xlseek(file, 0, SEEK_END) / sizeof(ut) * sizeof(ut); | ||
| 189 | for (;;) { | 189 | for (;;) { |
| 190 | pos -= (off_t)sizeof(ut); | 190 | pos -= (off_t)sizeof(ut); |
| 191 | /* Bug? What if file changes size? | 191 | if (pos < 0) { |
| 192 | * What if size is not a multiple of sizeof(ut)? */ | ||
| 193 | if (lseek(file, pos, SEEK_END) < 0) { | ||
| 194 | /* Beyond the beginning of the file boundary => | 192 | /* Beyond the beginning of the file boundary => |
| 195 | * the whole file has been read. */ | 193 | * the whole file has been read. */ |
| 196 | break; | 194 | break; |
| 197 | } | 195 | } |
| 198 | if (full_read(file, &ut, sizeof(ut)) != sizeof(ut)) | 196 | xlseek(file, pos, SEEK_SET); |
| 199 | break; | 197 | xread(file, &ut, sizeof(ut)); |
| 198 | /* rewritten by each record, eventially will have | ||
| 199 | * first record's ut_time: */ | ||
| 200 | start_time = ut.ut_time; | ||
| 200 | 201 | ||
| 201 | switch (get_ut_type(&ut)) { | 202 | switch (get_ut_type(&ut)) { |
| 202 | case SHUTDOWN_TIME: | 203 | case SHUTDOWN_TIME: |
| @@ -281,7 +282,6 @@ int last_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
| 281 | llist_free(zlist, free); | 282 | llist_free(zlist, free); |
| 282 | } | 283 | } |
| 283 | 284 | ||
| 284 | exit: | ||
| 285 | printf("\nwtmp begins %s", ctime(&start_time)); | 285 | printf("\nwtmp begins %s", ctime(&start_time)); |
| 286 | 286 | ||
| 287 | if (ENABLE_FEATURE_CLEAN_UP) | 287 | if (ENABLE_FEATURE_CLEAN_UP) |
