1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
|
/*
** wsys.c System / OS-dependent routines
**
** Part of willus.com general purpose C code library.
**
** Copyright (C) 2021 http://willus.com
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
*/
#include "willus.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <locale.h>
#ifdef HAVE_WIN32_API
#include <windows.h>
#endif
#ifdef UNIX
#include <errno.h>
#include <unistd.h>
#endif
#include <fcntl.h> /* MinGW has this -- using for file locking */
/*
Digital Mars: __DMC__ == 0x700 (7.0) 0x720 (7.2) 0x800 (8.0)
*/
/*
OS/X predefines:
#define __MACH__ 1
#define __NATURAL_ALIGNMENT__ 1
#define __APPLE__ 1
#define __GNUC_MINOR__ 95
#define __ppc__ 1
#define __GNUC__ 2
#define __STDC__ 1
#define __DYNAMIC__ 1
#define __APPLE_CC__ 926
#define __BIG_ENDIAN__ 1
*/
#if (defined(__GNUC__))
static void gnu_compiler(char *compiler_version);
#endif
static int wsys_decimal_is_period(void);
void wsys_system_version(char *system,char *_os,char *_chip,char *_compiler)
{
char compiler_version[80],compname[100];
int ccode,oscode,chipcode;
static char *compiler[] = {"Unknown C compiler","Watcom C","Gnu C",
"HPUX ANSI C","Sun C","Cray C","Turbo C",
"Microsoft Visual C++","Gnu C (DJGPP)",
"Gnu C (RSXNT/EMX)","Intel C/C++","HPUX C++",
"Digital Mars C/C++","LCC","Watcom C/C++",
"Borland C/C++","Gnu C (Mingw32)",
"Intel C++ for Linux","Gnu C (Mingw64)","Tiny CC",
"CLang (Mingw64)","CLang (Mingw32)","CLang"};
static char *os[] = {"Unknown O/S","Unix","VMS","Unicos","SunOS","HPUX",
"MS-DOS","Win32","MS-DOS (32-bit)","OS/X",
"Linux","SuSE Linux","Win64"};
static char *chip[] = {"Unknown architecture","CRAY2","CRAY","hppa 1.0",
"hppa 1.1","sparc","i386","hppa 2.0","PPC","x64","ARM64"};
ccode=0;
oscode=0;
chipcode=0;
compiler_version[0]='\0';
/* O/S's (more general) */
#if (defined(UNIX))
oscode=1;
#elif (defined(__vax__) || defined(__vax) || defined(__vms))
oscode=2;
#endif
/* Specific O/S's */
#if (defined(WIN64))
oscode=12;
#elif (defined(WIN32))
if (wsys_win32_api())
oscode=7;
else
oscode=8;
chipcode=6;
#elif (defined(MSDOS32))
oscode=8;
chipcode=6;
#elif (defined(MSDOS))
oscode=6;
chipcode=6;
#elif (defined(__linux) && defined(__VERSION__))
if (in_string(__VERSION__,"SuSE")>=0)
oscode=11;
else
oscode=10;
#elif (defined(__linux))
oscode=10;
#elif (defined(__sun) || defined(sun))
oscode=4;
ccode=4;
#elif (defined(hpux) || defined(__hpux))
ccode=3;
oscode=5;
ccode=3;
#if (defined(__cplusplus))
ccode=11;
#endif
#elif (defined(_CRAY))
ccode=5;
oscode=3;
chipcode=2;
#elif (defined(_CRAY2))
ccode=5;
oscode=3;
chipcode=1;
#elif (defined(__ppc__) || defined(__MACH__))
oscode=9;
#endif
/* Specific chips */
#if (defined(i386) || defined(__i386__) || defined(__i386) || defined(__386__) || defined(_M_IX86))
chipcode=6;
#elif (defined(__x86_64) || defined(__amd64__))
chipcode=9;
#elif (defined(__sparc) || defined(sparc))
chipcode=5;
#elif (defined(_PA_RISC1_1))
chipcode=4;
#elif (defined(_PA_RISC2_0))
chipcode=7;
#elif (defined(__ppc__))
chipcode=8;
#elif (defined(__arm64__))
chipcode=10;
#endif
/* Specific compilers */
#if (defined(__DMC__))
ccode=12;
sprintf(compiler_version,"v%d.%d",((__DMC__ &0xf00)>>8),((__DMC__ &0xf0)>>4));
#elif (defined(__LCC__))
ccode=13;
strcpy(compiler_version,"3.7");
#elif (defined(__BORLANDC__))
ccode=15;
sprintf(compiler_version,"v%d.%d.%d",
(__BORLANDC__ >> 8)&0xf,
(__BORLANDC__ >> 4)&0xf,
(__BORLANDC__ )&0xf);
#elif (defined(__WATCOMC__))
ccode=14;
sprintf(compiler_version,"%.2f",(double)__WATCOMC__/100.);
#elif (defined(DJGPP))
ccode=8;
gnu_compiler(compiler_version);
#elif (defined(__MINGW64__))
#if (defined(__clang__))
ccode=20;
sprintf(compiler_version,"v%d.%d.%d",__clang_major__,__clang_minor__,__clang_patchlevel__);
#else
ccode=18;
gnu_compiler(compiler_version);
#endif
#elif (defined(__MINGW32__))
#if (defined(__clang__))
ccode=21;
sprintf(compiler_version,"v%d.%d.%d",__clang_major__,__clang_minor__,__clang_patchlevel__);
#else
ccode=16;
gnu_compiler(compiler_version);
#endif
#elif (defined(EMX))
ccode=9;
gnu_compiler(compiler_version);
#elif (defined(__clang__))
ccode=22;
sprintf(compiler_version,"v%d.%d.%d",__clang_major__,__clang_minor__,__clang_patchlevel__);
#elif (defined(__GNUC__))
ccode=2;
gnu_compiler(compiler_version);
#elif (defined(__TURBOC__))
ccode=6;
#elif (defined(__ICL))
ccode=10;
sprintf(compiler_version,"v%4.2f",__ICL/100.);
#elif (defined(_MSC_VER))
ccode=7;
sprintf(compiler_version,"v%4.2f",_MSC_VER/100.-6.0);
#elif (defined(__386__))
ccode=1;
#elif (defined(__TINYC__))
ccode=19;
compiler_version[0]='\0';
#elif (defined(__INTEL_COMPILER))
#if (defined(__linux__))
ccode=17;
#else
ccode=10;
#endif
sprintf(compiler_version,"v%4.2f",__INTEL_COMPILER/100.);
#endif
if (compiler_version[0]!='\0')
sprintf(compname,"%s %s",compiler[ccode],compiler_version);
else
strcpy(compname,compiler[ccode]);
if (system!=NULL)
sprintf(system,"%s, %s, %s",os[oscode],chip[chipcode],compname);
if (_os!=NULL)
strcpy(_os,os[oscode]);
if (_chip!=NULL)
strcpy(_chip,chip[chipcode]);
if (_compiler!=NULL)
strcpy(_compiler,compname);
}
#if (defined(__GNUC__))
static void gnu_compiler(char *compiler_version)
{
#if (defined(__GNUC_PATCHLEVEL__))
sprintf(compiler_version,"v%d.%d.%d",__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__);
#else
sprintf(compiler_version,"v%d.%d.x",__GNUC__,__GNUC_MINOR__);
#endif
}
#endif
int wsys_win32_api(void)
{
#if (defined(DJEMXWIN32))
/* extern int _emx_env,_emx_rev; */
if (_emx_env & 0x1000L)
return((_emx_rev>>16)==2);
else
return(0);
#elif (defined(HAVE_WIN32_API))
return(1);
#else
return(0);
#endif
}
/*
** Return the status of a process.
**
** This function has only been tested on Win32 and HPUX as of 2-5-2002.
** Only returns WPID_RUNNING or WPID_NO_PROCESS (can't determine if
** process is sleeping yet).
*/
int wsys_wpid_status(int pid)
{
#ifdef HAVE_WIN32_API
HANDLE prochandle;
prochandle = OpenProcess(PROCESS_QUERY_INFORMATION,0,pid);
if (prochandle==NULL)
{
/* Typically, GetLastError() here will return */
/* a value of 87--Incorrect Parameter */
/*
printf("open process fails, last error = %d\n",status);
printf(" = %s\n",win_lasterror());
*/
return(WPID_NO_PROCESS);
}
CloseHandle(prochandle);
return(WPID_RUNNING);
/*
status = GetExitCodeProcess(prochandle,&exitcode);
if (status==0)
{
printf("Error from GetExitCodeProcess = %s\n",win_lasterror());
return(WPID_UNKNOWN);
}
printf("Proc exit code = %d (STILL_ACTIVE=%d)\n",exitcode,STILL_ACTIVE);
if (exitcode==STILL_ACTIVE)
return(WPID_RUNNING);
return(WPID_NO_PROCESS);
*/
#else
#ifdef UNIX
int status;
status = getpgid(pid);
if (status < 0 && errno==ESRCH)
{
/*
printf("errno=%d, ESRCH=%d, EPERM (not in same session)=%d\n",errno,ESRCH,EPERM);
*/
return(WPID_NO_PROCESS);
}
return(WPID_RUNNING);
#else
return(WPID_UNKNOWN);
#endif /* UNIX */
#endif /* HAVE_WIN32_API */
}
void wsys_sleep(int secs)
{
#ifdef HAVE_WIN32_API
win_sleep(secs*1000);
#else
sleep(secs);
#endif
}
void wsys_sleep_ms(int ms)
{
#ifdef HAVE_WIN32_API
win_sleep(ms);
#else
usleep(ms*1000);
#endif
}
int wsys_num_cpus(void)
{
#ifdef HAVE_WIN32_API
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return(sysinfo.dwNumberOfProcessors);
#else
return((int)sysconf(_SC_NPROCESSORS_ONLN));
#endif
}
char *wsys_full_exe_name(char *s)
{
#ifdef HAVE_WIN32_API
return(win_full_exe_name(s));
#else
if (!wfile_is_symlink_ex("/proc/self/exe",s))
s[0]='\0';
return(s);
#endif
}
void wsys_append_nul_redirect(char *s)
{
#if (defined(WIN32) || defined(WIN64))
strcat(s," 1> nul 2> nul");
#else
strcat(s," > /dev/null");
#endif
}
int wsys_which(char *exactname,char *exename)
{
#ifdef HAVE_WIN32_API
return(win_which(exactname,exename));
#else
#if (defined(LINUX) || defined(UNIXPURE))
return(linux_which(exactname,exename));
#else
return(0);
#endif
#endif
}
#ifndef NO_FILELIST
int wsys_most_recent_in_path(char *exename,char *wildcard)
{
#ifdef HAVE_WIN32_API
return(win_most_recent_in_path(exename,wildcard));
#else
#if (defined(LINUX) || defined(UNIXPURE))
return(linux_most_recent_in_path(exename,wildcard));
#else
return(0);
#endif
#endif
}
#endif
void wsys_computer_name(char *name,int maxlen)
{
#ifdef HAVE_WIN32_API
int n;
n=maxlen;
GetComputerName(name,(LPDWORD)&n);
#else
gethostname(name,maxlen);
#endif
}
void wsys_enter_to_exit(char *mesg)
{
#ifdef HAVE_WIN32_API
if (win_has_own_window())
#endif
{
char buf[16];
if (mesg==NULL || mesg[0]=='\0')
printf("Press <ENTER> to exit.");
else
aprintf("%s",mesg);
fgets(buf,15,stdin);
}
}
int wsys_filename_8dot3(char *dst,char *src,int maxlen)
{
#ifdef HAVE_WIN32_API
return(GetShortPathName(src,dst,maxlen));
#else
strncpy(dst,src,maxlen-1);
dst[maxlen-1]='\0';
return(strlen(dst));
#endif
}
double wsys_year_double(struct tm *date)
{
return(1900.+date->tm_year
+date->tm_mon/12.
+((date->tm_mday-1.0)/365.25)
+date->tm_hour/(365.25*24.)
+date->tm_min/(365.25*24.*60.)
+date->tm_sec/(365.25*24.*3600.));
}
double wsys_utc_offset(void)
{
time_t now;
struct tm t1,t2;
double tz;
time(&now);
t1=(*localtime(&now));
t2=(*gmtime(&now));
tz=wfile_date_diff(&t1,&t2)/3600.;
return(tz);
}
char *wsys_utc_string(void)
{
double tz;
int c,hr,min;
static char buf[32];
tz = wsys_utc_offset();
if (tz<0)
{
tz = -tz;
c='-';
}
else
c='+';
hr = (int)(tz+1e-6);
min=(tz-hr)*60;
min = (min+8)/15;
min *= 15;
sprintf(buf,"%c%02d'%02d",c,hr%24,min);
return(buf);
}
/*
** Call with 1 to set decimal point to a period.
** Call with 0 to restore locale to original value.
*/
int wsys_set_decimal_period(int setit)
{
static int sys_setlocale=0;
static char orglocale[64];
static char *localenames[] = {"C","en_US","en_us","English_United States","English",
"english",""};
int i;
char *p;
if (!setit)
{
if (sys_setlocale)
setlocale(LC_NUMERIC,orglocale);
return(1);
}
if (wsys_decimal_is_period())
return(1);
/* Store original locale */
if (!sys_setlocale)
{
p=setlocale(LC_NUMERIC,NULL);
strncpy(orglocale,p==NULL?"":p,63);
orglocale[63]='\0';
sys_setlocale=1;
}
/* Try new locales until the decimal point is a period. */
for (i=0;1;i++)
{
p=setlocale(LC_NUMERIC,localenames[i]);
if (wsys_decimal_is_period())
return(1);
if (localenames[i][0]=='\0')
break;
}
setlocale(LC_NUMERIC,orglocale);
return(0);
}
static int wsys_decimal_is_period(void)
{
double x;
char buf[16];
x=12.34;
sprintf(buf,"%5.2f",x);
if (buf[2]!='.')
return(0);
strcpy(buf,"12.34");
return(fabs(atof(buf)-x) < .01);
}
/*
** Returns 0 for okay, negative value for error.
** Only implemented in Windows for now.
** system==NZ for system level var, 0 for user var.
**
** 7-20-2014: Only use Windows calls in Windows.
*/
int wsys_set_envvar(char *varname,char *value,int system)
{
#ifdef HAVE_WIN32_API
int status;
HKEY newkey;
#else
char *estr;
static char *funcname="wsys_set_envvar";
int assign;
#endif
#if (!defined(HAVE_WIN32_API))
/* Linux / OSX */
assign=1;
if (value[0]=='\0')
{
estr=getenv(varname);
if (estr==NULL);
assign=0;
}
if (assign)
{
willus_mem_alloc_warn((void **)&estr,strlen(varname)+strlen(value)+16,funcname,10);
sprintf(estr,"%s=%s",varname,value);
putenv(estr);
willus_mem_free((double **)&estr,funcname);
}
#else
/* Make permanent by changing Windows registry */
if (system)
status=RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",0,KEY_WRITE,&newkey);
else
status=RegOpenKeyEx(HKEY_CURRENT_USER,"Environment",0,KEY_WRITE,&newkey);
if (status!=ERROR_SUCCESS)
return(-1);
if (value[0]!='\0')
status=RegSetValueEx(newkey,varname,0,REG_SZ,(unsigned char *)value,strlen(value)+1);
else
status=RegDeleteValue(newkey,varname);
if (status!=ERROR_SUCCESS)
return(-1);
RegCloseKey(newkey);
#endif /* WIN32 || WIN64 */
return(0);
}
/*
** Return 0 for okay, negative for error.
**
** If windows, uses registry first.
**
** 7-20-2014: Only use Windows functions for env var in Windows.
**
*/
int wsys_get_envvar_ex(char *varname,char *value,int maxlen)
{
char *p;
#ifdef HAVE_WIN32_API
int i,status;
HKEY newkey;
status=RegOpenKeyEx(HKEY_CURRENT_USER,"Environment",0,KEY_READ,&newkey);
if (status==ERROR_SUCCESS)
{
for (i=0;1;i++)
{
int size,valuesize,type;
char buf[1024];
char valuename[256];
size=1023;
valuesize=255;
status=RegEnumValue(newkey,(DWORD)i,valuename,(LPDWORD)&valuesize,(LPDWORD)NULL,
(LPDWORD)&type,(LPBYTE)buf,(LPDWORD)&size);
if (status==ERROR_SUCCESS)
{
if (!stricmp(valuename,varname))
{
strncpy(value,buf,maxlen-1);
value[maxlen-1]='\0';
RegCloseKey(newkey);
return(0);
}
}
else
break;
}
RegCloseKey(newkey);
}
return(-10);
#endif
value[0]='\0';
p=getenv(varname);
if (p==NULL)
return(-10);
strncpy(value,p,maxlen-1);
value[maxlen-1]='\0';
return(0);
}
/*
** Returns -1 for no lock, otherwise, file lock obtained
** and returns file descriptor.
*/
int wsys_file_lock(char *filename)
{
return(open(filename,O_CREAT|O_EXCL,0644));
}
/*
** Returns -1 or -2 for failure
** Returns 0 for file correctly unlocked
*/
int wsys_file_unlock(char *filename,int fd)
{
int status;
status=close(fd);
if (status!=0)
return(-1);
status=remove(filename);
if (status!=0)
return(-2);
return(0);
}
/*
** use NULL to send output to /dev/null or nul
** use "" to send output to stdout/stderr (no redirect)
*/
int wsys_shell_command(char *cmd,char *stdoutfile,char *stderrfile)
{
static char *funcname="wsys_shell_command";
char *syscmd;
int status;
#ifdef HAVE_WIN32_API
static char *nullname="nul";
#else
static char *nullname="/dev/null";
#endif
syscmd=NULL;
willus_mem_alloc_warn((void **)&syscmd,strlen(cmd)
+(stdoutfile==NULL?strlen(nullname):strlen(stdoutfile))
+(stderrfile==NULL?strlen(nullname):strlen(stderrfile))+32,funcname,10);
/* I originally had this code in for unix, but it seems that even if
you shell out from a csh-type shell, the redirects still need to
use bourne-shell style (1> and 2>).
{
char *p;
p=getenv("SHELL");
if (p!=NULL && strlen(p)>3 && !strcmp(&p[strlen(p)-3],"csh"))
{
strcat(cmd," >>& ");
strcat(cmd,logfile);
}
else
{
strcat(cmd," 1>> ");
strcat(cmd,logfile);
strcat(cmd," 2>> ");
strcat(cmd,errfile);
}
}
*/
strcpy(syscmd,cmd);
if (stdoutfile==NULL || stdoutfile[0]!='\0')
{
strcat(syscmd," 1> \"");
strcat(syscmd,stdoutfile==NULL?nullname:stdoutfile);
strcat(syscmd,"\"");
}
if (stderrfile==NULL || stderrfile[0]!='\0')
{
strcat(syscmd," 2> \"");
strcat(syscmd,stderrfile==NULL?nullname:stderrfile);
strcat(syscmd,"\"");
}
status=system(syscmd);
willus_mem_free((double **)&syscmd,funcname);
return(status);
}
|