bool convert_file(const char *from, const char *to, convert_type_e type) { if (!from || !to) return false; char temp[80]; bool ok = false; bool inplace = streq(from, to); if (inplace) { file_temp_name(temp, sizeof(temp)); to = temp; } FILE *fp = fopen(to, "w"); if (!fp) return false; mmap_t m = { .oflag = O_RDONLY }; if (mmap_open(from, &m) != 0) { convert_to_file(type, m.ptr, m.size, fp); mmap_close(&m); ok = true; } fclose(fp); if (inplace) { if (ok) rename(temp, from); else unlink(temp); } return ok; }
int main(int argc, char **argv) { if(argc < 2) { fprintf(stderr, "Please specify the file to parse.\n"); return 1; } ast *t; position p; const char *err; if((err = mmap_open(argv[1], &p))) { fprintf(stderr, "mmap_open failed on file \"%s\": %s\n", argv[1], err); return 1; } t = alloc_ast(); if(!t) { fprintf(stderr, "alloc_ast could not allocate memory.\n"); mmap_close(&p); return 1; } if((err = start_parser(t, &p))) { log_err(err, p); mmap_close(&p); } else { mmap_close(&p); ast_callback(t, argc - 2, argv + 2); } delete_ast(t); }
void mmap_close(struct vm_area_struct *vma){ struct mmap_info *info = (struct mmap_info *)vma->vm_private_data; struct page *page; int i; printk("[GNoM_km]: Mmap_closed called...\n"); #ifdef DO_GNOM_TX for(i=0; i<1024; ++i){ // Unpin all the pages #else for(i=0; i<512; ++i){ // Unpin all the pages #endif page = virt_to_page((size_t)info->data + (i*CPU_PAGE_SIZE)); if(PageReserved(page)){ ClearPageReserved(page); } } info->reference_cnt--; } static int device_mmap(struct file *filp, struct vm_area_struct *vma){ printk("[GNoM_km]: device_mmap called...\n"); vma->vm_ops = &mmap_vm_ops; vma->vm_flags |= VM_RESERVED; vma->vm_private_data = filp->private_data; mmap_open(vma); return 0; }
bool open() { int rc = mmap_open(&info_, file_, mode_, info_.flags); if (rc == 0) { opened_ = true; return true; } else return false; }
int my_mmap(struct file *filp, struct vm_area_struct *vma) { vma->vm_ops = &mmap_vm_ops; vma->vm_flags |= VM_RESERVED; /* assign the file private data to the vm private data */ vma->vm_private_data = filp->private_data; mmap_open(vma); return 0; }
void post_quote_file(const char *orig, const char *output, post_quote_e mode, bool mail, bool utf8, filter_t filter) { if (mode != POST_QUOTE_NOTHING) { FILE *fp = fopen(output, "w"); if (fp) { mmap_t m = { .oflag = O_RDONLY }; if (mmap_open(orig, &m) == 0) { post_quote_string(m.ptr, m.size, fp, mode, mail, utf8, filter); mmap_close(&m); } fclose(fp); } }
char *post_convert_to_utf8(const char *file) { char *utf8_content = NULL; mmap_t m = { .oflag = O_RDONLY }; if (mmap_open(file, &m) == 0) { if (m.size) { utf8_content = malloc(m.size * 2); convert(CONVERT_G2U, m.ptr, CONVERT_ALL, utf8_content, m.size * 2, NULL, NULL); mmap_close(&m); } } return utf8_content; }
static void override_info(void) { char file[HOMELEN]; sethomefile(file, currentuser.userid, "friends"); mmap_t m; m.oflag = O_RDONLY; if (mmap_open(file, &m) < 0) return; hash_t ht; if (hash_create(&ht, 0, NULL) < 0) { mmap_close(&m); return; } int count = m.size / sizeof(override_t); if (count > 0) { override_t *ov = m.ptr; for (int i = 0; i < count; i++) { hash_set(&ht, ov->id, HASH_KEY_STRING, ov->id); ov++; } time_t now = time(NULL); struct user_info *uinfo = utmpshm->uinfo; struct userec *user; const char *ip; int idle; for (int i = 0; i < MAXACTIVE; ++i) { if (uinfo->active && !(uinfo->invisible && !HAS_PERM(PERM_SEECLOAK)) && hash_get(&ht, uinfo->userid, HASH_KEY_STRING)) { user = uidshm->passwd + (uinfo->uid - 1); if (HAS_DEFINE(user->userdefine, DEF_NOTHIDEIP)) ip = mask_host(uinfo->from); else ip = "......"; if (uinfo->mode == BBSNET) idle = 0; else idle = (now - uinfo->idle_time) / 60; printf("<ov id='%s' action='%s' idle='%d' ip='%s'>", uinfo->userid, mode_type(uinfo->mode), idle, ip); xml_fputs(uinfo->username, stdout); printf("</ov>"); } uinfo++; } } hash_destroy(&ht); mmap_close(&m); }
int my_mmap(struct file *filp, struct vm_area_struct *vma){ // printk(KERN_INFO "my_mmap()\n"); mmap_pointer = kmalloc(sizeof(struct mmap_info), GFP_KERNEL); /* obtain new memory */ mmap_pointer->data = (char *)get_zeroed_page(GFP_KERNEL); filp->private_data = mmap_pointer; vma->vm_ops = &mmap_vm_ops; vma->vm_flags |= VM_RESERVED; /* assign the file private data to the vm private data */ vma->vm_private_data = filp->private_data; mmap_open(vma); return 0; }
int bbsmybrd_main(void) { if (!loginok) return BBS_ELGNREQ; int type = strtol(getparm("type"), NULL, 10); if (type != 0) return read_submit(); // Read '.goodbrd'. char file[HOMELEN]; sethomefile(file, currentuser.userid, ".goodbrd"); mmap_t m; m.oflag = O_RDONLY; if (mmap_open(file, &m) < 0) return BBS_ENOFILE; struct goodbrdheader *iter, *end; int num = m.size / sizeof(struct goodbrdheader); if (num > GOOD_BRC_NUM) num = GOOD_BRC_NUM; end = (struct goodbrdheader *)m.ptr + num; // Print 'bid's of favorite boards. xml_header("bbs"); printf("<bbsmybrd "); print_session(); printf(" limit='%d'>", GOOD_BRC_NUM); for (iter = m.ptr; iter != end; iter++) { if (!gbrd_is_custom_dir(iter)) printf("<my bid='%d'/>", iter->pos + 1); } mmap_close(&m); // Print all boards available. struct boardheader *b; for (int i = 0; i < MAXBOARD; i++) { b = bcache + i; if (b->filename[0] <= 0x20 || b->filename[0] > 'z') continue; if (!hasreadperm(¤tuser, b)) continue; printf("<mbrd bid='%d' desc='%s' %s/>", i + 1, b->title + 11, is_board_dir(b) ? "dir='1'" : ""); } printf("</bbsmybrd>"); return 0; }
int compile(XML_Parser p, const char *templ, const char *src, const char *dst) { char *out, *cp, *buf; size_t sz, sargsz; int fd, rc; FILE *f; struct pargs arg; struct article *sargs; memset(&arg, 0, sizeof(struct pargs)); rc = 0; buf = out = NULL; fd = -1; f = NULL; sz = 0; sargs = NULL; sargsz = 0; if ( ! sblg_parse(p, src, &sargs, &sargsz)) goto out; if (0 == sargsz) { fprintf(stderr, "%s: contains no article\n", src); goto out; } else if (sargsz > 1) fprintf(stderr, "%s: contains multiple " "articles (using the first)\n", src); arg.article = &sargs[0]; if (NULL == dst) { /* * If we have no output file name, then name it the same * as the input but with ".html" at the end. * However, if we have ".xml", then replace that with * ".html". */ sz = strlen(src); if (NULL == (cp = strrchr(src, '.')) || strcasecmp(cp + 1, "xml")) { /* Append .html to input name. */ out = xmalloc(sz + 6); strlcpy(out, src, sz + 6); strlcat(out, ".html", sz + 6); } else { /* Replace .xml with .html. */ out = xmalloc(sz + 2); strlcpy(out, src, sz - 2); strlcat(out, "html", sz + 2); } } else out = xstrdup(dst); f = stdout; if (strcmp(out, "-") && NULL == (f = fopen(out, "w"))) { perror(out); goto out; } if ( ! mmap_open(templ, &fd, &buf, &sz)) goto out; arg.f = f; arg.src = src; arg.dst = strcmp(out, "-") ? out : NULL; arg.p = p; XML_ParserReset(p, NULL); XML_SetElementHandler(p, template_begin, template_end); XML_SetDefaultHandlerExpand(p, template_text); XML_SetUserData(p, &arg); if (XML_STATUS_OK != XML_Parse(p, buf, (int)sz, 1)) { fprintf(stderr, "%s:%zu:%zu: %s\n", templ, XML_GetCurrentLineNumber(p), XML_GetCurrentColumnNumber(p), XML_ErrorString(XML_GetErrorCode(p))); goto out; } xmltextx(arg.f, arg.buf, arg.dst, arg.article, 1, 0); xmlstrflush(arg.buf, &arg.bufsz); fputc('\n', f); rc = 1; out: mmap_close(fd, buf, sz); if (NULL != f && stdin != f) fclose(f); sblg_free(sargs, sargsz); free(out); free(arg.buf); return(rc); }
int web_sector(void) { int sid = 0; board_t parent = { .id = 0 }; db_res_t *res = NULL; const char *sname = web_get_param("s"); if (*sname) { res = db_query("SELECT id, descr" " FROM board_sectors WHERE name = %s", sname); if (!res || db_res_rows(res) < 1) { db_clear(res); return BBS_EINVAL; } } else { const char *pname = web_get_param("board"); if (*pname) get_board(pname, &parent); else get_board_by_bid(strtol(web_get_param("bid"), NULL, 10), &parent); if (!parent.id || !(parent.flag & BOARD_FLAG_DIR) || !has_read_perm(&parent)) return BBS_ENOBRD; } xml_header(NULL); printf("<bbsboa link='%sdoc' ", get_post_list_type_string()); if (*sname) { char path[HOMELEN]; sprintf(path, "%s/info/egroup%d/icon.jpg", BBSHOME, (int) strtol(sname, NULL, 16)); if (dashf(path)) printf(" icon='%s'", path); const char *utf8_sector = db_get_value(res, 0, 1); if (web_request_type(UTF8)) { printf(" title='%s'>", utf8_sector); } else { GBK_BUFFER(sector, BOARD_SECTOR_NAME_CCHARS); convert_u2g(utf8_sector, gbk_sector); printf(" title='%s'>", gbk_sector); } sid = db_get_integer(res, 0, 0); db_clear(res); } else { if (web_request_type(UTF8)) { printf(" dir= '1' title='%s'>", parent.descr); } else { GBK_BUFFER(descr, BOARD_DESCR_CCHARS); convert_u2g(parent.descr, gbk_descr); printf(" dir= '1' title='%s'>", gbk_descr); } } if (sid) res = db_query(BOARD_SELECT_QUERY_BASE "WHERE b.sector = %d", sid); else res = db_query(BOARD_SELECT_QUERY_BASE "WHERE b.parent = %d", parent.id); if (res && db_res_rows(res) > 0) show_board(res); db_clear(res); print_session(); printf("</bbsboa>"); return 0; } int bbsclear_main(void) { if (!session_id()) return BBS_ELGNREQ; board_t board; if (!get_board(web_get_param("board"), &board) || !has_read_perm(&board)) return BBS_ENOBRD; session_set_board(board.id); const char *start = web_get_param("start"); brc_init(currentuser.userid, board.name); brc_clear_all(); brc_sync(currentuser.userid); char buf[STRLEN]; snprintf(buf, sizeof(buf), "doc?board=%s&start=%s", board.name, start); http_header(); refreshto(0, buf); printf("</head></html>"); return 0; } int bbsnot_main(void) { board_t board; if (!get_board(web_get_param("board"), &board) || !has_read_perm(&board)) return BBS_ENOBRD; if (board.flag & BOARD_FLAG_DIR) return BBS_EINVAL; session_set_board(board.id); char fname[HOMELEN]; snprintf(fname, sizeof(fname), "vote/%s/notes", board.name); mmap_t m; m.oflag = O_RDONLY; if (mmap_open(fname, &m) < 0) return BBS_ENOFILE; xml_header(NULL); printf("<bbsnot brd='%s'>", board.name); xml_fputs2((char *) m.ptr, m.size); mmap_close(&m); print_session(); printf("</bbsnot>"); return 0; }
// TODO: Handle user-defined directories. static int read_submit(void) { if (!loginok) return BBS_ELGNREQ; if (parse_post_data() < 0) return BBS_EINVAL; // Read parameters. bool boards[MAXBOARD] = {0}; int num = 0; for (int i = 0; i < param_num; i++) { if (!strcasecmp(param_val[i], "on")) { int bid = strtol(param_name[i], NULL, 10); if (bid > 0 && bid <= MAXBOARD && hasreadperm(¤tuser, bcache + bid - 1)) { boards[bid - 1] = true; ++num; } } } if (num > GOOD_BRC_NUM) return BBS_EBRDQE; if (num <= 0) return BBS_EINVAL; // Read '.goodbrd'. char file[HOMELEN]; sethomefile(file, currentuser.userid, ".goodbrd"); mmap_t m; m.oflag = O_RDWR; if (mmap_open(file, &m) < 0) return BBS_ENOFILE; // TODO: empty? if (mmap_truncate(&m, num * sizeof(struct goodbrdheader)) < 0) { return BBS_EINTNL; } struct goodbrdheader *iter, *end; end = (struct goodbrdheader *)m.ptr + num; // Remove deselected boards. struct goodbrdheader *dst = m.ptr; int id = 0; for (iter = m.ptr; iter != end; ++iter) { if (boards[iter->pos] == true) { boards[iter->pos] = false; id++; if (iter != dst) { iter->id = id; iter->pid = 0; *dst = *iter; } ++dst; } } // Write out newly selected boards. for (int i = 0; i < MAXBOARD; ++i) { if (boards[i] == true) { id++; if (id > GOOD_BRC_NUM) break; dst->id = num; dst->pid = 0; dst->pos = i; dst->flag = bcache[i].flag; strlcpy(dst->filename, bcache[i].filename, sizeof(dst->filename)); strlcpy(dst->title, bcache[i].title, sizeof(dst->title)); ++dst; } } mmap_close(&m); xml_header("bbs"); printf("<bbsmybrd "); print_session(); printf(" limit='%d' selected='%d'></bbsmybrd>", GOOD_BRC_NUM, num); return 0; }