-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtscat.c
1195 lines (1133 loc) · 52.8 KB
/
tscat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*####################################################################
#
# TSCAT - A "cat" Command Which Can Reprodude the Timing of Flow
#
# USAGE : tscat [-c|-e|-z] [-Z] [-k] [-u] [-y] [-p n] [file [...]]
# Args : file ........ Filepath to be send ("-" means STDIN)
# The file MUST be a textfile and MUST have
# a timestamp at the first field to make the
# timing of flow. The first space character
# <0x20> of every line will be regarded as
# the field delimiter.
# And, the string from the top of the line to
# the charater will be cut before outgoing to
# the stdout.
# Options : -c,-e,-z .... Specify the format for timestamp. You can choose
# one of them.
# -c ... "YYYYMMDDhhmmss[.n]" (default)
# Calendar time (standard time) in your
# timezone (".n" is the digits under
# second. You can specify up to nano
# second.)
# -e ... "n[.n]"
# The number of seconds since the UNIX
# epoch (".n" is the same as -x)
# -z ... "n[.n]"
# The number of seconds since this
# command has startrd (".n" is the same
# as -x)
# -Z .......... Define the time when the first line came as 0.
# For instance, imagine that the first field of
# the first line is "20200229235959," and the
# second line's one is "20200301000004." when
# "-c" option is given. In this case, the first
# line is sent to stdout immediately, and after
# five seconds, the second line is sent.
# -k .......... Keep the timestamp at the head of each line
# when outputting the line to the stdout.
# -u .......... Set the date in UTC when -c option is set
# (same as that of date command)
# -y .......... "Typing mode": Do not output the LF character
# at the end of each line in the input file unless
# the line has no other letters. This mode is
# useful to resconstruct the timing of key typing
# recorded by as in the following.
# $ typeliner -e | linets -c3 > mytyping.txt
# $ tscat -ycZ mytyping.txt
# [The following option is for professional]
# -p n ........ Process priority setting [0-3] (if possible)
# 0: Normal process
# 1: Weakest realtime process (default)
# 2: Strongest realtime process for generic users
# (for only Linux, equivalent 1 for otheres)
# 3: Strongest realtime process of this host
# Larger numbers maybe require a privileged user,
# but if failed, it will try the smaller numbers.
# Return : Return 0 only when finished successfully
#
# How to compile : cc -O3 -o __CMDNAME__ __SRCNAME__ -lrt
# (if it doesn't work)
# How to compile : cc -O3 -o __CMDNAME__ __SRCNAME__
#
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2024-06-23
#
# This is a public-domain software (CC0). It means that all of the
# people can use this for any purposes with no restrictions at all.
# By the way, We are fed up with the side effects which are brought
# about by the major licenses.
#
# The latest version is distributed at the following page.
# https://github.com/ShellShoccar-jpn/tokideli
#
####################################################################*/
/*####################################################################
# Initial Configuration
####################################################################*/
/*=== Initial Setting ==============================================*/
/*--- headers ------------------------------------------------------*/
#if defined(__linux) || defined(__linux__)
/* This definition is for strptime() on Linux */
#define _XOPEN_SOURCE 700
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/select.h>
#include <time.h>
#include <fcntl.h>
#include <signal.h>
#include <locale.h>
#if defined(_POSIX_PRIORITY_SCHEDULING) && !defined(__OpenBSD__) && !defined(__APPLE__)
#include <sched.h>
#include <sys/resource.h>
#endif
/*--- macro constants ----------------------------------------------*/
/* Some OSes, such as HP-UX, may not know the following macros whenever
<limit.h> is included. So, this source code defines them by itself. */
#ifndef LONG_MAX
#define LONG_MAX 2147483647
#endif
#ifndef LLONG_MAX
#define LLONG_MAX 9223372036854775807
#endif
/*--- prototype functions ------------------------------------------*/
void get_time_data_arrived(int iFd, struct timespec *ptsTime);
int read_1st_field_as_a_timestamp(FILE *fp, char *pszTime);
int read_and_write_a_line(FILE *fp);
int skip_over_a_line(FILE *fp);
int parse_calendartime(char* pszTime, struct timespec *ptsTime);
int parse_unixtime(char* pszTime, struct timespec *ptsTime);
void spend_my_spare_time(struct timespec *ptsTo, struct timespec *ptsOffset);
int change_to_rtprocess(int iPrio);
/*--- global variables ---------------------------------------------*/
char* gpszCmdname; /* The name of this command */
int giTypingmode; /* Typing mode by option -y is on if >0 */
int giVerbose; /* speaks more verbosely by the greater number */
struct timespec gtsZero; /* The zero-point time */
/*=== Define the functions for printing usage and error ============*/
/*--- exit with usage ----------------------------------------------*/
void print_usage_and_exit(void) {
fprintf(stderr,
#if defined(_POSIX_PRIORITY_SCHEDULING) && !defined(__OpenBSD__) && !defined(__APPLE__)
"USAGE : %s [-c|-e|-z] [-Z] [-k] [-u] [-y] [-p n] [file [...]]\n"
#else
"USAGE : %s [-c|-e|-z] [-Z] [-k] [-u] [-y] [file [...]]\n"
#endif
"Args : file ........ Filepath to be send (\"-\" means STDIN)\n"
" The file MUST be a textfile and MUST have\n"
" a timestamp at the first field to make the\n"
" timing of flow. The first space character\n"
" <0x20> of every line will be regarded as\n"
" the field delimiter.\n"
" And, the string from the top of the line to\n"
" the charater will be cut before outgoing to\n"
" the stdout.\n"
"Options : -c,-e,-z .... Specify the format for timestamp. You can choose\n"
" one of them.\n"
" -c ... \"YYYYMMDDhhmmss[.n]\" (default)\n"
" Calendar time (standard time) in your\n"
" timezone (\".n\" is the digits under\n"
" second. You can specify up to nano\n"
" second.)\n"
" -e ... \"n[.n]\"\n"
" The number of seconds since the UNIX\n"
" epoch (\".n\" is the same as -c)\n"
" -z ... \"n[.n]\"\n"
" The number of seconds since this\n"
" command has started (\".n\" is the same\n"
" as -c)\n"
" -Z .......... Define the time when the first line came as 0.\n"
" For instance, imagine that the first field of\n"
" the first line is \"20200229235959,\" and the\n"
" second line's one is \"20200301000004.\" when\n"
" \"-c\" option is given. In this case, the first\n"
" line is sent to stdout immediately, and after\n"
" five seconds, the second line is sent.\n"
" -k .......... Keep the timestamp at the head of each line\n"
" when outputting the line to the stdout.\n"
" -u .......... Set the date in UTC when -c option is set\n"
" (same as that of date command)\n"
" -y .......... \"Typing mode\": Do not output the LF character\n"
" at the end of each line in the input file unless\n"
" the line has no other letters. This mode is\n"
" useful to resconstruct the timing of key typing\n"
" recorded by as in the following.\n"
" $ typeliner -e | linets -c3 > mytyping.txt\n"
" $ tscat -ycZ mytyping.txt\n"
#if defined(_POSIX_PRIORITY_SCHEDULING) && !defined(__OpenBSD__) && !defined(__APPLE__)
" [The following option is for professional]\n"
" -p n ........ Process priority setting [0-3] (if possible)\n"
" 0: Normal process\n"
" 1: Weakest realtime process (default)\n"
" 2: Strongest realtime process for generic users\n"
" (for only Linux, equivalent 1 for otheres)\n"
" 3: Strongest realtime process of this host\n"
" Larger numbers maybe require a privileged user,\n"
" but if failed, it will try the smaller numbers.\n"
#endif
"Version : 2024-06-23 13:28:01 JST\n"
" (POSIX C language)\n"
"\n"
"Shell-Shoccar Japan (@shellshoccarjpn), No rights reserved.\n"
"This is public domain software. (CC0)\n"
"\n"
"The latest version is distributed at the following page.\n"
"https://github.com/ShellShoccar-jpn/tokideli\n"
,gpszCmdname);
exit(1);
}
/*--- print warning message ----------------------------------------*/
void warning(const char* szFormat, ...) {
va_list va;
va_start(va, szFormat);
fprintf(stderr,"%s: ",gpszCmdname);
vfprintf(stderr, szFormat, va);
va_end(va);
return;
}
/*--- exit with error message --------------------------------------*/
void error_exit(int iErrno, const char* szFormat, ...) {
va_list va;
va_start(va, szFormat);
fprintf(stderr,"%s: ",gpszCmdname);
vfprintf(stderr, szFormat, va);
va_end(va);
exit(iErrno);
}
/*####################################################################
# Main
####################################################################*/
/*=== Initialization ===============================================*/
int main(int argc, char *argv[]) {
/*--- Variables ----------------------------------------------------*/
int iMode; /* 0:"-c" 1:"-e" 2:"-z",
4:"-cZ" 5:"-eZ" 6:"-zZ" */
int iKeepTs; /* -k option flag (0>:Keep timestamps, =0:Drop)*/
int iPrio; /* -p option number (default 1) */
int iRet; /* return code */
int iGotOffset; /* 0:NotYet 1:GetZeroPoint 2:Done */
char szTime[34]; /* Buffer for the 1st field of lines */
struct timespec tsTime; /* Parsed time for the 1st field */
struct timespec tsOffset; /* Zero-point time to adjust the 1st field one */
char *pszPath; /* filepath on arguments */
char *pszFilename; /* filepath (for message) */
int iFileno; /* file# of filepath */
int iFd; /* file descriptor */
FILE *fp; /* file handle */
int i; /* all-purpose int */
/*--- Initialize ---------------------------------------------------*/
if (clock_gettime(CLOCK_REALTIME,>sZero) != 0) {
error_exit(errno,"clock_gettime() at initialize: %s\n",strerror(errno));
}
gpszCmdname = argv[0];
for (i=0; *(gpszCmdname+i)!='\0'; i++) {
if (*(gpszCmdname+i)=='/') {gpszCmdname=gpszCmdname+i+1;}
}
if (setenv("POSIXLY_CORRECT","1",1) < 0) {
error_exit(errno,"setenv() at initialization: \n", strerror(errno));
}
setlocale(LC_CTYPE, "");
/*=== Parse arguments ==============================================*/
/*--- Set default parameters of the arguments ----------------------*/
iMode = 0; /* 0:"-c"(default) 1:"-e" 2:"-z" 4:"-cZ" 5:"-eZ" 6:"-zZ" */
iKeepTs = 0; /* 0>:Keep timestamps, =0:Drop(default) */
giTypingmode = 0;
iPrio = 1;
giVerbose = 0;
/*--- Parse options which start by "-" -----------------------------*/
while ((i=getopt(argc, argv, "cep:kuyvhZz")) != -1) {
switch (i) {
case 'c': iMode&=4; iMode+=0; break;
case 'e': iMode&=4; iMode+=1; break;
case 'z': iMode&=4; iMode+=2; break;
case 'Z': iMode&=3; iMode+=4; break;
case 'k': iKeepTs=1; break;
case 'u': (void)setenv("TZ", "UTC0", 1); break;
case 'y': giTypingmode=1; break;
#if defined(_POSIX_PRIORITY_SCHEDULING) && !defined(__OpenBSD__) && !defined(__APPLE__)
case 'p': if (sscanf(optarg,"%d",&iPrio) != 1) {print_usage_and_exit();}
break;
#endif
case 'v': giVerbose++; break;
case 'h': print_usage_and_exit();
default : print_usage_and_exit();
}
}
argc -= optind-1;
argv += optind ;
if (giVerbose>0) {warning("verbose mode (level %d)\n",giVerbose);}
/*=== Switch buffer mode ===========================================*/
if (setvbuf(stdout,NULL,(giTypingmode>0)?_IONBF:_IOLBF,0)!=0) {
error_exit(255,"Failed to switch to line-buffered mode\n");
}
/*=== Try to make me a realtime process ============================*/
if (change_to_rtprocess(iPrio)==-1) {print_usage_and_exit();}
/*=== Each file loop ===============================================*/
iRet = 0;
iGotOffset = 0;
iFileno = 0;
iFd = -1;
while ((pszPath = argv[iFileno]) != NULL || iFileno == 0) {
/*--- Open one of the input files --------------------------------*/
if (pszPath == NULL || strcmp(pszPath, "-") == 0) {
pszFilename = "stdin" ;
iFd = STDIN_FILENO ;
} else {
pszFilename = pszPath ;
while ((iFd=open(pszPath, O_RDONLY)) < 0) {
iRet = 1;
warning("%s: %s\n", pszFilename, strerror(errno));
iFileno++;
break;
}
if (iFd < 0) {continue;}
}
if (iFd == STDIN_FILENO) {
fp = stdin;
if (feof(stdin)) {clearerr(stdin);} /* Reset EOF condition when stdin */
} else {
fp = fdopen(iFd, "r");
}
/*--- Reading and writing loop -----------------------------------*/
switch (iMode) {
case 0: /* "-c" Calendar time mode */
while (1) {
switch (read_1st_field_as_a_timestamp(fp, szTime)) {
case 1: /* read successfully */
if (! parse_calendartime(szTime, &tsTime)) {
warning("%s: %s: Invalid calendar-time, "
"skip this line\n", pszFilename, szTime);
iRet = 1;
switch (skip_over_a_line(fp)) {
case 1: /* expected LF */
break;
case -1: /* expected EOF */
goto CLOSE_THISFILE;
case -2: /* file access error */
warning("%s: File access error, "
"skip it\n", pszFilename);
goto CLOSE_THISFILE;
break;
default: /* bug of system error */
error_exit(1,"Unexpected error at %d\n",
__LINE__);
break;
}
break;
}
spend_my_spare_time(&tsTime, NULL);
if (iKeepTs) {
if (fputs(szTime, stdout)==EOF) {
error_exit(errno,"stdout write error #m1: %s\n",
strerror(errno));
}
}
switch (read_and_write_a_line(fp)) {
case 1: /* expected LF */
break;
case -1: /* expected EOF */
goto CLOSE_THISFILE;
case -2: /* file access error */
warning("%s: File access error, "
"skip it\n", pszFilename);
iRet = 1;
goto CLOSE_THISFILE;
break;
default: /* bug of system error */
error_exit(1,"Unexpected error at %d\n",
__LINE__);
break;
}
break;
case 0: /* unexpected LF */
warning("%s: %s: Invalid timestamp field found, "
"skip this line.\n", pszFilename, szTime);
iRet = 1;
break;
case -2: /* unexpected EOF */
warning("%s: Came to EOF suddenly\n", pszFilename);
iRet = 1;
case -1: /* expected EOF */
goto CLOSE_THISFILE;
break;
case -3: /* file access error */
warning("%s: File access error, skip it\n",
pszFilename);
iRet = 1;
goto CLOSE_THISFILE;
default: /* bug or system error */
error_exit(1,"Unexpected error at %d\n", __LINE__);
}
}
break;
case 1: /* "-e" UNIX epoch time mode */
while (1) {
switch (read_1st_field_as_a_timestamp(fp, szTime)) {
case 1: /* read successfully */
if (! parse_unixtime(szTime, &tsTime)) {
warning("%s: %s: Invalid UNIX-time, "
"skip this line\n", pszFilename, szTime);
iRet = 1;
switch (skip_over_a_line(fp)) {
case 1: /* expected LF */
break;
case -1: /* expected EOF */
goto CLOSE_THISFILE;
case -2: /* file access error */
warning("%s: File access error, "
"skip it\n", pszFilename);
goto CLOSE_THISFILE;
break;
default: /* bug of system error */
error_exit(1,"Unexpected error at %d\n",
__LINE__);
break;
}
break;
}
spend_my_spare_time(&tsTime, NULL);
if (iKeepTs) {
if (fputs(szTime, stdout)==EOF) {
error_exit(errno,"stdout write error #m2: %s\n",
strerror(errno));
}
}
switch (read_and_write_a_line(fp)) {
case 1: /* expected LF */
break;
case -1: /* expected EOF */
goto CLOSE_THISFILE;
case -2: /* file access error */
warning("%s: File access error, "
"skip it\n", pszFilename);
iRet = 1;
goto CLOSE_THISFILE;
break;
default: /* bug of system error */
error_exit(1,"Unexpected error at %d\n",
__LINE__);
break;
}
break;
case 0: /* unexpected LF */
warning("%s: %s: Invalid timestamp field found, "
"skip this line.\n", pszFilename, szTime);
iRet = 1;
break;
case -2: /* unexpected EOF */
warning("%s: Came to EOF suddenly\n", pszFilename);
iRet = 1;
case -1: /* expected EOF */
goto CLOSE_THISFILE;
break;
case -3: /* file access error */
warning("%s: File access error, skip it\n",
pszFilename);
iRet = 1;
goto CLOSE_THISFILE;
default: /* bug or system error */
error_exit(1,"Unexpected error at %d\n", __LINE__);
}
}
break;
case 2: /* "-z" Zero time mode */
while (1) {
switch (read_1st_field_as_a_timestamp(fp, szTime)) {
case 1: /* read successfully */
if (! parse_unixtime(szTime, &tsTime)) {
warning("%s: %s: Invalid number of seconds, "
"skip this line\n", pszFilename, szTime);
iRet = 1;
switch (skip_over_a_line(fp)) {
case 1: /* expected LF */
break;
case -1: /* expected EOF */
goto CLOSE_THISFILE;
case -2: /* file access error */
warning("%s: File access error, "
"skip it\n", pszFilename);
goto CLOSE_THISFILE;
break;
default: /* bug of system error */
error_exit(1,"Unexpected error at %d\n",
__LINE__);
break;
}
break;
}
if (iGotOffset<2) {
/* tsOffset = gtsZero */
tsOffset.tv_sec = gtsZero.tv_sec ;
tsOffset.tv_nsec = gtsZero.tv_nsec;
iGotOffset=2;
}
spend_my_spare_time(&tsTime, &tsOffset);
if (iKeepTs) {
if (fputs(szTime, stdout)==EOF) {
error_exit(errno,"stdout write error #m3: %s\n",
strerror(errno));
}
}
switch (read_and_write_a_line(fp)) {
case 1: /* expected LF */
break;
case -1: /* expected EOF */
goto CLOSE_THISFILE;
case -2: /* file access error */
warning("%s: File access error, "
"skip it\n", pszFilename);
iRet = 1;
goto CLOSE_THISFILE;
break;
default: /* bug of system error */
error_exit(1,"Unexpected error at %d\n",
__LINE__);
break;
}
break;
case 0: /* unexpected LF */
warning("%s: %s: Invalid timestamp field found, "
"skip this line.\n", pszFilename, szTime);
iRet = 1;
break;
case -2: /* unexpected EOF */
warning("%s: Came to EOF suddenly\n", pszFilename);
iRet = 1;
case -1: /* expected EOF */
goto CLOSE_THISFILE;
break;
case -3: /* file access error */
warning("%s: File access error, skip it\n",
pszFilename);
iRet = 1;
goto CLOSE_THISFILE;
default: /* bug or system error */
error_exit(1,"Unexpected error at %d\n", __LINE__);
}
}
break;
case 4: /* "-cZ" Calendar time with immediate outgoing mode */
if (iGotOffset==0) {
get_time_data_arrived(iFd, >sZero);
iGotOffset=1;
}
while (1) {
switch (read_1st_field_as_a_timestamp(fp, szTime)) {
case 1: /* read successfully */
if (! parse_calendartime(szTime, &tsTime)) {
warning("%s: %s: Invalid calendar-time, "
"skip this line\n", pszFilename, szTime);
iRet = 1;
switch (skip_over_a_line(fp)) {
case 1: /* expected LF */
break;
case -1: /* expected EOF */
goto CLOSE_THISFILE;
case -2: /* file access error */
warning("%s: File access error, "
"skip it\n", pszFilename);
goto CLOSE_THISFILE;
break;
default: /* bug of system error */
error_exit(1,"Unexpected error at %d\n",
__LINE__);
break;
}
break;
}
if (iGotOffset==1) {
/* tsOffset = gtsZero - tsTime */
if ((gtsZero.tv_nsec - tsTime.tv_nsec) < 0) {
tsOffset.tv_sec = gtsZero.tv_sec
- tsTime.tv_sec - 1;
tsOffset.tv_nsec = gtsZero.tv_nsec
- tsTime.tv_nsec + 1000000000;
} else {
tsOffset.tv_sec = gtsZero.tv_sec -tsTime.tv_sec ;
tsOffset.tv_nsec = gtsZero.tv_nsec-tsTime.tv_nsec;
}
iGotOffset=2;
}
spend_my_spare_time(&tsTime, &tsOffset);
if (iKeepTs) {
if (fputs(szTime, stdout)==EOF) {
error_exit(errno,"stdout write error #m4: %s\n",
strerror(errno));
}
}
switch (read_and_write_a_line(fp)) {
case 1: /* expected LF */
break;
case -1: /* expected EOF */
goto CLOSE_THISFILE;
case -2: /* file access error */
warning("%s: File access error, "
"skip it\n", pszFilename);
iRet = 1;
goto CLOSE_THISFILE;
break;
default: /* bug of system error */
error_exit(1,"Unexpected error at %d\n",
__LINE__);
break;
}
break;
case 0: /* unexpected LF */
warning("%s: %s: Invalid timestamp field found, "
"skip this line.\n", pszFilename, szTime);
iRet = 1;
break;
case -2: /* unexpected EOF */
warning("%s: Came to EOF suddenly\n", pszFilename);
iRet = 1;
case -1: /* expected EOF */
goto CLOSE_THISFILE;
break;
case -3: /* file access error */
warning("%s: File access error, skip it\n",
pszFilename);
goto CLOSE_THISFILE;
default: /* bug or system error */
error_exit(1,"Unexpected error at %d\n", __LINE__);
}
}
break;
case 5: /* "-eZ" UNIX epoch time with immediate outgoing mode */
case 6: /* "-zZ" Zero time with immediate outgoing mode */
if (iGotOffset==0) {
get_time_data_arrived(iFd, >sZero);
iGotOffset=1;
}
while (1) {
switch (read_1st_field_as_a_timestamp(fp, szTime)) {
case 1: /* read successfully */
if (! parse_unixtime(szTime, &tsTime)) {
warning("%s: %s: Invalid timestamp, "
"skip this line\n", pszFilename, szTime);
iRet = 1;
switch (skip_over_a_line(fp)) {
case 1: /* expected LF */
break;
case -1: /* expected EOF */
goto CLOSE_THISFILE;
case -2: /* file access error */
warning("%s: File access error, "
"skip it\n", pszFilename);
goto CLOSE_THISFILE;
break;
default: /* bug of system error */
error_exit(1,"Unexpected error at %d\n",
__LINE__);
break;
}
break;
}
if (iGotOffset==1) {
/* tsOffset = gtsZero - tsTime */
if ((gtsZero.tv_nsec - tsTime.tv_nsec) < 0) {
tsOffset.tv_sec = gtsZero.tv_sec
- tsTime.tv_sec - 1;
tsOffset.tv_nsec = gtsZero.tv_nsec
- tsTime.tv_nsec + 1000000000;
} else {
tsOffset.tv_sec = gtsZero.tv_sec -tsTime.tv_sec ;
tsOffset.tv_nsec = gtsZero.tv_nsec-tsTime.tv_nsec;
}
iGotOffset=2;
}
spend_my_spare_time(&tsTime, &tsOffset);
if (iKeepTs) {
if (fputs(szTime, stdout)==EOF) {
error_exit(errno,"stdout write error #m5: %s\n",
strerror(errno));
}
}
switch (read_and_write_a_line(fp)) {
case 1: /* expected LF */
break;
case -1: /* expected EOF */
goto CLOSE_THISFILE;
case -2: /* file access error */
warning("%s: File access error, "
"skip it\n", pszFilename);
iRet = 1;
goto CLOSE_THISFILE;
break;
default: /* bug of system error */
error_exit(1,"Unexpected error at %d\n",
__LINE__);
break;
}
break;
case 0: /* unexpected LF */
warning("%s: %s: Invalid timestamp field found, "
"skip this file.\n", pszFilename, szTime);
iRet = 1;
break;
case -2: /* unexpected EOF */
warning("%s: Came to EOF suddenly\n", pszFilename);
iRet = 1;
case -1: /* expected EOF */
goto CLOSE_THISFILE;
break;
case -3: /* file access error */
warning("%s: File access error, skip it\n",
pszFilename);
iRet = 1;
goto CLOSE_THISFILE;
default: /* bug or system error */
error_exit(1,"Unexpected error at %d\n", __LINE__);
}
}
break;
default:
error_exit(255,"main() #L1: Invalid mode number\n");
}
CLOSE_THISFILE:
/*--- Close the input file ---------------------------------------*/
if (fp != stdin) {fclose(fp);}
/*--- End loop ---------------------------------------------------*/
if (pszPath == NULL) {break;}
iFileno++;
}
/*=== Finish normally ==============================================*/
return(iRet);}
/*####################################################################
# Functions
####################################################################*/
/*=== Read and write only one line having a timestamp ================
* [in] iFd : File descriptor number for read
* ptsTime : Pointer to return the time when data arrived
* [ret] (none) : This function alway calls error_exit() if any error
occured. */
void get_time_data_arrived(int iFd, struct timespec *ptsTime) {
/*--- Variables --------------------------------------------------*/
fd_set fdsRead;
/*--- Wait for data arriving -------------------------------------*/
FD_ZERO(&fdsRead);
FD_SET(iFd, &fdsRead);
if (select(iFd+1, &fdsRead, NULL, NULL, NULL) == -1) {
error_exit(errno,"select() in get_time_data_arrived(): %s\n",
strerror(errno));
}
/*--- Set the time -----------------------------------------------*/
if (clock_gettime(CLOCK_REALTIME,ptsTime) != 0) {
error_exit(errno,"clock_gettime() in get_time_data_arrived(): %s\n",
strerror(errno));
}
}
/*=== Read and write only one line having a timestamp ================
* [in] fp : Filehandle for read
* pszTime : Pointer for the string buffer to get the timestamp on
* the 1st field with a white space (if it exists)
* (Size of the buffer you give MUST BE 34 BYTES or more!)
* [ret] == 0 : Finished reading due to '\n'
* == 1 : Finished reading successfully, you may use the result in
* the buffer
* ==-1 : Finished reading because no more data in the "fp"
* ==-2 : Finished reading due to the end of file
* ==-3 : Finished reading due to a file reading error
* other: Finished reading due to a system error */
int read_1st_field_as_a_timestamp(FILE *fp, char *pszTime) {
/*--- Variables --------------------------------------------------*/
int iTslen = 0; /* length of the timestamp string */
int iChar;
/*--- Reading and writing a line ---------------------------------*/
while (1) {
iChar = getc(fp);
switch (iChar) {
case ' ' :
case '\t':
pszTime[iTslen ]=iChar;
pszTime[iTslen+1]= 0;
return 1;
case EOF :
if (feof( fp)) {
if (iTslen==0) {
return -1;
} else {
if (giVerbose>0) {
warning("EOF came while reading 1st field\n");
}
return -2;
}
} else if (ferror(fp)) {
if (giVerbose>0) {
warning("error while reading 1st field\n");
}
return -3;
} else {
return -4;
}
case '\n':
return 0;
default :
if (iTslen>32) { continue;}
else {pszTime[iTslen]=iChar; iTslen++; continue;}
}
}
}
/*=== Read and write only one line ===================================
* [in] fp : Filehandle for read
* [ret] == 1 : Finished reading/writing due to '\n', which is the last
* char of the file
* ==-1 : Finished reading due to the end of file
* ==-2 : Finished reading due to a file reading error
* ==-3 : Finished reading due to a system error */
int read_and_write_a_line(FILE *fp) {
/*--- Variables --------------------------------------------------*/
int iChar;
/*--- Reading and writing a line (normal mode) -------------------*/
if (! giTypingmode) {
while (1) {
iChar = getc(fp);
switch (iChar) {
case EOF :
if (feof( fp)) {return -1;}
if (ferror(fp)) {return -2;}
else {return -3;}
case '\n':
if (putchar('\n' )==EOF) {
error_exit(errno,"stdout write error #f1: %s\n",
strerror(errno));
}
return 1;
default :
if (putchar(iChar)==EOF) {
error_exit(errno,"stdout write error #f2: %s\n",
strerror(errno));
}
break;
}
}
return -3;
}
/*--- Reading and writing a line (typing mode) -------------------*/
iChar = getc(fp);
switch (iChar) {
case EOF :
if (feof( fp)) {return -1;}
if (ferror(fp)) {return -2;}
else {return -3;}
case '\n':
if (putchar('\n' )==EOF) {
error_exit(errno,"stdout write error #f3: %s\n",
strerror(errno));
}
return 1;
default :
if (putchar(iChar)==EOF) {
error_exit(errno,"stdout write error #f4: %s\n",
strerror(errno));
}
break;
}
while (1) {
iChar = getc(fp);
switch (iChar) {
case EOF :
if (feof( fp)) {return -1;}
if (ferror(fp)) {return -2;}
else {return -3;}
case '\n':
return 1;
default :
if (putchar(iChar)==EOF) {
error_exit(errno,"stdout write error #f5: %s\n",
strerror(errno));
}
break;
}
}
}
/*=== Read and throw away one line ===================================
* [in] fp : Filehandle for read
* [ret] == 1 : Finished reading/writing due to '\n', which is the last
* char of the file
* ==-1 : Finished reading due to the end of file
* ==-2 : Finished reading due to a file reading error
* ==-3 : Finished reading due to a system error */
int skip_over_a_line(FILE *fp) {
/*--- Variables --------------------------------------------------*/
int iChar;
/*--- Reading and writing a line ---------------------------------*/
while (1) {
iChar = getc(fp);
switch (iChar) {
case EOF :
if (feof( fp)) {return -1;}
if (ferror(fp)) {return -2;}
else {return -3;}
case '\n':
return 1;
default :
break;
}
}
}
/*=== Parse a local calendar time ====================================
* [in] pszTime : calendar-time string in the localtime
* (/[0-9]{11,20}(\.[0-9]{1,9})?/)
* ptsTime : To be set the parsed time ("timespec" structure)
* [ret] > 0 : success
* ==0 : error (failure to parse) */
int parse_calendartime(char* pszTime, struct timespec *ptsTime) {
/*--- Variables --------------------------------------------------*/
char szDate[21], szNsec[10], szDate2[26];
int i, j, k; /* +-- 0:(reading integer part) */
char c; /* +-- 1:finish reading without_decimals */
int iStatus = 0; /* <--------- 2:to_be_started reading decimals */
struct tm tmDate;
/*--- Separate pszTime into date and nanoseconds -----------------*/
for (i=0; i<20; i++) {
c = pszTime[i];
if ('0'<=c && c<='9' ) {szDate[i]=c; }
else if (c=='.' ) {szDate[i]=0; iStatus=2; i++; break;}
else if (c==0 || c=='\t' || c==' ') {szDate[i]=0; iStatus=1; break;}
else {if (giVerbose>0) {
warning("%c: Unexpected chr. in "
"the integer part\n",c);
}
return 0;
}
}
if ((iStatus==0) && (i==20)) {
switch (pszTime[20]) {
case '.': szDate[20]=0; iStatus=2; i++; break;
case 0 : szDate[20]=0; iStatus=1; break;
default : warning("The integer part of the timestamp is too big "
"as a calendar-time\n",c);
return 0;
}
}
switch (iStatus) {
case 1 : strcpy(szNsec,"000000000");
break;
case 2 : j=i+9;
k=0;
for (; i<j; i++) {
c = pszTime[i];
if ('0'<=c && c<='9' ) {szNsec[k]=c; k++;}
else if (c==0 || c=='\t' || c==' ') {break; }
else {
if (giVerbose>0) {
warning("%c: Unexpected chr. in the decimal part\n",c);
}
return 0;
}
}
for (; k<9; k++) {szNsec[k]='0';}
szNsec[9]=0;
break;
default: warning("Unexpected error in parse_calendartime(), "
"maybe a bug?\n");
return 0;
}
/*--- Pack the time-string into the timespec structure -----------*/
i = strlen(szDate)-10;
if (i<=0) {return 0;}
k =0; for (j=0; j<i; j++) {szDate2[k]=szDate[j];k++;} /* Y */
szDate2[k]='-'; k++;
i+=2; for ( ; j<i; j++) {szDate2[k]=szDate[j];k++;} /* m */
szDate2[k]='-'; k++;
i+=2; for ( ; j<i; j++) {szDate2[k]=szDate[j];k++;} /* d */
szDate2[k]='T'; k++;
i+=2; for ( ; j<i; j++) {szDate2[k]=szDate[j];k++;} /* H */
szDate2[k]=':'; k++;
i+=2; for ( ; j<i; j++) {szDate2[k]=szDate[j];k++;} /* M */
szDate2[k]=':'; k++;
i+=2; for ( ; j<i; j++) {szDate2[k]=szDate[j];k++;} /* S */