size_t knh_InputStream_read(Ctx *ctx, knh_InputStream_t *in, char *buf, size_t bufsiz) { if(knh_InputStream_isFILE(in)) { return SP(in)->dspi->fread(ctx, DP(in)->fd, buf, bufsiz); } else { size_t inbufsiz = (DP(in)->bufend - DP(in)->bufpos); if(bufsiz <= inbufsiz) { knh_memcpy(buf, DP(in)->buf, bufsiz); DP(in)->bufpos += bufsiz; { DP(in)->size += bufsiz; } return bufsiz; } // XXX when both InputStream.read and InputStream.readLine method call, // it seams strange. so, move DP(o)->buf's pointer to bufpos. knh_memcpy(buf, DP(in)->buf + DP(in)->bufpos, inbufsiz); DP(in)->bufpos += inbufsiz; DP(in)->size += bufsiz; buf += inbufsiz; size_t s = SP(in)->dspi->fread(ctx, DP(in)->fd, buf+inbufsiz, bufsiz-inbufsiz); DP(in)->size += s; return s + inbufsiz; } }
/* modified by Wakamori */ static void opt_enforce_security(int mode, const char *optstr) { if(optstr != NULL) { int len = knh_strlen(optstr); if (len < 64) { knh_memcpy(role, optstr, len); role[len] = '\0'; } } else { knh_memcpy(role, "Default", 7); role[7] = '\0'; } enforce_security = 1; }
static char* readline(const char* prompt) { static int checkCTL = 0; int ch, pos = 0; static char linebuf[1024]; // THREAD-UNSAFE fputs(prompt, stdout); while((ch = fgetc(stdin)) != EOF) { //fprintf(stderr, "%d: %c\n", ch, ch); if(ch == '\r') continue; if(ch == 27) { /* ^[[A */; fgetc(stdin); fgetc(stdin); if(checkCTL == 0) { fprintf(stdout, " - readline would provide you with better shell experience.\n"); checkCTL = 1; } continue; } if(ch == '\n' || pos == sizeof(linebuf) - 1) { linebuf[pos] = 0; break; } linebuf[pos] = ch; pos++; } if(ch == EOF) return NULL; { char *p = (char*)malloc(pos+1); knh_memcpy(p, linebuf, pos+1); return p; } }
void knh_EventTBL_expand(CTX) { size_t s = ctx->share->sizeEventTBL, max = ctx->share->capacityEventTBL * 2; knh_EventTBL_t *newt = (knh_EventTBL_t*)KCALLOC(SIZEOF_TEXPT(max)); knh_bzero(newt, SIZEOF_TEXPT(max)); knh_memcpy(newt, ctx->share->EventTBL, SIZEOF_TEXPT(s)); ((kshare_t*)ctx->share)->EventTBL = newt; ((kshare_t*)ctx->share)->capacityEventTBL = max; }
static char *write_b(char *p, char *ebuf, const char *text, size_t len) { if(ebuf - p > len) { knh_memcpy(p, text, len); return p+len; } else { return NULL; } }
void knh_Bytes_write(Ctx *ctx, knh_Bytes_t *ba, knh_bytes_t t) { if(t.len == 0) return ; if(BA_size(ba) + t.len >= ba->capacity) { size_t newsize = ba->capacity * 2; if(newsize < BA_size(ba) + t.len) newsize = knh_good_size(BA_size(ba) + t.len); knh_Bytes_expands(ctx, ba, newsize); } knh_memcpy(ba->bu.ubuf + BA_size(ba), t.ustr, t.len); BA_size(ba) += t.len; }
void knh_Bytes_write(CTX ctx, knh_Bytes_t *ba, knh_bytes_t t) { size_t capacity = ba->dim->capacity; if(t.len == 0) return ; if(BA_size(ba) + t.len >= capacity) { size_t newsize = k_grow(capacity); if(newsize < BA_size(ba) + t.len) newsize = k_goodsize(BA_size(ba) + t.len); knh_Bytes_expands(ctx, ba, newsize); } knh_memcpy(ba->bu.ubuf + BA_size(ba), t.utext, t.len); BA_size(ba) += t.len; }
static void knh_Stmt_terms_expand(Ctx *ctx, Stmt *o) { size_t i, newcapacity = DP(o)->capacity * 2; Object **newterms = (Object**)KNH_MALLOC(ctx, sizeof(Object*) * newcapacity); knh_memcpy(newterms, DP(o)->terms, sizeof(Object*) * DP(o)->capacity); for(i = DP(o)->capacity; i < newcapacity; i++) { KNH_INITv(newterms[i], KNH_NULL); } KNH_FREE(ctx, DP(o)->terms, sizeof(Object*) * DP(o)->capacity); DP(o)->terms = newterms; DP(o)->capacity = newcapacity; KNH_ASSERT(DP(o)->size < DP(o)->capacity); }
void knh_path_reset(CTX ctx, knh_path_t *ph, const char *scheme, knh_bytes_t t) { knh_index_t idx = knh_bytes_index(t, ':'); char *buf = P_buf(ph); if(t.len + 1 > K_PATHMAX) t.len = K_PATHMAX - 1; if(idx > 0 || scheme == NULL) { knh_memcpy(buf, t.text, t.len); buf[t.len] = 0; ph->pbody = idx + 1; ph->plen = t.len; } else { knh_snprintf(buf, K_PATHMAX, "%s:%s", scheme, t.text); ph->pbody = knh_strlen(scheme) + 1; ph->plen = t.len + ph->pbody; } DBG_ASSERT(buf[ph->plen] == 0); ph->isRealPath = 0; }
static void knh_Bytes_expands(Ctx *ctx, knh_Bytes_t *ba, size_t newsize) { if(ba->capacity == 0) { newsize = knh_good_size(newsize); ba->bu.ubuf = (knh_uchar_t*)KNH_MALLOC(ctx, newsize); ba->capacity = newsize; } else { knh_uchar_t *buf = ba->bu.ubuf; size_t capacity = ba->capacity; knh_uchar_t *newa = (knh_uchar_t*)KNH_MALLOC(ctx, newsize); knh_memcpy(newa, buf, capacity); knh_bzero(&newa[capacity], newsize - capacity); ba->bu.ubuf = newa; ba->capacity = newsize; KNH_FREE(ctx, buf, capacity); if(unlikely(ctx->bufa == ba)) { KNH_SYSLOG(ctx, LOG_INFO, "ExtendedContextBuffer", "*newsize=%ld, pointer=(%p => %p)", newsize, buf, newa); knh_Bytes_checkstack(ctx, buf, buf + ba->bu.len, newa); } } }