home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / microcode / pros2fs.c < prev    next >
C/C++ Source or Header  |  2000-12-05  |  10KB  |  321 lines

  1. /* -*-C-*-
  2.  
  3. $Id: pros2fs.c,v 1.18 2000/12/05 21:23:47 cph Exp $
  4.  
  5. Copyright (c) 1994-2000 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include "scheme.h"
  23. #include "prims.h"
  24. #include "os2.h"
  25. #include "osfs.h"
  26.  
  27. extern FILESTATUS3 * OS2_read_file_status (const char *);
  28. extern void OS2_write_file_status (const char *, FILESTATUS3 *);
  29. extern char * OS2_drive_type (char);
  30. extern long OS2_timezone (void);
  31. extern long OS2_daylight_savings_p (void);
  32. extern void EXFUN (OS_file_copy, (CONST char *, CONST char *));
  33.  
  34. static SCHEME_OBJECT time_to_integer (FDATE *, FTIME *);
  35. static void integer_to_time (SCHEME_OBJECT, FDATE *, FTIME *);
  36.  
  37. DEFINE_PRIMITIVE ("FILE-ATTRIBUTES", Prim_file_attributes, 1, 1,
  38.   "Return attributes of FILE, as an integer.")
  39. {
  40.   PRIMITIVE_HEADER (1);
  41.   {
  42.     FILESTATUS3 * info = (OS2_read_file_status (STRING_ARG (1)));
  43.     PRIMITIVE_RETURN
  44.       ((info == 0)
  45.        ? SHARP_F
  46.        : (LONG_TO_UNSIGNED_FIXNUM (info -> attrFile)));
  47.   }
  48. }
  49.  
  50. DEFINE_PRIMITIVE ("SET-FILE-ATTRIBUTES!", Prim_set_file_attributes, 2, 2,
  51.   "Set the attributes of FILE to ATTRIBUTES.")
  52. {
  53.   PRIMITIVE_HEADER (2);
  54.   {
  55.     FILESTATUS3 * info = (OS2_read_file_status (STRING_ARG (1)));
  56.     if (info == 0)
  57.       error_bad_range_arg (1);
  58.     (info -> attrFile) = (arg_index_integer (2, 0x10000));
  59.     OS2_write_file_status ((STRING_ARG (1)), info);
  60.     PRIMITIVE_RETURN (UNSPECIFIC);
  61.   }
  62. }
  63.  
  64. DEFINE_PRIMITIVE ("FILE-LENGTH", Prim_file_length, 1, 1,
  65.   "Return attributes of FILE, as an integer.")
  66. {
  67.   PRIMITIVE_HEADER (1);
  68.   {
  69.     FILESTATUS3 * info = (OS2_read_file_status (STRING_ARG (1)));
  70.     PRIMITIVE_RETURN
  71.       ((info == 0)
  72.        ? SHARP_F
  73.        : (ulong_to_integer (info -> cbFile)));
  74.   }
  75. }
  76.  
  77. DEFINE_PRIMITIVE ("GET-ENVIRONMENT-VARIABLE", Prim_get_environment_variable, 1, 1,
  78.   "Look up the value of a variable in the user's shell environment.\n\
  79. The argument, a variable name, must be a string.\n\
  80. The result is either a string (the variable's value),\n\
  81.  or #F indicating that the variable does not exist.")
  82. {
  83.   PRIMITIVE_HEADER (1);
  84.   {
  85.     PSZ result;
  86.     XTD_API_CALL
  87.       (dos_scan_env, ((STRING_ARG (1)), (& result)),
  88.        {
  89.      if (rc == ERROR_ENVVAR_NOT_FOUND)
  90.        PRIMITIVE_RETURN (SHARP_F);
  91.        });
  92.     PRIMITIVE_RETURN (char_pointer_to_string (result));
  93.   }
  94. }
  95.  
  96. DEFINE_PRIMITIVE ("FILE-EQ?", Prim_file_eq_p, 2, 2,
  97.   "True iff the two file arguments are the same file.")
  98. {
  99.   PRIMITIVE_HEADER (2);
  100.   CHECK_ARG (1, STRING_P);
  101.   CHECK_ARG (2, STRING_P);
  102.   {
  103.     unsigned long length = (STRING_LENGTH (ARG_REF (1)));
  104.     const char * s1 = (STRING_LOC ((ARG_REF (1)), 0));
  105.     const char * s2 = (STRING_LOC ((ARG_REF (2)), 0));
  106.     const char * e1 = (s1 + length);
  107.     if ((STRING_LENGTH (ARG_REF (2))) != length)
  108.       PRIMITIVE_RETURN (SHARP_F);
  109.     while (s1 < e1)
  110.       if ((char_upcase (*s1++)) != (char_upcase (*s2++)))
  111.     PRIMITIVE_RETURN (SHARP_F);
  112.     PRIMITIVE_RETURN (SHARP_T);
  113.   }
  114. }
  115.  
  116. DEFINE_PRIMITIVE ("FILE-MOD-TIME", Prim_file_mod_time, 1, 1, 0)
  117. {
  118.   PRIMITIVE_HEADER (1);
  119.   {
  120.     FILESTATUS3 * info = (OS2_read_file_status (STRING_ARG (1)));
  121.     PRIMITIVE_RETURN
  122.       ((info == 0)
  123.        ? SHARP_F
  124.        : (time_to_integer ((& (info -> fdateLastWrite)),
  125.                (& (info -> ftimeLastWrite)))));
  126.   }
  127. }
  128.  
  129. DEFINE_PRIMITIVE ("FILE-ACCESS-TIME", Prim_file_acc_time, 1, 1, 0)
  130. {
  131.   PRIMITIVE_HEADER (1);
  132.   {
  133.     FILESTATUS3 * info = (OS2_read_file_status (STRING_ARG (1)));
  134.     PRIMITIVE_RETURN
  135.       ((info == 0)
  136.        ? SHARP_F
  137.        : (time_to_integer ((& (info -> fdateLastAccess)),
  138.                (& (info -> ftimeLastAccess)))));
  139.   }
  140. }
  141.  
  142. static SCHEME_OBJECT
  143. time_to_integer (FDATE * date, FTIME * time)
  144. {
  145.   unsigned long accum;
  146.   accum = (date -> year);
  147.   accum = ((accum << 4) | (date -> month));
  148.   accum = ((accum << 5) | (date -> day));
  149.   accum = ((accum << 5) | (time -> hours));
  150.   accum = ((accum << 6) | (time -> minutes));
  151.   accum = ((accum << 5) | (time -> twosecs));
  152.   return (ulong_to_integer (accum));
  153. }
  154.  
  155. DEFINE_PRIMITIVE ("SET-FILE-TIMES!", Prim_set_file_times, 3, 3,
  156.   "Change the access and modification times of FILE.\n\
  157. The second and third arguments are the respective times.\n\
  158. The file must exist and you must be the owner (or superuser).")
  159. {
  160.   PRIMITIVE_HEADER (3);
  161.   {
  162.     FILESTATUS3 * info = (OS2_read_file_status (STRING_ARG (1)));
  163.     SCHEME_OBJECT atime = (ARG_REF (2));
  164.     SCHEME_OBJECT mtime = (ARG_REF (3));
  165.     if (info == 0)
  166.       error_bad_range_arg (1);
  167.     if (atime != SHARP_F)
  168.       {
  169.     if (!INTEGER_P (atime))
  170.       error_wrong_type_arg (2);
  171.     if (integer_negative_p (atime))
  172.       error_bad_range_arg (2);
  173.     integer_to_time (atime,
  174.              (& (info -> fdateLastAccess)),
  175.              (& (info -> ftimeLastAccess)));
  176.       }
  177.     if (mtime != SHARP_F)
  178.       {
  179.     if (!INTEGER_P (mtime))
  180.       error_wrong_type_arg (3);
  181.     if (integer_negative_p (mtime))
  182.       error_bad_range_arg (3);
  183.     integer_to_time (mtime,
  184.              (& (info -> fdateLastWrite)),
  185.              (& (info -> ftimeLastWrite)));
  186.       }
  187.     OS2_write_file_status ((STRING_ARG (1)), info);
  188.   }
  189.   PRIMITIVE_RETURN (UNSPECIFIC);
  190. }
  191.  
  192. static void
  193. integer_to_time (SCHEME_OBJECT encoding, FDATE * date, FTIME * time)
  194. {
  195.   unsigned long accum = (integer_to_ulong (encoding));
  196.   (time -> twosecs) = (accum & 0x1f);
  197.   accum >>= 5;
  198.   (time -> minutes) = (accum & 0x3f);
  199.   accum >>= 6;
  200.   (time -> hours) = (accum & 0x1f);
  201.   accum >>= 5;
  202.   (date -> day) = (accum & 0x1f);
  203.   accum >>= 5;
  204.   (date -> month) = (accum & 0x0f);
  205.   accum >>= 4;
  206.   (date -> year) = accum;
  207. }
  208.  
  209. DEFINE_PRIMITIVE ("FILE-INFO", Prim_file_info, 1, 1,
  210.   "Given a file name, return information about the file.\n\
  211. If the file exists and its information is accessible,\n\
  212.  the result is a vector of 6 items.\n\
  213. Otherwise the result is #F.")
  214. {
  215.   FILESTATUS3 * info;
  216.   SCHEME_OBJECT result;
  217.   PRIMITIVE_HEADER (1);
  218.  
  219.   info = (OS2_read_file_status (STRING_ARG (1)));
  220.   if (info == 0)
  221.     PRIMITIVE_RETURN (SHARP_F);
  222.   result = (allocate_marked_vector (TC_VECTOR, 8, true));
  223.   VECTOR_SET (result, 0,
  224.           ((((info -> attrFile) & FILE_DIRECTORY) != 0)
  225.            ? SHARP_T
  226.            : SHARP_F));
  227.   VECTOR_SET (result, 1,
  228.           (time_to_integer ((& (info -> fdateLastAccess)),
  229.                 (& (info -> ftimeLastAccess)))));
  230.   VECTOR_SET (result, 2,
  231.           (time_to_integer ((& (info -> fdateLastWrite)),
  232.                 (& (info -> ftimeLastWrite)))));
  233.   VECTOR_SET (result, 3,
  234.           (time_to_integer ((& (info -> fdateCreation)),
  235.                 (& (info -> ftimeCreation)))));
  236.   VECTOR_SET (result, 4, (ulong_to_integer (info -> cbFile)));
  237.   {
  238.     unsigned int attr = (info -> attrFile);
  239.     SCHEME_OBJECT modes = (allocate_string (5));
  240.     char * s = ((char *) (STRING_LOC (modes, 0)));
  241.     (s[0]) = (((attr & FILE_DIRECTORY) != 0) ? 'd' : '-');
  242.     (s[1]) = (((attr & FILE_READONLY)  != 0) ? 'r' : '-');
  243.     (s[2]) = (((attr & FILE_HIDDEN)    != 0) ? 'h' : '-');
  244.     (s[3]) = (((attr & FILE_SYSTEM)    != 0) ? 's' : '-');
  245.     (s[4]) = (((attr & FILE_ARCHIVED)  != 0) ? 'a' : '-');
  246.     VECTOR_SET (result, 5, modes);
  247.     VECTOR_SET (result, 6, (ulong_to_integer (attr)));
  248.   }
  249.   VECTOR_SET (result, 7, (ulong_to_integer (info -> cbFileAlloc)));
  250.   PRIMITIVE_RETURN (result);
  251. }
  252.  
  253. DEFINE_PRIMITIVE ("DRIVE-TYPE", Prim_drive_type, 1, 1, 0)
  254. {
  255.   SCHEME_OBJECT arg;
  256.   char * type;
  257.   PRIMITIVE_HEADER (1);
  258.  
  259.   CHECK_ARG (1, STRING_P);
  260.   arg = (ARG_REF (1));
  261.   if (! (((STRING_LENGTH (arg)) == 1) && (isalpha (STRING_REF (arg, 0)))))
  262.     error_bad_range_arg (1);
  263.   type = (OS2_drive_type (STRING_REF (arg, 0)));
  264.   PRIMITIVE_RETURN (char_pointer_to_string ((type == 0) ? "unknown" : type));
  265. }
  266.  
  267. DEFINE_PRIMITIVE ("CURRENT-PID", Prim_current_pid, 0, 0,
  268.   "Return Scheme's PID.")
  269. {
  270.   PRIMITIVE_HEADER (0);
  271.   PRIMITIVE_RETURN (ulong_to_integer (OS2_scheme_pid));
  272. }
  273.  
  274. DEFINE_PRIMITIVE ("DOS-QUERY-MEMORY", Prim_dos_query_memory, 2, 2, 0)
  275. {
  276.   PRIMITIVE_HEADER (2);
  277.   {
  278.     ULONG start = (arg_ulong_integer (1));
  279.     ULONG length = (arg_ulong_integer (2));
  280.     ULONG flags;
  281.     XTD_API_CALL
  282.       (dos_query_mem, (((PVOID) start), (&length), (&flags)),
  283.        {
  284.      if (rc == ERROR_INVALID_ADDRESS)
  285.        PRIMITIVE_RETURN (SHARP_F);
  286.        });
  287.     PRIMITIVE_RETURN (cons ((ulong_to_integer (length)),
  288.                 (ulong_to_integer (flags))));
  289.   }
  290. }
  291.  
  292. DEFINE_PRIMITIVE ("OS2-TIME-ZONE", Prim_OS2_timezone, 0, 0, 0)
  293. {
  294.   PRIMITIVE_HEADER (0);
  295.   PRIMITIVE_RETURN (long_to_integer (OS2_timezone ()));
  296. }
  297.  
  298. DEFINE_PRIMITIVE ("OS2-DAYLIGHT-SAVINGS-TIME?", Prim_OS2_dst_p, 0, 0, 0)
  299. {
  300.   PRIMITIVE_HEADER (0);
  301.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT (OS2_daylight_savings_p ()));
  302. }
  303.  
  304. DEFINE_PRIMITIVE ("OS2-COPY-FILE", Prim_OS2_copy_file, 2, 2, 0)
  305. {
  306.   PRIMITIVE_HEADER (2);
  307.   OS_file_copy ((STRING_ARG (1)), (STRING_ARG (2)));
  308.   PRIMITIVE_RETURN (UNSPECIFIC);
  309. }
  310.  
  311. DEFINE_PRIMITIVE ("OS2-SET-REL-MAX-FH", Prim_OS2_set_rel_max_fh, 1, 1, 0)
  312. {
  313.   PRIMITIVE_HEADER (1);
  314.   {
  315.     LONG req_max_fh = (arg_integer (1));
  316.     ULONG current_max_fh;
  317.     STD_API_CALL (dos_set_rel_max_fh, ((&req_max_fh), (¤t_max_fh)));
  318.     PRIMITIVE_RETURN (ulong_to_integer (current_max_fh));
  319.   }
  320. }
  321.