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 / uxenv.c < prev    next >
C/C++ Source or Header  |  2001-04-10  |  12KB  |  482 lines

  1. /* -*-C-*-
  2.  
  3. $Id: uxenv.c,v 1.21 2001/04/10 20:49:48 cph Exp $
  4.  
  5. Copyright (c) 1990-2001 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  20. USA.
  21. */
  22.  
  23. #include "ux.h"
  24. #include "osenv.h"
  25. #include "config.h"        /* For TRUE/FALSE & true/false */
  26.  
  27. time_t
  28. DEFUN_VOID (OS_encoded_time)
  29. {
  30.   time_t t;
  31.   STD_UINT_SYSTEM_CALL (syscall_time, t, (UX_time (0)));
  32.   return (t);
  33. }
  34.  
  35. void
  36. DEFUN (OS_decode_time, (t, buffer), time_t t AND struct time_structure * buffer)
  37. {
  38.   struct tm * ts;
  39.   STD_PTR_SYSTEM_CALL (syscall_localtime, ts, (UX_localtime (&t)));
  40.   (buffer -> year) = ((ts -> tm_year) + 1900);
  41.   (buffer -> month) = ((ts -> tm_mon) + 1);
  42.   (buffer -> day) = (ts -> tm_mday);
  43.   (buffer -> hour) = (ts -> tm_hour);
  44.   (buffer -> minute) = (ts -> tm_min);
  45.   (buffer -> second) = (ts -> tm_sec);
  46.   (buffer -> daylight_savings_time) = (ts -> tm_isdst);
  47. #ifdef HAVE_TM_GMTOFF
  48.   /* tm_gmtoff is in minutes east of UTC; we need minutes west.  */
  49.   (buffer -> time_zone) = (- (ts -> TM_GMTOFF));
  50.   if ((ts -> tm_isdst) > 0)
  51.     (buffer -> time_zone) += 3600;
  52. #else
  53. #ifdef HAVE_TIMEZONE
  54.   (buffer -> time_zone) = TIMEZONE;
  55. #else
  56.   (buffer -> time_zone) = INT_MAX;
  57. #endif
  58. #endif
  59.   {
  60.     /* In localtime() encoding, 0 is Sunday; in ours, it's Monday. */
  61.     int wday = (ts -> tm_wday);
  62.     (buffer -> day_of_week) = ((wday == 0) ? 6 : (wday - 1));
  63.   }
  64. }
  65.  
  66. void
  67. DEFUN (OS_decode_utc, (t, buffer), time_t t AND struct time_structure * buffer)
  68. {
  69.   struct tm * ts;
  70.   STD_PTR_SYSTEM_CALL (syscall_gmtime, ts, (UX_gmtime (&t)));
  71.   (buffer -> year) = ((ts -> tm_year) + 1900);
  72.   (buffer -> month) = ((ts -> tm_mon) + 1);
  73.   (buffer -> day) = (ts -> tm_mday);
  74.   (buffer -> hour) = (ts -> tm_hour);
  75.   (buffer -> minute) = (ts -> tm_min);
  76.   (buffer -> second) = (ts -> tm_sec);
  77.   (buffer -> daylight_savings_time) = 0;
  78.   (buffer -> time_zone) = 0;
  79.   {
  80.     /* In gmtime() encoding, 0 is Sunday; in ours, it's Monday. */
  81.     int wday = (ts -> tm_wday);
  82.     (buffer -> day_of_week) = ((wday == 0) ? 6 : (wday - 1));
  83.   }
  84. }
  85.  
  86. time_t
  87. DEFUN (OS_encode_time, (buffer), struct time_structure * buffer)
  88. {
  89. #ifdef HAVE_MKTIME
  90.   time_t t = 0;
  91.   struct tm ts;
  92.   (ts . tm_year) = ((buffer -> year) - 1900);
  93.   (ts . tm_mon) = ((buffer -> month) - 1);
  94.   (ts . tm_mday) = (buffer -> day);
  95.   (ts . tm_hour) = (buffer -> hour);
  96.   (ts . tm_min) = (buffer -> minute);
  97.   (ts . tm_sec) = (buffer -> second);
  98.   (ts . tm_isdst) = (buffer -> daylight_savings_time);
  99.   STD_UINT_SYSTEM_CALL (syscall_mktime, t, (UX_mktime (&ts)));
  100.  
  101.   /* mktime assumes its argument is local time, and converts it to
  102.      UTC; if the specified time zone is different, adjust the result.  */
  103. #ifdef HAVE_TM_GMTOFF
  104.   {
  105.     if ((buffer -> time_zone) != INT_MAX)
  106.       {
  107.     long assumed_zone = (- (ts . TM_GMTOFF));
  108.     if ((ts . tm_isdst) > 0)
  109.       assumed_zone += 3600;
  110.     if ((buffer -> time_zone) != assumed_zone)
  111.       t = ((t - assumed_zone) + (buffer -> time_zone));
  112.       }
  113.   }
  114. #else /* not HAVE_TM_GMTOFF */
  115. #ifdef HAVE_TIMEZONE
  116.   if (((buffer -> time_zone) != INT_MAX)
  117.       && ((buffer -> time_zone) != TIMEZONE))
  118.     t = ((t - TIMEZONE) + (buffer -> time_zone));
  119. #endif /* HAVE_TIMEZONE */
  120. #endif /* not HAVE_TM_GMTOFF */
  121.  
  122.   return (t);
  123.  
  124. #else /* not HAVE_MKTIME */
  125.   error_system_call (ENOSYS, syscall_mktime);
  126.   return (0);
  127. #endif /* not HAVE_MKTIME */
  128. }
  129.  
  130. static void
  131. DEFUN_VOID (initialize_timezone)
  132. {
  133. #ifdef __CYGWIN__
  134.   tzset ();
  135. #endif  
  136. }
  137.  
  138. #ifdef HAVE_TIMES
  139.  
  140. static clock_t initial_process_clock;
  141.  
  142. #ifdef __linux__
  143. /* Linux seems to record the time in an unusual way.
  144.    Time that Scheme programs spend computing do not seem to be recorded
  145.    as "user" time, but as "system" time.  So return the sum of both times.  */
  146. #define PROCESS_TIME(buffer) (((buffer) . tms_utime) + ((buffer) . tms_stime))
  147. #else
  148. #define PROCESS_TIME(buffer) ((buffer) . tms_utime)
  149. #endif
  150.  
  151. static void
  152. DEFUN_VOID (initialize_process_clock)
  153. {
  154.   struct tms buffer;
  155.   UX_times (&buffer);
  156.   initial_process_clock = (PROCESS_TIME (buffer));
  157. }
  158.  
  159. double
  160. DEFUN_VOID (OS_process_clock)
  161. {
  162.   double ct = ((double) (UX_SC_CLK_TCK ()));
  163.   struct tms buffer;
  164.   /* Was STD_VOID_SYSTEM_CALL, but at least one version of Ultrix
  165.      returns negative numbers other than -1 when there are no errors.  */
  166.   while ((UX_times (&buffer)) == (-1))
  167.     if (errno != EINTR)
  168.       error_system_call (errno, syscall_times);
  169.   return
  170.     (((((double) ((PROCESS_TIME (buffer)) - initial_process_clock)) * 2000.0)
  171.       + ct)
  172.      / (2.0 * ct));
  173. }
  174.  
  175. #else /* not HAVE_TIMES */
  176.  
  177. static void
  178. DEFUN_VOID (initialize_process_clock)
  179. {
  180. }
  181.  
  182. double
  183. DEFUN_VOID (OS_process_clock)
  184. {
  185.   /* This must not signal an error in normal use. */
  186.   return (0.0);
  187. }
  188.  
  189. #endif /* HAVE_TIMES */
  190.  
  191. #ifdef HAVE_GETTIMEOFDAY
  192.  
  193. static struct timeval initial_rtc;
  194.  
  195. static void
  196. DEFUN_VOID (initialize_real_time_clock)
  197. {
  198.   struct timezone tz;
  199.   UX_gettimeofday ((&initial_rtc), (&tz));
  200. }
  201.  
  202. double
  203. DEFUN_VOID (OS_real_time_clock)
  204. {
  205.   struct timeval rtc;
  206.   struct timezone tz;
  207.   STD_VOID_SYSTEM_CALL
  208.     (syscall_gettimeofday, (UX_gettimeofday ((&rtc), (&tz))));
  209.   return
  210.     ((((double) ((rtc . tv_sec) - (initial_rtc . tv_sec))) * 1000.0) +
  211.      ((((double) ((rtc . tv_usec) - (initial_rtc . tv_usec))) + 500.0)
  212.       / 1000.0));
  213. }
  214.  
  215. #else /* not HAVE_GETTIMEOFDAY */
  216. #ifdef HAVE_TIMES
  217.  
  218. static clock_t initial_rtc;
  219.  
  220. static void
  221. DEFUN_VOID (initialize_real_time_clock)
  222. {
  223.   struct tms buffer;
  224.   initial_rtc = (UX_times (&buffer));
  225. }
  226.  
  227. double
  228. DEFUN_VOID (OS_real_time_clock)
  229. {
  230.   double ct = ((double) (UX_SC_CLK_TCK ()));
  231.   struct tms buffer;
  232.   clock_t t;
  233.   /* Was STD_UINT_SYSTEM_CALL, but at least one version of Ultrix
  234.      returns negative numbers other than -1 when there are no errors.  */
  235.   while ((t = (UX_times (&buffer))) == (-1))
  236.     if (errno != EINTR)
  237.       error_system_call (errno, syscall_times);
  238.   return (((((double) (t - initial_rtc)) * 2000.0) + ct) / (2.0 * ct));
  239. }
  240.  
  241. #else /* not HAVE_TIMES */
  242.  
  243. static time_t initial_rtc;
  244.  
  245. static void
  246. DEFUN_VOID (initialize_real_time_clock)
  247. {
  248.   initial_rtc = (time (0));
  249. }
  250.  
  251. double
  252. DEFUN_VOID (OS_real_time_clock)
  253. {
  254.   time_t t;
  255.   STD_UINT_SYSTEM_CALL (syscall_time, t, (UX_time (0)));
  256.   return (((double) (t - initial_rtc)) * 1000.0);
  257. }
  258.  
  259. #endif /* HAVE_TIMES */
  260. #endif /* HAVE_GETTIMEOFDAY */
  261.  
  262. #ifdef HAVE_SETITIMER
  263.  
  264. static void
  265. DEFUN (set_timer, (which, first, interval),
  266.        int which AND
  267.        clock_t first AND
  268.        clock_t interval)
  269. {
  270.   struct itimerval value;
  271.   struct itimerval ovalue;
  272.   (value . it_value . tv_sec) = (first / 1000);
  273.   (value . it_value . tv_usec) = ((first % 1000) * 1000);
  274.   (value . it_interval . tv_sec) = (interval / 1000);
  275.   (value . it_interval . tv_usec) = ((interval % 1000) * 1000);
  276.   STD_VOID_SYSTEM_CALL
  277.     (syscall_setitimer, (UX_setitimer (which, (&value), (&ovalue))));
  278. }
  279.  
  280. void
  281. DEFUN (OS_process_timer_set, (first, interval),
  282.        clock_t first AND
  283.        clock_t interval)
  284. {
  285.   set_timer (ITIMER_VIRTUAL, first, interval);
  286. }
  287.  
  288. void
  289. DEFUN_VOID (OS_process_timer_clear)
  290. {
  291.   set_timer (ITIMER_VIRTUAL, 0, 0);
  292. }
  293.  
  294. void
  295. DEFUN (OS_profile_timer_set, (first, interval),
  296.        clock_t first AND
  297.        clock_t interval)
  298. {
  299.   set_timer (ITIMER_PROF, first, interval);
  300. }
  301.  
  302. void
  303. DEFUN_VOID (OS_profile_timer_clear)
  304. {
  305.   set_timer (ITIMER_PROF, 0, 0);
  306. }
  307.  
  308. void
  309. DEFUN (OS_real_timer_set, (first, interval),
  310.        clock_t first AND
  311.        clock_t interval)
  312. {
  313.   set_timer (ITIMER_REAL, first, interval);
  314. }
  315.  
  316. void
  317. DEFUN_VOID (OS_real_timer_clear)
  318. {
  319.   set_timer (ITIMER_REAL, 0, 0);
  320. }
  321.  
  322. #else /* not HAVE_SETITIMER */
  323.  
  324. static unsigned int alarm_interval;
  325.  
  326. void
  327. DEFUN_VOID (reschedule_alarm)
  328. {
  329.   UX_alarm (alarm_interval);
  330. }
  331.  
  332. void
  333. DEFUN (OS_process_timer_set, (first, interval),
  334.        clock_t first AND
  335.        clock_t interval)
  336. {
  337.   error_unimplemented_primitive ();
  338. }
  339.  
  340. void
  341. DEFUN_VOID (OS_process_timer_clear)
  342. {
  343.   return;
  344. }
  345.  
  346. void
  347. DEFUN (OS_profile_timer_set, (first, interval),
  348.        clock_t first AND
  349.        clock_t interval)
  350. {
  351.   error_unimplemented_primitive ();
  352. }
  353.  
  354. void
  355. DEFUN_VOID (OS_profile_timer_clear)
  356. {
  357.   return;
  358. }
  359.  
  360. void
  361. DEFUN (OS_real_timer_set, (first, interval),
  362.        clock_t first AND
  363.        clock_t interval)
  364. {
  365.   alarm_interval = ((interval + 999) / 1000);
  366.   UX_alarm ((first + 999) / 1000);
  367. }
  368.  
  369. void
  370. DEFUN_VOID (OS_real_timer_clear)
  371. {
  372.   alarm_interval = 0;
  373.   UX_alarm (0);
  374. }
  375.  
  376. #endif /* HAVE_SETITIMER */
  377.  
  378. void
  379. DEFUN_VOID (UX_initialize_environment)
  380. {
  381.   initialize_timezone ();
  382.   initialize_process_clock ();
  383.   initialize_real_time_clock ();
  384. #ifndef HAVE_SETITIMER
  385.   alarm_interval = 0;
  386. #endif
  387. }
  388.  
  389. static size_t current_dir_path_size = 0;
  390. static char * current_dir_path = 0;
  391.  
  392. CONST char *
  393. DEFUN_VOID (OS_working_dir_pathname)
  394. {
  395.   if (current_dir_path) {
  396.     return (current_dir_path);
  397.   }
  398.   if (current_dir_path_size == 0)
  399.     {
  400.       current_dir_path = (UX_malloc (1024));
  401.       if (current_dir_path == 0)
  402.     error_system_call (ENOMEM, syscall_malloc);
  403.       current_dir_path_size = 1024;
  404.     }
  405.   while (1)
  406.     {
  407.       if ((UX_getcwd (current_dir_path, current_dir_path_size)) != 0)
  408.     return (current_dir_path);
  409.       if (errno != ERANGE)
  410.     error_system_call (errno, syscall_getcwd);
  411.       current_dir_path_size *= 2;
  412.       {
  413.     char * new_current_dir_path =
  414.       (UX_realloc (current_dir_path, current_dir_path_size));
  415.     if (new_current_dir_path == 0)
  416.       /* ANSI C requires `path' to be unchanged -- we may have to
  417.          discard it for systems that don't behave thus. */
  418.       error_system_call (ENOMEM, syscall_realloc);
  419.     current_dir_path = new_current_dir_path;
  420.       }
  421.     }
  422. }
  423.  
  424. void
  425. DEFUN (OS_set_working_dir_pathname, (name), CONST char * name)
  426. {
  427.   size_t name_size = strlen (name);
  428.   STD_VOID_SYSTEM_CALL (syscall_chdir, (UX_chdir (name)));
  429.   while (1) {
  430.     if (name_size < current_dir_path_size) {
  431.       strcpy(current_dir_path, name);
  432.       return;
  433.     } 
  434.     current_dir_path_size *= 2;
  435.     {
  436.       char * new_current_dir_path =
  437.     (UX_realloc (current_dir_path, current_dir_path_size));
  438.       if (new_current_dir_path == 0)
  439.     error_system_call (ENOMEM, syscall_realloc);
  440.       current_dir_path = new_current_dir_path;
  441.     }
  442.   }
  443. }
  444.  
  445. CONST char *
  446. DEFUN_VOID (OS_current_user_name)
  447. {
  448.   {
  449.     CONST char * result = (UX_getlogin ());
  450.     if ((result != 0) && (*result != '\0'))
  451.       return (result);
  452.   }
  453.   {
  454.     struct passwd * entry = (UX_getpwuid (UX_geteuid ()));
  455.     if (entry != 0)
  456.       return (entry -> pw_name);
  457.   }
  458.   error_external_return ();
  459.   return (0);
  460. }
  461.  
  462. CONST char *
  463. DEFUN_VOID (OS_current_user_home_directory)
  464. {
  465.   {
  466.     char * user_name = (UX_getlogin ());
  467.     if (user_name != 0)
  468.       {
  469.     struct passwd * entry = (UX_getpwnam (user_name));
  470.     if (entry != 0)
  471.       return (entry -> pw_dir);
  472.       }
  473.   }
  474.   {
  475.     struct passwd * entry = (UX_getpwuid (UX_getuid ()));
  476.     if (entry != 0)
  477.       return (entry -> pw_dir);
  478.   }
  479.   error_external_return ();
  480.   return (0);
  481. }
  482.