home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume36 / chiaro / part05 < prev    next >
Text File  |  1993-03-25  |  56KB  |  1,658 lines

  1. Newsgroups: comp.sources.misc
  2. From: jwbirdsa@picarefy.picarefy.com (James W. Birdsall)
  3. Subject: v36i075:  chiaro - Image Utilities, Part05/18
  4. Message-ID: <1993Mar25.181037.20162@sparky.imd.sterling.com>
  5. X-Md4-Signature: 96fc77e15ddb98dc61a691b8ad54cb79
  6. Date: Thu, 25 Mar 1993 18:10:37 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: jwbirdsa@picarefy.picarefy.com (James W. Birdsall)
  10. Posting-number: Volume 36, Issue 75
  11. Archive-name: chiaro/part05
  12. Environment: UNIX, Sun, DECstation, 3B1
  13.  
  14. #! /bin/sh
  15. # This is a shell archive.  Remove anything before this line, then feed it
  16. # into a shell via "sh file" or similar.  To overwrite existing files,
  17. # type "sh file -c".
  18. # Contents:  src/chils.c src/sr.c
  19. # Wrapped by kent@sparky on Thu Mar 25 11:20:03 1993
  20. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
  21. echo If this archive is complete, you will see the following message:
  22. echo '          "shar: End of archive 5 (of 18)."'
  23. if test -f 'src/chils.c' -a "${1}" != "-c" ; then 
  24.   echo shar: Will not clobber existing file \"'src/chils.c'\"
  25. else
  26.   echo shar: Extracting \"'src/chils.c'\" \(41782 characters\)
  27.   sed "s/^X//" >'src/chils.c' <<'END_OF_FILE'
  28. X/***************************************************************************
  29. X*   CHILS.C                                                                *
  30. X*   MODULE:  -                                                             *
  31. X*   OS:      UNIX                                                          *
  32. X*                                                                          *
  33. X*   Copyright (c) 1993 James W. Birdsall. All Rights Reserved.             *
  34. X*                                                                          *
  35. X*   The Graphics Interchange Format(c) is the Copyright property of        *
  36. X*   CompuServe Incorporated. GIF(sm) is a Service Mark property of         *
  37. X*   CompuServe Incorporated.                                               *
  38. X*                                                                          *
  39. X*   $Id: chils.c,v 1.18 1993/03/18 23:01:27 jwbirdsa Exp $
  40. X*                                                                          *
  41. X*   Chiaro suite LS program.                                               *
  42. X*                                                                          *
  43. X***************************************************************************/
  44. X
  45. X#include "config.h"
  46. X
  47. X/*
  48. X** system includes <>
  49. X*/
  50. X
  51. X#include <stdio.h>
  52. X#include <ctype.h>
  53. X#ifndef NO_STDLIB
  54. X#include <stdlib.h>
  55. X#endif
  56. X#ifndef NO_MEMOP
  57. X#include <memory.h>
  58. X#endif
  59. X#ifndef NO_MALLOCHDR
  60. X#include <malloc.h>
  61. X#endif
  62. X#ifndef NO_STR_INC
  63. X#ifdef STRING_PLURAL
  64. X#include <strings.h>
  65. X#else
  66. X#include <string.h>
  67. X#endif
  68. X#endif
  69. X
  70. X
  71. X/*
  72. X** custom includes ""
  73. X*/
  74. X
  75. X#include "depend.h"
  76. X
  77. X#include "chils.h"
  78. X
  79. X#include "fb.h"
  80. X
  81. X#include "formats.h"
  82. X#include "gif.h"
  83. X#include "img.h"
  84. X#include "jfif.h"
  85. X#include "sr.h"
  86. X#include "tga.h"
  87. X#include "pnm.h"
  88. X#include "xbm.h"
  89. X#include "bmp.h"
  90. X#include "pcx.h"
  91. X
  92. X#include "comsrch.h"
  93. X
  94. X#include "patchlevel.h"
  95. X
  96. X
  97. X/*
  98. X** local #defines
  99. X*/
  100. X
  101. X#define CFG_NAME    "chils.cfg"   /* CHILS configuration file name. */
  102. X#define ENVVAR_NAME "CHIHOME"     /* CHILS environment variable name. */
  103. X
  104. X
  105. X/*
  106. X** misc: copyright strings, version macros, etc.
  107. X*/
  108. X
  109. Xstatic char CONST copyright[] = "Copyright (c) 1993 James W. Birdsall. All Rights Reserved";
  110. Xstatic char CONST rcsid[] = "$Id: chils.c,v 1.18 1993/03/18 23:01:27 jwbirdsa Exp $";
  111. X
  112. X#define VERS        "1.0"
  113. X
  114. X
  115. X/*
  116. X** typedefs
  117. X*/
  118. X
  119. X/*
  120. X** global variables
  121. X*/
  122. X
  123. Xint flags = NAMETRUNC;            /* Global function flags. */
  124. Xflist *formatlist = NULL;         /* Check for specific formats. */
  125. Xchar *searchstr = NULL;           /* Complex search string. */
  126. Xchar scratch[80];                 /* Global scratchpad. */
  127. X
  128. X
  129. X/*
  130. X** function prototypes
  131. X*/
  132. X
  133. X#ifdef  __STDC__
  134. X# define P_(s) s
  135. X#else
  136. X# define P_(s) ()
  137. X#endif
  138. X
  139. Xstatic VOID examine P_((char *filename));
  140. Xstatic VOID short_examine P_((char *filename));
  141. Xstatic VOID long_examine P_((char *filename));
  142. X
  143. Xstatic VOID usage();
  144. Xstatic VOID cleanup P_((int exitcode));
  145. Xstatic char *errxlate P_((ULONG errcode));
  146. X
  147. X#undef P_
  148. X
  149. X
  150. X/*
  151. X** functions
  152. X*/
  153. X
  154. X
  155. X/***************************************************************************
  156. X *   FUNCTION: main                                                        *
  157. X ***************************************************************************/
  158. X#ifdef __STDC__
  159. Xmain(int argc, char *argv[])
  160. X#else
  161. Xmain(argc, argv)
  162. Xint argc;
  163. Xchar *argv[];
  164. X#endif
  165. X{
  166. X    int loop, loop2;
  167. X    char **namelist;
  168. X    int onlyflags = 1;
  169. X    flist *ftemp;
  170. X    ULONG status;
  171. X
  172. X    /*
  173. X    ** Searches the entire command line looking for options and sets
  174. X    ** global flag variables appropriately.
  175. X    */
  176. X
  177. X    for (loop = 1; loop < argc; loop++)
  178. X    {
  179. X        if ('-' == argv[loop][0])
  180. X        {
  181. X            switch (argv[loop][1])
  182. X            {
  183. X                /* Verbose option. */
  184. X                case 'v':
  185. X                    flags |= VERBOSE;
  186. X                    break;
  187. X
  188. X                /* Front-end option. */
  189. X                case 'f':
  190. X                    flags |= FRONTEND;
  191. X                    break;
  192. X
  193. X                /* Full name option. */
  194. X                case 'n':
  195. X                    flags ^= NAMETRUNC;
  196. X                    break;
  197. X
  198. X                /* Width option. */
  199. X                case 'w':
  200. X                    if (isdigit(argv[loop][2]) == 0)
  201. X                    {
  202. X                        /* Auto-set to 80. */
  203. X
  204. X                        setcols(80);
  205. X                    }
  206. X                    else
  207. X                    {
  208. X                        /* Set to number. */
  209. X
  210. X                        int cols = 0;
  211. X
  212. X                        for (loop2 = 2; isdigit(argv[loop][loop2]); loop2++)
  213. X                        {
  214. X                            cols *= 10;
  215. X                            cols += (argv[loop][loop2] - '0');
  216. X                        }
  217. X                        setcols(cols);
  218. X                    }
  219. X                    break;
  220. X
  221. X                /* Format search option. */
  222. X                case 't':
  223. X                    /* If complex search specified, ignore this option. */
  224. X
  225. X                    if (searchstr)
  226. X                    {
  227. X                        break;
  228. X                    }
  229. X
  230. X                    /* Search through table of formats for specified type. */
  231. X
  232. X                    for (loop2=0; formatsearch[loop2].name[0] != '\0'; loop2++)
  233. X                    {
  234. X                        /* Check against table of strings. */
  235. X
  236. X                        if (stricmp(formatsearch[loop2].name, (argv[loop] + 2)))
  237. X                        {
  238. X                            continue;
  239. X                        }
  240. X
  241. X                        /* String matched in order to get here, so break. */
  242. X
  243. X                        break;
  244. X                    }
  245. X
  246. X                    /* Was a match found? */
  247. X
  248. X                    if ('\0' == formatsearch[loop2].name[0])
  249. X                    {
  250. X                        fprintf(stderr, "\nERROR: Bad format type string %s\n",
  251. X                                (argv[loop] + 2));
  252. X                        cleanup(3);
  253. X                    }
  254. X
  255. X                    /* Allocate a format list structure. */
  256. X
  257. X                    if ((ftemp = (flist *) malloc(sizeof(flist))) ==
  258. X                        (flist *) NULL)
  259. X                    {
  260. X                        fprintf(stderr, "\nERROR: Out of memory.\n");
  261. X                        cleanup(3);
  262. X                    }
  263. X
  264. X                    /* Initialize. */
  265. X
  266. X                    memcpy(ftemp, &(formatsearch[loop2].value), sizeof(flist));
  267. X
  268. X                    /* Put in format list. */
  269. X
  270. X                    ftemp->next = formatlist;
  271. X                    formatlist = ftemp;
  272. X                    break;
  273. X
  274. X                /* Complex search option. */
  275. X                case 's':
  276. X                    /* Save search string. */
  277. X
  278. X                    searchstr = &(argv[loop][2]);
  279. X
  280. X                    /* Check syntax. */
  281. X
  282. X                    if (0 == comcheck(searchstr))
  283. X                    {
  284. X                        usage();
  285. X                    }
  286. X
  287. X                    /* Free any format searches present. */
  288. X
  289. X                    while (formatlist != (flist *) NULL)
  290. X                    {
  291. X                        ftemp = formatlist->next;
  292. X                        free(formatlist);
  293. X                        formatlist = ftemp;
  294. X                    }
  295. X                    break;
  296. X
  297. X                /* Help option. */
  298. X                case 'h':
  299. X                /* Unrecognized option -- print out help message. */
  300. X                default:
  301. X                    usage();
  302. X                    break;
  303. X            }
  304. X        }
  305. X        else
  306. X        {
  307. X            /*
  308. X            ** onlyflags == 1 when the command line contains only options,
  309. X            ** and therefore the current directory should be scanned. If
  310. X            ** this clause is executed, means there is at least one arg
  311. X            ** which is not an option and therefore is a target.
  312. X            */
  313. X
  314. X            onlyflags = 0;
  315. X        }
  316. X    }
  317. X
  318. X    /* If using as front end, ignore VERBOSE flag. */
  319. X
  320. X    if (flags & FRONTEND)
  321. X    {
  322. X        flags ^= VERBOSE;
  323. X    }
  324. X
  325. X    /* Initialize format recognizer. */
  326. X
  327. X    if ((status = re_init(argv[0], CFG_NAME, ENVVAR_NAME)) != 0)
  328. X    {
  329. X        fprintf(stderr, "%s\n", errxlate(status));
  330. X        if ((SEVERITY(status) == SEVERITY(ST_ERROR)) ||
  331. X            (SEVERITY(status) == SEVERITY(ST_FATAL)))
  332. X        {
  333. X            cleanup(3);
  334. X        }
  335. X    }
  336. X
  337. X    /* Initialize FB module. */
  338. X
  339. X    if (fb_init(2, FB_MINBUFFER) != 0)
  340. X    {
  341. X        fprintf(stderr, "%s\n", errxlate(fb_error));
  342. X        cleanup(3);
  343. X    }
  344. X
  345. X    /* Retrieve number of screen columns. */
  346. X
  347. X    getcols();
  348. X
  349. X    /* If the command line contains only options, process the current dir. */
  350. X
  351. X    if (onlyflags)
  352. X    {
  353. X        /* Get filenames. */
  354. X
  355. X        if ((namelist = scand(".")) == NULL)
  356. X        {
  357. X            fprintf(stderr, "\nERROR: Error reading current directory.\n");
  358. X            cleanup(3);
  359. X        }
  360. X
  361. X        /* Are there any files? */
  362. X
  363. X        if (NULL == namelist[0])
  364. X        {
  365. X            fprintf(stderr, "\nNo files found in current directory.\n");
  366. X        }
  367. X        else
  368. X        {
  369. X            /* Process each file. */
  370. X
  371. X            for (loop = 0; namelist[loop]; loop++)
  372. X            {
  373. X                examine(namelist[loop]);
  374. X                free(namelist[loop]);
  375. X            }
  376. X        }
  377. X
  378. X        /* Cleanup. */
  379. X
  380. X        free(namelist);
  381. X    }
  382. X    else
  383. X    {
  384. X        /* Process the command line arguments that aren't options. */
  385. X
  386. X        for (loop = 1; loop < argc; loop++)
  387. X        {
  388. X            /* Only process if not an option. */
  389. X
  390. X            if (argv[loop][0] != '-')
  391. X            {
  392. X                /* Get filenames. */
  393. X
  394. X                if ((namelist = scand(argv[loop])) == NULL)
  395. X                {
  396. X                    fprintf(stderr, "\nERROR: Bad target %s\n", argv[loop]);
  397. X                    continue;
  398. X                }
  399. X                if (NULL == namelist[0])
  400. X                {
  401. X                    fprintf(stderr, "\nNo files matching %s found.\n",
  402. X                            argv[loop]);
  403. X                }
  404. X                else
  405. X                {
  406. X                    /* Process each one. */
  407. X
  408. X                    for (loop2 = 0; namelist[loop2]; loop2++)
  409. X                    {
  410. X                        examine(namelist[loop2]);
  411. X                        free(namelist[loop2]);
  412. X                    }
  413. X                }
  414. X
  415. X                /* Cleanup. */
  416. X
  417. X                free(namelist);
  418. X            }
  419. X        }
  420. X    }
  421. X
  422. X    /* Done. */
  423. X
  424. X    cleanup(0);
  425. X} /* end of main() */
  426. X
  427. X
  428. X/***************************************************************************
  429. X*   FUNCTION: examine  STATIC                                              *
  430. X*                                                                          *
  431. X*   DESCRIPTION:                                                           *
  432. X*                                                                          *
  433. X*       Examines a given file to determine if it is of a known bitmap      *
  434. X*       format.                                                            *
  435. X*                                                                          *
  436. X*       If it is not, it is ignored unless the VERBOSE flag is             *
  437. X*       set, in which case a message is printed.                           *
  438. X*                                                                          *
  439. X*       If it is of a known bitmap format and the FRONTEND flag is set,    *
  440. X*       only the filename is printed.                                      *
  441. X*                                                                          *
  442. X*       If it is of a known bitmap format and the FRONTEND flag is not     *
  443. X*       set, various basic information on the file is printed.             *
  444. X*                                                                          *
  445. X*   ENTRY:                                                                 *
  446. X*                                                                          *
  447. X*       filename - name of file to be examined                             *
  448. X*                                                                          *
  449. X*   EXIT:                                                                  *
  450. X*                                                                          *
  451. X*       Nothing.                                                           *
  452. X*                                                                          *
  453. X*   CONSTRAINTS/SIDE EFFECTS:                                              *
  454. X*                                                                          *
  455. X***************************************************************************/
  456. Xstatic VOID 
  457. X#ifdef __STDC__
  458. Xexamine(char *filename)
  459. X#else
  460. Xexamine(filename)
  461. Xchar *filename;
  462. X#endif
  463. X{
  464. X    /*
  465. X    ** If FRONTEND flag set and no complex search, all we need is the
  466. X    ** short form. Otherwise do the whole thing.
  467. X    */
  468. X
  469. X    if ((flags & FRONTEND) && (NULL == searchstr))
  470. X    {
  471. X        short_examine(filename);
  472. X    }
  473. X    else
  474. X    {
  475. X        long_examine(filename);
  476. X    }
  477. X
  478. X    return;
  479. X} /* end of static examine() */
  480. X
  481. X
  482. X/***************************************************************************
  483. X*   FUNCTION: short_examine  STATIC                                        *
  484. X*                                                                          *
  485. X*   DESCRIPTION:                                                           *
  486. X*                                                                          *
  487. X*       Called when the FRONTEND flag set and there is no complex search.  *
  488. X*       Examines a given file to determine if it is of a known bitmap      *
  489. X*       format. If it is not, it is ignored. If it is of a known format    *
  490. X*       the filename is printed.                                           *
  491. X*                                                                          *
  492. X*   ENTRY:                                                                 *
  493. X*                                                                          *
  494. X*       filename - name of file to be examined                             *
  495. X*                                                                          *
  496. X*   EXIT:                                                                  *
  497. X*                                                                          *
  498. X*       Nothing.                                                           *
  499. X*                                                                          *
  500. X*   CONSTRAINTS/SIDE EFFECTS:                                              *
  501. X*                                                                          *
  502. X***************************************************************************/
  503. Xstatic VOID 
  504. X#ifdef __STDC__
  505. Xshort_examine(char *filename)
  506. X#else
  507. Xshort_examine(filename)
  508. Xchar *filename;
  509. X#endif
  510. X{
  511. X    ULONG format;
  512. X    ULONG status;
  513. X    flist *ftemp;
  514. X
  515. X    /* Attempt to identify the file. */
  516. X
  517. X    if ((status = re_ident(filename, &format)) != 0)
  518. X    {
  519. X        if (status != FM_UNRECOGNIZED_W)
  520. X        {
  521. X            fprintf(stderr, "\n%s File %s\n", errxlate(status), filename);
  522. X            return;
  523. X        }
  524. X        return;
  525. X    }
  526. X
  527. X    /* If there is a format list, make sure file's format is on list. */
  528. X
  529. X    for (ftemp = formatlist; ftemp; ftemp = ftemp->next)
  530. X    {
  531. X        /* check for match */
  532. X        if ((format & ftemp->mask) == ftemp->value)
  533. X        {
  534. X             /* If match, break out. */
  535. X
  536. X            break;
  537. X        }
  538. X    }
  539. X
  540. X    /* If match, or no format list, print filename. */
  541. X
  542. X    if ((ftemp != (flist *) NULL) || (((flist *) NULL) == formatlist))
  543. X    {
  544. X        printf("%s\n", filename);
  545. X    }
  546. X
  547. X    return;
  548. X} /* end of static short_examine() */
  549. X
  550. X
  551. X/***************************************************************************
  552. X*   FUNCTION: long_examine  STATIC                                         *
  553. X*                                                                          *
  554. X*   DESCRIPTION:                                                           *
  555. X*                                                                          *
  556. X*       Examines a given file to determine if it is of a known bitmap      *
  557. X*       format. Called when FRONTEND flag is not set or it is set but      *
  558. X*       there is a complex search.                                         *
  559. X*                                                                          *
  560. X*       If it is not, it is ignored unless the VERBOSE flag is             *
  561. X*       set, in which case a message is printed.                           *
  562. X*                                                                          *
  563. X*       If it is of a known bitmap format and the FRONTEND flag is not     *
  564. X*       set, various basic information on the file is printed. If the      *
  565. X*       FRONTEND flag is set, only the filename is printed.                *
  566. X*                                                                          *
  567. X*   ENTRY:                                                                 *
  568. X*                                                                          *
  569. X*       filename - name of file to be examined                             *
  570. X*                                                                          *
  571. X*   EXIT:                                                                  *
  572. X*                                                                          *
  573. X*       Nothing.                                                           *
  574. X*                                                                          *
  575. X*   CONSTRAINTS/SIDE EFFECTS:                                              *
  576. X*                                                                          *
  577. X***************************************************************************/
  578. Xstatic VOID 
  579. X#ifdef __STDC__
  580. Xlong_examine(char *filename)
  581. X#else
  582. Xlong_examine(filename)
  583. Xchar *filename;
  584. X#endif
  585. X{
  586. X    FILE *infile;
  587. X    FB *infile2;
  588. X    ULONG format;
  589. X    ULONG status;
  590. X    flist *ftemp;
  591. X    ULONG height, width;
  592. X    ULONG colors;
  593. X    long filesize;
  594. X    int searchval;
  595. X
  596. X    GIF_LSD gifinfo;
  597. X    IMG_HDR imginfo;
  598. X    JFIF_HDR jfifinfo;
  599. X    SR_HDR srinfo;
  600. X    TGA_HDR tgainfo;
  601. X    PNM_HDR pnminfo;
  602. X    XBM_HDR xbminfo;
  603. X    BMP_HDR bmpinfo;
  604. X    PCX_HDR pcxinfo;
  605. X
  606. X    /* Is the file of a known format? */
  607. X
  608. X    if ((status = re_ident(filename, &format)) != 0)
  609. X    {
  610. X        if (status != FM_UNRECOGNIZED_W)
  611. X        {
  612. X            fprintf(stderr, "\n%s File %s\n", errxlate(status), filename);
  613. X            return;
  614. X        }
  615. X    }
  616. X    if (FORMAT_NOT == format)
  617. X    {
  618. X        /*
  619. X        ** Do nothing for files which are not recognized, unless the verbose
  620. X        ** flag is set.
  621. X        */
  622. X
  623. X        if (flags & VERBOSE)
  624. X        {
  625. X            fprintf(stderr,"\nERROR: file %s is of unknown format\n", filename);
  626. X        }
  627. X        return;
  628. X    }
  629. X
  630. X    /* Format recognized, so start doing things. Open file. */
  631. X
  632. X    if ((infile = fopen(filename, FOPEN_READ_BINARY)) == (FILE *) NULL)
  633. X    {
  634. X        sprintf(scratch, "\nERROR: Error opening file %s", filename);
  635. X        perror(scratch);
  636. X        return;
  637. X    }
  638. X
  639. X    /* Extract information from the file. */
  640. X
  641. X    switch (FORMAT_TYPE(format))
  642. X    {
  643. X        case FORMAT_TYPE(FORMAT_GIF):
  644. X            if ((status = gif_lsdget(infile, &gifinfo)) != 0)
  645. X            {
  646. X                fprintf(stderr, "\n%s File %s\n", errxlate(status), filename);
  647. X                fclose(infile);
  648. X                return;
  649. X            }
  650. X            break;
  651. X
  652. X        case FORMAT_TYPE(FORMAT_IMG):
  653. X            if ((status = img_getheader(infile, &imginfo)) != 0)
  654. X            {
  655. X                fprintf(stderr, "\n%s File %s\n", errxlate(status), filename);
  656. X                fclose(infile);
  657. X                return;
  658. X            }
  659. X            break;
  660. X
  661. X        case FORMAT_TYPE(FORMAT_SR):
  662. X            if ((status = sr_getheader(infile, &srinfo)) != 0)
  663. X            {
  664. X                fprintf(stderr, "\n%s File %s\n", errxlate(status), filename);
  665. X                fclose(infile);
  666. X                return;
  667. X            }
  668. X            break;
  669. X
  670. X        case FORMAT_TYPE(FORMAT_TGA):
  671. X            if ((status = tga_getheader(infile, &tgainfo)) != 0)
  672. X            {
  673. X                fprintf(stderr, "\n%s File %s\n", errxlate(status), filename);
  674. X                fclose(infile);
  675. X                return;
  676. X            }
  677. X            break;
  678. X
  679. X        case FORMAT_TYPE(FORMAT_PBM):
  680. X            if ((status = pbm_getheader(infile, (PBM_HDR *) &pnminfo)) != 0)
  681. X            {
  682. X                fprintf(stderr, "\n%s File %s\n", errxlate(status), filename);
  683. X                fclose(infile);
  684. X                return;
  685. X            }
  686. X            break;
  687. X
  688. X        case FORMAT_TYPE(FORMAT_PGM):
  689. X            if ((status = pgm_getheader(infile, (PBM_HDR *) &pnminfo)) != 0)
  690. X            {
  691. X                fprintf(stderr, "\n%s File %s\n", errxlate(status), filename);
  692. X                fclose(infile);
  693. X                return;
  694. X            }
  695. X            break;
  696. X
  697. X        case FORMAT_TYPE(FORMAT_PPM):
  698. X            if ((status = ppm_getheader(infile, (PBM_HDR *) &pnminfo)) != 0)
  699. X            {
  700. X                fprintf(stderr, "\n%s File %s\n", errxlate(status), filename);
  701. X                fclose(infile);
  702. X                return;
  703. X            }
  704. X            break;
  705. X
  706. X        case FORMAT_TYPE(FORMAT_XBM):
  707. X            if ((status = xbm_getheader(infile, &xbminfo)) != 0)
  708. X            {
  709. X                fprintf(stderr, "\n%s File %s\n", errxlate(status), filename);
  710. X                fclose(infile);
  711. X                return;
  712. X            }
  713. X            break;
  714. X
  715. X        case FORMAT_TYPE(FORMAT_BMP):
  716. X            if ((status = bmp_getheader(infile, &bmpinfo)) != 0)
  717. X            {
  718. X                fprintf(stderr, "\n%s File %s\n", errxlate(status), filename);
  719. X                fclose(infile);
  720. X                return;
  721. X            }
  722. X            break;
  723. X
  724. X        case FORMAT_TYPE(FORMAT_PCX):
  725. X            if ((status = pcx_getheader(infile, &pcxinfo)) != 0)
  726. X            {
  727. X                fprintf(stderr, "\n%s File %s\n", errxlate(status), filename);
  728. X                fclose(infile);
  729. X                return;
  730. X            }
  731. X            break;
  732. X
  733. X        case FORMAT_TYPE(FORMAT_JFIF):
  734. X            infile2 = fb_retrofit(infile, 'r');
  735. X            if (fb_error != 0)
  736. X            {
  737. X                fprintf(stderr, "\n%s File %s\n", errxlate(status), filename);
  738. X                fclose(infile);
  739. X                return;
  740. X            }
  741. X            if ((status = jfif_headerget(infile2, &jfifinfo)) != 0)
  742. X            {
  743. X                fprintf(stderr, "\n%s File %s\n", errxlate(status), filename);
  744. X                fb_close(infile2);
  745. X                return;
  746. X            }
  747. X    
  748. X            /*
  749. X            ** Close file and reopen so stuff lower down is OK. Note that
  750. X            ** closing of infile is handled by the fb_close() of infile2.
  751. X            */
  752. X    
  753. X            fb_close(infile2);
  754. X            if ((infile = fopen(filename, FOPEN_READ_BINARY)) == (FILE *) NULL)
  755. X            {
  756. X                sprintf(scratch, "\nERROR: Error opening file %s", filename);
  757. X                perror(scratch);
  758. X                return;
  759. X            }
  760. X            break;
  761. X
  762. X        default:
  763. X            /* Should never get here. */
  764. X
  765. X            fprintf(stderr, "\nInternal error, bad format ID.\n");
  766. X            fclose(infile);
  767. X            return;
  768. X            break;
  769. X    }
  770. X
  771. X    /* If there is a format list, make sure file's format is on list.  */
  772. X
  773. X    for (ftemp = formatlist; ftemp; ftemp = ftemp->next)
  774. X    {
  775. X        /* check for match */
  776. X        if ((format & ftemp->mask) == ftemp->value)
  777. X        {
  778. X            /* if match, break out */
  779. X            break;
  780. X        }
  781. X    }
  782. X
  783. X    /* If no match, return. */
  784. X
  785. X    if ((((flist *) NULL) == ftemp) && (formatlist != (flist *) NULL))
  786. X    {
  787. X        fclose(infile);
  788. X        return;
  789. X    }
  790. X
  791. X    /* Obtain size of file. */
  792. X
  793. X    if (fseek(infile, 0L, SEEK_END))
  794. X    {
  795. X        sprintf(scratch, "\nERROR: Error seeking in file %s", filename);
  796. X        perror(scratch);
  797. X        fclose(infile);
  798. X        return;
  799. X    }
  800. X    if ((filesize = ftell(infile)) == -1L)
  801. X    {
  802. X        sprintf(scratch, "\nERROR: Error determining size of %s", filename);
  803. X        perror(scratch);
  804. X        fclose(infile);
  805. X        return;
  806. X    }
  807. X
  808. X    /* If there is a complex search string, process it now.  */
  809. X
  810. X    if (searchstr != (char *) NULL)
  811. X    {
  812. X        /* extract necessary data */
  813. X
  814. X        switch FORMAT_TYPE(format)
  815. X        {
  816. X            case FORMAT_TYPE(FORMAT_GIF):
  817. X                height = gifinfo.scr_hi;
  818. X                width = gifinfo.scr_wid;
  819. X                colors = (0x1L << gifinfo.gct_size);
  820. X                break;
  821. X
  822. X            case FORMAT_TYPE(FORMAT_IMG):
  823. X                height = imginfo.imhi;
  824. X                width = imginfo.imwid;
  825. X                colors = (0x1L << imginfo.planes);
  826. X                break;
  827. X
  828. X            case FORMAT_TYPE(FORMAT_SR):
  829. X                height = srinfo.imhi;
  830. X                width = srinfo.imwid;
  831. X                colors = (0x1L << srinfo.pixelbits);
  832. X                break;
  833. X
  834. X            case FORMAT_TYPE(FORMAT_TGA):
  835. X                height = tgainfo.imhi;
  836. X                width = tgainfo.imwid;
  837. X                switch (tgainfo.type)
  838. X                {
  839. X                    /* No image data, rummage around looking for clues. */
  840. X                    case 0:
  841. X                        if (tgainfo.maptype != 0)
  842. X                        {
  843. X                            colors = tgainfo.maplen;
  844. X                        }
  845. X                        else if (tgainfo.pixelbits != 0)
  846. X                        {
  847. X                            colors = (0x1L << tgainfo.pixelbits);
  848. X                            /* Overflow on 32-bit color. */
  849. X                            colors = ((0 == colors) ? 0xFFFFFFFFL : colors);
  850. X                        }
  851. X                        else
  852. X                        {
  853. X                            colors = 0;
  854. X                        }
  855. X                        break;
  856. X                        
  857. X                    /* Color-mapped image. */
  858. X                    case 1:
  859. X                    case 9:
  860. X                    case 32:
  861. X                    case 33:
  862. X                        colors = tgainfo.maplen;
  863. X                        break;
  864. X
  865. X                    /* True-color image. */
  866. X                    case 2:
  867. X                    case 3:
  868. X                    case 10:
  869. X                    case 11:
  870. X                        colors = (0x1L << tgainfo.pixelbits);
  871. X                        /* Overflow on 32-bit color. */
  872. X                        colors = ((0 == colors) ? 0xFFFFFFFFL : colors);
  873. X                        break;
  874. X
  875. X                    default:
  876. X                        fprintf(stderr,
  877. X                                "\nERROR: internal error, bad Targa ID.\n");
  878. X                        fclose(infile);
  879. X                        return;
  880. X                }
  881. X                break;
  882. X
  883. X            case FORMAT_TYPE(FORMAT_JFIF):
  884. X                height = jfifinfo.height;
  885. X                width = jfifinfo.width;
  886. X                if ((jfifinfo.components * jfifinfo.bits) > 31)
  887. X                {
  888. X                    colors = 0xFFFFFFFFL;
  889. X                }
  890. X                else
  891. X                {
  892. X                    colors = (0x1L << (jfifinfo.components * jfifinfo.bits));
  893. X                }
  894. X                break;
  895. X
  896. X            case FORMAT_TYPE(FORMAT_PBM):
  897. X                height = pnminfo.imhi;
  898. X                width = pnminfo.imwid;
  899. X                colors = 2;
  900. X                break;
  901. X
  902. X            case FORMAT_TYPE(FORMAT_PGM):
  903. X                height = pnminfo.imhi;
  904. X                width = pnminfo.imwid;
  905. X                colors = pnminfo.maxpix + 1;
  906. X                break;
  907. X
  908. X            case FORMAT_TYPE(FORMAT_PPM):
  909. X                height = pnminfo.imhi;
  910. X                width = pnminfo.imwid;
  911. X                colors = pnminfo.maxpix + 1;
  912. X                colors = colors * colors * colors;
  913. X                break;
  914. X
  915. X            case FORMAT_TYPE(FORMAT_XBM):
  916. X                height = xbminfo.imhi;
  917. X                width = xbminfo.imwid;
  918. X                colors = 2;
  919. X                break;
  920. X
  921. X            case FORMAT_TYPE(FORMAT_BMP):
  922. X                height = bmpinfo.imhi;
  923. X                width = bmpinfo.imwid;
  924. X                colors = (0x1L << (bmpinfo.planes * bmpinfo.pixbits));
  925. X                if (((BMP_WIN3 == bmpinfo.version) || 
  926. X                     (BMP_OS2_20 == bmpinfo.version)) &&
  927. X                    (bmpinfo.more != NULL) &&
  928. X                    ((bmpinfo.planes * bmpinfo.pixbits) != 24))
  929. X                {
  930. X                    if ((BMP_WIN3 == bmpinfo.version) &&
  931. X                        (((BMP_H3 *) bmpinfo.more)->clrused != 0))
  932. X                    {
  933. X                        colors = ((BMP_H3 *) bmpinfo.more)->clrused;
  934. X                    }
  935. X                    else if ((BMP_OS2_20 == bmpinfo.version) &&
  936. X                             (((BMP_H20 *) bmpinfo.more)->more.clrused != 0))
  937. X                    {
  938. X                        colors = ((BMP_H20 *) bmpinfo.more)->more.clrused;
  939. X                    }
  940. X                }
  941. X                break;
  942. X
  943. X            case FORMAT_TYPE(FORMAT_PCX):
  944. X                height = pcxinfo.imhi;
  945. X                width = pcxinfo.imwid;
  946. X                colors = (0x1L << (pcxinfo.planes * pcxinfo.pixbits));
  947. X                break;
  948. X
  949. X            default:
  950. X                fprintf(stderr, "\nERROR: internal error, bad format ID.\n");
  951. X                fclose(infile);
  952. X                return;
  953. X        }
  954. X        searchval = comsearch(height, width, colors, format, (ULONG) filesize,
  955. X                              searchstr);
  956. X
  957. X        /* Check for error. */
  958. X
  959. X        if (com_errorflag != 0)
  960. X        {
  961. X            fprintf(stderr, "\nERROR: internal error during search parse.\n");
  962. X            fclose(infile);
  963. X            return;
  964. X        }
  965. X
  966. X        /* Check for match. */
  967. X
  968. X        if (0 == searchval)
  969. X        {
  970. X            /* Does not match. */
  971. X
  972. X            fclose(infile);
  973. X            return;
  974. X        }
  975. X    }
  976. X
  977. X    /*
  978. X    ** If FRONTEND, just print filename and return.
  979. X    ** Will arrive here when FRONTEND and complex search.
  980. X    */
  981. X
  982. X    if (flags & FRONTEND)
  983. X    {
  984. X        printf("%s\n", filename);
  985. X        fclose(infile);
  986. X        return;
  987. X    }
  988. X
  989. X    /*
  990. X    ** Now we know what format the file is, so process further by format
  991. X    ** including output to screen.
  992. X    */
  993. X
  994. X    switch FORMAT_TYPE(format)
  995. X    {
  996. X        case FORMAT_TYPE(FORMAT_GIF):
  997. X            gif_display(&gifinfo, infile, filename, filesize);
  998. X            break;
  999. X
  1000. X        case FORMAT_TYPE(FORMAT_IMG):
  1001. X            img_display(&imginfo, filename, filesize);
  1002. X            break;
  1003. X
  1004. X        case FORMAT_TYPE(FORMAT_JFIF):
  1005. X            jfif_display(&jfifinfo, filename, filesize);
  1006. X            break;
  1007. X
  1008. X        case FORMAT_TYPE(FORMAT_SR):
  1009. X            sr_display(&srinfo, filename, filesize);
  1010. X            break;
  1011. X
  1012. X        case FORMAT_TYPE(FORMAT_TGA):
  1013. X            tga_display(&tgainfo, filename, filesize);
  1014. X            break;
  1015. X
  1016. X        case FORMAT_TYPE(FORMAT_PBM):
  1017. X        case FORMAT_TYPE(FORMAT_PGM):
  1018. X        case FORMAT_TYPE(FORMAT_PPM):
  1019. X            pnm_display(&pnminfo, filename, filesize);
  1020. X            break;
  1021. X
  1022. X        case FORMAT_TYPE(FORMAT_XBM):
  1023. X            xbm_display(&xbminfo, filename, filesize);
  1024. X            break;
  1025. X
  1026. X        case FORMAT_TYPE(FORMAT_BMP):
  1027. X            bmp_display(&bmpinfo, filename, filesize);
  1028. X            break;
  1029. X
  1030. X        case FORMAT_TYPE(FORMAT_PCX):
  1031. X            pcx_display(&pcxinfo, filename, filesize);
  1032. X            break;
  1033. X
  1034. X        default:
  1035. X            fprintf(stderr, "\nERROR: Internal error!\n");
  1036. X            fclose(infile);
  1037. X            return;
  1038. X    }
  1039. X
  1040. X    /* Clean up and return. */
  1041. X
  1042. X    fclose(infile);
  1043. X
  1044. X    return;
  1045. X} /* end of static long_examine() */
  1046. X
  1047. X
  1048. X/***************************************************************************
  1049. X*   FUNCTION: usage  STATIC                                                *
  1050. X*                                                                          *
  1051. X*   DESCRIPTION:                                                           *
  1052. X*                                                                          *
  1053. X*       Prints a usage message.                                            *
  1054. X*                                                                          *
  1055. X*   ENTRY:                                                                 *
  1056. X*                                                                          *
  1057. X*       Nothing.                                                           *
  1058. X*                                                                          *
  1059. X*   EXIT:                                                                  *
  1060. X*                                                                          *
  1061. X*       Does not return.                                                   *
  1062. X*                                                                          *
  1063. X*   CONSTRAINTS/SIDE EFFECTS:                                              *
  1064. X*                                                                          *
  1065. X***************************************************************************/
  1066. Xstatic VOID
  1067. Xusage()
  1068. X{
  1069. X    int loop;
  1070. X
  1071. X    printf("\n");
  1072. X    printf("CHILS version %s, patchlevel %d by James W. Birdsall\n", VERS,
  1073. X           PATCHLEVEL);
  1074. X    printf("   Prints out various data on files of various bitmap formats\n");
  1075. X    printf("   usage: chils [options] [target] [target target...]\n");
  1076. X    printf("     target       filename or directory. If a directory, all files in that\n");
  1077. X    printf("                   directory will be processed. If no targets are given,\n");
  1078. X    printf("                   all files in the current directory will be processed.\n");
  1079. X    printf("     -spattern    COMPLEX SEARCH: Allows complicated Boolean searches based on\n");
  1080. X    printf("                   height, width, colors, and format. Format keywords are as\n");
  1081. X    printf("                   for the -t option, below. Decimal numbers are allowed.\n");
  1082. X    printf("                   Other operators are:\n");
  1083. X    printf("        ( )   parentheses          =     equal           H     height\n");
  1084. X#ifdef DOS_COMSRCH
  1085. X    printf("        }     greater than         {     less than       W     width\n");
  1086. X    printf("        }=    greater or equal     {=    less or equal   C     colors\n");
  1087. X    printf("        +     logical and          ,     logical or      F     file length\n");
  1088. X#else
  1089. X    printf("        >     greater than         <     less than       W     width\n");
  1090. X    printf("        >=    greater or equal     <=    less or equal   C     colors\n");
  1091. X    printf("        &     logical and          |     logical or      F     file length\n");
  1092. X#endif
  1093. X    printf("                   Since many operators are also shell metacharacters, search\n");
  1094. X    printf("                   strings should be quoted.\n");
  1095. X#ifdef DOS_COMSRCH
  1096. X    printf("                   Example: -s'(W}=640+H}=480),(GIF+C=256)' searches for\n");
  1097. X#else
  1098. X    printf("                   Example: -s'(W>=640&H>=480)|(GIF&C=256)' searches for\n");
  1099. X#endif
  1100. X    printf("                   images at least 640 by 480, or any GIF with 256 colors.\n");
  1101. X    printf("     -tformat     TYPE: Only files of type format will be processed. Ignores\n");
  1102. X    printf("                   -v option. Multiple -t options may be specified. Formats\n");
  1103. X    printf("                   recognized (case insensitive) are:");
  1104. X
  1105. X    for (loop = 0; formatsearch[loop].name[0] != '\0'; loop++)
  1106. X    {
  1107. X        if ((loop % 6) == 0)
  1108. X        {
  1109. X            printf("\n                   ");
  1110. X        }
  1111. X        printf(formatsearch[loop].name);
  1112. X        if (formatsearch[loop + 1].name[0] != '\0')
  1113. X        {
  1114. X            printf(", ");
  1115. X        }
  1116. X    }
  1117. X
  1118. X    printf("\n");
  1119. X    printf("     -v           VERBOSE: Prints a line for every file checked, whether the\n");
  1120. X    printf("                   format was recognized or not.\n");
  1121. X    printf("     -f           FRONT END: For use as front end for other CHIARO programs.\n");
  1122. X    printf("                   Prints list of matching files on stdout. Ignores -v option.\n");
  1123. X    printf("     -n           NO TRUNCATE: Does not strip directories from filenames\n");
  1124. X    printf("                   in output.\n");
  1125. X    printf("     -w[n]        WIDTH: Overrides automatic determination of output width in\n");
  1126. X    printf("                   columns. If no number is specified, sets width to 80,\n");
  1127. X    printf("                   otherwise sets it to number specified (must be at least 80).\n");
  1128. X    printf("     -h           HELP: Prints this message.\n");
  1129. X    printf("\n");
  1130. X    printf("   Options may not be combined.\n");
  1131. X
  1132. X    cleanup(3);
  1133. X} /* end of static usage() */
  1134. X
  1135. X
  1136. X/***************************************************************************
  1137. X*   FUNCTION: errxlate  STATIC                                             *
  1138. X*                                                                          *
  1139. X*   DESCRIPTION:                                                           *
  1140. X*                                                                          *
  1141. X*       Translates an error code into a string.                            *
  1142. X*                                                                          *
  1143. X*   ENTRY:                                                                 *
  1144. X*                                                                          *
  1145. X*       errcode - code to be translated                                    *
  1146. X*                                                                          *
  1147. X*   EXIT:                                                                  *
  1148. X*                                                                          *
  1149. X*       Returns a pointer to the appropriate string.                       *
  1150. X*                                                                          *
  1151. X*   CONSTRAINTS/SIDE EFFECTS:                                              *
  1152. X*                                                                          *
  1153. X***************************************************************************/
  1154. Xstatic char *
  1155. X#ifdef __STDC__
  1156. Xerrxlate(ULONG errcode)
  1157. X#else
  1158. Xerrxlate(errcode)
  1159. XULONG errcode;
  1160. X#endif
  1161. X{
  1162. X    char *errstring;
  1163. X    char *sever;
  1164. X
  1165. X    /* Determine severity of error. */
  1166. X
  1167. X    switch (SEVERITY(errcode))
  1168. X    {
  1169. X        case SEVERITY(ST_SUCCESS):
  1170. X            sever = "SUCCESS";
  1171. X            break;
  1172. X
  1173. X        case SEVERITY(ST_WARNING):
  1174. X            sever = "WARNING";
  1175. X            break;
  1176. X
  1177. X        case SEVERITY(ST_ERROR):
  1178. X            sever = "ERROR";
  1179. X            break;
  1180. X
  1181. X        case SEVERITY(ST_FATAL):
  1182. X        default:
  1183. X            sever = "FATAL";
  1184. X            break;
  1185. X    }
  1186. X
  1187. X    /* Get error string from module. */
  1188. X
  1189. X    switch (MODULE(errcode))
  1190. X    {
  1191. X        case MODULE(GIF_MODULE):
  1192. X            errstring = gif_errstring(errcode);
  1193. X            break;
  1194. X
  1195. X        case MODULE(IMG_MODULE):
  1196. X            errstring = img_errstring(errcode);
  1197. X            break;
  1198. X
  1199. X        case MODULE(JFIF_MODULE):
  1200. X            errstring = jfif_errstring(errcode);
  1201. X            break;
  1202. X
  1203. X        case MODULE(SR_MODULE):
  1204. X            errstring = sr_errstring(errcode);
  1205. X            break;
  1206. X
  1207. X        case MODULE(TGA_MODULE):
  1208. X            errstring = tga_errstring(errcode);
  1209. X            break;
  1210. X
  1211. X        case MODULE(PBM_MODULE):
  1212. X            errstring = pbm_errstring(errcode);
  1213. X            break;
  1214. X
  1215. X        case MODULE(PGM_MODULE):
  1216. X            errstring = pgm_errstring(errcode);
  1217. X            break;
  1218. X
  1219. X        case MODULE(PPM_MODULE):
  1220. X            errstring = ppm_errstring(errcode);
  1221. X            break;
  1222. X
  1223. X        case MODULE(XBM_MODULE):
  1224. X            errstring = xbm_errstring(errcode);
  1225. X            break;
  1226. X
  1227. X        case MODULE(BMP_MODULE):
  1228. X            errstring = bmp_errstring(errcode);
  1229. X            break;
  1230. X
  1231. X        case MODULE(PCX_MODULE):
  1232. X            errstring = pcx_errstring(errcode);
  1233. X            break;
  1234. X
  1235. X        case MODULE(FB_MODULE):
  1236. X            errstring = fb_errstring(errcode);
  1237. X            break;
  1238. X
  1239. X        case MODULE(FM_MODULE):
  1240. X            errstring = re_errstring(errcode);
  1241. X            break;
  1242. X
  1243. X        default:
  1244. X            errstring = NULL;
  1245. X            break;
  1246. X    }
  1247. X
  1248. X    /* Composite strings. */
  1249. X
  1250. X    if (errstring != NULL)
  1251. X    {
  1252. X        sprintf(scratch, "%s: %s", sever, errstring);
  1253. X    }
  1254. X    else
  1255. X    {
  1256. X        /* Error not recognized by any module. */
  1257. X
  1258. X        sprintf(scratch, "%s: %08lX.", sever, errcode);
  1259. X    }
  1260. X
  1261. X    return scratch;
  1262. X} /* end of static errxlate() */
  1263. X
  1264. X
  1265. X/***************************************************************************
  1266. X*   FUNCTION: cleanup  STATIC                                              *
  1267. X*                                                                          *
  1268. X*   DESCRIPTION:                                                           *
  1269. X*                                                                          *
  1270. X*       Frees format list, if present, and exits with specified code.      *
  1271. X*                                                                          *
  1272. X*   ENTRY:                                                                 *
  1273. X*                                                                          *
  1274. X*       exitcode - code to exit with                                       *
  1275. X*                                                                          *
  1276. X*   EXIT:                                                                  *
  1277. X*                                                                          *
  1278. X*       Does not return.                                                   *
  1279. X*                                                                          *
  1280. X*   CONSTRAINTS/SIDE EFFECTS:                                              *
  1281. X*                                                                          *
  1282. X***************************************************************************/
  1283. Xstatic VOID
  1284. X#ifdef __STDC__
  1285. Xcleanup(int exitcode)
  1286. X#else
  1287. Xcleanup(exitcode)
  1288. Xint exitcode;
  1289. X#endif
  1290. X{
  1291. X    flist *temp;
  1292. X
  1293. X    /* Free format list, if present. */
  1294. X
  1295. X    while (formatlist != (flist *) NULL)
  1296. X    {
  1297. X        temp = formatlist;
  1298. X        formatlist = formatlist->next;
  1299. X        free(temp);
  1300. X    }
  1301. X
  1302. X    /* Free extension list, if present. */
  1303. X
  1304. X    re_deinit();
  1305. X
  1306. X    /* Exit. */
  1307. X
  1308. X    exit(exitcode);
  1309. X} /* end of static cleanup() */
  1310. X
  1311. END_OF_FILE
  1312.   if test 41782 -ne `wc -c <'src/chils.c'`; then
  1313.     echo shar: \"'src/chils.c'\" unpacked with wrong size!
  1314.   fi
  1315.   # end of 'src/chils.c'
  1316. fi
  1317. if test -f 'src/sr.c' -a "${1}" != "-c" ; then 
  1318.   echo shar: Will not clobber existing file \"'src/sr.c'\"
  1319. else
  1320.   echo shar: Extracting \"'src/sr.c'\" \(10626 characters\)
  1321.   sed "s/^X//" >'src/sr.c' <<'END_OF_FILE'
  1322. X/***************************************************************************
  1323. X*   SR.C                                                                   *
  1324. X*   MODULE:  SR (Sun Raster)                                               *
  1325. X*   OS:      UNIX                                                          *
  1326. X*                                                                          *
  1327. X*   Copyright (c) 1993 James W. Birdsall. All Rights Reserved.             *
  1328. X*                                                                          *
  1329. X*   $Id: sr.c,v 1.1 1993/03/02 00:59:36 jwbirdsa Exp $
  1330. X*                                                                          *
  1331. X*   This file contains functions to process Sun Raster format files.       *
  1332. X*   Functions:                                                             *
  1333. X*      sr_verify    - checks filename to see if it is an Sun Raster file   *
  1334. X*      sr_getheader - extracts header data from SR file                   *
  1335. X*                                                                          *
  1336. X*      sr_errstring - converts error code into message                     *
  1337. X*                                                                          *
  1338. X***************************************************************************/
  1339. X
  1340. X#include "config.h"
  1341. X
  1342. X/*
  1343. X** system includes <>
  1344. X*/
  1345. X
  1346. X#include <stdio.h>
  1347. X
  1348. X
  1349. X/*
  1350. X** custom includes ""
  1351. X*/
  1352. X
  1353. X#include "depend.h"
  1354. X#include "formats.h"
  1355. X#include "sr.h"
  1356. X
  1357. X
  1358. X/*
  1359. X** local #defines
  1360. X*/
  1361. X
  1362. X/*
  1363. X** misc: copyright strings, version macros, etc.
  1364. X*/
  1365. X
  1366. X/*
  1367. X** typedefs
  1368. X*/
  1369. X
  1370. X/*
  1371. X** global variables
  1372. X*/
  1373. X
  1374. X/*
  1375. X** static globals
  1376. X*/
  1377. X
  1378. Xstatic char CONST rcsid[] = "$Id: sr.c,v 1.1 1993/03/02 00:59:36 jwbirdsa Exp $";
  1379. X
  1380. X
  1381. X/*
  1382. X** function prototypes
  1383. X*/
  1384. X
  1385. X/*
  1386. X** functions
  1387. X*/
  1388. X
  1389. X
  1390. X/***************************************************************************
  1391. X*   FUNCTION:    sr_verify                                                 *
  1392. X*                                                                          *
  1393. X*   DESCRIPTION:                                                           *
  1394. X*                                                                          *
  1395. X*       Verifies that a file is a Sun Raster file by checking for the      *
  1396. X*       magic number at the beginning of the file.                         *
  1397. X*                                                                          *
  1398. X*   ENTRY:                                                                 *
  1399. X*                                                                          *
  1400. X*       filename - name of file to be verified                             *
  1401. X*       version  - pointer to unsigned long in which format/version value  *
  1402. X*                  is returned                                             *
  1403. X*       exts     - optional list of file extensions                        *
  1404. X*                                                                          *
  1405. X*   EXIT:                                                                  *
  1406. X*                                                                          *
  1407. X*       Returns an error/status code.                                      *
  1408. X*                                                                          *
  1409. X*   CONSTRAINTS/SIDE EFFECTS:                                              *
  1410. X*                                                                          *
  1411. X***************************************************************************/
  1412. XULONG
  1413. X#ifdef __STDC__
  1414. Xsr_verify(char *filename, ULONG *version, char **exts)
  1415. X#else
  1416. Xsr_verify(filename, version, exts)
  1417. Xchar *filename;
  1418. XULONG *version;
  1419. Xchar **exts;
  1420. X#endif
  1421. X{
  1422. X    FILE *srfile;
  1423. X    UCHAR verbuf[4];
  1424. X    ULONG retval;
  1425. X
  1426. X    /* If a list of extensions was supplied, check against it. */
  1427. X
  1428. X    if (((char **) NULL) != exts)
  1429. X    {
  1430. X        /* Search for '.' marking extension. */
  1431. X    
  1432. X        int loop;
  1433. X        char *extptr = (char *) strrchr(filename, '.');
  1434. X
  1435. X        if (NULL == extptr)
  1436. X        {
  1437. X            /* No extension, cannot classify. */
  1438. X    
  1439. X            *version = SR_NOT;
  1440. X            return 0;
  1441. X        }
  1442. X        extptr++;
  1443. X    
  1444. X        /* Now we have the extension, check against list. */
  1445. X    
  1446. X        for (loop = 0; exts[loop] != NULL; loop++)
  1447. X        {
  1448. X            /* Case-sensitive string compare. */
  1449. X    
  1450. X            if (strcmp(extptr, exts[loop]) == 0)
  1451. X            {
  1452. X                /* Match, so break out of loop. */
  1453. X    
  1454. X                break;
  1455. X            }
  1456. X        }
  1457. X    
  1458. X        /* Check exit from loop. */
  1459. X    
  1460. X        if (NULL == exts[loop])
  1461. X        {
  1462. X            /* No match, return. */
  1463. X    
  1464. X            *version = SR_NOT;
  1465. X            return 0;
  1466. X        }
  1467. X    
  1468. X        /* Extension is valid for type SR, so process accordingly. */
  1469. X    }
  1470. X
  1471. X    /* Open the file so we can check the magic number. */
  1472. X
  1473. X    if ((srfile = fopen(filename, FOPEN_READ_BINARY)) == (FILE *) NULL)
  1474. X    {
  1475. X        return SR_FILEERR_E;
  1476. X    }
  1477. X
  1478. X    /* Read all four bytes of the magic number. */
  1479. X
  1480. X    if (fread(verbuf, sizeof(UCHAR), 4, srfile) != 4)
  1481. X    {
  1482. X        *version = SR_NOT;
  1483. X        retval = (feof(srfile) ? ST_SUCCESS : SR_FILEERR_E);
  1484. X        fclose(srfile);
  1485. X        return retval;
  1486. X    }
  1487. X
  1488. X    /* Set version return according to magic number. */
  1489. X
  1490. X    *version = ((SR_MAGIC == CONSTRUCT_M_ULONG(verbuf)) ? SR_1 : SR_NOT);
  1491. X
  1492. X    /* Close file. */
  1493. X
  1494. X    if (fclose(srfile))
  1495. X    {
  1496. X        return SR_FILEERR_E;
  1497. X    }
  1498. X
  1499. X    /* Return OK. */
  1500. X
  1501. X    return 0;
  1502. X} /* end of sr_verify() */
  1503. X
  1504. X
  1505. X/***************************************************************************
  1506. X*   FUNCTION:    sr_getheader                                             *
  1507. X*                                                                          *
  1508. X*   DESCRIPTION:                                                           *
  1509. X*                                                                          *
  1510. X*       Assumes that file is an SR file. Reads header from file, extracts *
  1511. X*       data into SR_HDR structure.                                       *
  1512. X*                                                                          *
  1513. X*   ENTRY:                                                                 *
  1514. X*                                                                          *
  1515. X*       infile  - file to be processed                                     *
  1516. X*       results - pointer to SR_HDR structure in which data from header   *
  1517. X*                 is returned                                              *
  1518. X*                                                                          *
  1519. X*   EXIT:                                                                  *
  1520. X*                                                                          *
  1521. X*       Returns an error/status code.                                      *
  1522. X*                                                                          *
  1523. X*   CONSTRAINTS/SIDE EFFECTS:                                              *
  1524. X*                                                                          *
  1525. X*       Leaves file pointing to beginning of image data.                   *
  1526. X*                                                                          *
  1527. X***************************************************************************/
  1528. XULONG
  1529. X#ifdef __STDC__
  1530. Xsr_getheader(FILE *infile, SR_HDR *results)
  1531. X#else
  1532. Xsr_getheader(infile, results)
  1533. XFILE *infile;
  1534. XSR_HDR *results;
  1535. X#endif
  1536. X{
  1537. X    UCHAR rawhdr[SR_HDR_LEN];
  1538. X
  1539. X    /* Make sure we're at beginning of file. */
  1540. X
  1541. X    if (fseek(infile, 0L, SEEK_SET))
  1542. X    {
  1543. X        return SR_FILEERR_E;
  1544. X    }
  1545. X
  1546. X    /* Read raw bytes into buffer. */
  1547. X
  1548. X    if (fread(rawhdr, 1, SR_HDR_LEN, infile) != SR_HDR_LEN)
  1549. X    {
  1550. X        return (feof(infile) ? SR_UNEOF_E : SR_FILEERR_E);
  1551. X    }
  1552. X
  1553. X    /* Extract info from raw header. */
  1554. X
  1555. X    results->imwid = CONSTRUCT_M_ULONG(rawhdr + SR_HDR_IMWID_OFF);
  1556. X    results->imhi = CONSTRUCT_M_ULONG(rawhdr + SR_HDR_IMHI_OFF);
  1557. X    results->pixelbits = (int) CONSTRUCT_M_ULONG(rawhdr + SR_HDR_BITS_OFF);
  1558. X    results->rasterlen = CONSTRUCT_M_ULONG(rawhdr + SR_HDR_RLEN_OFF);
  1559. X    results->rastype = (unsigned int)
  1560. X                       CONSTRUCT_M_ULONG(rawhdr + SR_HDR_TYPE_OFF);
  1561. X    results->maptype = (int) CONSTRUCT_M_ULONG(rawhdr + SR_HDR_MTYPE_OFF);
  1562. X    results->maplen = CONSTRUCT_M_ULONG(rawhdr + SR_HDR_MLEN_OFF);
  1563. X
  1564. X    /* Set file to point to start of colormap. */
  1565. X
  1566. X    if (fseek(infile, (long)(SR_HDR_LEN), SEEK_SET))
  1567. X    {
  1568. X        return SR_FILEERR_E;
  1569. X    }
  1570. X
  1571. X    /* Return OK. */
  1572. X
  1573. X    return 0;
  1574. X} /* end of sr_getheader() */
  1575. X
  1576. X
  1577. X/***************************************************************************
  1578. X*   FUNCTION: sr_errstring                                                *
  1579. X*                                                                          *
  1580. X*   DESCRIPTION:                                                           *
  1581. X*                                                                          *
  1582. X*      Returns a string corresponding to an error code.                    *
  1583. X*                                                                          *
  1584. X*   ENTRY:                                                                 *
  1585. X*                                                                          *
  1586. X*      errcode - error code to be translated                               *
  1587. X*                                                                          *
  1588. X*   EXIT:                                                                  *
  1589. X*                                                                          *
  1590. X*      Returns a pointer to the appropriate string, or NULL if there is    *
  1591. X*      no appropriate string.                                              *
  1592. X*                                                                          *
  1593. X*   CONSTRAINTS/SIDE EFFECTS:                                              *
  1594. X*                                                                          *
  1595. X***************************************************************************/
  1596. Xchar *
  1597. X#ifdef __STDC__
  1598. Xsr_errstring(ULONG errcode)
  1599. X#else
  1600. Xsr_errstring(errcode)
  1601. XULONG errcode;
  1602. X#endif
  1603. X{
  1604. X    char *temp;
  1605. X
  1606. X    /* If error code not from this module, return NULL. */
  1607. X
  1608. X    if ((errcode & ST_MOD_MASK) != SR_MODULE)
  1609. X    {
  1610. X        return NULL;
  1611. X    }
  1612. X
  1613. X    /* Process by code. */
  1614. X
  1615. X    switch (ERRSEV(errcode))
  1616. X    {
  1617. X        case ERRSEV(SR_NOTSR_E):
  1618. X            temp = "File is not a SR format file.";
  1619. X            break;
  1620. X        case ERRSEV(SR_FILEERR_E):
  1621. X            temp = "Error accessing file.";
  1622. X            break;
  1623. X        case ERRSEV(SR_UNEOF_E):
  1624. X            temp = "Unexpected End of File";
  1625. X            break;
  1626. X
  1627. X        default:
  1628. X            temp = NULL;
  1629. X            break;
  1630. X    }
  1631. X
  1632. X    return temp;
  1633. X} /* end of sr_errstring() */
  1634. X
  1635. END_OF_FILE
  1636.   if test 10626 -ne `wc -c <'src/sr.c'`; then
  1637.     echo shar: \"'src/sr.c'\" unpacked with wrong size!
  1638.   fi
  1639.   # end of 'src/sr.c'
  1640. fi
  1641. echo shar: End of archive 5 \(of 18\).
  1642. cp /dev/null ark5isdone
  1643. MISSING=""
  1644. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ; do
  1645.     if test ! -f ark${I}isdone ; then
  1646.     MISSING="${MISSING} ${I}"
  1647.     fi
  1648. done
  1649. if test "${MISSING}" = "" ; then
  1650.     echo You have unpacked all 18 archives.
  1651.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1652. else
  1653.     echo You still must unpack the following archives:
  1654.     echo "        " ${MISSING}
  1655. fi
  1656. exit 0
  1657. exit 0 # Just in case...
  1658.