home *** CD-ROM | disk | FTP | other *** search
- /* coffdate
- *
- * displays the date embedded in COFF files and optionally alters the
- * files' access and modify dates to be that of their COFF f_timdat.
- *
- * usage: coffdate [ -a ] [ -m ] [ - { c | s } ] filelist
- *
- * where: -a resets file's access date&time to the COFF f_timdat
- * -m resets file's modify date&time to the COFF f_timdat
- * -c list only COFF files (i.e. suppress "not a COFF file")
- * -s silent operation
- *
- * options may be combined noting that -c and -s are mutually exclusive.
- *
- * This version is for mc68k COFF files; to function with other systems'
- * COFF files the equates in chkCOFF() need to be changed to reflect those
- * in /usr/include/sgs.h or /usr/include/filehdr.h on those other systems.
- *
- * V1.0 30-Dec-1990, Thad Floryan (thad@cup.portal.com)
- */
-
- #include <fcntl.h> /* for low-level I/O definitions */
- #include <filehdr.h> /* for COFF header first part */
- #include <sgs.h> /* for COFF magic */
- #include <stdio.h> /* for standard I/O defs, BUFSIZ, etc. */
- #include <time.h> /* for ctime(3C) definitions */
- #include <sys/stat.h>
- #include <sys/types.h>
-
- struct utimbuf { /* structure for utime(2); not in any *.h file */
- time_t actime; /* access time */
- time_t modtime;/* modify time */
- };
-
- #define CROFFSET 24 /* offset of '\n' in ctime(3C) string */
- #define CTIMELEN 26 /* length of ctime(3C) string */
-
- static char *version = "@(#) coffdate V1.0, Thad Floryan, 30-Dec-1990";
-
- static char *usage_text[] = {
- "usage: coffdate [ -a ] [ -m ] [ - { c | s } ] filelist\n",
- "where: -a resets file's access date&time to the COFF f_timdat",
- " -m resets file's modify date&time to the COFF f_timdat",
- " -c list only COFF files (i.e. suppress \"not a COFF file\")",
- " -s silent operation",
- 0
- };
-
- static int OPTaccess = 0; /* option switch for access dates */
- static int OPTmodify = 0; /* option switch for modify dates */
- static int OPTcoff = 0; /* option switch for COFF-only */
- static int OPTsilent = 0; /* option switch for silent operation */
-
- main( argc, argv )
- int argc;
- char *argv[];
- {
- extern char *malloc();
- extern int close(), free(), getopt(), open(), read(), stat(), utime();
-
- extern int optind;
-
- long chkCOFF();
- void dpyCOFF(), usage();
-
- long COFFdate; /* -1 if not COFF, else COFF's f_timdat */
- void *bufptr; /* file data input buffer */
- char errstr[80]; /* holds expanded error string */
- int fd; /* file descriptor */
- int ndx; /* general loop index and subscript */
- int numread; /* number of bytes read from file */
- int optchr; /* option char from getopt(3C) */
- struct stat statbuf; /* buffer for stat(2) results */
- struct utimbuf ambuf; /* buffer for utime(2) input */
-
-
- while ( ( optchr = getopt( argc, argv, "amcs?hH" ) ) != EOF )
- {
- switch (optchr)
- {
- case 'a': ++OPTaccess;
- break;
- case 'm': ++OPTmodify;
- break;
- case 'c': if ( OPTsilent )
- usage();
- else
- ++OPTcoff;
- break;
- case 's': if ( OPTcoff )
- usage();
- else
- ++OPTsilent;
- break;
- case '?':
- case 'h':
- case 'H':
- default: usage();
- }
- }
-
- if ( optind == argc )
- {
- usage(); /* does exit(1) and never returns */
- }
-
- bufptr = malloc( BUFSIZ );
-
- for ( ndx = optind; ndx < argc; ndx++ )
- {
- if ( ( fd = open( argv[ndx], O_RDONLY, 0 ) ) < 0 )
- {
- sprintf( errstr, "?%s: cannot open %s", argv[0], argv[ndx] );
- perror( errstr );
- }
- else
- {
- COFFdate = -1L;
-
- if ( (numread = read( fd, bufptr, BUFSIZ ) ) < 0 )
- {
- sprintf( errstr, "?%s: error reading %s",
- argv[0], argv[ndx] );
- perror( errstr );
- }
- else
- {
- COFFdate = chkCOFF( bufptr, numread );
- dpyCOFF( COFFdate, argv[ndx] );
- }
- close( fd );
-
- if ( COFFdate >= 0 && ( OPTaccess || OPTmodify ) )
- {
- if ( stat ( argv[ndx], &statbuf ) < 0 )
- {
- sprintf( errstr,"?%s: stat() failure on %s",
- argv[0], argv[ndx] );
- perror( errstr );
- }
- else
- {
- ambuf.actime = (OPTaccess) ? (time_t) COFFdate :
- statbuf.st_atime;
- ambuf.modtime = (OPTmodify) ? (time_t) COFFdate :
- statbuf.st_mtime;
- if ( utime( argv[ndx], &ambuf ) < 0 )
- {
- sprintf( errstr,"?%s: utime() failure for %s",
- argv[0], argv[ndx] );
- perror( errstr );
- }
- }
- }
- }
- }
- free( bufptr );
- }
-
- long chkCOFF( bufptr, bufsize )
- FILHDR *bufptr;
- int bufsize;
- {
- int magic;
-
- if ( bufsize > FILHSZ )
- {
- magic = bufptr->f_magic;
-
- if (magic == AOUT1MAGIC ||
- magic == AOUT2MAGIC ||
- magic == AOUT3MAGIC ||
- magic == MC68KWRMAGIC ||
- magic == MC68KROMAGIC ||
- magic == MC68KPGMAGIC )
- {
- return bufptr->f_timdat;
- }
- else
- {
- return -1L;
- }
- }
- else
- {
- return -1L;
- }
- }
-
- void dpyCOFF( ldate, filename )
- long ldate;
- char *filename;
- {
- char timebuf[CTIMELEN];
-
- if ( OPTsilent ) return;
-
- if ( ldate >= 0L )
- {
- strcpy( timebuf, ctime( &ldate ) );
- timebuf[CROFFSET] = '\0';
- fprintf( stdout, "%s %s\n", timebuf, filename );
- }
- else if ( OPTcoff == 0 )
- {
- fprintf( stdout, "not a COFF file %s\n", filename );
- }
- }
-
- void usage()
- {
- int ndx = 0;
-
- while ( usage_text[ndx] != 0 )
- {
- fprintf( stderr, "%s\n", usage_text[ndx++] );
- }
- fprintf( stderr, "%s\n", version );
- exit( 1 );
- }
-