/*** Create a unique temporary file. @function mkstemp @string templ pattern that ends in six 'X' characters @treturn[1] int open file descriptor @treturn[2] string path to file, if successful @return[2] nil @treturn[2] string error message @treturn[2] int errnum @see mkstemp(3) @usage P.mkstemp 'wooXXXXXX' */ static int Pmkstemp(lua_State *L) { const char *path = luaL_checkstring(L, 1); size_t path_len = strlen(path) + 1; void *ud; lua_Alloc lalloc; char *tmppath; int r; checknargs(L, 1); lalloc = lua_getallocf(L, &ud); if ((tmppath = lalloc(ud, NULL, 0, path_len)) == NULL) return pusherror(L, "lalloc"); strcpy(tmppath, path); r = mkstemp(tmppath); if (r != -1) { lua_pushinteger(L, r); lua_pushstring(L, tmppath); } lalloc(ud, tmppath, path_len, 0); return (r == -1) ? pusherror(L, path) : 2; }
/*** Receive a message from a socket. @function recv @int fd socket descriptor to act on @int count maximum number of bytes to receive @treturn[1] int received bytes, if successful @return[2] nil @treturn[2] string error message @treturn[2] int errnum @see recv(2) */ static int Precv(lua_State *L) { int fd = checkint(L, 1); int count = checkint(L, 2), ret; void *ud, *buf; lua_Alloc lalloc; checknargs(L, 2); lalloc = lua_getallocf(L, &ud); /* Reset errno in case lalloc doesn't set it */ errno = 0; if ((buf = lalloc(ud, NULL, 0, count)) == NULL && count > 0) return pusherror(L, "lalloc"); ret = recv(fd, buf, count, 0); if (ret < 0) { lalloc(ud, buf, count, 0); return pusherror(L, NULL); } lua_pushlstring(L, buf, ret); lalloc(ud, buf, count, 0); return 1; }
/*** Receive a message from a socket. @function recvfrom @int fd socket descriptor to act on @int count maximum number of bytes to receive @treturn[1] int received bytes @treturn[1] sockaddr address of message source, if successful @return[2] nil @treturn[2] string error message @treturn[2] int errnum @see recvfrom(2) */ static int Precvfrom(lua_State *L) { void *ud, *buf; socklen_t salen; struct sockaddr_storage sa; int r; int fd = checkint(L, 1); int count = checkint(L, 2); lua_Alloc lalloc; checknargs(L, 2); lalloc = lua_getallocf(L, &ud); /* Reset errno in case lalloc doesn't set it */ errno = 0; if ((buf = lalloc(ud, NULL, 0, count)) == NULL && count > 0) return pusherror(L, "lalloc"); salen = sizeof(sa); r = recvfrom(fd, buf, count, 0, (struct sockaddr *)&sa, &salen); if (r < 0) { lalloc(ud, buf, count, 0); return pusherror(L, NULL); } lua_pushlstring(L, buf, r); lalloc(ud, buf, count, 0); return 1 + pushsockaddrinfo(L, sa.ss_family, (struct sockaddr *)&sa); }
/*** Create a unique temporary directory. @function mkdtemp @string templ pattern that ends in six 'X' characters @treturn[1] string path to directory, if successful @return[2] nil @treturn[2] string error message @treturn[2] int errnum @see mkdtemp(3) */ static int Pmkdtemp(lua_State *L) { #if defined LPOSIX_2008_COMPLIANT const char *path = luaL_checkstring(L, 1); size_t path_len = strlen(path) + 1; void *ud; lua_Alloc lalloc; char *tmppath; char *r; checknargs(L, 1); lalloc = lua_getallocf(L, &ud); if ((tmppath = lalloc(ud, NULL, 0, path_len)) == NULL) return pusherror(L, "lalloc"); strcpy(tmppath, path); if ((r = mkdtemp(tmppath))) lua_pushstring(L, tmppath); lalloc(ud, tmppath, path_len, 0); return (r == NULL) ? pusherror(L, path) : 1; #else return binding_notimplemented(L, "mkdtemp", "C"); #endif }
/*** Send message to a message queue @function msgsnd @int id message queue identifier returned by @{msgget} @int type arbitrary message type @string message content @int[opt=0] flags optionally `IPC_NOWAIT` @treturn int 0, if successful @return[2] nil @treturn[2] string error message @treturn[2] int errnum @see msgsnd(2) */ static int Pmsgsnd(lua_State *L) { void *ud; lua_Alloc lalloc = lua_getallocf(L, &ud); struct { long mtype; char mtext[0]; } *msg; size_t len; size_t msgsz; ssize_t r; int msgid = checkint(L, 1); long msgtype = checklong(L, 2); const char *msgp = luaL_checklstring(L, 3, &len); int msgflg = optint(L, 4, 0); checknargs(L, 4); msgsz = sizeof(long) + len; if ((msg = lalloc(ud, NULL, 0, msgsz)) == NULL) return pusherror(L, "lalloc"); msg->mtype = msgtype; memcpy(msg->mtext, msgp, len); r = msgsnd(msgid, msg, msgsz, msgflg); lua_pushinteger(L, r); lalloc(ud, msg, msgsz, 0); return (r == -1 ? pusherror(L, NULL) : 1); }
/*** Receive message from a message queue @function msgrcv @int id message queue identifier returned by @{msgget} @int size maximum message size @int type message type (optional, default - 0) @int[opt=0] flags bitwise OR of zero or more of `IPC_NOWAIT`, `MSG_EXCEPT` and `MSG_NOERROR` @treturn[1] int message type from @{msgsnd} @treturn[1] string message text, if successful @return[2] nil @treturn[2] string error message @treturn[2] int errnum @see msgrcv(2) */ static int Pmsgrcv(lua_State *L) { int msgid = checkint(L, 1); size_t msgsz = checkint(L, 2); long msgtyp = optint(L, 3, 0); int msgflg = optint(L, 4, 0); void *ud; lua_Alloc lalloc; struct { long mtype; char mtext[0]; } *msg; checknargs(L, 4); lalloc = lua_getallocf(L, &ud); if ((msg = lalloc(ud, NULL, 0, msgsz)) == NULL) return pusherror(L, "lalloc"); int res = msgrcv(msgid, msg, msgsz, msgtyp, msgflg); if (res != -1) { lua_pushinteger(L, msg->mtype); lua_pushlstring(L, msg->mtext, res - sizeof(long)); } lalloc(ud, msg, msgsz, 0); return (res == -1) ? pusherror(L, NULL) : 2; }
/*** Current working directory for this process. @function getcwd @treturn[1] string path of current working directory, if successful @return[2] nil @treturn[2] string error message @treturn[2] int errnum @see getcwd(3) */ static int Pgetcwd(lua_State *L) { #ifdef __GNU__ char *b = get_current_dir_name(); checknargs(L, 0); if (b == NULL) /* we return the same error as below */ return pusherror(L, "."); return pushstringresult(b); #else long size = pathconf(".", _PC_PATH_MAX); void *ud; lua_Alloc lalloc; char *b, *r; checknargs(L, 0); lalloc = lua_getallocf(L, &ud); if (size == -1) size = _POSIX_PATH_MAX; /* FIXME: Retry if this is not long enough */ if ((b = lalloc(ud, NULL, 0, (size_t)size + 1)) == NULL) return pusherror(L, "lalloc"); r = getcwd(b, (size_t)size); if (r != NULL) lua_pushstring(L, b); lalloc(ud, b, (size_t)size + 1, 0); return (r == NULL) ? pusherror(L, ".") : 1; #endif }
/* addtree: add a node with w, at or below p */ struct tnode *addtree(struct tnode *p, char *w) { int cond; struct tlinen *nln; if (p == NULL) { p = talloc(); p->word = _strdup(w); p->left = p->right = NULL; p->linen = lalloc(); p->linen->line = cline; p->linen->next = NULL; } else if ((cond = strcmp(w, p->word)) == 0) { for (nln = p->linen; nln != NULL && nln->line != cline; nln = nln->next); if (nln == NULL) { nln = lalloc(); nln->line = cline; nln->next = p->linen; p->linen = nln; } } else if (cond < 0) p->left = addtree(p->left, w); else p->right = addtree(p->right, w); return p; }
static int Preadlink(lua_State *L) { char *b; struct stat s; const char *path = luaL_checkstring(L, 1); void *ud; lua_Alloc lalloc; ssize_t n, bufsiz; int err; checknargs(L, 1); lalloc = lua_getallocf(L, &ud); errno = 0; /* ignore outstanding unreported errors */ /* s.st_size is length of linkname, with no trailing \0 */ if (lstat(path, &s) < 0) return pusherror(L, path); /* diagnose non-symlinks */ if (!S_ISLNK(s.st_mode)) { lua_pushnil(L); lua_pushfstring(L, "%s: not a symbolic link", path); lua_pushinteger(L, EINVAL); return 3; } /* allocate a buffer for linkname, with no trailing \0 */ bufsiz = s.st_size > 0 ? s.st_size : PATH_MAX; if ((b = (char*)lalloc(ud, NULL, 0, bufsiz)) == NULL) return pusherror(L, "lalloc"); n = readlink(path, b, bufsiz); err = errno; /* save readlink error code, if any */ if (n > 0) lua_pushlstring(L, b, n); lalloc(ud, b, bufsiz, 0); /* report new errors from this function */ if (n < 0) { errno = err; /* restore readlink error code */ return pusherror(L, "readlink"); } else if (n < s.st_size) { lua_pushnil(L); lua_pushfstring(L, "%s: readlink wrote only %d of %d bytes", path, n, s.st_size); return 2; } return 1; }
/** * Get selection from clipboard * */ void clip_mch_request_selection(VimClipboard *cbd) { QClipboard *clip = QApplication::clipboard(); if ( clip->text((QClipboard::Mode)cbd->clipboardMode).size() == 0 ) { return; } QByteArray text = VimWrapper::convertTo(clip->text( (QClipboard::Mode)cbd->clipboardMode)); if ( text.isEmpty() ) { // This should not happen, but if it does vim // behaves badly so lets be extra carefull return; } char_u *buffer; buffer = lalloc( text.size(), TRUE); if (buffer == NULL) return; for (int i = 0; i < text.size(); ++i) { buffer[i] = text[i]; } clip_yank_selection(MAUTO, buffer, text.size(), cbd); vim_free(buffer); }
static int Pgetgroups(lua_State *L) /** getgroups() */ { int n_group_slots = getgroups(0, NULL); if (n_group_slots >= 0) { int n_groups; void *ud; gid_t *group; lua_Alloc lalloc = lua_getallocf(L, &ud); if ((group = lalloc(ud, NULL, 0, n_group_slots * sizeof *group)) == NULL) return 0; if ((n_groups = getgroups(n_group_slots, group)) >= 0) { int i; lua_createtable(L, n_groups, 0); for (i = 0; i < n_groups; i++) { lua_pushinteger(L, group[i]); lua_rawseti(L, -2, i + 1); } free (group); return 1; } free(group); } return 0; }
struct treenode *addtree2(struct treenode *node, char *word, int linenumber){ int condition; if(node == NULL){ //new word arrived so setup new struct and allocate memory node = talloc(); node->word = stringduplicate(word); //so we use strdup to get memory for string //had we not bothered and pointed to the string parameter (word), the memory //will be cleaned up after the function call allow the memory to be re-used //as it is from the stack //alloc gets heap which is there till we remove it node->count = 1; node->left = node->right = NULL; //set pointers left & right to NULL node->startinglinelist = lalloc(); node->startinglinelist->linenumbervalue = linenumber; node->startinglinelist->next = NULL; } else if((condition = strcmp(word, node->word)) == 0){ node->count++; linenode(node, linenumber); }else if(condition < 0) node->left = addtree2(node->left, word, linenumber); else node->right = addtree2(node->right, word, linenumber); return node; }
list_t *create_jims_ir_sensor_lookup_table() { list_t *lookup_table = lalloc(); ladd(lookup_table, (void *) new_ir_measurement(118, 360)); ladd(lookup_table, (void *) new_ir_measurement(146, 300)); ladd(lookup_table, (void *) new_ir_measurement(169, 245)); ladd(lookup_table, (void *) new_ir_measurement(199, 215)); ladd(lookup_table, (void *) new_ir_measurement(215, 195)); ladd(lookup_table, (void *) new_ir_measurement(224, 175)); ladd(lookup_table, (void *) new_ir_measurement(245, 165)); ladd(lookup_table, (void *) new_ir_measurement(250, 155)); ladd(lookup_table, (void *) new_ir_measurement(265, 145)); ladd(lookup_table, (void *) new_ir_measurement(279, 135)); ladd(lookup_table, (void *) new_ir_measurement(303, 125)); ladd(lookup_table, (void *) new_ir_measurement(327, 115)); ladd(lookup_table, (void *) new_ir_measurement(370, 105)); ladd(lookup_table, (void *) new_ir_measurement(394, 95)); ladd(lookup_table, (void *) new_ir_measurement(409, 85)); ladd(lookup_table, (void *) new_ir_measurement(457, 75)); ladd(lookup_table, (void *) new_ir_measurement(522, 65)); ladd(lookup_table, (void *) new_ir_measurement(562, 55)); ladd(lookup_table, (void *) new_ir_measurement(696, 45)); ladd(lookup_table, (void *) new_ir_measurement(912, 35)); ladd(lookup_table, (void *) new_ir_measurement(1019, 27)); return lookup_table; }
/* ARGSUSED */ int definemacro(int f, int n) { struct line *lp1, *lp2; macrocount = 0; if (macrodef) { ewprintf("already defining macro"); return (macrodef = FALSE); } /* free lines allocated for string arguments */ if (maclhead != NULL) { for (lp1 = maclhead->l_fp; lp1 != maclhead; lp1 = lp2) { lp2 = lp1->l_fp; free(lp1); } free(lp1); } if ((maclhead = lp1 = lalloc(0)) == NULL) return (FALSE); ewprintf("Defining Keyboard Macro..."); maclcur = lp1->l_fp = lp1->l_bp = lp1; return (macrodef = TRUE); }
/* decode a binary patch into a hunk list */ static struct flist *decode(const char *bin, Py_ssize_t len) { struct flist *l; struct frag *lt; int pos = 0; /* assume worst case size, we won't have many of these lists */ l = lalloc(len / 12); if (!l) return NULL; lt = l->tail; while (pos >= 0 && pos < len) { lt->start = getbe32(bin + pos); lt->end = getbe32(bin + pos + 4); lt->len = getbe32(bin + pos + 8); if (lt->start > lt->end) break; /* sanity check */ lt->data = bin + pos + 12; pos += 12 + lt->len; lt++; } if (pos != len) { if (!PyErr_Occurred()) PyErr_SetString(mpatch_Error, "patch cannot be decoded"); lfree(l); return NULL; } l->tail = lt; return l; }
/* * The argument "fmt" points to a format string. Append this line to the * buffer. Handcraft the EOL on the end. Return TRUE if it worked and * FALSE if you ran out of room. */ int addlinef(struct buffer *bp, char *fmt, ...) { va_list ap; struct line *lp; if ((lp = lalloc(0)) == NULL) return (FALSE); va_start(ap, fmt); if (vasprintf(&lp->l_text, fmt, ap) == -1) { lfree(lp); va_end(ap); return (FALSE); } lp->l_used = strlen(lp->l_text); va_end(ap); bp->b_headp->l_bp->l_fp = lp; /* Hook onto the end */ lp->l_bp = bp->b_headp->l_bp; bp->b_headp->l_bp = lp; lp->l_fp = bp->b_headp; bp->b_lines++; return (TRUE); }
/* push an unreal line onto the undo stack * lp should be the new line, _after_ insertion, so * lforw() and lback() are right */ int tag_for_undo(LINE *lp) { int status = FALSE; LINE *nlp; TRACE2((T_CALLED "tag_for_undo(%p)\n", lp)); if (needundocleanup) preundocleanup(); if (liscopied(lp)) { status = TRUE; } else if ((nlp = lalloc(LINENOTREAL, curbp)) == 0) { TRACE(("tag_for_undo: no memory\n")); status = ABORT; } else { set_lforw(nlp, lforw(lp)); set_lback(nlp, lback(lp)); pushline(nlp, BACKSTK(curbp)); lsetcopied(lp); FORWDOT(curbp).l = lp; FORWDOT(curbp).o = DOT.o; status = TRUE; } return2Code(status); }
static struct tnode *treeadd(struct tnode *p, char *w, char *uw, int line) { int cond; if (p == NULL) { // Create new tnode p = talloc(); p->word = mystrdup(w); p->words = lwalloc(); p->words->word = mystrdup(uw); p->words->next = NULL; p->wordcount = 1; p->lines = lalloc(); p->lines->linenum = line; p->lines->next = NULL; p->right = NULL; p->left = NULL; } else if ((cond = strncmp(w, p->word, MAXWORDSIZE)) == 0) { // Update existing tnode addword(p, uw); addline(p, line); } else if (cond < 0) // Traverse left-hand side of tree p->left = treeadd(p->left, w, uw, line); else // Traverse right-hand side of tree p->right = treeadd(p->right, w, uw, line); return p; }
void *Lmalloc(lua_State *L, size_t size) { void *ud; lua_Alloc lalloc = lua_getallocf(L, &ud); void *p = lalloc(L, NULL, 0, size); if(p == NULL) luaL_error(L, "malloc failed"); return p; }
struct linelist *addline(struct linelist *p, int l) { if (p == NULL) { p = lalloc(); p->next = NULL; p->line = l; } else p->next = addline(p->next, l); return p; }
struct line_link *addline(struct line_link *p) { struct line_link *temp = p; p = lalloc(); p->linum = linum; p->next = temp; return p; }
/*** Directory name of path. @function dirname @string path file to act on @treturn string directory part of *path* @see dirname(3) */ static int Pdirname(lua_State *L) { char *b; size_t len; void *ud; lua_Alloc lalloc; const char *path = luaL_checklstring(L, 1, &len); size_t path_len; checknargs(L, 1); path_len = strlen(path) + 1; lalloc = lua_getallocf(L, &ud); if ((b = (char*)lalloc(ud, NULL, 0, path_len)) == NULL) return pusherror(L, "lalloc"); lua_pushstring(L, dirname(strcpy(b,path))); lalloc(ud, b, path_len, 0); return 1; }
/* * Create a new buffer and put it in the list of * all buffers. */ static struct buffer * bnew(const char *bname) { struct buffer *bp; struct line *lp; int i; size_t len; bp = calloc(1, sizeof(struct buffer)); if (bp == NULL) { dobeep(); ewprintf("Can't get %d bytes", sizeof(struct buffer)); return (NULL); } if ((lp = lalloc(0)) == NULL) { free(bp); return (NULL); } bp->b_altb = bp->b_bufp = NULL; bp->b_dotp = lp; bp->b_doto = 0; bp->b_markp = NULL; bp->b_marko = 0; bp->b_flag = defb_flag; /* if buffer name starts and ends with '*', we ignore changes */ len = strlen(bname); if (len) { if (bname[0] == '*' && bname[len - 1] == '*') bp->b_flag |= BFIGNDIRTY; } bp->b_nwnd = 0; bp->b_headp = lp; bp->b_nmodes = defb_nmodes; TAILQ_INIT(&bp->b_undo); bp->b_undoptr = NULL; i = 0; do { bp->b_modes[i] = defb_modes[i]; } while (i++ < defb_nmodes); bp->b_fname[0] = '\0'; bp->b_cwd[0] = '\0'; bzero(&bp->b_fi, sizeof(bp->b_fi)); lp->l_fp = lp; lp->l_bp = lp; bp->b_bufp = bheadp; bheadp = bp; bp->b_dotline = bp->b_markline = 1; bp->b_lines = 1; if ((bp->b_bname = strdup(bname)) == NULL) { dobeep(); ewprintf("Can't get %d bytes", strlen(bname) + 1); return (NULL); } return (bp); }
/* * readbuf - reads in a buffer. */ void readbuf(char **buf) { register LINE *lp1; register LINE *lp2; register BUFFER *bp; register WINDOW *wp; register int i; register int s; char *sptr; /* pointer into buffer string */ int nbytes; char line[NLINE]; CELL ac; bp = curbp; bp->b_flag &= ~(BFTEMP|BFCHG); sptr = *buf; ac.a = 0; while((s=sgetline(&sptr,&nbytes,line,NLINE)) == FIOSUC || s == FIOLNG){ if ((lp1=lalloc(nbytes)) == NULL) { s = FIOERR; /* Keep message on the */ break; /* display. */ } lp2 = lback(curbp->b_linep); lp2->l_fp = lp1; lp1->l_fp = curbp->b_linep; lp1->l_bp = lp2; curbp->b_linep->l_bp = lp1; for (i=0; i<nbytes; ++i){ ac.c = line[i]; lputc(lp1, i, ac); } } for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) { if (wp->w_bufp == curbp) { wheadp->w_linep = lforw(curbp->b_linep); wheadp->w_dotp = lback(curbp->b_linep); wheadp->w_doto = 0; wheadp->w_markp = NULL; wheadp->w_marko = 0; wheadp->w_flag |= WFHARD; } } strncpy(bp->b_bname, "main", sizeof(bp->b_bname)); bp->b_bname[sizeof(bp->b_bname)-1] = '\0'; strncpy(bp->b_fname, "", sizeof(bp->b_fname)); bp->b_fname[sizeof(bp->b_fname)-1] = '\0'; bp->b_dotp = bp->b_linep; bp->b_doto = 0; }
int main() { int i ; int j = 0 ; int blks[100] ; init_heap() ; for( i=0; i<20; i++ ) blks[j++] = lalloc( rand()%500 ) ; dump_heap( "after alloc" ) ; lfree( &blks[10] ) ; lfree( &blks[11] ) ; dump_heap( "coalesce with upper" ) ; lfree( &blks[14] ) ; lfree( &blks[13] ) ; dump_heap( "coalesce with lower" ) ; lfree( &blks[5] ) ; lfree( &blks[7] ) ; lfree( &blks[6] ) ; dump_heap( "coalesce with both" ) ; for( i=0; i<20; i++ ) if (blks[i] != 0 ) { lfree( &blks[i] ) ; } dump_heap( "free everything " ) ; blks[0] = lalloc( 40000 ) ; dump_heap( "blew the top off" ) ; return 0 ; }
/* * Find a buffer, by name. Return a pointer * to the buffer structure associated with it. * If the buffer is not found * and the "cflag" is TRUE, create it. The "bflag" is * the settings for the flags in in buffer. */ struct buffer *bfind(char *bname, int cflag, int bflag) { struct buffer *bp; struct buffer *sb; /* buffer to insert after */ struct line *lp; bp = bheadp; while (bp != NULL) { if (strcmp(bname, bp->b_bname) == 0) return bp; bp = bp->b_bufp; } if (cflag != FALSE) { if ((bp = (struct buffer *)malloc(sizeof(struct buffer))) == NULL) return NULL; if ((lp = lalloc(0)) == NULL) { free((char *) bp); return NULL; } /* find the place in the list to insert this buffer */ if (bheadp == NULL || strcmp(bheadp->b_bname, bname) > 0) { /* insert at the beginning */ bp->b_bufp = bheadp; bheadp = bp; } else { sb = bheadp; while (sb->b_bufp != NULL) { if (strcmp(sb->b_bufp->b_bname, bname) > 0) break; sb = sb->b_bufp; } /* and insert it */ bp->b_bufp = sb->b_bufp; sb->b_bufp = bp; } /* and set up the other buffer fields */ bp->b_active = TRUE; bp->b_dotp = lp; bp->b_doto = 0; bp->b_markp = NULL; bp->b_marko = 0; bp->b_flag = bflag; bp->b_mode = gmode; bp->b_nwnd = 0; bp->b_linep = lp; strcpy(bp->b_fname, ""); strcpy(bp->b_bname, bname); lp->l_fp = lp; lp->l_bp = lp; } return bp; }
/* * Find a buffer, by name. Return a pointer to the BUFFER structure associated * with it. If the named buffer is found, but is a TEMP buffer (like the * buffer list), complain. */ BUFFER * bfind(char *fname) { BUFFER *bp, *sb; LINE *lp; bp = bheadp; while (bp != 0) { if (strcmp(fname, bp->b_fname) == 0) { if ((bp->b_flag & BFTEMP) != 0) { mlwrite("Cannot select builtin buffer"); return (FALSE); } return (bp); } bp = bp->b_bufp; } if ((bp = (BUFFER *) malloc(sizeof(BUFFER))) == NULL) return (FALSE); if ((lp = lalloc(0)) == NULL) { free(bp); return (BUFFER *)0; } /* find the place in the list to insert this buffer */ if (bheadp == NULL || strcmp(bheadp->b_fname, fname) > 0) { /* insert at the begining */ bp->b_bufp = bheadp; bheadp = bp; } else { sb = bheadp; while (sb->b_bufp != 0) { if (strcmp(sb->b_bufp->b_fname, fname) > 0) break; sb = sb->b_bufp; } /* and insert it */ bp->b_bufp = sb->b_bufp; sb->b_bufp = bp; } /* and set up the other buffer fields */ bp->b_dotp = lp; bp->b_doto = 0; bp->b_markp = 0; bp->b_marko = 0; bp->b_flag = 0; bp->b_linep = lp; bp->b_lines = 1; lp->l_fp = lp; lp->l_bp = lp; return (bp); }
static int eja_socket_read(lua_State *L) { int ret; int fd=luaL_checkint(L, 1); int count=luaL_checkint(L, 2); void *ud, *buf; lua_Alloc lalloc=lua_getallocf(L, &ud); if ( (buf=lalloc(ud, NULL, 0, count)) == NULL && count > 0) { lua_pushnil(L); } else { ret=recv(fd, buf, count, 0); if (ret >= 0) { lua_pushlstring(L, buf, ret); } else { lua_pushnil(L); } lalloc(ud, buf, count, 0); } return 1; }
static int Pmkstemp(lua_State *L) /** mkstemp(path) */ { const char *path = luaL_checkstring(L, 1); void *ud; lua_Alloc lalloc = lua_getallocf(L, &ud); char *tmppath; int res; if ((tmppath = lalloc(ud, NULL, 0, strlen(path) + 1)) == NULL) return 0; strcpy(tmppath, path); res = mkstemp(tmppath); if (res == -1) return pusherror(L, path); lua_pushinteger(L, res); lua_pushstring(L, tmppath); lalloc(ud, tmppath, 0, 0); return 2; }
/* * Insert a newline into the buffer at the current location of dot in the * current window. The funny ass-backwards way it does things is not a botch; * it just makes the last line in the file not a special case. Return TRUE if * everything works out and FALSE on error (memory allocation failure). The * update of dot and mark is a bit easier then in the above case, because the * split forces more updating. */ int lnewline(void) { char *cp1; char *cp2; struct line *lp1; struct line *lp2; int doto; struct window *wp; if (curbp->b_mode & MDVIEW) /* don't allow this command if */ return rdonly(); /* we are in read only mode */ #if SCROLLCODE lchange(WFHARD | WFINS); #else lchange(WFHARD); #endif lp1 = curwp->w_dotp; /* Get the address and */ doto = curwp->w_doto; /* offset of "." */ if ((lp2 = lalloc(doto)) == NULL) /* New first half line */ return FALSE; cp1 = &lp1->l_text[0]; /* Shuffle text around */ cp2 = &lp2->l_text[0]; while (cp1 != &lp1->l_text[doto]) *cp2++ = *cp1++; cp2 = &lp1->l_text[0]; while (cp1 != &lp1->l_text[lp1->l_used]) *cp2++ = *cp1++; lp1->l_used -= doto; lp2->l_bp = lp1->l_bp; lp1->l_bp = lp2; lp2->l_bp->l_fp = lp2; lp2->l_fp = lp1; wp = wheadp; /* Windows */ while (wp != NULL) { if (wp->w_linep == lp1) wp->w_linep = lp2; if (wp->w_dotp == lp1) { if (wp->w_doto < doto) wp->w_dotp = lp2; else wp->w_doto -= doto; } if (wp->w_markp == lp1) { if (wp->w_marko < doto) wp->w_markp = lp2; else wp->w_marko -= doto; } wp = wp->w_wndp; } return TRUE; }