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_MSDOS.C < prev    next >
C/C++ Source or Header  |  1992-07-14  |  6KB  |  208 lines

  1. /* Copyright (C) 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_msdos.c */
  21. /* Common platform-specific routines for MS-DOS (any compiler) */
  22. #include <stdio.h>
  23. #include <fcntl.h>
  24. #include "dos_.h"
  25. #include "string_.h"
  26. #include "gx.h"
  27. #include "gp.h"
  28.  
  29. /* ------ Date and time ------ */
  30.  
  31. /* Read the current date (in days since Jan. 1, 1980) */
  32. /* and time (in milliseconds since midnight). */
  33. void
  34. gp_get_clock(long *pdt)
  35. {    union REGS osdate, ostime;
  36.     long idate;
  37.     static int mstart[12] =
  38.        { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  39.     osdate.h.ah = 0x2a;        /* get date */
  40.     intdos(&osdate, &osdate);
  41. #define da_year rshort.cx
  42. #define da_mon h.dh
  43. #define da_day h.dl
  44.     ostime.h.ah = 0x2c;        /* get time */
  45.     intdos(&ostime, &ostime);
  46. #define ti_hour h.ch
  47. #define ti_min h.cl
  48. #define ti_sec h.dh
  49. #define ti_hund h.dl
  50.     idate = (long)osdate.da_year * 365 +
  51.         (osdate.da_year / 4 + 1 +    /* account for leap years */
  52.          mstart[osdate.da_mon - 1] +    /* month is 1-origin */
  53.          osdate.da_day - 1);        /* day of month is 1-origin */
  54.     if ( osdate.da_mon <= 2 && osdate.da_year % 4 == 0 )        /* Jan. or Feb. of leap year */
  55.         idate--;
  56.     pdt[0] = idate;
  57.     pdt[1] =
  58.         (ostime.ti_hour * 60 + ostime.ti_min) * 60000L +
  59.         (ostime.ti_sec * 100 + ostime.ti_hund) * 10L;
  60. }
  61.  
  62. /* ------ Printer accessing ------ */
  63.  
  64. /* Put the printer into binary mode.  This is not a standard gp procedure, */
  65. /* but all MS-DOS configurations need it. */
  66. void
  67. gp_set_printer_binary(int prnfno)
  68. {    union REGS regs;
  69.     regs.h.ah = 0x44;    /* ioctl */
  70.     regs.h.al = 0;        /* get device info */
  71.     regs.rshort.bx = prnfno;
  72.     intdos(®s, ®s);
  73.     regs.h.dl |= 0x20;    /* binary (no ^Z intervention) */
  74.     regs.h.dh = 0;
  75.     regs.h.ah = 0x44;    /* ioctl */
  76.     regs.h.al = 1;        /* set device info */
  77.     intdos(®s, ®s);
  78. }
  79.  
  80. /* ------ File names ------ */
  81.  
  82. /* Define the character used for separating file names in a list. */
  83. const char gp_file_name_list_separator = ';';
  84.  
  85. /* Define the default scratch file name prefix. */
  86. const char gp_scratch_file_name_prefix[] = "_temp_";
  87.  
  88. /* Define whether case is insignificant in file names. */
  89. const int gp_file_names_ignore_case = 1;
  90.  
  91. /* Define the string to be concatenated with the file mode */
  92. /* for opening files without end-of-line conversion. */
  93. const char gp_fmode_binary_suffix[] = "b";
  94. /* Define the file modes for binary reading or writing. */
  95. const char gp_fmode_rb[] = "rb";
  96. const char gp_fmode_wb[] = "wb";
  97.  
  98. /* Answer whether a file name contains a directory/device specification, */
  99. /* i.e. is absolute (not directory- or device-relative). */
  100. int
  101. gp_file_name_is_absolute(const char *fname, uint len)
  102. {    /* A file name is absolute if it contains a drive specification */
  103.     /* (second character is a :) or if it start with / or \. */
  104.     return ( len >= 1 && (*fname == '/' || *fname == '\\' ||
  105.         (len >= 2 && fname[1] == ':')) );
  106. }
  107.  
  108. /* Answer the string to be used for combining a directory/device prefix */
  109. /* with a base file name.  The file name is known to not be absolute. */
  110. const char *
  111. gp_file_name_concat_string(const char *prefix, uint plen,
  112.   const char *fname, uint len)
  113. {    if ( plen > 0 )
  114.       switch ( prefix[plen - 1] )
  115.        {    case ':': case '/': case '\\': return "";
  116.        };
  117.     return "\\";
  118. }
  119.  
  120. /* ------ File enumeration ------ */
  121.  
  122. struct file_enum_s {
  123.     ff_struct_t ffblk;
  124.     char *pattern;
  125.     int patlen;            /* allocated length of pattern */
  126.     int head_size;            /* pattern length through last */
  127.                     /* : or \ */
  128.     int first_time;
  129.     gs_memory_procs mprocs;
  130. };
  131.  
  132. /* Initialize an enumeration.  Note that * and ? in a directory */
  133. /* don't work, and \* and \? don't work. */
  134. file_enum *
  135. gp_enumerate_files_init(const char *pat, uint patlen,
  136.   proc_alloc_t palloc, proc_free_t pfree)
  137. {    file_enum *pfen = (file_enum *)(*palloc)(1, sizeof(file_enum), "gp_enumerate_files");
  138.     char *pattern;
  139.     char *p;
  140.     int hsize = 0;
  141.     int i;
  142.     if ( pfen == 0 ) return 0;
  143.     pattern = (*palloc)(patlen + 1, 1,
  144.                 "gp_enumerate_files(pattern)");
  145.     if ( pattern == 0 ) return 0;
  146.     p = pattern;
  147.     for ( i = 0; i < patlen; i++ )
  148.        {    switch ( pat[i] )
  149.          {
  150.         case '*':
  151.            /* Skip to . or end of string so DOS can do it. */
  152.            *p++ = '*';
  153.            while ( i < patlen && pat[i] != '.' ) i++;
  154.            i--;
  155.            continue;
  156.         case '\\':
  157.            i++;
  158.            if ( pat[i] != '\\' && pat[i] != '/' && pat[i] != ':' )
  159.             break;
  160.            /* falls through */
  161.         case ':':
  162.         case '/':
  163.            hsize = p + 1 - pattern;
  164.          }
  165.         *p++ = pat[i];
  166.        }
  167.     *p = 0;
  168.     pfen->pattern = pattern;
  169.     pfen->patlen = patlen;
  170.     pfen->head_size = hsize;
  171.     pfen->mprocs.alloc = palloc;
  172.     pfen->mprocs.free = pfree;
  173.     pfen->first_time = 1;
  174.     return pfen;
  175. }
  176.  
  177. /* Enumerate the next file. */
  178. uint
  179. gp_enumerate_files_next(file_enum *pfen, char *ptr, uint maxlen)
  180. {    int code;
  181.     char *p, *q;
  182.     if ( pfen->first_time )
  183.        {    code = dos_findfirst(pfen->pattern, &pfen->ffblk);
  184.         pfen->first_time = 0;
  185.        }
  186.     else
  187.         code = dos_findnext(&pfen->ffblk);
  188.     if ( code != 0 )
  189.        {    /* All done, clean up. */
  190.         gp_enumerate_files_close(pfen);
  191.         return ~(uint)0;
  192.        }
  193.     if ( maxlen < 13 + pfen->head_size ) return maxlen + 1;    /* cop out! */
  194.     memcpy(ptr, pfen->pattern, pfen->head_size);
  195.     for ( p = &pfen->ffblk.ff_name[0], q = ptr + pfen->head_size; *p; p++ )
  196.       if ( *p != ' ' ) *q++ = *p;
  197.     return q - ptr;
  198. }
  199.  
  200. /* Clean up the file enumeration. */
  201. void
  202. gp_enumerate_files_close(file_enum *pfen)
  203. {    proc_free_t pfree = pfen->mprocs.free;
  204.     (*pfree)(pfen->pattern, pfen->patlen + 1, 1,
  205.          "gp_enumerate_files_close(pattern)");
  206.     (*pfree)((char *)pfen, 1, sizeof(file_enum), "gp_enumerate_files_close");
  207. }
  208.