/* * Set the environment variable "name" to the value "value". If the variable * exists already, override it or just skip. */ static void envp_add(char *** envp, const char * name, const char * value, int env_override) { int i = 0; size_t len = strlen(name); char ** env = *envp; assert(name); assert(value); if (env == NULL) { env = *envp = (char **)tmp_alloc_zero(sizeof(char *) * 2); } else { for (i = 0; env[i]; i++) { if (strncmp(env[i], name, len) == 0 && env[i][len] == '=') break; } if (env[i]) { /* override */ if (!env_override) return; } else { /* new variable */ env = *envp = (char **)tmp_realloc(env, sizeof(char *) * (i + 2)); env[i + 1] = NULL; } } len += strlen(value) + 2; env[i] = (char *)tmp_alloc(len); snprintf(env[i], len, "%s=%s", name, value); }
static void add(unsigned n) { if (buf_pos >= buf_max) { buf_max *= 2; buf = (U1_T *)tmp_realloc(buf, buf_max); } buf[buf_pos++] = (U1_T)n; }
static void read_memory_fill_array_cb(InputStream * inp, void * args) { MemoryFillBuffer * buf = (MemoryFillBuffer *)args; if (buf->pos >= buf->max) { buf->buf = (char *)tmp_realloc(buf->buf, buf->max *= 2); } buf->buf[buf->pos++] = (char)json_read_ulong(inp); }
/* * adds a string to current string value */ void v_strcat(var_t *var, const char *string) { if (var->type == V_INT || var->type == V_NUM) { v_tostr(var); } if (var->type == V_STR) { var->v.p.size = strlen((char *) var->v.p.ptr) + strlen(string) + 1; var->v.p.ptr = tmp_realloc(var->v.p.ptr, var->v.p.size); strcat((char *) var->v.p.ptr, string); } else { err_typemismatch(); } }
// returns the contents of the given url char *Controller::readConnection(const char *url) { char *result = NULL; logEntered(); _output->print("\033[ LLoading..."); MAHandle conn = maConnect(url); if (conn < 1) { logPrint("Failed connecting to %s\n", url); } else { _runMode = conn_state; logPrint("Connecting to %s\n", url); bool connected = false; byte buffer[1024]; int length = 0; int size = 0; int now = maGetMilliSecondCount(); MAEvent event; // pause until connected while (_runMode == conn_state) { event = processEvents(EVENT_WAIT_INFINITE, EVENT_TYPE_CONN); if (event.type == EVENT_TYPE_CONN) { switch (event.conn.opType) { case CONNOP_CONNECT: // connection established if (!connected) { connected = (event.conn.result > 0); if (connected) { memset(buffer, 0, sizeof(buffer)); maConnRead(conn, buffer, sizeof(buffer)); } else { logPrint("Connection error\n"); _runMode = init_state; } } break; case CONNOP_READ: // connRead completed if (event.conn.result > 0) { size = event.conn.result; if (result == NULL) { result = (char *)tmp_alloc(size + 1); memcpy(result, buffer, size); length = size; } else { result = (char *)tmp_realloc(result, length + size + 1); memcpy(result + length, buffer, size); length += size; } result[length] = 0; memset(buffer, 0, sizeof(buffer)); maConnRead(conn, buffer, sizeof(buffer)); } else { // no more data _runMode = init_state; } break; default: logPrint("Connection error\n"); _runMode = init_state; } } } logPrint("Loaded in %d msecs\n", maGetMilliSecondCount() - now); } maConnClose(conn); return result; }