From 70cb37620830caf1d117e3cb3c14e396cc7905e4 Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Sat, 24 Dec 2016 21:59:57 +0800 Subject: [PATCH] Replace sys_signame with strsignal and rename malloc functions --- src/protocol.c | 24 ++++++++++++------------ src/server.c | 28 ++++++++++++++-------------- src/utils.c | 36 +++++------------------------------- src/utils.h | 8 ++------ 4 files changed, 33 insertions(+), 63 deletions(-) diff --git a/src/protocol.c b/src/protocol.c index e8e1637..6317e31 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -60,7 +60,7 @@ parse_window_size(const char *json) { rows = json_object_get_int(o); json_object_put(obj); - struct winsize *size = t_malloc(sizeof(struct winsize)); + struct winsize *size = xmalloc(sizeof(struct winsize)); memset(size, 0, sizeof(struct winsize)); size->ws_col = (unsigned short) columns; size->ws_row = (unsigned short) rows; @@ -112,7 +112,7 @@ tty_client_destroy(struct tty_client *client) { // free the buffer if (client->buffer != NULL) - t_free(client->buffer); + free(client->buffer); // remove from clients list pthread_mutex_lock(&server->lock); @@ -162,10 +162,10 @@ thread_run_command(void *args) { if (FD_ISSET (pty, &des_set)) { memset(buf, 0, BUF_SIZE); bytes = (int) read(pty, buf, BUF_SIZE); - struct pty_data *frame = (struct pty_data *) t_malloc(sizeof(struct pty_data)); + struct pty_data *frame = (struct pty_data *) xmalloc(sizeof(struct pty_data)); frame->len = bytes; if (bytes > 0) { - frame->data = t_malloc((size_t) bytes); + frame->data = xmalloc((size_t) bytes); memcpy(frame->data, buf, bytes); } pthread_mutex_lock(&client->lock); @@ -234,7 +234,7 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason, // read error or client exited, close connection if (frame->len <= 0) { STAILQ_REMOVE_HEAD(&client->queue, list); - t_free(frame); + free(frame); return -1; } @@ -244,7 +244,7 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason, unsigned char *p = &message[LWS_PRE]; size_t n = sprintf((char *) p, "%c%s", OUTPUT, b64_text); - t_free(b64_text); + free(b64_text); if (lws_write(wsi, p, n, LWS_WRITE_TEXT) < n) { lwsl_err("lws_write\n"); @@ -252,8 +252,8 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason, } STAILQ_REMOVE_HEAD(&client->queue, list); - t_free(frame->data); - t_free(frame); + free(frame->data); + free(frame); if (lws_partial_buffered(wsi)) { lws_callback_on_writable(wsi); @@ -265,11 +265,11 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason, case LWS_CALLBACK_RECEIVE: if (client->buffer == NULL) { - client->buffer = t_malloc(len + 1); + client->buffer = xmalloc(len + 1); client->len = len; memcpy(client->buffer, in, len); } else { - client->buffer = t_realloc(client->buffer, client->len + len + 1); + client->buffer = xrealloc(client->buffer, client->len + len + 1); memcpy(client->buffer + client->len, in, len); client->len += len; } @@ -312,7 +312,7 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason, if (ioctl(client->pty, TIOCSWINSZ, size) == -1) { lwsl_err("ioctl TIOCSWINSZ: %d (%s)\n", errno, strerror(errno)); } - t_free(size); + free(size); } break; case JSON_DATA: @@ -337,7 +337,7 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason, } if (client->buffer != NULL) { - t_free(client->buffer); + free(client->buffer); client->buffer = NULL; } break; diff --git a/src/server.c b/src/server.c index 8c215cb..088f536 100644 --- a/src/server.c +++ b/src/server.c @@ -77,7 +77,7 @@ tty_server_new(int argc, char **argv, int start) { struct tty_server *ts; size_t cmd_len = 0; - ts = t_malloc(sizeof(struct tty_server)); + ts = xmalloc(sizeof(struct tty_server)); memset(ts, 0, sizeof(struct tty_server)); LIST_INIT(&ts->clients); @@ -90,7 +90,7 @@ tty_server_new(int argc, char **argv, int start) { int cmd_argc = argc - start; char **cmd_argv = &argv[start]; - ts->argv = t_malloc(sizeof(char *) * (cmd_argc + 1)); + ts->argv = xmalloc(sizeof(char *) * (cmd_argc + 1)); for (int i = 0; i < cmd_argc; i++) { ts->argv[i] = strdup(cmd_argv[i]); cmd_len += strlen(ts->argv[i]); @@ -100,7 +100,7 @@ tty_server_new(int argc, char **argv, int start) { } ts->argv[cmd_argc] = NULL; - ts->command = t_malloc(cmd_len); + ts->command = xmalloc(cmd_len); char *ptr = ts->command; for (int i = 0; i < cmd_argc; i++) { ptr = stpcpy(ptr, ts->argv[i]); @@ -129,7 +129,7 @@ int calc_command_start(int argc, char **argv) { // make a copy of argc and argv int argc_copy = argc; - char **argv_copy = t_malloc(sizeof(char *) * argc); + char **argv_copy = xmalloc(sizeof(char *) * argc); for (int i = 0; i < argc; i++) { argv_copy[i] = strdup(argv[i]); } @@ -152,9 +152,9 @@ calc_command_start(int argc, char **argv) { // free argv copy for (int i = 0; i < argc; i++) { - t_free(argv_copy[i]); + free(argv_copy[i]); } - t_free(argv_copy); + free(argv_copy); // reset for next use opterr = 1; @@ -287,7 +287,7 @@ main(int argc, char **argv) { return -1; } char *value = strsep(&option, "="); - t_free(option); + free(option); struct json_object *obj = json_tokener_parse(value); json_object_object_add(client_prefs, key, obj != NULL ? obj : json_object_new_string(value)); } @@ -380,16 +380,16 @@ main(int argc, char **argv) { // cleanup if (server->credential != NULL) - t_free(server->credential); - t_free(server->command); - t_free(server->prefs_json); + free(server->credential); + free(server->command); + free(server->prefs_json); int i = 0; do { - t_free(server->argv[i++]); + free(server->argv[i++]); } while (server->argv[i] != NULL); - t_free(server->argv); - t_free(server->sig_name); - t_free(server); + free(server->argv); + free(server->sig_name); + free(server); return 0; } diff --git a/src/utils.c b/src/utils.c index 4d6005a..4b2a4a0 100644 --- a/src/utils.c +++ b/src/utils.c @@ -6,30 +6,8 @@ #include #include -// http://web.mit.edu/~svalente/src/kill/kill.c -#ifdef __linux__ -/* - * sys_signame -- an ordered list of signals. - * lifted from /usr/include/linux/signal.h - * this particular order is only correct for linux. - * this is _not_ portable. - */ -const char *sys_signame[NSIG] = { - "zero", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", "UNUSED", - "FPE", "KILL", "USR1", "SEGV", "USR2", "PIPE", "ALRM", "TERM", - "STKFLT","CHLD", "CONT", "STOP", "TSTP", "TTIN", "TTOU", "IO", - "XCPU", "XFSZ", "VTALRM","PROF", "WINCH", NULL -}; -#endif - -#ifdef __CYGWIN__ -#define SIG_NAME(x) strsignal(x) -#else -#define SIG_NAME(x) sys_signame[x] -#endif - void * -t_malloc(size_t size) { +xmalloc(size_t size) { if (size == 0) return NULL; void *p = malloc(size); @@ -38,12 +16,8 @@ t_malloc(size_t size) { return p; } -void t_free(void *p) { - free(p); -} - void * -t_realloc(void *p, size_t size) { +xrealloc(void *p, size_t size) { if ((size == 0) && (p == NULL)) return NULL; p = realloc(p, size); @@ -63,7 +37,7 @@ uppercase(char *str) { int get_sig_name(int sig, char *buf) { - int n = sprintf(buf, "SIG%s", sig < NSIG ? SIG_NAME(sig) : "unknown"); + int n = sprintf(buf, "SIG%s", sig < NSIG ? strsignal(sig) : "unknown"); uppercase(buf); return n; } @@ -74,7 +48,7 @@ get_sig(const char *sig_name) { return -1; } for (int sig = 1; sig < NSIG; sig++) { - const char *name = SIG_NAME(sig); + const char *name = strsignal(sig); if (strcasecmp(name, sig_name + 3) == 0) return sig; } @@ -90,7 +64,7 @@ base64_encode(const unsigned char *buffer, size_t length) { int i_shift = 0; int bytes_remaining = (int) length; - ret = dst = t_malloc((size_t) (((length + 2) / 3 * 4) + 1)); + ret = dst = xmalloc((size_t) (((length + 2) / 3 * 4) + 1)); while (bytes_remaining) { i_bits = (i_bits << 8) + *buffer++; bytes_remaining--; diff --git a/src/utils.h b/src/utils.h index c8ffade..5df992c 100644 --- a/src/utils.h +++ b/src/utils.h @@ -3,15 +3,11 @@ // malloc with NULL check void * -t_malloc(size_t size); - -// free with NULL check -void -t_free(void *p); +xmalloc(size_t size); // realloc with NULL check void * -t_realloc(void *p, size_t size); +xrealloc(void *p, size_t size); // Convert a string to upper case char * -- 2.43.4