home *** CD-ROM | disk | FTP | other *** search
/ Der Mediaplex Sampler - Die 6 von Plex / 6_v_plex.zip / 6_v_plex / DISK5 / DOS_14 / GS252DVX.ZIP / GP_UNIX.C < prev    next >
C/C++ Source or Header  |  1992-11-25  |  8KB  |  254 lines

  1. /* Copyright (C) 1989, 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_unix.c */
  21. /* Unix-specific routines for Ghostscript */
  22. #include "memory_.h"
  23. #include "string_.h"
  24. #include "gx.h"
  25. #include "gp.h"
  26. #include "stat_.h"
  27. #include "time_.h"
  28.  
  29. /* popen isn't POSIX-standard, so we declare it here. */
  30. /*
  31. extern FILE *popen();
  32. extern int pclose();
  33. */
  34.  
  35. /* Do platform-dependent initialization. */
  36. void
  37. gp_init()
  38. {
  39. }
  40.  
  41. /* Do platform-dependent cleanup. */
  42. void
  43. gp_exit()
  44. {
  45. }
  46.  
  47. /* ------ Date and time ------ */
  48.  
  49. /* Read the current date (in days since Jan. 1, 1980) */
  50. /* and time (in milliseconds since midnight). */
  51. void
  52. gp_get_clock(long *pdt)
  53. {    long secs_since_1980;
  54.     struct timeval tp;
  55.     struct timezone tzp;
  56.     time_t tsec;
  57.     struct tm *tm, *localtime();
  58.  
  59.     if ( gettimeofday(&tp, &tzp) == -1 )
  60.        {    lprintf("Ghostscript: gettimeofday failed!\n");
  61.         gs_exit(1);
  62.        }
  63.  
  64.     /* tp.tv_sec is #secs since Jan 1, 1970 */
  65.  
  66.     /* subtract off number of seconds in 10 years */
  67.     /* leap seconds are not accounted for */
  68.     secs_since_1980 = tp.tv_sec - (long)(60 * 60 * 24 * 365.25 * 10);
  69.  
  70.     /* adjust for timezone */
  71.     secs_since_1980 -= (tzp.tz_minuteswest * 60);
  72.  
  73.     /* adjust for daylight savings time - assume dst offset is 1 hour */
  74.     tsec = tp.tv_sec;
  75.     tm = localtime(&tsec);
  76.     if ( tm->tm_isdst )
  77.         secs_since_1980 += (60 * 60);
  78.  
  79.     /* divide secs by #secs/day to get #days (integer division truncates) */
  80.     pdt[0] = secs_since_1980 / (60 * 60 * 24);
  81.     /* modulo + microsecs/1000 gives number of millisecs since midnight */
  82.     pdt[1] = (secs_since_1980 % (60 * 60 * 24)) * 1000 + tp.tv_usec / 1000;
  83. #ifdef DEBUG_CLOCK
  84.     printf("tp.tv_sec = %d  tp.tv_usec = %d  pdt[0] = %ld  pdt[1] = %ld\n",
  85.         tp.tv_sec, tp.tv_usec, pdt[0], pdt[1]);
  86. #endif
  87. }
  88.  
  89. /* ------ Screen management ------ */
  90.  
  91. /* Write a string to the console. */
  92. void
  93. gp_console_puts(const char *str, uint size)
  94. {    fwrite(str, 1, size, stdout);
  95. }
  96.  
  97. /* Make the console current on the screen. */
  98. int
  99. gp_make_console_current(gx_device *dev)
  100. {    return 0;
  101. }
  102.  
  103. /* Make the graphics current on the screen. */
  104. int
  105. gp_make_graphics_current(gx_device *dev)
  106. {    return 0;
  107. }
  108.  
  109. /* ------ Printer accessing ------ */
  110.  
  111. /* Open a connection to a printer.  A null file name means use the */
  112. /* standard printer connected to the machine, if any. */
  113. /* Return NULL if the connection could not be opened. */
  114. extern void gp_set_printer_binary(P1(int));
  115. FILE *
  116. gp_open_printer(char *fname)
  117. {    if ( strlen(fname) == 0 || !strcmp(fname, "PRN") )
  118.        {    gp_set_printer_binary(fileno(stdprn));
  119.         return stdprn;
  120.        }
  121.     else
  122.         return fopen(fname, "wb");
  123. }
  124.  
  125. /* Close the connection to the printer. */
  126. void
  127. gp_close_printer(FILE *pfile, const char *fname)
  128. {    if ( pfile != stdprn )
  129.         fclose(pfile);
  130. }
  131.  
  132. /* ------ File names ------ */
  133.  
  134. /* Define the character used for separating file names in a list. */
  135. /* 11/23/92 dvx mod  tjb */
  136. const char gp_file_name_list_separator = ';';
  137.  
  138. /* Define the default scratch file name prefix. */
  139. const char gp_scratch_file_name_prefix[] = "/tmp/gs_";
  140.  
  141. /* Define whether case is insignificant in file names. */
  142. /* 11/23/92 dvx mod  tjb */
  143. const int gp_file_names_ignore_case = 1;
  144.  
  145. /* Define the string to be concatenated with the file mode */
  146. /* for opening files without end-of-line conversion. */
  147. /* 11/23/92 dvx mod  tjb */
  148. const char gp_fmode_binary_suffix[] = "b";
  149. /* Define the file modes for binary reading or writing. */
  150. const char gp_fmode_rb[] = "rb";
  151. const char gp_fmode_wb[] = "wb";
  152.  
  153. /* Create and open a scratch file with a given name prefix. */
  154. /* Write the actual file name at fname. */
  155. FILE *
  156. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  157. {    strcpy(fname, prefix);
  158.     strcat(fname, "XXXXXX");
  159.     mktemp(fname);
  160.     return fopen(fname, mode);
  161. }
  162.  
  163. /* Answer whether a file name contains a directory/device specification, */
  164. /* i.e. is absolute (not directory- or device-relative). */
  165. /* 11/23/92 dvx mod  tjb */
  166. int
  167. gp_file_name_is_absolute(const char *fname, uint len)
  168. {    /* A file name is absolute if it contains a drive specification */
  169.     /* (second character is a :) or if it start with / or \. */
  170.     return ( len >= 1 && (*fname == '/' || *fname == '\\' ||
  171.         (len >= 2 && fname[1] == ':')) );
  172. }
  173.  
  174. /* Answer the string to be used for combining a directory/device prefix */
  175. /* with a base file name.  The file name is known to not be absolute. */
  176. const char *
  177. gp_file_name_concat_string(const char *prefix, uint plen,
  178.   const char *fname, uint len)
  179. {    if ( plen > 0 )
  180.       switch ( prefix[plen - 1] )
  181.        {    case ':': case '/': case '\\': return "";
  182.        };
  183.     return "\\";
  184. }
  185.  
  186. /* ------ File operations ------ */
  187.  
  188. /* If the file given by fname exists, fill in its status and return 1; */
  189. /* otherwise return 0. */
  190. int
  191. gp_file_status(const char *fname, file_status *pstatus)
  192. {    struct stat sbuf;
  193.     /* The RS/6000 prototype for stat doesn't include const, */
  194.     /* so we have to explicitly remove the const modifier. */
  195.     if ( stat((char *)fname, &sbuf) < 0 ) return 0;
  196.     pstatus->size_pages = stat_blocks(&sbuf);    /* st_blocks is */
  197.                     /* missing on some systems, */
  198.                     /* see stat_.h */
  199.     pstatus->size_bytes = sbuf.st_size;
  200.     pstatus->time_referenced = sbuf.st_mtime;
  201.     pstatus->time_created = sbuf.st_ctime;
  202.     return 1;
  203. }
  204.  
  205. /* ------ File enumeration ------ */
  206.  
  207. /****** THIS IS NOT SUPPORTED ON UNIX SYSTEMS. ******/
  208. /* Amazingly enough, there is no standard Unix library routine */
  209. /* for enumerating the files matching a pattern, */
  210. /* or even for enumerating (conveniently) the files in a directory. */
  211.  
  212. struct file_enum_s {
  213.     char *pattern;
  214.     int first_time;
  215.     gs_memory_procs mprocs;
  216. };
  217.  
  218. /* Initialize an enumeration.  NEEDS WORK ON HANDLING * ? \. */
  219. file_enum *
  220. gp_enumerate_files_init(const char *pat, uint patlen,
  221.   proc_alloc_t palloc, proc_free_t pfree)
  222. {    file_enum *pfen = (file_enum *)(*palloc)(1, sizeof(file_enum), "gp_enumerate_files");
  223.     char *pattern;
  224.     if ( pfen == 0 ) return 0;
  225.     pattern = (*palloc)(patlen + 1, 1,
  226.                 "gp_enumerate_files(pattern)");
  227.     if ( pattern == 0 ) return 0;
  228.     memcpy(pattern, pat, patlen);
  229.     pattern[patlen] = 0;
  230.     pfen->pattern = pattern;
  231.     pfen->mprocs.alloc = palloc;
  232.     pfen->mprocs.free = pfree;
  233.     pfen->first_time = 1;
  234.     return pfen;
  235. }
  236.  
  237. /* Enumerate the next file. */
  238. uint
  239. gp_enumerate_files_next(file_enum *pfen, char *ptr, uint maxlen)
  240. {    if ( pfen->first_time )
  241.        {    pfen->first_time = 0;
  242.        }
  243.     return -1;
  244. }
  245.  
  246. /* Clean up the file enumeration. */
  247. void
  248. gp_enumerate_files_close(file_enum *pfen)
  249. {    proc_free_t pfree = pfen->mprocs.free;
  250.     (*pfree)(pfen->pattern, strlen(pfen->pattern) + 1, 1,
  251.          "gp_enumerate_files_close(pattern)");
  252.     (*pfree)((char *)pfen, 1, sizeof(file_enum), "gp_enumerate_files_close");
  253. }
  254.