home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / GP_IWATC.C < prev    next >
C/C++ Source or Header  |  1992-07-11  |  4KB  |  146 lines

  1. /* Copyright (C) 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gp_iwatc.c */
  21. /* Intel processor, Watcom C-specific routines for Ghostscript */
  22. #include "dos_.h"
  23. #include <fcntl.h>
  24. #include <signal.h>
  25. #include <stdlib.h>
  26. #include "stat_.h"
  27. #include "string_.h"
  28. #include "gx.h"
  29. #include "gp.h"
  30.  
  31. /* Library routines not declared in a standard header */
  32. extern char *getenv(P1(const char *));
  33.  
  34. /* Define a substitute for stdprn (see below). */
  35. private FILE *gs_stdprn;
  36.  
  37. /* Forward declarations */
  38. private void handle_FPE(P1(int));
  39.  
  40. /* Do platform-dependent initialization. */
  41. extern void gp_init_console(P0());
  42. void
  43. gp_init()
  44. {    gs_stdprn = 0;
  45.     /* Set up the handler for numeric exceptions. */
  46.     signal(SIGFPE, handle_FPE);
  47.     gp_init_console();
  48. }
  49.  
  50. /* Trap numeric exceptions.  Someday we will do something */
  51. /* more appropriate with these. */
  52. private void
  53. handle_FPE(int sig)
  54. {    eprintf("Numeric exception:\n");
  55.     exit(1);
  56. }
  57.  
  58. /* Do platform-dependent cleanup. */
  59. void
  60. gp_exit()
  61. {
  62. }
  63.  
  64. /* ------ Printer accessing ------ */
  65.  
  66. /* Open a connection to a printer.  A null file name means use the */
  67. /* standard printer connected to the machine, if any. */
  68. /* Return NULL if the connection could not be opened. */
  69. extern void gp_set_printer_binary(P1(int));
  70. FILE *
  71. gp_open_printer(char *fname)
  72. {    if ( strlen(fname) == 0 || !strcmp(fname, "PRN") )
  73.        {    if ( gs_stdprn == 0 )
  74.            {    /* We have to effectively reopen the printer, */
  75.             /* because the Watcom library does \n -> \r\n */
  76.             /* substitution on the stdprn stream. */
  77.             int fno = dup(fileno(stdprn));
  78.             setmode(fno, O_BINARY);
  79.             gs_stdprn = fdopen(fno, "wb");
  80.             gp_set_printer_binary(fileno(gs_stdprn));
  81.            }
  82.         return gs_stdprn;
  83.        }
  84.     else
  85.         return fopen(fname, "wb");
  86. }
  87.  
  88. /* Close the connection to the printer. */
  89. void
  90. gp_close_printer(FILE *pfile, const char *fname)
  91. {    fclose(pfile);
  92.     if ( pfile == gs_stdprn )
  93.         gs_stdprn = 0;
  94. }
  95.  
  96. /* ------ File names ------ */
  97.  
  98. /* Create and open a scratch file with a given name prefix. */
  99. /* Write the actual file name at fname. */
  100. FILE *
  101. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  102. {    /* Unfortunately, Watcom C doesn't provide mktemp, */
  103.     /* so we have to simulate it ourselves. */
  104.     char *temp;
  105.     struct stat fst;
  106.     char *end;
  107.     if ( (temp = getenv("TEMP")) == NULL )
  108.         *fname = 0;
  109.     else
  110.     {    strcpy(fname, temp);
  111.         /* Prevent X's in path from being converted by mktemp. */
  112.         for ( temp = fname; *temp; temp++ )
  113.             *temp = tolower(*temp);
  114.         strcat(fname, "\\");
  115.     }
  116.     strcat(fname, prefix);
  117.     strcat(fname, "AA.AAA");
  118.     end = fname + strlen(fname) - 1;
  119.     while ( stat(fname, &fst) == 0 )
  120.        {    char *inc = end;
  121.         while ( *inc == 'Z' || *inc == '.' )
  122.            {    if ( *inc == 'Z' ) *inc = 'A';
  123.             inc--;
  124.             if ( end - inc == 6 ) return 0;
  125.            }
  126.         ++*inc;
  127.        }
  128.     return fopen(fname, mode);
  129. }
  130.  
  131. /* ------ File operations ------ */
  132.  
  133. /* If the file given by fname exists, fill in its status and return 1; */
  134. /* otherwise return 0. */
  135. int
  136. gp_file_status(const char *fname, file_status *pstatus)
  137. {    struct stat fst;
  138.     if ( stat(fname, &fst) < 0 ) return 0;
  139.     pstatus->size_pages = (fst.st_size + 1023) >> 10;
  140.     pstatus->size_bytes = fst.st_size;
  141.     /****** CONVERSION PROBABLY REQUIRED HERE ******/
  142.     pstatus->time_referenced = fst.st_atime;
  143.     pstatus->time_created = fst.st_mtime;
  144.     return 1;
  145. }
  146.