jsystem.cpp
13.2 KB
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
/***************************************************************************
* Copyright (C) 2005 by Jeff Ferr *
* root@sat *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "Stdafx.h"
#include "jsystem.h"
#include "jruntimeexception.h"
#include "jinvalidargumentexception.h"
#include <sstream>
#ifdef _WIN32
#else
#include <sys/vfs.h>
#endif
#ifndef CLOCK_TICK_RATE
#define CLOCK_TICK_RATE 1193180
#endif
#define DEFAULT_FREQ 440 /* Middle A */
#define DEFAULT_DELAY 100 /* milliseconds */
namespace jcommon {
bool _keyboard;
#ifdef _WIN32
#else
struct termios g_old_kbd_mode;
int console = -1;
bool init = false;
static void cooked()
{
tcsetattr(0, TCSANOW, &g_old_kbd_mode);
if(console > 0) {
ioctl(console, KIOCSOUND, 0);
close(console);
}
}
#endif
System::~System()
{
#ifdef _WIN32
#else
cooked();
#endif
}
void System::Beep(int freq, int delay)
{
#ifdef _WIN32
Beep(freq, delay);
#else
if (freq < 0) {
freq = DEFAULT_FREQ;
}
if (delay < 0) {
delay = DEFAULT_DELAY;
}
if ((console = open("/dev/console", O_WRONLY)) == -1) {
printf("\a");
return;
}
if (ioctl(console, KIOCSOUND, (int)(CLOCK_TICK_RATE/freq)) < 0) {
printf("\a");
return;
}
usleep(1000*delay);
ioctl(console, KIOCSOUND, 0);
close(console);
console = -1;
#endif
}
std::string System::GetCurrentUserName()
{
#ifdef _WIN32
char name[256];
DWORD r = 256;
GetUserName(name , &r);
return name;
#else
struct passwd *pw;
pw = getpwuid(0);
if (pw != NULL) {
// WARNNING:: free pointer
return pw->pw_name;
}
return "";
#endif
}
void System::Exit(int i)
{
exit(i);
}
void System::Abort()
{
abort();
}
int System::GetUserID()
{
#ifdef _WIN32
return 0;
#else
return (int)getuid();
#endif
}
int System::GetProcessID()
{
#ifdef _WIN32
return 0;
#else
return (int)getuid();
#endif
}
std::string System::GetResourceDirectory()
{
return _DATA_PREFIX;
}
std::string System::GetHomeDirectory()
{
#ifdef _WIN32
// CHANGE:: procurar por getuserdirectory ou algo parecido
char name[256];
DWORD r = 256;
GetWindowsDirectory( name , r);
return name;
#else
struct passwd *pw;
pw = getpwuid(0);
if (pw != NULL) {
return pw->pw_dir;
}
#endif
return "";
}
std::string System::GetCurrentDirectory()
{
#ifdef _WIN32
char buffer[_MAX_PATH];
DWORD n;
n = ::GetCurrentDirectory((DWORD)sizeof(buffer), (LPSTR)buffer);
if (n < 0 || n > sizeof(buffer)) {
throw FileException("Cannot return the path");
}
std::string result(buffer, n);
if (result[n - 1] != '\\') {
result.append("\\");
}
return std::string(result);
#else
char path[65536];
if (getcwd(path, 65536) == NULL) {
throw RuntimeException(strerror(errno));
}
return path;
#endif
}
std::string System::GetEnviromentVariable(std::string key_, std::string default_)
{
#ifdef _WIN32
char name[4096];
GetEnvironmentVariable(key_.c_str(), name, 4096);
if (name == NULL) {
return default_;
}
return name;
#else
// CHANGE:: man environ
char *var = getenv(key_.c_str());
if (var == NULL) {
return default_;
}
return var;
#endif
}
void System::UnsetEnviromentVariable(std::string key_)
{
#ifdef _WIN32
SetEnvironmentVariable(key_.c_str(), "");
#else
unsetenv(key_.c_str());
#endif
}
int System::SetEnviromentVariable(std::string key_, std::string value_, bool overwrite_)
{
#ifdef _WIN32
SetEnvironmentVariable(key_.c_str(), value_.c_str());
#else
return setenv(key_.c_str(), value_.c_str(), overwrite_);
#endif
}
int System::GetLastErrorCode()
{
#ifdef _WIN32
return GetLastError();
#else
return errno;
#endif
}
std::string System::GetLastErrorMessage()
{
#ifdef _WIN32
DWORD code = GetLastErrorCode();
char *msg;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
0, // no source buffer needed
code, // error code for this message
0, // default language ID
(LPTSTR)&msg, // allocated by fcn
0, // minimum size of buffer
(va_list *)NULL); // no inserts
return msg;
#else
return strerror(GetLastErrorCode());
#endif
}
void System::EnableKeyboardBuffer(bool b)
{
#ifdef _WIN32
#else
struct termios new_kbd_mode;
static char init;
if (init == false) {
init = true;
tcgetattr(0, &g_old_kbd_mode);
atexit(cooked); // when we exit, go back to normal, "cooked" mode
}
// put keyboard (stdin, actually) in raw, unbuffered mode
memcpy(&new_kbd_mode, &g_old_kbd_mode, sizeof(struct termios));
if (b == true) {
new_kbd_mode.c_lflag |= (ICANON | ECHO);
new_kbd_mode.c_cc[VTIME] = 0;
new_kbd_mode.c_cc[VMIN] = 1;
tcsetattr(0, TCSANOW, &new_kbd_mode);
} else {
new_kbd_mode.c_lflag &= ~(ICANON | ECHO);
new_kbd_mode.c_cc[VTIME] = 0;
new_kbd_mode.c_cc[VMIN] = 1;
tcsetattr(0, TCSANOW, &new_kbd_mode);
}
_keyboard = b;
#endif
}
void System::EnableEcho(bool b)
{
#ifdef _WIN32
// TODO::
#else
struct termios new_kbd_mode;
static char init;
if (init == false) {
init = true;
tcgetattr(0, &g_old_kbd_mode);
atexit(cooked); // when we exit, go back to normal, "cooked" mode
}
// put keyboard (stdin, actually) in raw, unbuffered mode
memcpy(&new_kbd_mode, &g_old_kbd_mode, sizeof(struct termios));
if (b == true) {
new_kbd_mode.c_lflag |= ECHO;
tcsetattr(0, TCSANOW, &new_kbd_mode);
} else {
new_kbd_mode.c_lflag &= ~ECHO;
tcsetattr(0, TCSANOW, &new_kbd_mode);
}
_keyboard = b;
#endif
}
int System::KbHit(void)
{
#ifdef _WIN32
return 0;
#else
struct timeval timeout;
fd_set read_handles;
int status;
/* check stdin (fd 0) for activity */
FD_ZERO(&read_handles);
FD_SET(0, &read_handles);
timeout.tv_sec = timeout.tv_usec = 0;
status = select(0 + 1, &read_handles, NULL, NULL, &timeout);
if(status < 0) {
throw RuntimeException("select() failed in kbhit()");
}
return status;
#endif
}
int System::Getch(void)
{
#ifdef _WIN32
return getch();
#else
uint8_t temp;
/* stdin = fd 0 */
if(read(0, &temp, 1) != 1) {
return 0;
}
return temp;
#endif
}
void System::ChangeWorkingDirectory(std::string dir)
{
#ifdef WIN32
if (_chdir(dir.c_str()) != 0) {
#else
if (chdir(dir.c_str()) != 0) {
#endif
throw new InvalidArgumentException("Change working directory exception");
}
}
std::string System::GetProcessName()
{
#ifdef _WIN32
char path[512];
if (!GetModuleFileName(NULL, path, sizeof(path)) || !path[0]) {
return "unknown";
}
return std::string(path);
#else
std::ostringstream o;
pid_t pid = getpid();
o << "/proc/" << pid << "/execname";
return o.str();
#endif
}
std::string System::GetUserName()
{
#ifdef _WIN32
char buf[256];
DWORD size = (DWORD)sizeof(buf);
if (::GetUserName(ubuf, &size) != TRUE) {
throw new Exception("Cannot retrieve user name");
}
return buf;
#else
passwd *pw = getpwuid(geteuid());
if (!pw) {
throw new Exception("getpwuid error");
}
return pw->pw_name;
#endif
}
std::string System::GetHostName()
{
#ifdef _WIN32
char hbuf[512];
DWORD size = (DWORD)sizeof(hbuf);
if(::GetComputerName(hbuf, &size) != TRUE) {
throw new Exception("Cannot retrieve the computer name");
}
return hbuf;
#else
struct utsname uts;
uname(&uts);
return uts.nodename;
#endif
}
std::string System::GetHostArchitecture()
{
#ifdef _WIN32
SYSTEM_INFO sys_info;
::GetSystemInfo(&sys_info);
switch(sys_info.dwProcessorType) {
case PROCESSOR_INTEL_386:
case PROCESSOR_INTEL_486:
case PROCESSOR_INTEL_PENTIUM:
return "x86";
case 2200:
return "ia64";
#ifdef PROCESSOR_AMD_X8664
case PROCESSOR_AMD_X8664:
return "amd8664";
#endif
default:
return "unknown";
}
#else
struct utsname uts;
uname(&uts);
return uts.machine;
#endif
}
std::string System::GetOSName()
{
#ifdef _WIN32
return "Windows";
#else
struct utsname uts;
uname(&uts);
return uts.sysname;
#endif
}
std::string System::GetOSVersion()
{
#ifdef _WIN32
OSVERSIONINFO os_info;
os_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
::GetVersionEx(&os_info);
switch(os_info.dwMajorVersion) {
case 4:
switch(os_info.dwMinorVersion) {
case 0:
if(os_info.dwPlatformId == VER_PLATFORM_WIN32_NT)
return "NT 4.0";
else
return "95";
break;
case 10:
return "98";
case 90:
return "ME";
default:
return "unknown";
}
break;
case 5:
switch(os_info.dwMinorVersion) {
case 0:
return "2000";
case 1:
return "XP";
case 2:
return "Server 2003";
default:
return "unknown";
}
break;
case 6:
return "Vista";
case 7:
return "7";
default:
return "unknown";
}
#else
struct utsname uts;
uname(&uts);
return uts.release;
#endif
}
std::string System::GetTempDirectory()
{
#ifdef _WIN32
// DWORD length = ::GetTempPath(0, NULL);
char buf = char[512];
::GetTempPath(0, buf);
return buf;
#else
std::string buf;
buf = GetEnviromentVariable("TMPDIR", "nono");
if (buf != "nono") {
return buf;
}
buf = GetEnviromentVariable("TMPDIR", "nono");
if (buf != "nono") {
return buf;
}
return "/tmp";
#endif
}
uint64_t System::GetDiskFreeSpace()
{
#ifdef _WIN32
ULARGE_INTEGER x, avail;
if(::GetDiskFreeSpaceEx("\\", &x, &x, &avail) == FALSE) {
return 0LL;
}
return (uint64_t)avail.QuadPart;
#else
struct statfs buf;
if(::statfs("/", &buf) != 0) {
return 0LL;
}
return (uint64_t)(buf.f_bavail) * 1024LL;
#endif
}
int System::GetProcessorCount()
{
#ifdef _WIN32
SYSTEM_INFO si;
::GetSystemInfo(&si);
return si.dwNumberOfProcessors;
#else
return sysconf(_SC_NPROCESSORS_CONF);
/*
int nprocs = sysconf(_SC_NPROCESSORS_ONLN);
if (nprocs < 1) {
return -1;
}
nprocs_max = sysconf(_SC_NPROCESSORS_CONF);
if (nprocs_max < 1) {
return -1;
}
printf ("%ld of %ld processors online\n",nprocs, nprocs_max);
*/
#endif
}
int System::ResetSystem()
{
#ifdef _WIN32
InitiateSystemShutdown(
NULL,
NULL,
0,
TRUE,
TRUE
);
#else
return kill(1, SIGINT);
#endif
}
int System::ShutdownSystem()
{
#ifdef _WIN32
/* CHANGE:: somanete pega privilegios
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
return;
}
// Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS) {
return;
}
*/
// Shut down the system and force all applications to close.
/*
ExitWindowsEx(EWX_SHUTDOWN |
EWX_FORCE,
SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
SHTDN_REASON_MINOR_UPGRADE |
SHTDN_REASON_FLAG_PLANNED);
*/
InitiateSystemShutdown(
NULL,
NULL,
0,
TRUE,
FALSE
);
return TRUE;
#else
return kill(1, SIGUSR2);
#endif
}
void System::Logout()
{
#ifdef _WIN32
ExitWindows(0, 0);
#else
// TODO:: logout
#endif
}
void System::ResetProgram(std::string program, char **argv, char **envp)
{
#ifdef _WIN32
PROCESS_INFORMATION info;
/* TODO::
BOOL CreateProcess(
program.c_str(),
argv,
NULL,
NULL,
FALSE,
INHERIT_CALLER_PRIORITY,
NULL,
NULL,
NULL,
&info
);
*/
#else
execve(program.c_str(), argv, envp);
#endif
}
}