| 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 |
#include <windows.h> |
|---|
| 26 |
#include <stdlib.h> |
|---|
| 27 |
#include <stdio.h> |
|---|
| 28 |
#include <time.h> |
|---|
| 29 |
#include <direct.h> |
|---|
| 30 |
|
|---|
| 31 |
#include "ntlib.h" |
|---|
| 32 |
|
|---|
| 33 |
#define MAXPATHLEN _MAX_PATH |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
void |
|---|
| 39 |
sleep(unsigned long seconds) |
|---|
| 40 |
{ |
|---|
| 41 |
Sleep (seconds * 1000); |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
char * |
|---|
| 46 |
getwd (char *dir) |
|---|
| 47 |
{ |
|---|
| 48 |
if (GetCurrentDirectory (MAXPATHLEN, dir) > 0) |
|---|
| 49 |
return dir; |
|---|
| 50 |
return NULL; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
static HANDLE getppid_parent; |
|---|
| 54 |
static int getppid_ppid; |
|---|
| 55 |
|
|---|
| 56 |
int |
|---|
| 57 |
getppid(void) |
|---|
| 58 |
{ |
|---|
| 59 |
char *ppid; |
|---|
| 60 |
DWORD result; |
|---|
| 61 |
|
|---|
| 62 |
ppid = getenv ("EM_PARENT_PROCESS_ID"); |
|---|
| 63 |
if (!ppid) |
|---|
| 64 |
{ |
|---|
| 65 |
printf("no pid.\n"); |
|---|
| 66 |
return 0; |
|---|
| 67 |
} |
|---|
| 68 |
else |
|---|
| 69 |
{ |
|---|
| 70 |
getppid_ppid = atoi (ppid); |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
if (!getppid_parent) |
|---|
| 74 |
{ |
|---|
| 75 |
getppid_parent = OpenProcess (SYNCHRONIZE, FALSE, atoi(ppid)); |
|---|
| 76 |
if (!getppid_parent) |
|---|
| 77 |
{ |
|---|
| 78 |
printf ("Failed to open handle to parent process: %d\n", |
|---|
| 79 |
GetLastError()); |
|---|
| 80 |
exit (1); |
|---|
| 81 |
} |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
result = WaitForSingleObject (getppid_parent, 0); |
|---|
| 85 |
switch (result) |
|---|
| 86 |
{ |
|---|
| 87 |
case WAIT_TIMEOUT: |
|---|
| 88 |
|
|---|
| 89 |
return getppid_ppid; |
|---|
| 90 |
case WAIT_OBJECT_0: |
|---|
| 91 |
|
|---|
| 92 |
return 1; |
|---|
| 93 |
case WAIT_FAILED: |
|---|
| 94 |
default: |
|---|
| 95 |
printf ("Checking parent status failed: %d\n", GetLastError()); |
|---|
| 96 |
exit (1); |
|---|
| 97 |
} |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
char * |
|---|
| 101 |
getlogin () |
|---|
| 102 |
{ |
|---|
| 103 |
static char user_name[256]; |
|---|
| 104 |
DWORD length = sizeof (user_name); |
|---|
| 105 |
|
|---|
| 106 |
if (GetUserName (user_name, &length)) |
|---|
| 107 |
return user_name; |
|---|
| 108 |
return NULL; |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
char * |
|---|
| 112 |
cuserid (char * s) |
|---|
| 113 |
{ |
|---|
| 114 |
char * name = getlogin (); |
|---|
| 115 |
if (s) |
|---|
| 116 |
return strcpy (s, name ? name : ""); |
|---|
| 117 |
return name; |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
int |
|---|
| 121 |
getuid () |
|---|
| 122 |
{ |
|---|
| 123 |
return 0; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
int |
|---|
| 127 |
setuid (int uid) |
|---|
| 128 |
{ |
|---|
| 129 |
return 0; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
struct passwd * |
|---|
| 133 |
getpwuid (int uid) |
|---|
| 134 |
{ |
|---|
| 135 |
return NULL; |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
char * |
|---|
| 139 |
getpass (const char * prompt) |
|---|
| 140 |
{ |
|---|
| 141 |
static char input[256]; |
|---|
| 142 |
HANDLE in; |
|---|
| 143 |
HANDLE err; |
|---|
| 144 |
DWORD count; |
|---|
| 145 |
|
|---|
| 146 |
in = GetStdHandle (STD_INPUT_HANDLE); |
|---|
| 147 |
err = GetStdHandle (STD_ERROR_HANDLE); |
|---|
| 148 |
|
|---|
| 149 |
if (in == INVALID_HANDLE_VALUE || err == INVALID_HANDLE_VALUE) |
|---|
| 150 |
return NULL; |
|---|
| 151 |
|
|---|
| 152 |
if (WriteFile (err, prompt, strlen (prompt), &count, NULL)) |
|---|
| 153 |
{ |
|---|
| 154 |
int istty = (GetFileType (in) == FILE_TYPE_CHAR); |
|---|
| 155 |
DWORD old_flags; |
|---|
| 156 |
int rc; |
|---|
| 157 |
|
|---|
| 158 |
if (istty) |
|---|
| 159 |
{ |
|---|
| 160 |
if (GetConsoleMode (in, &old_flags)) |
|---|
| 161 |
SetConsoleMode (in, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT); |
|---|
| 162 |
else |
|---|
| 163 |
istty = 0; |
|---|
| 164 |
} |
|---|
| 165 |
rc = ReadFile (in, input, sizeof (input), &count, NULL); |
|---|
| 166 |
if (count >= 2 && input[count - 2] == '\r') |
|---|
| 167 |
input[count - 2] = '\0'; |
|---|
| 168 |
else |
|---|
| 169 |
{ |
|---|
| 170 |
char buf[256]; |
|---|
| 171 |
while (ReadFile (in, buf, sizeof (buf), &count, NULL) > 0) |
|---|
| 172 |
if (count >= 2 && buf[count - 2] == '\r') |
|---|
| 173 |
break; |
|---|
| 174 |
} |
|---|
| 175 |
WriteFile (err, "\r\n", 2, &count, NULL); |
|---|
| 176 |
if (istty) |
|---|
| 177 |
SetConsoleMode (in, old_flags); |
|---|
| 178 |
if (rc) |
|---|
| 179 |
return input; |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
return NULL; |
|---|
| 183 |
} |
|---|
| 184 |
|
|---|
| 185 |
int |
|---|
| 186 |
fchown (int fd, int uid, int gid) |
|---|
| 187 |
{ |
|---|
| 188 |
return 0; |
|---|
| 189 |
} |
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
char * |
|---|
| 195 |
sys_ctime (const time_t *t) |
|---|
| 196 |
{ |
|---|
| 197 |
char *str = (char *) ctime (t); |
|---|
| 198 |
return (str ? str : "Sun Jan 01 00:00:00 1970"); |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
FILE * |
|---|
| 202 |
sys_fopen(const char * path, const char * mode) |
|---|
| 203 |
{ |
|---|
| 204 |
return fopen (path, mode); |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
int |
|---|
| 208 |
sys_chdir (const char * path) |
|---|
| 209 |
{ |
|---|
| 210 |
return _chdir (path); |
|---|
| 211 |
} |
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|