home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / printing / ghostscrip / source / specific / c / gp_unix < prev    next >
Encoding:
Text File  |  1991-10-26  |  4.5 KB  |  145 lines

  1. /* Copyright (C) 1989, 1990, 1991 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 "time_.h"
  27.  
  28. /* Do platform-dependent initialization */
  29. void
  30. gp_init()
  31. {
  32. }
  33.  
  34. /* Read the current date (in days since Jan. 1, 1980) */
  35. /* and time (in milliseconds since midnight). */
  36. void
  37. gp_get_clock(long *pdt)
  38. {    long secs_since_1980;
  39.     struct timeval tp;
  40.     struct timezone tzp;
  41.     struct tm *tm, *localtime();
  42.  
  43.     if ( gettimeofday(&tp, &tzp) == -1 )
  44.        {    perror("Ghostscript: gettimeofday failed:");
  45.         exit(-1);
  46.        }
  47.  
  48.     /* tp.tv_sec is #secs since Jan 1, 1970 */
  49.  
  50.     /* subtract off number of seconds in 10 years */
  51.     /* leap seconds are not accounted for */
  52.     secs_since_1980 = tp.tv_sec - (long)(60 * 60 * 24 * 365.25 * 10);
  53.  
  54.     /* adjust for timezone */
  55.     secs_since_1980 -= (tzp.tz_minuteswest * 60);
  56.  
  57.     /* adjust for daylight savings time - assume dst offset is 1 hour */
  58.     tm = localtime(&(tp.tv_sec));
  59.     if ( tm->tm_isdst )
  60.         secs_since_1980 += (60 * 60);
  61.  
  62.     /* divide secs by #secs/day to get #days (integer division truncates) */
  63.     pdt[0] = secs_since_1980 / (60 * 60 * 24);
  64.     /* modulo + microsecs/1000 gives number of millisecs since midnight */
  65.     pdt[1] = (secs_since_1980 % (60 * 60 * 24)) * 1000 + tp.tv_usec / 1000;
  66. #ifdef DEBUG_CLOCK
  67.     printf("tp.tv_sec = %d  tp.tv_usec = %d  pdt[0] = %ld  pdt[1] = %ld\n",
  68.         tp.tv_sec, tp.tv_usec, pdt[0], pdt[1]);
  69. #endif
  70. }
  71.  
  72. /* ------ File name syntax ------ */
  73.  
  74. /* Define the character used for separating file names in a list. */
  75. char gp_file_name_list_separator = ':';
  76.  
  77. /* Define the default scratch file name template. */
  78. char gp_scratch_file_name_template[] = "/usr/tmp/gs_XXXXXX";
  79.  
  80. /* Answer whether a file name contains a directory/device specification, */
  81. /* i.e. is absolute (not directory- or device-relative). */
  82. int
  83. gp_file_name_is_absolute(char *fname, uint len)
  84. {    /* A file name is absolute if it starts with a /. */
  85.     return ( len >= 1 && *fname == '/' );
  86. }
  87.  
  88. /* Answer the string to be used for combining a directory/device prefix */
  89. /* with a base file name.  The file name is known to not be absolute. */
  90. char *
  91. gp_file_name_concat_string(char *prefix, uint plen, char *fname, uint len)
  92. {    if ( plen > 0 && prefix[plen - 1] == '/' )
  93.         return "";
  94.     return "/";
  95. }
  96.  
  97. /* ------ File enumeration ------ */
  98.  
  99. /****** THIS IS NOT SUPPORTED ON UNIX SYSTEMS. ******/
  100. /* Amazingly enough, there is no standard Unix library routine */
  101. /* for enumerating the files matching a pattern, */
  102. /* or even for enumerating (conveniently) the files in a directory. */
  103.  
  104. struct file_enum_s {
  105.     char *pattern;
  106.     int first_time;
  107.     gs_memory_procs mprocs;
  108. };
  109.  
  110. /* Initialize an enumeration.  NEEDS WORK ON HANDLING * ? \. */
  111. file_enum *
  112. gp_enumerate_files_init(char *pat, uint patlen, proc_alloc_t palloc, proc_free_t pfree)
  113. {    file_enum *pfen = (file_enum *)(*palloc)(1, sizeof(file_enum), "gp_enumerate_files");
  114.     char *pattern;
  115.     if ( pfen == 0 ) return 0;
  116.     pattern = (*palloc)(patlen + 1, 1,
  117.                 "gp_enumerate_files(pattern)");
  118.     if ( pattern == 0 ) return 0;
  119.     memcpy(pattern, pat, patlen);
  120.     pattern[patlen] = 0;
  121.     pfen->pattern = pattern;
  122.     pfen->mprocs.alloc = palloc;
  123.     pfen->mprocs.free = pfree;
  124.     pfen->first_time = 1;
  125.     return pfen;
  126. }
  127.  
  128. /* Enumerate the next file. */
  129. uint
  130. gp_enumerate_files_next(file_enum *pfen, char *ptr, uint maxlen)
  131. {    if ( pfen->first_time )
  132.        {    pfen->first_time = 0;
  133.        }
  134.     return -1;
  135. }
  136.  
  137. /* Clean up the file enumeration. */
  138. void
  139. gp_enumerate_files_close(file_enum *pfen)
  140. {    proc_free_t pfree = pfen->mprocs.free;
  141.     (*pfree)(pfen->pattern, strlen(pfen->pattern) + 1, 1,
  142.          "gp_enumerate_files_close(pattern)");
  143.     (*pfree)((char *)pfen, 1, sizeof(file_enum), "gp_enumerate_files_close");
  144. }
  145.