diff options
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 141 |
1 files changed, 90 insertions, 51 deletions
diff --git a/src/utils.c b/src/utils.c index 0523451..f3e87d2 100644 --- a/src/utils.c +++ b/src/utils.c @@ -7,26 +7,20 @@ #include <string.h> #if defined(__linux__) && !defined(__ANDROID__) -// https://github.com/karelzak/util-linux/blob/master/misc-utils/kill.c const char *sys_signame[NSIG] = { - "zero", "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "UNUSED", "FPE", - "KILL", "USR1", "SEGV", "USR2", "PIPE", "ALRM", "TERM", "STKFLT", "CHLD", - "CONT", "STOP", "TSTP", "TTIN", "TTOU", "URG", "XCPU", "XFSZ", "VTALRM", - "PROF", "WINCH", "IO", "PWR", "SYS", NULL}; + "zero", "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "UNUSED", "FPE", "KILL", "USR1", + "SEGV", "USR2", "PIPE", "ALRM", "TERM", "STKFLT", "CHLD", "CONT", "STOP", "TSTP", "TTIN", + "TTOU", "URG", "XCPU", "XFSZ", "VTALRM", "PROF", "WINCH", "IO", "PWR", "SYS", NULL}; #endif #if defined(_WIN32) || defined(__CYGWIN__) -#include <shellapi.h> #include <windows.h> -// https://github.com/mirror/newlib-cygwin/blob/master/winsup/cygwin/strsig.cc -#ifndef NSIG +#undef NSIG #define NSIG 33 -#endif const char *sys_signame[NSIG] = { - "zero", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", "EMT", "FPE", - "KILL", "BUS", "SEGV", "SYS", "PIPE", "ALRM", "TERM", "URG", "STOP", - "TSTP", "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", "XFSZ", "VTALRM", - "PROF", "WINCH", "PWR", "USR1", "USR2", NULL}; + "zero", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", "EMT", "FPE", "KILL", "BUS", + "SEGV", "SYS", "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", "CONT", "CHLD", "TTIN", + "TTOU", "IO", "XCPU", "XFSZ", "VTALRM", "PROF", "WINCH", "PWR", "USR1", "USR2", NULL}; #endif void *xmalloc(size_t size) { @@ -43,12 +37,20 @@ void *xrealloc(void *p, size_t size) { return p; } -char *uppercase(char *str) { - int i = 0; - do { - str[i] = (char)toupper(str[i]); - } while (str[i++] != '\0'); - return str; +char *uppercase(char *s) { + while(*s) { + *s = (char)toupper((int)*s); + s++; + } + return s; +} + +char *lowercase(char *s) { + while(*s) { + *s = (char)tolower((int)*s); + s++; + } + return s; } bool endswith(const char *str, const char *suffix) { @@ -58,8 +60,7 @@ bool endswith(const char *str, const char *suffix) { } int get_sig_name(int sig, char *buf, size_t len) { - int n = - snprintf(buf, len, "SIG%s", sig < NSIG ? sys_signame[sig] : "unknown"); + int n = snprintf(buf, len, "SIG%s", sig < NSIG ? sys_signame[sig] : "unknown"); uppercase(buf); return n; } @@ -67,27 +68,19 @@ int get_sig_name(int sig, char *buf, size_t len) { int get_sig(const char *sig_name) { for (int sig = 1; sig < NSIG; sig++) { const char *name = sys_signame[sig]; - if (name != NULL && (strcasecmp(name, sig_name) == 0 || - strcasecmp(name, sig_name + 3) == 0)) + if (name != NULL && (strcasecmp(name, sig_name) == 0 || strcasecmp(name, sig_name + 3) == 0)) return sig; } return atoi(sig_name); } -bool fd_set_cloexec(const int fd) { - int flags = fcntl(fd, F_GETFD); - if (flags < 0) return false; - return (flags & FD_CLOEXEC) == 0 || - fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1; -} - int open_uri(char *uri) { #ifdef __APPLE__ char command[256]; sprintf(command, "open %s > /dev/null 2>&1", uri); return system(command); #elif defined(_WIN32) || defined(__CYGWIN__) - return ShellExecute(0, 0, uri, 0, 0, SW_SHOW) > 32 ? 0 : 1; + return ShellExecute(0, 0, uri, 0, 0, SW_SHOW) > (HINSTANCE)32 ? 0 : 1; #else // check if X server is running if (system("xset -q > /dev/null 2>&1")) return 1; @@ -97,28 +90,74 @@ int open_uri(char *uri) { #endif } -// https://github.com/darkk/redsocks/blob/master/base64.c -char *base64_encode(const unsigned char *buffer, size_t length) { - static const char b64[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - char *ret, *dst; - unsigned i_bits = 0; - int i_shift = 0; - int bytes_remaining = (int)length; +#ifdef _WIN32 +char *strsep(char **sp, char *sep) { + char *p, *s; + if (sp == NULL || *sp == NULL || **sp == '\0') return (NULL); + s = *sp; + p = s + strcspn(s, sep); + if (*p != '\0') *p++ = '\0'; + *sp = p; + return s; +} - ret = dst = xmalloc((size_t)(((length + 2) / 3 * 4) + 1)); - while (bytes_remaining) { - i_bits = (i_bits << 8) + *buffer++; - bytes_remaining--; - i_shift += 8; +const char *quote_arg(const char *arg) { + int len = 0, n = 0; + int force_quotes = 0; + char *q, *d; + const char *p = arg; + if (!*p) force_quotes = 1; + while (*p) { + if (isspace(*p) || *p == '*' || *p == '?' || *p == '{' || *p == '\'') + force_quotes = 1; + else if (*p == '"') + n++; + else if (*p == '\\') { + int count = 0; + while (*p == '\\') { + count++; + p++; + len++; + } + if (*p == '"' || !*p) n += count * 2 + 1; + continue; + } + len++; + p++; + } + if (!force_quotes && n == 0) return arg; - do { - *dst++ = b64[(i_bits << 6 >> i_shift) & 0x3f]; - i_shift -= 6; - } while (i_shift > 6 || (bytes_remaining == 0 && i_shift > 0)); + d = q = xmalloc(len + n + 3); + *d++ = '"'; + while (*arg) { + if (*arg == '"') + *d++ = '\\'; + else if (*arg == '\\') { + int count = 0; + while (*arg == '\\') { + count++; + *d++ = *arg++; + } + if (*arg == '"' || !*arg) { + while (count-- > 0) *d++ = '\\'; + if (!*arg) break; + *d++ = '\\'; + } + } + *d++ = *arg++; } - while ((dst - ret) & 3) *dst++ = '='; - *dst = '\0'; + *d++ = '"'; + *d++ = '\0'; + return q; +} - return ret; +void print_error(char *func) { + LPVOID buffer; + DWORD dw = GetLastError(); + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buffer, 0, NULL); + wprintf(L"== %s failed with error %d: %s", func, dw, buffer); + LocalFree(buffer); } +#endif |