4848#include <string.h>
4949#include <time.h>
5050#include <unistd.h>
51+ #include <stdbool.h>
5152
5253#include <linux/can.h>
5354#include <linux/can/raw.h>
@@ -89,11 +90,11 @@ const int canfx_on = 1;
8990
9091extern int optind , opterr , optopt ;
9192
92- typedef struct {
93+ struct sleep {
9394 struct timeval * sleep_vector ;
9495 size_t idx ;
9596 size_t size ;
96- } sleep_struct ;
97+ };
9798
9899static void print_usage (char * prg )
99100{
@@ -260,28 +261,36 @@ static int add_assignment(char *mode, int socket, char *txname,
260261 return 0 ;
261262}
262263
263- void load_gaps_from_file (FILE * fp , sleep_struct * a )
264+ void load_gaps_from_file (FILE * fp , struct sleep * timestamps )
264265{
265266 char * line = NULL ;
266267 size_t len = 0 ;
267268 ssize_t read ;
268269
269270 while ((read = getline (& line , & len , fp )) != -1 ) {
270- if (a -> idx == a -> size ) {
271- a -> size ++ ;
272- a -> sleep_vector = realloc (a -> sleep_vector , a -> size * sizeof (struct timespec ));
271+ if (timestamps -> idx == timestamps -> size ) {
272+ timestamps -> size ++ ;
273+ timestamps -> sleep_vector = realloc (timestamps -> sleep_vector , timestamps -> size * sizeof (struct timeval ));
274+ if (!timestamps -> sleep_vector ) {
275+ fprintf (stderr , "Failed to create the timestamps vector!\n" );
276+ exit (1 );
277+ }
273278 }
274- sscanf (line , "(%lu.%lu)" , & a -> sleep_vector [a -> idx ].tv_sec , & a -> sleep_vector [a -> idx ].tv_usec );
275- a -> idx ++ ;
279+ sscanf (line , "(%lu.%lu)" , & timestamps -> sleep_vector [timestamps -> idx ].tv_sec , & timestamps -> sleep_vector [timestamps -> idx ].tv_usec );
280+ timestamps -> idx ++ ;
276281 }
277282 free (line );
278283}
279284
280- void init_sleep_struct ( sleep_struct * a , size_t init_size )
285+ void init_timestamps ( struct sleep * timestamps , size_t init_size )
281286{
282- a -> size = init_size ;
283- a -> sleep_vector = malloc (init_size * sizeof (struct timespec ));
284- a -> idx = 0 ;
287+ timestamps -> size = init_size ;
288+ timestamps -> sleep_vector = calloc (init_size , sizeof (* (timestamps -> sleep_vector )));
289+ if (!timestamps -> sleep_vector ) {
290+ fprintf (stderr , "Failed to create the timestamps vector!\n" );
291+ exit (1 );
292+ }
293+ timestamps -> idx = 0 ;
285294}
286295
287296int main (int argc , char * * argv )
@@ -310,8 +319,8 @@ int main(int argc, char **argv)
310319 int eof , txmtu , i , j ;
311320 char * fret ;
312321 unsigned long long sec , usec ;
313- int gap_from_file = 0 ;
314- sleep_struct time_arr ;
322+ bool gap_from_file = false ;
323+ struct sleep timestamps ;
315324 struct timeval send_time , act_time , init_time ;
316325
317326 while ((opt = getopt (argc , argv , "I:l:tin:g:s:xvr?" )) != -1 ) {
@@ -370,14 +379,14 @@ int main(int argc, char **argv)
370379 break ;
371380
372381 case 'r' :
373- if (infile == stdin ) {
382+ if (! isatty ( fileno ( infile )) ) {
374383 fprintf (stderr , "Specify an input file for option -r !\n" );
375384 exit (EXIT_FAILURE );
376385 }
377- gap_from_file = 1 ; /* using time delta from file */
378- init_sleep_struct ( & time_arr , 1 );
379- load_gaps_from_file (infile , & time_arr );
380- time_arr .idx = 0 ;
386+ gap_from_file = true ; /* using time delta from file */
387+ init_timestamps ( & timestamps , 1 );
388+ load_gaps_from_file (infile , & timestamps );
389+ timestamps .idx = 0 ;
381390 break ;
382391
383392 case '?' :
@@ -555,8 +564,8 @@ int main(int argc, char **argv)
555564 addr .can_family = AF_CAN ;
556565 addr .can_ifindex = txidx ; /* send via this interface */
557566
558- if (gap_from_file && time_arr .idx > 0 ) {
559- send_time = time_arr .sleep_vector [time_arr .idx ];
567+ if (gap_from_file && timestamps .idx > 0 ) {
568+ send_time = timestamps .sleep_vector [timestamps .idx ];
560569 gettimeofday (& act_time , NULL );
561570 timersub (& act_time , & init_time , & act_time );
562571
@@ -565,12 +574,12 @@ int main(int argc, char **argv)
565574 timersub (& act_time , & init_time , & act_time );
566575 }
567576 }
568- if (gap_from_file && time_arr .idx == 0 ) {
577+ if (gap_from_file && timestamps .idx == 0 ) {
569578 gettimeofday (& init_time , NULL );
570579 gettimeofday (& act_time , NULL );
571580 timersub (& act_time , & init_time , & act_time );
572581 }
573- time_arr .idx ++ ;
582+ timestamps .idx ++ ;
574583
575584 if (sendto (s , & cu , txmtu , 0 , (struct sockaddr * )& addr , sizeof (addr )) != txmtu ) {
576585 perror ("sendto" );
0 commit comments