home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / dv_x / gs261dvx.zip / gp_unix.c < prev    next >
C/C++ Source or Header  |  1993-06-17  |  8KB  |  254 lines

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