ssize_t mf_write(mf_handle_t mf, const void* buf, size_t count, off_t offset) { int res_code = 0; if((buf == NULL) || (mf == MF_OPEN_FAILED)) { errno = EINVAL; write_log_to_file(Error,"mf_write: invalid input!\n"); return -1; } ch_pool_t* ch_pool = (ch_pool_t*)mf; sem_wait(&(ch_pool -> lock)); off_t file_size = ch_pool -> file_size; size_t chunk_size_min = ch_pool -> chunk_size_min; if((offset < 0) || (offset > file_size)) { sem_post(&(ch_pool -> lock)); return -1; } if(file_size == 0) { write_log_to_file(Error,"mf_write: size of file = 0!\n"); sem_post(&(ch_pool -> lock)); return 0; } if((offset + count > file_size) && (offset < file_size)) { count = file_size - offset; } off_t index = offset/chunk_size_min; off_t length = 0; if((offset + count) % chunk_size_min != 0) { length = (offset + count)/chunk_size_min - index + 1; } else { length = (offset+ count)/chunk_size_min - index; } chunk_t* chunk_ptr = take_value_ptr(ch_pool -> h_table, index, length); if(chunk_ptr == NULL) { res_code = ch_init(index, length, ch_pool); if(res_code) { write_log_to_file(Error,"mf_read: initialization of chunk failed!\n"); sem_post(&(ch_pool -> lock)); return res_code; } chunk_ptr = take_value_ptr(ch_pool -> h_table, index, length); } memcpy((void*)((char*)(chunk_ptr -> data) + offset - (chunk_ptr -> index) * chunk_size_min), buf, count); sem_post(&(ch_pool -> lock)); return count; }
static int ch_construct(cpool_t *cpool, off_t idx, off_t len, chunk_t **ch) { int err = _malloc(sizeof(chunk_t), (void **)ch); if(err) return err; err = ch_init(idx, len, cpool, *ch); if(err) return err; err = cpool_add(*ch); if(err) return err; return 0; }
static void *worker_loop(void *p) { struct work_info *wi = p; struct channel ch; char desc[40]; struct net_socket sock; while (wi->active) { if (net_accept(&wi->li->sock, &sock, sizeof(desc), desc)) break; ch_init(&ch, sock, desc); ht_process(&ch); ch_close(&ch); } return NULL; }
static int net_accept(struct listen *li, struct channel *ch) { struct sockaddr_storage ss; socklen_t ss_len = sizeof(ss); int newfd; do { newfd = accept(li->fd, (struct sockaddr*)&ss, &ss_len); } while (newfd < 0 && errno == EINTR); if (newfd < 0) { sys_error(); return -1; } ch_init(ch, newfd); return 0; }
/* el_init(): * Initialize editline and set default parameters. */ public EditLine * el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr) { EditLine *el = (EditLine *) el_malloc(sizeof(EditLine)); if (el == NULL) return (NULL); memset(el, 0, sizeof(EditLine)); el->el_infile = fin; el->el_outfile = fout; el->el_errfile = ferr; el->el_infd = fileno(fin); if ((el->el_prog = el_strdup(prog)) == NULL) { el_free(el); return NULL; } /* * Initialize all the modules. Order is important!!! */ el->el_flags = 0; if (term_init(el) == -1) { el_free(el->el_prog); el_free(el); return NULL; } (void) key_init(el); (void) map_init(el); if (tty_init(el) == -1) el->el_flags |= NO_TTY; (void) ch_init(el); (void) search_init(el); (void) hist_init(el); (void) prompt_init(el); (void) sig_init(el); (void) read_init(el); return (el); }
/* * Main command processor. * Accept and execute commands until a quit command, then return. */ void commands(void) { int c, action; last_mca = 0; nscroll = (sc_height + 1) / 2; for (;;) { mca = 0; number = 0; /* * See if any signals need processing. */ if (sigs) { psignals(); if (quitting) quit(); } /* * Display prompt and accept a character. */ CMD_RESET; if (!prompt()) { next_file(1); continue; } noprefix(); c = getcc(); again: if (sigs) continue; /* * If we are in a multicharacter command, call mca_char. * Otherwise we call cmd_decode to determine the * action to be performed. */ if (mca) switch (mca_char(c)) { case MCA_MORE: /* * Need another character. */ c = getcc(); goto again; case MCA_DONE: /* * Command has been handled by mca_char. * Start clean with a prompt. */ continue; case NO_MCA: /* * Not a multi-char command * (at least, not anymore). */ break; } /* decode the command character and decide what to do. */ switch (action = cmd_decode(c)) { case A_DIGIT: /* first digit of a number */ start_mca(A_DIGIT, ":"); goto again; case A_F_SCREEN: /* forward one screen */ CMD_EXEC; if (number <= 0 && (number = sc_window) <= 0) number = sc_height - 1; forward(number, 1); break; case A_B_SCREEN: /* backward one screen */ CMD_EXEC; if (number <= 0 && (number = sc_window) <= 0) number = sc_height - 1; backward(number, 1); break; case A_F_LINE: /* forward N (default 1) line */ CMD_EXEC; forward(number <= 0 ? 1 : number, 0); break; case A_B_LINE: /* backward N (default 1) line */ CMD_EXEC; backward(number <= 0 ? 1 : number, 0); break; case A_F_SCROLL: /* forward N lines */ CMD_EXEC; if (number > 0) nscroll = number; forward(nscroll, 0); break; case A_B_SCROLL: /* backward N lines */ CMD_EXEC; if (number > 0) nscroll = number; backward(nscroll, 0); break; case A_FREPAINT: /* flush buffers and repaint */ if (!ispipe) { ch_init(0, 0); clr_linenum(); } /* FALLTHROUGH */ case A_REPAINT: /* repaint the screen */ CMD_EXEC; repaint(); break; case A_GOLINE: /* go to line N, default 1 */ CMD_EXEC; if (number <= 0) number = 1; jump_back(number); break; case A_PERCENT: /* go to percent of file */ CMD_EXEC; if (number < 0) number = 0; else if (number > 100) number = 100; jump_percent(number); break; case A_GOEND: /* go to line N, default end */ CMD_EXEC; if (number <= 0) jump_forw(); else jump_back(number); break; case A_STAT: /* print file name, etc. */ longprompt = 1; continue; case A_QUIT: /* exit */ quit(); case A_F_SEARCH: /* search for a pattern */ case A_B_SEARCH: if (number <= 0) number = 1; start_mca(action, (action==A_F_SEARCH) ? "/" : "?"); last_mca = mca; wsearch = 1; c = getcc(); if (c == '!') { /* * Invert the sense of the search; set wsearch * to 0 and get a new character for the start * of the pattern. */ start_mca(action, (action == A_F_SEARCH) ? "!/" : "!?"); wsearch = 0; c = getcc(); } goto again; case A_AGAIN_SEARCH: /* repeat previous search */ if (number <= 0) number = 1; if (wsearch) start_mca(last_mca, (last_mca == A_F_SEARCH) ? "/" : "?"); else start_mca(last_mca, (last_mca == A_F_SEARCH) ? "!/" : "!?"); CMD_EXEC; (void)search(mca == A_F_SEARCH, (char *)NULL, number, wsearch); break; case A_HELP: /* help */ lower_left(); clear_eol(); fputs("help", stdout); CMD_EXEC; help(); break; case A_TAGFILE: /* tag a new file */ CMD_RESET; start_mca(A_TAGFILE, "Tag: "); c = getcc(); goto again; case A_FILE_LIST: /* show list of file names */ CMD_EXEC; showlist(); repaint(); break; case A_EXAMINE: /* edit a new file */ CMD_RESET; start_mca(A_EXAMINE, "Examine: "); c = getcc(); goto again; case A_VISUAL: /* invoke the editor */ if (ispipe) { error("Cannot edit standard input"); break; } CMD_EXEC; editfile(); ch_init(0, 0); clr_linenum(); break; case A_NEXT_FILE: /* examine next file */ if (number <= 0) number = 1; next_file(number); break; case A_PREV_FILE: /* examine previous file */ if (number <= 0) number = 1; prev_file(number); break; case A_SETMARK: /* set a mark */ lower_left(); clear_eol(); start_mca(A_SETMARK, "mark: "); c = getcc(); if (c == erase_char || c == kill_char) break; setmark(c); break; case A_GOMARK: /* go to mark */ lower_left(); clear_eol(); start_mca(A_GOMARK, "goto mark: "); c = getcc(); if (c == erase_char || c == kill_char) break; gomark(c); break; case A_PREFIX: /* * The command is incomplete (more chars are needed). * Display the current char so the user knows what's * going on and get another character. */ if (mca != A_PREFIX) start_mca(A_PREFIX, ""); if (CONTROL_CHAR(c)) { putchar('^'); c = CARAT_CHAR(c); } putchar(c); c = getcc(); goto again; default: putchar('\7'); break; } } }
void *mf_map(mf_handle_t mf, off_t offset, size_t size, mf_mapmem_handle_t *mapmem_handle) { int res_code = 0; if((mf == MF_OPEN_FAILED) || (mapmem_handle == NULL)) { errno = EINVAL; *mapmem_handle = MF_OPEN_FAILED; write_log_to_file(Error,"mf_map: invalid input!\n"); return NULL; } ch_pool_t* ch_pool = (ch_pool_t *)mf; sem_wait(&(ch_pool -> lock)); chunk_t** chunk = (chunk_t**)mapmem_handle; off_t file_size = ch_pool -> file_size; size_t chunk_size_min = ch_pool -> chunk_size_min; if((offset > file_size) || (offset < 0)) { *mapmem_handle = MF_OPEN_FAILED; write_log_to_file(Error,"mf_map: invalid input!\n"); sem_post(&(ch_pool -> lock)); return NULL; } if((offset + size > file_size) && (offset < file_size)) { size = file_size - offset; if(size % chunk_size_min != 0) { } } if(size == 0) { write_log_to_file(Error,"mf_map: size of mapping = 0!\n"); sem_post(&(ch_pool -> lock)); return NULL; } off_t index = offset / chunk_size_min; off_t length = 0; if((offset + size) % chunk_size_min != 0) { length = (offset + size)/chunk_size_min - index + 1; } else { length = (offset + size)/chunk_size_min - index; } if(ch_pool -> flag == 0) { *chunk = take_value_ptr(ch_pool -> h_table, index, length); } else { *chunk = find_in_range(ch_pool, offset, size); } if ((*chunk) == NULL) { int res_code = ch_init(index, length, ch_pool); if(res_code) { write_log_to_file(Error,"mf_map: initialization of chunk failed!\n"); *mapmem_handle = MF_MAP_FAILED; sem_post(&(ch_pool -> lock)); return NULL; } *chunk = take_value_ptr(ch_pool -> h_table, index, length); } void* ptr = NULL; ptr = ((*chunk) -> data) + offset - ((*chunk) -> index)*chunk_size_min; if(ptr == MF_MAP_FAILED) { write_log_to_file(Error,"mf_map: pointer to memory is NULL!\n"); sem_post(&(ch_pool -> lock)); return MF_MAP_FAILED; } sem_post(&(ch_pool -> lock)); return ptr; }