]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - candump.c
fflush the new configurable stdout output to allow proper pipe/netcat setups.
[sojka/can-utils.git] / candump.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * candump.c
7  *
8  * Copyright (c) 2002-2009 Volkswagen Group Electronic Research
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of Volkswagen nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * Alternatively, provided that this notice is retained in full, this
24  * software may be distributed under the terms of the GNU General
25  * Public License ("GPL") version 2, in which case the provisions of the
26  * GPL apply INSTEAD OF those given above.
27  *
28  * The provided data structures and external interfaces from this code
29  * are not restricted to be used by modules with a GPL compatible license.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
42  * DAMAGE.
43  *
44  * Send feedback to <socketcan-users@lists.berlios.de>
45  *
46  */
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <string.h>
52 #include <signal.h>
53 #include <ctype.h>
54 #include <libgen.h>
55 #include <time.h>
56
57 #include <sys/time.h>
58 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <sys/ioctl.h>
61 #include <sys/uio.h>
62 #include <net/if.h>
63
64 #include <linux/can.h>
65 #include <linux/can/raw.h>
66
67 #include "terminal.h"
68 #include "lib.h"
69
70 #define MAXSOCK 16    /* max. number of CAN interfaces given on the cmdline */
71 #define MAXIFNAMES 30 /* size of receive name index to omit ioctls */
72 #define MAXCOL 6      /* number of different colors for colorized output */
73 #define ANYDEV "any"  /* name of interface to receive from any CAN interface */
74 #define ANL "\r\n"    /* newline in ASC mode */
75
76 #define SILENT_INI 42 /* detect user setting on commandline */
77 #define SILENT_OFF 0  /* no silent mode */
78 #define SILENT_ANI 1  /* silent mode with animation */
79 #define SILENT_ON  2  /* silent mode (completely silent) */
80
81 #define BOLD    ATTBOLD
82 #define RED     ATTBOLD FGRED
83 #define GREEN   ATTBOLD FGGREEN
84 #define YELLOW  ATTBOLD FGYELLOW
85 #define BLUE    ATTBOLD FGBLUE
86 #define MAGENTA ATTBOLD FGMAGENTA
87 #define CYAN    ATTBOLD FGCYAN
88
89 const char col_on [MAXCOL][19] = {BLUE, RED, GREEN, BOLD, MAGENTA, CYAN};
90 const char col_off [] = ATTRESET;
91
92 static char devname[MAXIFNAMES][IFNAMSIZ+1];
93 static int  dindex[MAXIFNAMES];
94 static int  max_devname_len; /* to prevent frazzled device name output */ 
95
96 #define MAXANI 4
97 const char anichar[MAXANI] = {'|', '/', '-', '\\'};
98
99 extern int optind, opterr, optopt;
100
101 static volatile int running = 1;
102
103 void print_usage(char *prg)
104 {
105         fprintf(stderr, "\nUsage: %s [options] <CAN interface>+\n", prg);
106         fprintf(stderr, "  (use CTRL-C to terminate %s)\n\n", prg);
107         fprintf(stderr, "Options: -t <type>   (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
108         fprintf(stderr, "         -c          (increment color mode level)\n");
109         fprintf(stderr, "         -i          (binary output - may exceed 80 chars/line)\n");
110         fprintf(stderr, "         -a          (enable additional ASCII output)\n");
111         fprintf(stderr, "         -S          (swap byte order in printed CAN data[] - marked with '%c' )\n", SWAP_DELIMITER);
112         fprintf(stderr, "         -s <level>  (silent mode - %d: off (default) %d: animation %d: silent)\n", SILENT_OFF, SILENT_ANI, SILENT_ON);
113         fprintf(stderr, "         -b <can>    (bridge mode - send received frames to <can>)\n");
114         fprintf(stderr, "         -B <can>    (bridge mode - like '-b' with disabled loopback)\n");
115         fprintf(stderr, "         -l          (log CAN-frames into file. Sets '-s %d' by default)\n", SILENT_ON);
116         fprintf(stderr, "         -L          (use log file format on stdout)\n");
117         fprintf(stderr, "         -n <count>  (terminate after receiption of <count> CAN frames)\n");
118         fprintf(stderr, "         -r <size>   (set socket receive buffer to <size>)\n");
119         fprintf(stderr, "\n");
120         fprintf(stderr, "Up to %d CAN interfaces with optional filter sets can be specified\n", MAXSOCK);
121         fprintf(stderr, "on the commandline in the form: <ifname>[,filter]*\n");
122         fprintf(stderr, "\nComma separated filters can be specified for each given CAN interface:\n");
123         fprintf(stderr, " <can_id>:<can_mask> (matches when <received_can_id> & mask == can_id & mask)\n");
124         fprintf(stderr, " <can_id>~<can_mask> (matches when <received_can_id> & mask != can_id & mask)\n");
125         fprintf(stderr, " #<error_mask>       (set error frame filter, see include/linux/can/error.h)\n");
126         fprintf(stderr, "\nCAN IDs, masks and data content are given and expected in hexadecimal values.\n");
127         fprintf(stderr, "When can_id and can_mask are both 8 digits, they are assumed to be 29 bit EFF.\n");
128         fprintf(stderr, "Without any given filter all data frames are received ('0:0' default filter).\n");
129         fprintf(stderr, "\nUse interface name '%s' to receive from all CAN interfaces.\n", ANYDEV);
130         fprintf(stderr, "\nExamples:\n");
131         fprintf(stderr, "%s -c -c -ta can0,123:7FF,400:700,#000000FF can2,400~7F0 can3 can8\n", prg);
132         fprintf(stderr, "%s -l any,0~0,#FFFFFFFF    (log only error frames but no(!) data frames)\n", prg);
133         fprintf(stderr, "%s -l any,0:0,#FFFFFFFF    (log error frames and also all data frames)\n", prg);
134         fprintf(stderr, "%s vcan2,92345678:DFFFFFFF (match only for extended CAN ID 12345678)\n", prg);
135         fprintf(stderr, "%s vcan2,123:7FF (matches CAN ID 123 - including EFF and RTR frames)\n", prg);
136         fprintf(stderr, "%s vcan2,123:C00007FF (matches CAN ID 123 - only SFF and non-RTR frames)\n", prg);
137         fprintf(stderr, "\n");
138 }
139
140 void sigterm(int signo)
141 {
142         running = 0;
143 }
144
145 int idx2dindex(int ifidx, int socket) {
146
147         int i;
148         struct ifreq ifr;
149
150         for (i=0; i < MAXIFNAMES; i++) {
151                 if (dindex[i] == ifidx)
152                         return i;
153         }
154
155         /* create new interface index cache entry */
156
157         /* remove index cache zombies first */
158         for (i=0; i < MAXIFNAMES; i++) {
159                 if (dindex[i]) {
160                         ifr.ifr_ifindex = dindex[i];
161                         if (ioctl(socket, SIOCGIFNAME, &ifr) < 0)
162                                 dindex[i] = 0;
163                 }
164         }
165
166         for (i=0; i < MAXIFNAMES; i++)
167                 if (!dindex[i]) /* free entry */
168                         break;
169
170         if (i == MAXIFNAMES) {
171                 fprintf(stderr, "Interface index cache only supports %d interfaces.\n",
172                        MAXIFNAMES);
173                 exit(1);
174         }
175
176         dindex[i] = ifidx;
177
178         ifr.ifr_ifindex = ifidx;
179         if (ioctl(socket, SIOCGIFNAME, &ifr) < 0)
180                 perror("SIOCGIFNAME");
181
182         if (max_devname_len < strlen(ifr.ifr_name))
183                 max_devname_len = strlen(ifr.ifr_name);
184
185         strcpy(devname[i], ifr.ifr_name);
186
187 #ifdef DEBUG
188         printf("new index %d (%s)\n", i, devname[i]);
189 #endif
190
191         return i;
192 }
193
194 int main(int argc, char **argv)
195 {
196         fd_set rdfs;
197         int s[MAXSOCK];
198         int bridge = 0;
199         unsigned char timestamp = 0;
200         unsigned char silent = SILENT_INI;
201         unsigned char silentani = 0;
202         unsigned char color = 0;
203         unsigned char view = 0;
204         unsigned char log = 0;
205         unsigned char logfrmt = 0;
206         int count = 0;
207         int rcvbuf_size = 0;
208         int opt, ret;
209         int currmax, numfilter;
210         char *ptr, *nptr;
211         struct sockaddr_can addr;
212         struct can_filter *rfilter;
213         can_err_mask_t err_mask;
214         struct can_frame frame;
215         int nbytes, i;
216         struct ifreq ifr;
217         struct timeval tv, last_tv;
218         FILE *logfile = NULL;
219
220         signal(SIGTERM, sigterm);
221         signal(SIGHUP, sigterm);
222         signal(SIGINT, sigterm);
223
224         last_tv.tv_sec  = 0;
225         last_tv.tv_usec = 0;
226
227         while ((opt = getopt(argc, argv, "t:ciaSs:b:B:lLn:r:h?")) != -1) {
228                 switch (opt) {
229                 case 't':
230                         timestamp = optarg[0];
231                         if ((timestamp != 'a') && (timestamp != 'A') &&
232                             (timestamp != 'd') && (timestamp != 'z')) {
233                                 fprintf(stderr, "%s: unknown timestamp mode '%c' - ignored\n",
234                                        basename(argv[0]), optarg[0]);
235                                 timestamp = 0;
236                         }
237                         break;
238
239                 case 'c':
240                         color++;
241                         break;
242
243                 case 'i':
244                         view |= CANLIB_VIEW_BINARY;
245                         break;
246
247                 case 'a':
248                         view |= CANLIB_VIEW_ASCII;
249                         break;
250
251                 case 'S':
252                         view |= CANLIB_VIEW_SWAP;
253                         break;
254
255                 case 's':
256                         silent = atoi(optarg);
257                         if (silent > SILENT_ON) {
258                                 print_usage(basename(argv[0]));
259                                 exit(1);
260                         }
261                         break;
262
263                 case 'b':
264                 case 'B':
265                         if (strlen(optarg) >= IFNAMSIZ) {
266                                 fprintf(stderr, "Name of CAN device '%s' is too long!\n\n", optarg);
267                                 return 1;
268                         } else {
269                                 bridge = socket(PF_CAN, SOCK_RAW, CAN_RAW);
270                                 if (bridge < 0) {
271                                         perror("bridge socket");
272                                         return 1;
273                                 }
274                                 addr.can_family = AF_CAN;
275                                 strcpy(ifr.ifr_name, optarg);
276                                 if (ioctl(bridge, SIOCGIFINDEX, &ifr) < 0)
277                                         perror("SIOCGIFINDEX");
278                                 addr.can_ifindex = ifr.ifr_ifindex;
279                 
280                                 if (!addr.can_ifindex) {
281                                         perror("invalid bridge interface");
282                                         return 1;
283                                 }
284
285                                 /* disable default receive filter on this write-only RAW socket */
286                                 setsockopt(bridge, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
287
288                                 if (opt == 'B') {
289                                         int loopback = 0;
290
291                                         setsockopt(bridge, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
292                                                    &loopback, sizeof(loopback));
293                                 }
294
295                                 if (bind(bridge, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
296                                         perror("bridge bind");
297                                         return 1;
298                                 }
299                         }
300                         break;
301             
302                 case 'l':
303                         log = 1;
304                         break;
305
306                 case 'L':
307                         logfrmt = 1;
308                         break;
309
310                 case 'n':
311                         count = atoi(optarg);
312                         if (count < 1) {
313                                 print_usage(basename(argv[0]));
314                                 exit(1);
315                         }
316                         break;
317
318                 case 'r':
319                         rcvbuf_size = atoi(optarg);
320                         if (rcvbuf_size < 1) {
321                                 print_usage(basename(argv[0]));
322                                 exit(1);
323                         }
324                         break;
325
326                 default:
327                         print_usage(basename(argv[0]));
328                         exit(1);
329                         break;
330                 }
331         }
332
333         if (optind == argc) {
334                 print_usage(basename(argv[0]));
335                 exit(0);
336         }
337         
338         if (logfrmt && view) {
339                 fprintf(stderr, "Log file format selected: Please disable ASCII/BINARY/SWAP options!\n");
340                 exit(0);
341         }
342
343         if (silent == SILENT_INI) {
344                 if (log) {
345                         fprintf(stderr, "\nDisabled standard output while logging.");
346                         silent = SILENT_ON; /* disable output on stdout */
347                 } else
348                         silent = SILENT_OFF; /* default output */
349         }
350
351         currmax = argc - optind; /* find real number of CAN devices */
352
353         if (currmax > MAXSOCK) {
354                 fprintf(stderr, "More than %d CAN devices given on commandline!\n", MAXSOCK);
355                 return 1;
356         }
357
358         for (i=0; i < currmax; i++) {
359
360                 ptr = argv[optind+i];
361                 nptr = strchr(ptr, ',');
362
363 #ifdef DEBUG
364                 printf("open %d '%s'.\n", i, ptr);
365 #endif
366
367                 s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW);
368                 if (s[i] < 0) {
369                         perror("socket");
370                         return 1;
371                 }
372
373                 if (nptr)
374                         nbytes = nptr - ptr;  /* interface name is up the first ',' */
375                 else
376                         nbytes = strlen(ptr); /* no ',' found => no filter definitions */
377
378                 if (nbytes >= IFNAMSIZ) {
379                         fprintf(stderr, "name of CAN device '%s' is too long!\n", ptr);
380                         return 1;
381                 }
382
383                 if (nbytes > max_devname_len)
384                         max_devname_len = nbytes; /* for nice printing */
385
386                 addr.can_family = AF_CAN;
387
388                 memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
389                 strncpy(ifr.ifr_name, ptr, nbytes);
390
391 #ifdef DEBUG
392                 printf("using interface name '%s'.\n", ifr.ifr_name);
393 #endif
394
395                 if (strcmp(ANYDEV, ifr.ifr_name)) {
396                         if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
397                                 perror("SIOCGIFINDEX");
398                                 exit(1);
399                         }
400                         addr.can_ifindex = ifr.ifr_ifindex;
401                 } else
402                         addr.can_ifindex = 0; /* any can interface */
403
404                 if (nptr) {
405
406                         /* found a ',' after the interface name => check for filters */
407
408                         /* determine number of filters to alloc the filter space */
409                         numfilter = 0;
410                         ptr = nptr;
411                         while (ptr) {
412                                 numfilter++;
413                                 ptr++; /* hop behind the ',' */
414                                 ptr = strchr(ptr, ','); /* exit condition */
415                         }
416
417                         rfilter = malloc(sizeof(struct can_filter) * numfilter);
418                         if (!rfilter) {
419                                 fprintf(stderr, "Failed to create filter space!\n");
420                                 return 1;
421                         }
422
423                         numfilter = 0;
424                         err_mask = 0;
425
426                         while (nptr) {
427
428                                 ptr = nptr+1; /* hop behind the ',' */
429                                 nptr = strchr(ptr, ','); /* update exit condition */
430
431                                 if (sscanf(ptr, "%lx:%lx",
432                                            (long unsigned int *)
433                                            &rfilter[numfilter].can_id, 
434                                            (long unsigned int *)
435                                            &rfilter[numfilter].can_mask) == 2) {
436                                         rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
437                                         numfilter++;
438                                 } else if (sscanf(ptr, "%lx~%lx",
439                                                   (long unsigned int *)
440                                                   &rfilter[numfilter].can_id, 
441                                                   (long unsigned int *)
442                                                   &rfilter[numfilter].can_mask) == 2) {
443                                         rfilter[numfilter].can_id |= CAN_INV_FILTER;
444                                         rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
445                                         numfilter++;
446                                 } else if (sscanf(ptr, "#%lx",
447                                                   (long unsigned int *)&err_mask) != 1) { 
448                                         fprintf(stderr, "Error in filter option parsing: '%s'\n", ptr);
449                                         exit(1);
450                                 }
451                         }
452
453                         if (err_mask)
454                                 setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
455                                            &err_mask, sizeof(err_mask));
456
457                         if (numfilter)
458                                 setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FILTER,
459                                            rfilter, numfilter * sizeof(struct can_filter));
460
461                         free(rfilter);
462
463                 } /* if (nptr) */
464
465                 if (rcvbuf_size) {
466
467                         int curr_rcvbuf_size;
468                         socklen_t curr_rcvbuf_size_len = sizeof(curr_rcvbuf_size);
469
470                         if (setsockopt(s[i], SOL_SOCKET, SO_RCVBUF,
471                                        &rcvbuf_size, sizeof(rcvbuf_size)) < 0) {
472                                 perror("setsockopt SO_RCVBUF");
473                                 exit(1);
474                         }
475
476                         if (getsockopt(s[i], SOL_SOCKET, SO_RCVBUF,
477                                        &curr_rcvbuf_size, &curr_rcvbuf_size_len) < 0) {
478                                 perror("getsockopt SO_RCVBUF");
479                                 exit(1);
480                         }
481
482                         /* Only print a warning the first time we detect the adjustment */
483                         /* n.b.: The wanted size is doubled in Linux in net/sore/sock.c */
484                         if (!i && curr_rcvbuf_size < rcvbuf_size*2)
485                                 fprintf(stderr, "The socket receive buffer size was "
486                                         "adjusted due to /proc/sys/net/core/rmem_max.\n");
487                 }
488
489                 if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
490                         perror("bind");
491                         return 1;
492                 }
493         }
494
495         if (log) {
496                 time_t currtime;
497                 struct tm now;
498                 char fname[sizeof("candump-2006-11-20_202026.log")+1];
499
500                 if (time(&currtime) == (time_t)-1) {
501                         perror("time");
502                         return 1;
503                 }
504
505                 localtime_r(&currtime, &now);
506
507                 sprintf(fname, "candump-%04d-%02d-%02d_%02d%02d%02d.log",
508                         now.tm_year + 1900,
509                         now.tm_mon + 1,
510                         now.tm_mday,
511                         now.tm_hour,
512                         now.tm_min,
513                         now.tm_sec);
514
515                 if (silent != SILENT_ON)
516                         printf("\nWarning: console output active while logging!");
517
518                 fprintf(stderr, "\nEnabling Logfile '%s'\n\n", fname);
519
520                 logfile = fopen(fname, "w");
521                 if (!logfile) {
522                         perror("logfile");
523                         return 1;
524                 }
525         }
526
527         while (running) {
528
529                 FD_ZERO(&rdfs);
530                 for (i=0; i<currmax; i++)
531                         FD_SET(s[i], &rdfs);
532
533                 if ((ret = select(s[currmax-1]+1, &rdfs, NULL, NULL, NULL)) < 0) {
534                         //perror("select");
535                         running = 0;
536                         continue;
537                 }
538
539                 for (i=0; i<currmax; i++) {  /* check all CAN RAW sockets */
540
541                         if (FD_ISSET(s[i], &rdfs)) {
542
543                                 socklen_t len = sizeof(addr);
544                                 int idx;
545
546                                 nbytes = recvfrom(s[i], &frame, sizeof(struct can_frame), 0,
547                                                   (struct sockaddr*)&addr, &len);
548                                 if (nbytes < 0) {
549                                         perror("read");
550                                         return 1;
551                                 }
552
553                                 if (nbytes < sizeof(struct can_frame)) {
554                                         fprintf(stderr, "read: incomplete CAN frame\n");
555                                         return 1;
556                                 }
557
558                                 if (count && (--count == 0))
559                                         running = 0;
560
561                                 if (bridge) {
562                                         nbytes = write(bridge, &frame, sizeof(struct can_frame));
563                                         if (nbytes < 0) {
564                                                 perror("bridge write");
565                                                 return 1;
566                                         } else if (nbytes < sizeof(struct can_frame)) {
567                                                 fprintf(stderr,"bridge write: incomplete CAN frame\n");
568                                                 return 1;
569                                         }
570                                 }
571                     
572                                 if (timestamp || log || logfrmt)
573                                         if (ioctl(s[i], SIOCGSTAMP, &tv) < 0)
574                                                 perror("SIOCGSTAMP");
575
576
577                                 idx = idx2dindex(addr.can_ifindex, s[i]);
578
579                                 if (log) {
580                                         /* log CAN frame with absolute timestamp & device */
581                                         fprintf(logfile, "(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
582                                         fprintf(logfile, "%*s ", max_devname_len, devname[idx]);
583                                         /* without seperator as logfile use-case is parsing */
584                                         fprint_canframe(logfile, &frame, "\n", 0);
585                                 }
586
587                                 if (logfrmt) {
588                                         /* print CAN frame in log file style to stdout */
589                                         printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
590                                         printf("%*s ", max_devname_len, devname[idx]);
591                                         fprint_canframe(stdout, &frame, "\n", 0);
592                                         goto out_fflush; /* no other output to stdout */
593                                 }
594
595                                 if (silent != SILENT_OFF){
596                                         if (silent == SILENT_ANI) {
597                                                 printf("%c\b", anichar[silentani%=MAXANI]);
598                                                 silentani++;
599                                         }
600                                         goto out_fflush; /* no other output to stdout */
601                                 }
602                       
603                                 printf(" %s", (color>2)?col_on[idx%MAXCOL]:"");
604
605                                 switch (timestamp) {
606
607                                 case 'a': /* absolute with timestamp */
608                                         printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
609                                         break;
610
611                                 case 'A': /* absolute with date */
612                                 {
613                                         struct tm tm;
614                                         char timestring[25];
615
616                                         tm = *localtime(&tv.tv_sec);
617                                         strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
618                                         printf("(%s.%06ld) ", timestring, tv.tv_usec);
619                                 }
620                                 break;
621
622                                 case 'd': /* delta */
623                                 case 'z': /* starting with zero */
624                                 {
625                                         struct timeval diff;
626
627                                         if (last_tv.tv_sec == 0)   /* first init */
628                                                 last_tv = tv;
629                                         diff.tv_sec  = tv.tv_sec  - last_tv.tv_sec;
630                                         diff.tv_usec = tv.tv_usec - last_tv.tv_usec;
631                                         if (diff.tv_usec < 0)
632                                                 diff.tv_sec--, diff.tv_usec += 1000000;
633                                         if (diff.tv_sec < 0)
634                                                 diff.tv_sec = diff.tv_usec = 0;
635                                         printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
636                                 
637                                         if (timestamp == 'd')
638                                                 last_tv = tv; /* update for delta calculation */
639                                 }
640                                 break;
641
642                                 default: /* no timestamp output */
643                                         break;
644                                 }
645
646                                 printf(" %s", (color && (color<3))?col_on[idx%MAXCOL]:"");
647                                 printf("%*s", max_devname_len, devname[idx]);
648                                 printf("%s  ", (color==1)?col_off:"");
649
650                                 fprint_long_canframe(stdout, &frame, NULL, view);
651
652                                 printf("%s", (color>1)?col_off:"");
653                                 printf("\n");
654                         }
655
656                 out_fflush:
657                         fflush(stdout);
658                 }
659         }
660
661         for (i=0; i<currmax; i++)
662                 close(s[i]);
663
664         if (bridge)
665                 close(bridge);
666
667         if (log)
668                 fclose(logfile);
669
670         return 0;
671 }