00001
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <sys/time.h>
00015 #include <sys/timeb.h>
00016 #include <sys/types.h>
00017 #include <stdlib.h>
00018 #include <stdio.h>
00019 #include <time.h>
00020 #include <pwd.h>
00021 #include <unistd.h>
00022
00023 #include "CTB.hxx"
00024 #include "CTBcstringBase.hxx"
00025 #include "CTBcstring.hxx"
00026
00027
00028 #include "CTBsystem.hxx"
00029
00030
00031 #if defined(_AIX) | defined(__GNUC__)
00032 #define true_clk_tck CLOCKS_PER_SEC
00033 #else
00034 #define true_clk_tck CLK_TCK
00035 #endif
00036
00037 #if defined(__VMS) & !defined(CTB__GenericCode)
00038 #pragma message disable DOLLARID
00039 #include <starlet.h>
00040 #include <lib$routines.h>
00041 #include <jpidef.h>
00042 #endif
00043
00049
00051
00052 #if defined(__VMS) && !defined(CTB__GenericCode)
00053 double CTBsystem::CpuTime()
00054 {
00055 int i_rc;
00056 CTBint32 i_item_code = JPI$_CPUTIM;
00057 CTBuint32 i_cputicks;
00058
00059 i_rc = lib$getjpi(&i_item_code,0,0,&i_cputicks,0,0);
00060 if (!(i_rc & 0x1)) throw CTBexception();
00061 return 1.e-2 * (double) i_cputicks;
00062 }
00063
00064 #else
00065 double CTBsystem::CpuTime()
00066 {
00067 return (double) clock() / (double) true_clk_tck;
00068 }
00069
00070 #endif
00071
00072
00074
00075 #if defined(__VMS) && !defined(CTB__GenericCode)
00076 double CTBsystem::ElapsedTime()
00077 {
00078 int i_rc;
00079 CTBuint32 i_vmstime[2];
00080
00081 i_rc = sys$gettim(&i_vmstime);
00082 if (!(i_rc & 0x1)) throw CTBexception();
00083 return 1.e-7 * ((double) i_vmstime[0] + 4294967296. * (double) i_vmstime[1]);
00084 }
00085
00086 #else
00087
00088 double CTBsystem::ElapsedTime()
00089 {
00090 struct timeb s_time;
00091
00092 ftime(&s_time);
00093 return (double) s_time.time + 1.e-3 * (double) s_time.millitm;
00094 }
00095 #endif
00096
00100
00101
00103
00110 int CTBsystem::SelectRead(int i_fd, float f_timeout)
00111 {
00112 struct timeval timeout;
00113 struct timeval* p_timeout = 0;
00114
00115 fd_set fdset;
00116
00117 if (i_fd < 0) return -1;
00118
00119 if (f_timeout >= 0.) {
00120 timeout.tv_sec = (int) f_timeout;
00121 timeout.tv_usec = (int) (1000000. * (f_timeout - (float) timeout.tv_sec));
00122 p_timeout = &timeout;
00123 }
00124
00125 FD_ZERO(&fdset);
00126 FD_SET(i_fd,&fdset);
00127
00128 return select(i_fd+1,&fdset,0,0,p_timeout);
00129 }
00130
00131
00133
00140 int CTBsystem::SelectWrite(int i_fd, float f_timeout)
00141 {
00142 struct timeval timeout;
00143 struct timeval* p_timeout = 0;
00144
00145 fd_set fdset;
00146
00147 if (i_fd < 0) return -1;
00148
00149 if (f_timeout >= 0.) {
00150 timeout.tv_sec = (int) f_timeout;
00151 timeout.tv_usec = (int) (1000000. * (f_timeout - (float) timeout.tv_sec));
00152 p_timeout = &timeout;
00153 }
00154
00155 FD_ZERO(&fdset);
00156 FD_SET(i_fd,&fdset);
00157
00158 return select(i_fd+1,0,&fdset,0,p_timeout);
00159 }
00160
00161
00163
00170 void CTBsystem::Sleep(float f_time)
00171 {
00172 float f_trest = f_time;
00173 unsigned long i_utime;
00174
00175 if (f_time <= 0.) return;
00176 while (f_trest > 0.) {
00177 if (f_trest > 1800.) {
00178 i_utime = 1800 * 1000000;
00179 f_trest -= 1800.;
00180 } else {
00181 i_utime = (unsigned long) (1000000. * f_trest);
00182 f_trest = 0.;
00183 }
00184 usleep(i_utime);
00185 }
00186 return;
00187 }
00188
00189
00191
00196 void CTBsystem::ExpandFilename(char* c_dst, int i_size, const char* c_src)
00197 {
00198 CTBcstring<FILENAME_MAX> c_field;
00199 CTBcstring<FILENAME_MAX> c_output;
00200 int i_nfield = CTBcstringBase::NField(c_src,-1,"/");
00201
00202 for (int i = 0; i < i_nfield; i++) {
00203 if (i != 0) c_output += "/";
00204 c_field.CopyField(c_src,i,"/");
00205
00206 if (i == 0 && c_field[0] == '~') {
00207
00208 #if !defined(__VMS) || (__CRTL_VER >= 70000000)
00209 struct passwd* p_pwd = 0;
00210 if (c_field.Length() == 1) {
00211 char* c_user = getenv("USER");
00212 if (c_user != 0) p_pwd = getpwnam(c_user);
00213 } else {
00214 p_pwd = getpwnam(&c_field[1]);
00215 }
00216 c_output += (p_pwd != 0) ? p_pwd->pw_dir : (char *) c_field;
00217 #else
00218 c_output += c_field;
00219 #endif
00220
00221 } else if (c_field[0] == '$') {
00222 char* c_value = getenv(&c_field[1]);
00223 c_output += (c_value != 0) ? c_value : (char*) c_field;
00224
00225 } else {
00226 c_output += c_field;
00227 }
00228 }
00229
00230 CTBcstringBase::Copy(c_dst,i_size,c_output);
00231
00232 return;
00233 }
00234
00235
00236
00237
00238