1 #ifndef R_SYS_H
2 #define R_SYS_H
3 
4 #include <r_list.h>
5 
6 #if __WINDOWS__
7 #define R_SYS_DEVNULL "nul"
8 #else
9 #define R_SYS_DEVNULL "/dev/null"
10 #endif
11 
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 #define R_SYS_BITS_8 1
18 #define R_SYS_BITS_16 2
19 #define R_SYS_BITS_32 4
20 #define R_SYS_BITS_64 8
21 
22 typedef struct {
23 	char *sysname;
24 	char *nodename;
25 	char *release;
26 	char *version;
27 	char *machine;
28 } RSysInfo;
29 
30 R_API RSysInfo *r_sys_info(void);
31 R_API void r_sys_info_free(RSysInfo *si);
32 
33 R_API int r_sys_sigaction(int *sig, void (*handler) (int));
34 R_API int r_sys_signal(int sig, void (*handler) (int));
35 R_API void r_sys_env_init(void);
36 R_API char **r_sys_get_environ(void);
37 R_API void r_sys_set_environ(char **e);
38 
39 R_API int r_sys_fork(void);
40 // nocleanup = false => exit(); true => _exit()
41 R_API void r_sys_exit(int status, bool nocleanup);
42 R_API bool r_sys_stop(void);
43 R_API char *r_sys_pid_to_path(int pid);
44 R_API int r_sys_run(const ut8 *buf, int len);
45 R_API int r_sys_run_rop(const ut8 *buf, int len);
46 R_API int r_sys_getpid(void);
47 R_API int r_sys_crash_handler(const char *cmd);
48 R_API const char *r_sys_arch_str(int arch);
49 R_API int r_sys_arch_id(const char *arch);
50 R_API bool r_sys_arch_match(const char *archstr, const char *arch);
51 R_API RList *r_sys_dir(const char *path);
52 R_API void r_sys_perror_str(const char *fun);
53 #if __WINDOWS__
54 #define r_sys_mkdir_failed() (GetLastError () != ERROR_ALREADY_EXISTS)
55 #else
56 #define r_sys_mkdir_failed() (errno != EEXIST)
57 #endif
58 R_API const char *r_sys_prefix(const char *pfx);
59 R_API bool r_sys_mkdir(const char *dir);
60 R_API bool r_sys_mkdirp(const char *dir);
61 R_API int r_sys_sleep(int secs);
62 R_API int r_sys_usleep(int usecs);
63 R_API char *r_sys_getenv(const char *key);
64 R_API bool r_sys_getenv_asbool(const char *key);
65 R_API int r_sys_setenv(const char *key, const char *value);
66 R_API int r_sys_clearenv(void);
67 R_API char *r_sys_whoami(void);
68 R_API char *r_sys_getdir(void);
69 R_API int r_sys_chdir(const char *s);
70 R_API bool r_sys_aslr(int val);
71 R_API int r_sys_thp_mode(void);
72 R_API int r_sys_cmd_str_full(const char *cmd, const char *input, char **output, int *len, char **sterr);
73 #if __WINDOWS__
74 #if UNICODE
75 #define W32_TCHAR_FSTR "%S"
76 #define W32_TCALL(name) name"W"
77 #define r_sys_conv_utf8_to_win(buf) r_utf8_to_utf16 (buf)
78 #define r_sys_conv_utf8_to_win_l(buf, len) r_utf8_to_utf16_l (buf, len)
79 #define r_sys_conv_win_to_utf8(buf) r_utf16_to_utf8 (buf)
80 #define r_sys_conv_win_to_utf8_l(buf, len) r_utf16_to_utf8_l ((wchar_t *)buf, len)
81 #else
82 #define W32_TCHAR_FSTR "%s"
83 #define W32_TCALL(name) name"A"
84 #define r_sys_conv_utf8_to_win(buf) r_utf8_to_acp (buf)
85 #define r_sys_conv_utf8_to_win_l(buf, len) r_utf8_to_acp_l (buf, len)
86 #define r_sys_conv_win_to_utf8(buf) r_acp_to_utf8 (buf)
87 #define r_sys_conv_win_to_utf8_l(buf, len) r_acp_to_utf8_l (buf, len)
88 #endif
89 R_API char *r_sys_get_src_dir_w32(void);
90 R_API bool r_sys_cmd_str_full_w32(const char *cmd, const char *input, char **output, int *outlen, char **sterr);
91 R_API bool r_sys_create_child_proc_w32(const char *cmdline, HANDLE in, HANDLE out, HANDLE err);
92 #endif
93 R_API int r_sys_truncate(const char *file, int sz);
94 R_API int r_sys_cmd(const char *cmd);
95 R_API int r_sys_cmdbg(const char *cmd);
96 R_API int r_sys_cmdf(const char *fmt, ...) R_PRINTF_CHECK(1, 2);
97 R_API char *r_sys_cmd_str(const char *cmd, const char *input, int *len);
98 R_API char *r_sys_cmd_strf(const char *cmd, ...) R_PRINTF_CHECK(1, 2);
99 //#define r_sys_cmd_str(cmd, input, len) r_sys_cmd_str_full(cmd, input, len, 0)
100 R_API void r_sys_backtrace(void);
101 R_API bool r_sys_tts(const char *txt, bool bg);
102 
103 #if __WINDOWS__
104 #  define r_sys_breakpoint() { __debugbreak  (); }
105 #else
106 #if __GNUC__
107 #  define r_sys_breakpoint() __builtin_trap()
108 #elif __i386__ || __x86_64__
109 #   define r_sys_breakpoint() __asm__ volatile ("int3");
110 #elif __arm64__ || __aarch64__
111 #  define r_sys_breakpoint() __asm__ volatile ("brk 0");
112 // #define r_sys_breakpoint() __asm__ volatile ("brk #1");
113 #elif (__arm__ || __thumb__)
114 #  if __ARM_ARCH > 5
115 #    define r_sys_breakpoint() __asm__ volatile ("bkpt $0");
116 #  else
117 #    define r_sys_breakpoint() __asm__ volatile ("svc $1");
118 #  endif
119 #elif __mips__
120 #  define r_sys_breakpoint() __asm__ volatile ("break");
121 // #  define r_sys_breakpoint() __asm__ volatile ("teq $0, $0");
122 #elif __EMSCRIPTEN__
123 // TODO: cannot find a better way to breakpoint in wasm/asm.js
124 #  define r_sys_breakpoint() { char *a = NULL; *a = 0; }
125 #else
126 #  warning r_sys_breakpoint not implemented for this platform
127 #  define r_sys_trap() __asm__ __volatile__ (".word 0");
128 #   define r_sys_breakpoint() { char *a = NULL; *a = 0; }
129 #endif
130 #endif
131 
132 /* syscmd */
133 R_API char *r_syscmd_ls(const char *input);
134 R_API char *r_syscmd_cat(const char *file);
135 R_API char *r_syscmd_mkdir(const char *dir);
136 R_API bool r_syscmd_mv(const char *input);
137 R_API char *r_syscmd_uniq(const char *file);
138 R_API char *r_syscmd_head(const char *file, int count);
139 R_API char *r_syscmd_tail(const char *file, int count);
140 R_API char *r_syscmd_join(const char *file1, const char *file2);
141 R_API char *r_syscmd_sort(const char *file);
142 
143 #ifdef __cplusplus
144 }
145 #endif
146 
147 #endif //  R_SYS_H
148