520b7791c11f4739cd763022c6e3e0be9fde0022
[feisty_meow.git] / nucleus / library / basis / environment.cpp
1 //////////////
2 // Name   : environment
3 // Author : Chris Koeritz
4 //////////////
5 // Copyright (c) 1994-$now By Author.  This program is free software; you can
6 // redistribute it and/or modify it under the terms of the GNU General Public
7 // License as published by the Free Software Foundation:
8 //     http://www.gnu.org/licenses/gpl.html
9 // or under the terms of the GNU Library license:
10 //     http://www.gnu.org/licenses/lgpl.html
11 // at your preference.  Those licenses describe your legal rights to this
12 // software, and no other rights or warranties apply.
13 // Please send updates for this code to: fred@gruntose.com -- Thanks, fred.
14 //////////////
15
16 #include "environment.h"
17
18 #include <stdlib.h>
19 #include <sys/types.h>
20 #ifdef __UNIX__
21   #include <unistd.h>
22   #include <sys/times.h>
23 #endif
24 #ifdef __WIN32__
25   #define _WINSOCKAPI_  // make windows.h happy about winsock.
26   #include <windows.h>
27   #include <mmsystem.h>
28 #endif
29
30 namespace basis {
31
32 astring environment::TMP()
33 {
34   const static astring TMP_VARIABLE_NAME("TMP");
35   astring to_return = get(TMP_VARIABLE_NAME);
36   if (!to_return) {
37     // they did not see fit to set this in the environment.  let's make something up.
38 #ifdef __WIN32__
39     // windows default does not necessarily exist.
40     to_return = "c:/tmp";
41 #else
42     // most reasonable OSes have a /tmp directory.
43     to_return = "/tmp";
44 #endif
45     if (!!to_return) set("TMP", to_return);
46   }
47   return to_return;
48 }
49
50 astring environment::get(const astring &variable_name)
51 {
52 #ifdef __WIN32__
53   char *value = getenv(variable_name.upper().observe());
54     // dos & os/2 require upper case for the name, so we just do it that way.
55 #else
56   char *value = getenv(variable_name.observe());
57     // reasonable OSes support mixed-case environment variables.
58 #endif
59   astring to_return;
60   if (value)
61     to_return = astring(value);
62   return to_return;
63 }
64
65 bool environment::set(const astring &variable_name, const astring &value)
66 {
67   int ret = 0;
68 #ifdef __WIN32__
69   astring assignment = variable_name + "=" + value;
70   ret = _putenv(assignment.s());
71 #else
72   ret = setenv(variable_name.s(), value.s(), true);
73 #endif
74   return !ret;
75 }
76
77 basis::un_int environment::system_uptime()
78 {
79 #ifdef __WIN32__
80   return timeGetTime();
81 #else
82   static clock_t __ctps = sysconf(_SC_CLK_TCK);  // clock ticks per second.
83   static const double __multiplier = 1000.0 / double(__ctps);
84     // the multiplier gives us our full range for the tick counter.
85
86   // read uptime info from the OS.
87   tms uptime;
88   basis::un_int real_ticks = times(&uptime);
89
90   // now turn this into the number of milliseconds.
91   double ticks_up = (double)real_ticks;
92   ticks_up = ticks_up * __multiplier;  // convert to time here.
93
94   // we use the previous version of this calculation, which expected a basis::u_int
95   // to double conversion to provide a modulo operation rather than just leaving
96   // the basis::un_int at its maximum value (2^32-1).  however, that expectation is not
97   // guaranteed on some platforms (e.g., ARM processor with floating point
98   // emulation) and thus it becomes a bug around 49 days and 17 hours into
99   // OS uptime because the value gets stuck at 2^32-1 and never rolls over.
100   return basis::un_int(ticks_up);
101 #endif
102 }
103
104 } //namespace.
105