Created
September 10, 2010 17:02
-
-
Save osamu/574001 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*** coreutils-8.5/src/tee.c.orig 2010-01-01 22:06:47.000000000 +0900 | |
--- coreutils-8.5/src/tee.c 2010-09-11 01:57:23.000000000 +0900 | |
*************** | |
*** 20,25 **** | |
--- 20,26 ---- | |
#include <sys/types.h> | |
#include <signal.h> | |
#include <getopt.h> | |
+ #include <time.h> | |
#include "system.h" | |
#include "error.h" | |
*************** | |
*** 39,50 **** | |
--- 40,55 ---- | |
/* If true, append to output files rather than truncating them. */ | |
static bool append; | |
+ static bool with_timestamp; | |
+ static char timestamp_format[128] = "%H:%M:%S "; | |
+ | |
/* If true, ignore interrupts. */ | |
static bool ignore_interrupts; | |
static struct option const long_options[] = | |
{ | |
{"append", no_argument, NULL, 'a'}, | |
+ {"times", optional_argument, NULL,'t'}, | |
{"ignore-interrupts", no_argument, NULL, 'i'}, | |
{GETOPT_HELP_OPTION_DECL}, | |
{GETOPT_VERSION_OPTION_DECL}, | |
*************** | |
*** 65,70 **** | |
--- 70,76 ---- | |
\n\ | |
-a, --append append to the given FILEs, do not overwrite\n\ | |
-i, --ignore-interrupts ignore interrupt signals\n\ | |
+ -t, --time [FORMAT] insert timestamp to each prefix of line. if no format is specified, %H:%M:%S is used. \n\ | |
"), stdout); | |
fputs (HELP_OPTION_DESCRIPTION, stdout); | |
fputs (VERSION_OPTION_DESCRIPTION, stdout); | |
*************** | |
*** 93,100 **** | |
append = false; | |
ignore_interrupts = false; | |
! while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1) | |
{ | |
switch (optc) | |
{ | |
--- 99,107 ---- | |
append = false; | |
ignore_interrupts = false; | |
+ with_timestamp = false; | |
! while ((optc = getopt_long (argc, argv, "ait:", long_options, NULL)) != -1) | |
{ | |
switch (optc) | |
{ | |
*************** | |
*** 106,111 **** | |
--- 113,124 ---- | |
ignore_interrupts = true; | |
break; | |
+ case 't': | |
+ with_timestamp = true; | |
+ if( optarg ) | |
+ strcpy( timestamp_format, optarg); | |
+ break; | |
+ | |
case_GETOPT_HELP_CHAR; | |
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); | |
*************** | |
*** 144,152 **** | |
--- 157,176 ---- | |
(O_BINARY | |
? (append ? "ab" : "wb") | |
: (append ? "a" : "w")); | |
+ time_t t; | |
+ struct tm *lt; | |
+ char *line; | |
+ size_t line_len = 0; | |
+ char *tbuffer, *p; | |
+ size_t tbuffer_maxsize = BUFSIZ + 1024; | |
+ size_t tbuffer_len = 0; | |
descriptors = xnmalloc (nfiles + 1, sizeof *descriptors); | |
+ if(with_timestamp){ | |
+ tbuffer = xnmalloc( tbuffer_maxsize, sizeof(char)); | |
+ } | |
+ | |
/* Move all the names `up' one in the argv array to make room for | |
the entry for standard output. This writes into argv[argc]. */ | |
for (i = nfiles; i >= 1; i--) | |
*************** | |
*** 176,184 **** | |
else | |
setvbuf (descriptors[i], NULL, _IONBF, 0); | |
} | |
- | |
while (1) | |
{ | |
bytes_read = read (0, buffer, sizeof buffer); | |
#ifdef EINTR | |
if (bytes_read < 0 && errno == EINTR) | |
--- 200,210 ---- | |
else | |
setvbuf (descriptors[i], NULL, _IONBF, 0); | |
} | |
while (1) | |
{ | |
+ line = buffer; | |
+ tbuffer_len = line_len = 0; | |
+ | |
bytes_read = read (0, buffer, sizeof buffer); | |
#ifdef EINTR | |
if (bytes_read < 0 && errno == EINTR) | |
*************** | |
*** 187,197 **** | |
if (bytes_read <= 0) | |
break; | |
/* Write to all NFILES + 1 descriptors. | |
Standard output is the first one. */ | |
for (i = 0; i <= nfiles; i++) | |
if (descriptors[i] | |
! && fwrite (buffer, bytes_read, 1, descriptors[i]) != 1) | |
{ | |
error (0, errno, "%s", files[i]); | |
descriptors[i] = NULL; | |
--- 213,249 ---- | |
if (bytes_read <= 0) | |
break; | |
+ for( i = 0 ; (i < bytes_read) & (with_timestamp) ; i++){ | |
+ line_len += 1; | |
+ if (buffer[i] =='\n'){ | |
+ time(&t); | |
+ lt = localtime(&t); | |
+ tbuffer_len += strftime( tbuffer + tbuffer_len , | |
+ tbuffer_maxsize - tbuffer_len, | |
+ timestamp_format,lt); | |
+ if ( tbuffer_len + line_len > tbuffer_maxsize ) { | |
+ tbuffer = xrealloc(tbuffer, sizeof(char)* (tbuffer_maxsize +1024)); | |
+ tbuffer_maxsize +=1024; | |
+ } | |
+ memcpy(tbuffer + tbuffer_len, line, line_len); | |
+ tbuffer_len += line_len; | |
+ if ( i+1 < bytes_read ){ | |
+ line = &(buffer[i+1]); | |
+ } | |
+ line_len = 0; | |
+ } | |
+ } | |
+ | |
+ if (!with_timestamp ){ | |
+ tbuffer = buffer; | |
+ tbuffer_len = bytes_read; | |
+ } | |
+ | |
/* Write to all NFILES + 1 descriptors. | |
Standard output is the first one. */ | |
for (i = 0; i <= nfiles; i++) | |
if (descriptors[i] | |
! && fwrite (tbuffer , tbuffer_len , 1, descriptors[i]) != 1) | |
{ | |
error (0, errno, "%s", files[i]); | |
descriptors[i] = NULL; |
osamu@papyrus% ping localhost | ./tee -t "%Y/%m/%d %H:%M:%S : "
2010/09/11 02:17:18 : PING localhost (127.0.0.1) 56(84) bytes of data.
2010/09/11 02:17:18 : 64 bytes from localhost (127.0.0.1): icmp_req=1 ttl=64 time=0.049 ms
2010/09/11 02:17:19 : 64 bytes from localhost (127.0.0.1): icmp_req=2 ttl=64 time=0.031 ms
2010/09/11 02:17:20 : 64 bytes from localhost (127.0.0.1): icmp_req=3 ttl=64 time=0.039 ms
2010/09/11 02:17:21 : 64 bytes from localhost (127.0.0.1): icmp_req=4 ttl=64 time=0.033 ms
2010/09/11 02:17:22 : 64 bytes from localhost (127.0.0.1): icmp_req=5 ttl=64 time=0.039 ms
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
osamu@papyrus% ./tee -t "%H:%M:%S "
this is test string
02:15:33 this is test string
each line have time
02:15:45 each line have time