Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

cf.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1991, 1994, 1995, 1996, 1998, 1999, 2001, 2004
00003  *      The Regents of the University of California.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that: (1) source code distributions
00007  * retain the above copyright notice and this paragraph in its entirety, (2)
00008  * distributions including binary code include the above copyright notice and
00009  * this paragraph in its entirety in the documentation or other materials
00010  * provided with the distribution, and (3) all advertising materials mentioning
00011  * features or use of this software display the following acknowledgement:
00012  * ``This product includes software developed by the University of California,
00013  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
00014  * the University nor the names of its contributors may be used to endorse
00015  * or promote products derived from this software without specific prior
00016  * written permission.
00017  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
00018  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00020  */
00021 
00022 #ifndef lint
00023 static const char copyright[] =
00024     "@(#) Copyright (c) 1991, 1994, 1995, 1996, 1998, 1999, 2001, 2004\n\
00025 The Regents of the University of California.  All rights reserved.\n";
00026 static const char rcsid[] =
00027     "@(#) $Id: cf.c,v 1.1 2004/12/09 04:25:29 jason Exp $ (LBL)";
00028 #endif
00029 
00030 #include <sys/types.h>
00031 
00032 #include <ctype.h>
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <time.h>
00037 #include <unistd.h>
00038 
00039 static char *argv0;
00040 
00041 extern char *optarg;
00042 extern int optind, opterr;
00043 
00044 int preserve = 0;
00045 int strict = 0;
00046 int utc = 0;
00047 char *fmt = "%b %e %H:%M:%S";
00048 char *lfmt = "%b %e %H:%M:%S %Y";
00049 
00050 /* Forwards */
00051 int main(int, char **);
00052 void doone(FILE *, FILE *);
00053 volatile void usage(void);
00054 
00055 int
00056 main(argc, argv)
00057         int argc;
00058         char **argv;
00059 {
00060         register char *cp;
00061         register int status, didany, op;
00062         FILE *f;
00063         int targc;
00064         char **targv;
00065 
00066         if ((cp = strrchr(argv[0], '/')) != NULL)
00067                 argv0 = cp + 1;
00068         else
00069                 argv0 = argv[0];
00070 
00071         opterr = 0;
00072         while ((op = getopt(argc, argv, "f:lpsu")) != EOF)
00073                 switch (op) {
00074 
00075                 case 'f':
00076                         fmt = optarg;
00077                         break;
00078 
00079                 case 'l':
00080                         fmt = lfmt;
00081                         break;
00082 
00083                 case 'p':
00084                         ++preserve;
00085                         break;
00086 
00087                 case 's':
00088                         ++strict;
00089                         break;
00090 
00091                 case 'u':
00092                         ++utc;
00093                         break;
00094 
00095                 default:
00096                         usage();
00097                         /* NOTREACHED */
00098                 }
00099         targc = argc - optind;
00100         targv = &argv[optind];
00101 
00102         status = 0;
00103         didany = 0;
00104         while (targc > 0) {
00105                 f = fopen(*targv, "r");
00106                 if (f) {
00107                         doone(f, stdout);
00108                         (void) fclose(f);
00109                 } else {
00110                         (void) fprintf(stderr, "%s: fopen: ", argv0);
00111                         perror(*targv);
00112                         status |= 1;
00113                 }
00114                 --targc;
00115                 ++targv;
00116                 ++didany;
00117         }
00118         if (!didany)
00119                 doone(stdin, stdout);
00120         exit(status);
00121 }
00122 
00123 void
00124 doone(fin, fout)
00125         FILE *fin, *fout;
00126 {
00127         time_t t;
00128         register char *bp, *dotbp;
00129         register struct tm *tp;
00130         register int dot_count;
00131         char buf[1024], ts[128];
00132 
00133         while (fgets(buf, sizeof(buf), fin)) {
00134                 bp = buf;
00135                 dotbp = NULL;
00136                 if (isdigit(*bp)) {
00137                         t = atol(bp);
00138                         ++bp;
00139                         dot_count = 0;
00140                         while (isdigit(*bp) || *bp == '.') {
00141                                 if (*bp == '.') {
00142                                         dotbp = bp;
00143                                         ++dot_count;
00144                                 }
00145                                 ++bp;
00146                         }
00147                         if (strict && (bp - buf < 9 || dot_count > 1 ||
00148                             (bp - buf > 10 && dot_count != 1))) {
00149                                 /* Doesn't look like a genuine timestamp -
00150                                  * skip it.
00151                                  */
00152                                 fputs(buf, fout);
00153                                 continue;
00154                         }
00155                         if (!utc)
00156                                 tp = localtime(&t);
00157                         else
00158                                 tp = gmtime(&t);
00159                         (void)strftime(ts, sizeof(ts), fmt, tp);
00160                         fputs(ts, fout);
00161                         if (preserve && dotbp != NULL)
00162                                 bp = dotbp;
00163                 }
00164                 fputs(bp, fout);
00165         }
00166 }
00167 
00168 volatile void
00169 usage()
00170 {
00171         extern char version[];
00172 
00173         (void)fprintf(stderr, "%s version %s\n", argv0, version);
00174         (void)fprintf(stderr, "usage: %s [-f fmt] [-lpsu] [file ...]\n", argv0);
00175         exit(1);
00176 }

Generated on Wed Sep 14 02:55:59 2005 for bro_docs by doxygen 1.3.5