static void popi2(void) { istk_t *istk; sym_t *m; initstk = (istk = initstk)->i_nxt; if (initstk == NULL) lerror("popi2() 1"); free(istk); istk = initstk; istk->i_cnt--; if (istk->i_cnt < 0) lerror("popi2() 3"); /* * If the removed element was a structure member, we must go * to the next structure member. */ if (istk->i_cnt > 0 && istk->i_type->t_tspec == STRUCT) { do { m = istk->i_mem = istk->i_mem->s_nxt; if (m == NULL) lerror("popi2() 2"); } while (m->s_field && m->s_name == unnamed); istk->i_subt = m->s_type; } }
value_t read_sexpr(FILE *f) { value_t v; switch (peek(f)) { case TOK_CLOSE: take(); lerror("read: error: unexpected ')'\n"); case TOK_DOT: take(); lerror("read: error: unexpected '.'\n"); case TOK_SYM: case TOK_NUM: take(); return tokval; case TOK_QUOTE: take(); v = read_sexpr(f); PUSH(v); v = cons_("E, cons(&Stack[SP-1], &NIL)); POPN(2); return v; case TOK_OPEN: take(); PUSH(NIL); read_list(f, &Stack[SP-1]); return POP(); } return NIL; }
static int process_download(int sock, struct trans_data_t *ptrans) { assert(ptrans != NULL); char file_path[MAX_FILE_PATH_LENGTH] = { '\0' }; snprintf(file_path, MAX_FILE_PATH_LENGTH-1, "%s%s", STORE_SERVER_DATA_PATH, ptrans->filename); file_path[MAX_FILE_PATH_LENGTH-1] = '\0'; if (-1 == get_file_size(file_path, &(ptrans->data_len))) { lwarn("call get_fail_size failed failed"); goto FAIL; } ldebug("be called, filename: %s, size: %d", ptrans->filename, ptrans->data_len); if (-1 == read_file(ptrans, file_path)) { lerror("read file failed, file_path: %s", file_path); goto FAIL; } if (-1 == send_data(sock, ptrans, get_trans_data_t_size(ptrans))) { lerror("send data failed, sock: %d, data_len: %d", sock, ptrans->data_len); goto ERR; } return 0; FAIL: assert(0); //TODO:未完成 return -1; ERR: return -1; }
static int client_upload(int sock, char *file_path) { assert(file_path != NULL); assert(sock >= 0); ldebug("upload file(%s), please wait a moment...", file_path); int file_size = 0; int total_len = 0; struct trans_data_t *ptrans = NULL; if (-1 == get_file_size(file_path, &file_size)) { lerror("call get_file_size() failed"); goto ERR; } if (!file_size) { lwarn("file is null, file_path: %s", file_path); goto ERR; } total_len = sizeof(struct trans_data_t) + file_size; ptrans = (struct trans_data_t *)calloc(1, total_len); if (NULL == ptrans) { lerror("malloc memory failed, err:%s", strerror(errno)); abort(); } ptrans->cmd = UD_UPLOAD; ptrans->data_len = file_size; snprintf(ptrans->filename, MAX_FILE_NAME_LENGTH-1, "%s", basename(file_path)); ptrans->filename[MAX_FILE_NAME_LENGTH - 1] = '0'; if (-1 == read_file(ptrans, file_path)) { lerror("call read_data() failed, file_path: %s", file_path); } if (-1 == send_data(sock, ptrans, get_trans_data_t_size(ptrans))) { lerror("call send_data() failed, sock: %d, data_len: %d", sock, ptrans->data_len); return -1; } ldebug("upload file success"); return 0; ERR: lwarn("upload file failed"); if (ptrans != NULL) { free(ptrans); } return -1; }
static char *get(char *type, char *line, int itype, int n) { int l; l = hempty(line) + my_strlen(type) + hempty(line + hempty(line) + my_strlen(type)); if (line[l] != '"') lerror(INVALID(itype), n); if (!IN(line + l + 1, '"')) lerror(INVALID(itype), n); return (my_strndup(line + l + 1, my_strilen(line + l + 1, '"'))); }
/* * switch_expr stmnt */ void switch2(void) { int nenum = 0, nclab = 0; sym_t *esym; clst_t *cl; if (cstk->c_swtype == NULL) lerror("switch2() 1"); /* * If the switch expression was of type enumeration, count the case * labels and the number of enumerators. If both counts are not * equal print a warning. */ if (cstk->c_swtype->t_isenum) { nenum = nclab = 0; if (cstk->c_swtype->t_enum == NULL) lerror("switch2() 2"); for (esym = cstk->c_swtype->t_enum->elem; esym != NULL; esym = esym->s_nxt) { nenum++; } for (cl = cstk->c_clst; cl != NULL; cl = cl->cl_nxt) nclab++; if (hflag && eflag && nenum != nclab && !cstk->c_default) { /* enumeration value(s) not handled in switch */ warning(206); } } if (cstk->c_break) { /* * end of switch alway reached (c_break is only set if the * break statement can be reached). */ reached = 1; } else if (!cstk->c_default && (!hflag || !cstk->c_swtype->t_isenum || nenum != nclab)) { /* * there are possible values which are not handled in * switch */ reached = 1; } /* * otherwise the end of the switch expression is reached * if the end of the last statement inside it is reached. */ popctrl(T_SWITCH); }
void xml_scan(CLASS_DISPLAY *d, int ac, char **av) { char *s; BOOL check; FD xml; check = FALSE; display_init(d); if (ac != 2 || (xml = open(av[1], O_RDWR)) == -1) lerror(USAGE); if ((check = check_fd(get_next_line(xml), xml, INIT, &s)) == FALSE) lerror(USAGE);// || WRONG_FILE ? while ((check = check_fd(s, xml, END, &s))) scan_line(d, xml, s); }
int main(void) { lua_State *L; L = luaL_newstate(); luaL_openlibs(L); if (luaL_loadstring(L, "require \"test.test\"")) lerror(L, "luaL_loadstring() failed"); if (lua_pcall(L, 0, 0, 0)) lerror(L, "lua_pcall() failed"); lua_close(L); return 0; }
int create_socket_stream(const char* hostname, const char* servname) { struct addrinfo hints; struct addrinfo *result, *rp; int sfd, s; memset(&hints, 0, sizeof(struct addrinfo)); /* init hints */ hints.ai_flags = AI_PASSIVE; /* Not only loopback if hostname is null */ hints.ai_family = AF_INET; /* AF_UNSPEC/AF_INET/AF_INET6 */ hints.ai_socktype = SOCK_STREAM; /* STREAM(TCP), DGRAM(UDP), SEQPACKET(?) */ hints.ai_protocol = 0; /* Any protocol */ hints.ai_canonname = NULL; hints.ai_addr = NULL; hints.ai_next = NULL; s = getaddrinfo(hostname, servname, &hints, &result); if (s != 0) { lprintf(LOG_NOTICE, "Error: %s", gai_strerror(s)); return -1; } for (rp = result; rp != NULL; rp = rp->ai_next) { sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (sfd == -1) { lerror(LOG_NOTICE, "socket"); continue; } if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0) { break; /* Success */ } else { lerror(LOG_NOTICE, "bind"); lprintf(LOG_NOTICE, "Tried address: %s, service: ", inet_ntoa(((const struct sockaddr_in*)(rp->ai_addr))->sin_addr), servname); } close(sfd); } freeaddrinfo(result); /* No longer needed */ if (rp == NULL) { /* No address succeeded */ lprintf(LOG_NOTICE, "Error: Could not bind"); return -1; } return sfd; }
int process_redirect(struct request *r) { char buf[STRLEN]; char *c; FILE *fp; if (r->method != M_GET && r->method != M_HEAD) { r->error = "invalid method for redirect"; return 405; } fp = fopen(r->path_translated, "r"); if (fp == 0) { lerror("fopen"); r->error = "cannot open redirect file"; return 500; } fgets(buf, STRLEN, fp); fclose(fp); c = strchr(buf, '\n'); if (c) *c = 0; else { r->error = "redirect url too long"; return 500; } escape_url(buf, r->newloc); r->location = r->newloc; return 302; }
static void reap_children(void) { pid_t pid; int errno_save, status, exitstatus, termsig; errno_save = errno; while (1) { pid = waitpid(-1, &status, WNOHANG); if (pid <= 0) break; if (WIFEXITED(status)) { ++stats.exited_children; exitstatus = WEXITSTATUS(status); if (exitstatus || debug) log_d("child process %d exited with status %d", (int) pid, exitstatus); } else if (WIFSIGNALED(status)) { ++stats.exited_children; termsig = WTERMSIG(status); log_d("child process %d killed by signal %d", (int) pid, termsig); } else log_d("child process %d stopped!?", (int) pid); } if (pid < 0 && errno != ECHILD) lerror("waitpid"); errno = errno_save; }
static int analyze_dkmount_tree(qelem *q, fsi_mount *parent, disk_fs *dk) { fsi_mount *mp; int errors = 0; ITER(mp, fsi_mount, q) { fsi_log("Mount %s:", mp->m_name); if (parent) { char n[MAXPATHLEN]; xsnprintf(n, sizeof(n), "%s/%s", parent->m_name, mp->m_name); if (*mp->m_name == '/') lerror(mp->m_ioloc, "sub-directory %s of %s starts with '/'", mp->m_name, parent->m_name); else if (STREQ(mp->m_name, "default")) lwarning(mp->m_ioloc, "sub-directory of %s is named \"default\"", parent->m_name); fsi_log("Changing name %s to %s", mp->m_name, n); XFREE(mp->m_name); mp->m_name = strdup(n); } mp->m_name_len = strlen(mp->m_name); mp->m_parent = parent; mp->m_dk = dk; if (mp->m_mount) analyze_dkmount_tree(mp->m_mount, mp, dk); }
/* * Check and fill in "exportfs" details. * Make sure the m_exported field references * the most local node with an "exportfs" entry. */ static int check_exportfs(qelem *q, fsi_mount *e) { fsi_mount *mp; int errors = 0; ITER(mp, fsi_mount, q) { if (ISSET(mp->m_mask, DM_EXPORTFS)) { if (e) lwarning(mp->m_ioloc, "%s has duplicate exportfs data", mp->m_name); mp->m_exported = mp; if (!ISSET(mp->m_mask, DM_VOLNAME)) set_mount(mp, DM_VOLNAME, strdup(mp->m_name)); } else { mp->m_exported = e; } /* * Recursively descend the mount tree */ if (mp->m_mount) errors += check_exportfs(mp->m_mount, mp->m_exported); /* * If a volume name has been specified, but this node and none * of its parents has been exported, report an error. */ if (ISSET(mp->m_mask, DM_VOLNAME) && !mp->m_exported) { lerror(mp->m_ioloc, "%s has a volname but no exportfs data", mp->m_name); errors++; } } return errors; }
static value_t fl_os_setenv(value_t *args, uint32_t nargs) { argcount("os.setenv", nargs, 2); int result; if (args[1] == FL_F) { #if defined WIN32 result = 1; #elif defined LINUX char *name = tostring(args[0], "os.setenv"); result = unsetenv(name); #else char *name = tostring(args[0], "os.setenv"); (void)unsetenv(name); result = 0; #endif } else { #if defined WIN32 result = 1; #else char *val = tostring(args[1], "os.setenv"); result = setenv(name, val, 1); #endif } if (result != 0) lerror(ArgError, "os.setenv: invalid environment variable"); return FL_T; }
static value_t fl_os_setenv(value_t *args, uint32_t nargs) { argcount("os.setenv", nargs, 2); char *name = tostring(args[0], "os.setenv"); int result; if (args[1] == FL_F) { #ifdef __linux__ result = unsetenv(name); #elif defined(__WIN32__) result = SetEnvironmentVariable(name,NULL); #else (void)unsetenv(name); result = 0; #endif } else { char *val = tostring(args[1], "os.setenv"); #if defined (__WIN32__) result = SetEnvironmentVariable(name,val); #else result = setenv(name, val, 1); #endif } if (result != 0) lerror(ArgError, "os.setenv: invalid environment variable"); return FL_T; }
int httpd_playfile(int fd, char *file) { FILE *fp; char chunk[1024]; fp = fopen(file, "r"); if (!fp) { lerror(file); return -1; } while (!feof(fp)) { if (fgets(chunk, sizeof(chunk), fp) == NULL) { break; } if (write(fd, chunk, strlen(chunk)) < 0) { fclose(fp); return -1; } memset(chunk, 0x0, sizeof(chunk)); } fclose(fp); return 1; }
// return NFC-normalized UTF8-encoded version of s, with // additional custom normalizations defined by jl_charmap above. static char *normalize(fl_context_t *fl_ctx, char *s) { // options equivalent to utf8proc_NFC: const int options = UTF8PROC_NULLTERM|UTF8PROC_STABLE|UTF8PROC_COMPOSE; ssize_t result; size_t newlen; result = utf8proc_decompose_custom((uint8_t*) s, 0, NULL, 0, (utf8proc_option_t)options, jl_charmap_map, NULL); if (result < 0) goto error; newlen = result * sizeof(int32_t) + 1; if (newlen > fl_ctx->jlbuflen) { fl_ctx->jlbuflen = newlen * 2; fl_ctx->jlbuf = realloc(fl_ctx->jlbuf, fl_ctx->jlbuflen); if (!fl_ctx->jlbuf) lerror(fl_ctx, fl_ctx->OutOfMemoryError, "error allocating UTF8 buffer"); } result = utf8proc_decompose_custom((uint8_t*)s,0, (int32_t*)fl_ctx->jlbuf,result, (utf8proc_option_t)options, jl_charmap_map, NULL); if (result < 0) goto error; result = utf8proc_reencode((int32_t*)fl_ctx->jlbuf,result, (utf8proc_option_t)options); if (result < 0) goto error; return (char*) fl_ctx->jlbuf; error: lerrorf(fl_ctx, symbol(fl_ctx, "error"), "error normalizing identifier %s: %s", s, utf8proc_errmsg(result)); }
static value_t read_vector(value_t label, u_int32_t closer) { value_t v=the_empty_vector, elt; u_int32_t i=0; PUSH(v); if (label != UNBOUND) ptrhash_put(&readstate->backrefs, (void*)label, (void*)v); while (peek() != closer) { if (ios_eof(F)) lerror(ParseError, "read: unexpected end of input"); if (i >= vector_size(v)) { v = Stack[SP-1] = vector_grow(v); if (label != UNBOUND) ptrhash_put(&readstate->backrefs, (void*)label, (void*)v); } elt = do_read_sexpr(UNBOUND); v = Stack[SP-1]; vector_elt(v,i) = elt; i++; } take(); if (i > 0) vector_setsize(v, i); return POP(); }
static int fill_connection(struct connection *cn) { struct pool *p; int poolleft, n, m; intmax_t fileleft; if (cn->rfd == -1) return 0; p = &cn->output; poolleft = p->ceiling - p->end; fileleft = cn->left; n = fileleft > poolleft ? poolleft : (int) fileleft; if (n <= 0) return 0; cn->left -= n; m = read(cn->rfd, p->end, n); if (debug) log_d("fill_connection: %d %d %d %d", cn->rfd, (int) (p->end - p->floor), n, m); if (m != n) { if (m == -1) lerror("read"); else log_d("premature end of file %s", cn->r->path_translated); return -1; } p->end += n; cn->file_offset += n; return n; }
// return NFC-normalized UTF8-encoded version of s static char *normalize(char *s) { static size_t buflen = 0; static void *buf = NULL; // persistent buffer (avoid repeated malloc/free) // options equivalent to utf8proc_NFC: const int options = UTF8PROC_NULLTERM|UTF8PROC_STABLE|UTF8PROC_COMPOSE; ssize_t result; size_t newlen; result = utf8proc_decompose((uint8_t*) s, 0, NULL, 0, options); if (result < 0) goto error; newlen = result * sizeof(int32_t) + 1; if (newlen > buflen) { buflen = newlen * 2; buf = realloc(buf, buflen); if (!buf) lerror(MemoryError, "error allocating UTF8 buffer"); } result = utf8proc_decompose((uint8_t*)s,0, (int32_t*)buf,result, options); if (result < 0) goto error; result = utf8proc_reencode((int32_t*)buf,result, options); if (result < 0) goto error; return (char*) buf; error: lerrorf(symbol("error"), "error normalizing identifier %s: %s", s, utf8proc_errmsg(result)); }
int open_log(const char *name) { char converted_name[PATHLEN]; const char *n; struct tm *tp; int rv; n = name; if (strchr(name, '%')) { current_time = time(0); if (log_gmt) tp = gmtime(¤t_time); else tp = localtime(¤t_time); if (tp) { if (strftime(converted_name, PATHLEN - 1, name, tp)) n = converted_name; } } rv = open(n, O_WRONLY | O_CREAT | O_APPEND, 0666); if (rv == -1) { log_d("cannot open %s", n); lerror("open"); } return rv; }
void gc(void) { static int grew = 0; unsigned char *temp; u_int32_t i; curheap = tospace; lim = curheap+heapsize-sizeof(cons_t); for (i=0; i < SP; i++) Stack[i] = relocate(Stack[i]); trace_globals(symtab); #ifdef VERBOSEGC printf("gc found %d/%d live conses\n", (curheap-tospace)/8, heapsize/8); #endif temp = tospace; tospace = fromspace; fromspace = temp; // if we're using > 80% of the space, resize tospace so we have // more space to fill next time. if we grew tospace last time, // grow the other half of the heap this time to catch up. if (grew || ((lim-curheap) < (int)(heapsize/5))) { temp = realloc(tospace, grew ? heapsize : heapsize*2); if (temp == NULL) lerror("out of memory\n"); tospace = temp; if (!grew) heapsize*=2; grew = !grew; } if (curheap > lim) // all data was live gc(); }
int process_imap(struct request *r) { FILE *fp; int fd; int retval; if (r->method == M_HEAD) { r->status = 204; return 0; } else if (r->method != M_GET) { r->status = 405; return 0; } fd = open(r->path_translated, O_RDONLY | O_NONBLOCK); if (fd == -1) { log_d("cannot open map file %s", r->path_translated); lerror("open"); r->status = 500; return 0; } fcntl(fd, F_SETFD, FD_CLOEXEC); fp = fdopen(fd, "r"); if (fp == 0) { log_d("process_imap: fdopen failed"); close(fd); r->status = 500; return 0; } retval = f_process_imap(r, fp); fclose(fp); return retval; }
void xopen(int ac, char **av, FD files[]) { char *dot; char *dotcor; if (ac != 2) lerror(USAGE, -1); if ((files[RD] = open(av[1], O_RD)) == ERROR) lerror(USAGE, -1); if (in(av[1], D_S) != ERROR) dot = my_strndup(av[1], in(av[1], D_S)); else dot = my_strdup(av[1]); dotcor = my_strcat(dot, D_COR); if ((files[WR] = open(dotcor, O_WR, FLAGS)) == ERROR) lerror(ERR_CREAT, -1); }
int hashtable_insert(hashtable *hash, char *key, void *data) { ENTRY e, *ep; int retval; hashtable_delete(hash, key); /* CRITICAL SECTION */ pthread_mutex_lock(&hash_mutex); retval = 1; e.key = strdup(key); if (!e.key) { lerror("calloc"); pthread_mutex_unlock(&hash_mutex); return -1; } e.data = strdup(data); if (!e.data) { lerror("calloc"); free(e.key); pthread_mutex_unlock(&hash_mutex); return -1; } if (!hash || hsearch_r(e, ENTER, &ep, hash) < 1) { free(e.key); free(e.data); lerror("hsearch_r"); retval = -1; } if (ep) { if (!ep->data) { ep->data = e.data; } } pthread_mutex_unlock(&hash_mutex); /* CRITICAL SECTION */ return retval; }
/* * Called at the end of a function definition. */ void funcend(void) { sym_t *arg; int n; if (reached) { cstk->c_noretval = 1; if (funcsym->s_type->t_subt->t_tspec != VOID && !funcsym->s_rimpl) { /* func. %s falls off bottom without returning value */ warning(217, funcsym->s_name); } } /* * This warning is printed only if the return value was implicitly * declared to be int. Otherwise the wrong return statement * has already printed a warning. */ if (cstk->c_noretval && cstk->c_retval && funcsym->s_rimpl) /* function %s has return (e); and return; */ warning(216, funcsym->s_name); /* Print warnings for unused arguments */ arg = dcs->d_fargs; n = 0; while (arg != NULL && (nargusg == -1 || n < nargusg)) { chkusg1(dcs->d_asm, arg); arg = arg->s_nxt; n++; } nargusg = -1; /* * write the information about the function definition to the * output file * inline functions explicitly declared extern are written as * declarations only. */ if (dcs->d_scl == EXTERN && funcsym->s_inline) { outsym(funcsym, funcsym->s_scl, DECL); } else { outfdef(funcsym, &dcs->d_fdpos, cstk->c_retval, funcsym->s_osdef, dcs->d_fargs); } /* * remove all symbols declared during argument declaration from * the symbol table */ if (dcs->d_nxt != NULL || dcs->d_ctx != EXTERN) lerror("funcend() 1"); rmsyms(dcs->d_fpsyms); /* must be set on level 0 */ reached = 1; }
int process_func(int trans_sock) { assert(trans_sock > 0); struct trans_data_t *ptrans = NULL; int max_len = sizeof(struct trans_data_t) + MAX_TRANSMIT_DATA_SIZE; ptrans = (struct trans_data_t *)calloc(1, max_len); if (NULL == ptrans) { lerror("calloc memory failed, err: %s", strerror(errno)); goto ERR; } ptrans->data_len = MAX_TRANSMIT_DATA_SIZE; if (-1 == recv_data(trans_sock, ptrans, max_len)) { lerror("call recv_data() failed"); goto ERR; } switch (ptrans->cmd) { case UD_UPLOAD: (void)process_upload(ptrans); break; case UD_DOWNLOAD: (void)process_download(trans_sock, ptrans); break; default: lerror("unknown cmd type, cmd: %d", ptrans->cmd); break; } free(ptrans); return 0; ERR: if (NULL != ptrans) { free(ptrans); } return -1; }
int gplace(char **av, int *i, int ac, t_get_champion *prev) { int place; 0[i]++; if (0[i] > ac) lerror(USAGE); place = my_atoi(av[i[0]]); if (place < 1 || place > 4) lerror(WRONG_PLACE); while (prev) { if (prev->nb == place) lerror(USED_PLACE); prev = prev->next; } 0[i]++; return (place); }
static void show_required(ioloc *l, int mask, char *info, char *hostname, char *strings[]) { int i; fsi_log("mask left for %s:%s is %#x", hostname, info, mask); for (i = 0; strings[i]; i++) if (ISSET(mask, i)) lerror(l, "%s:%s needs field \"%s\"", hostname, info, strings[i]); }
static int client_download(int sock, const char *file_name) { assert(sock > 0); assert(file_name != NULL); ldebug("download file(%s), please wait a moment...", file_name); struct trans_data_t *ptrans = NULL; int max_len = sizeof(struct trans_data_t) + MAX_TRANSMIT_DATA_SIZE; ptrans = (struct trans_data_t *)calloc(1, max_len); if (NULL == ptrans) { lerror("calloc failed, err: %s", strerror(errno)); return -1; } ptrans->cmd = UD_DOWNLOAD; ptrans->data_len = 0; snprintf(ptrans->filename, MAX_FILE_NAME_LENGTH-1, file_name); ptrans->filename[MAX_FILE_NAME_LENGTH-1] = '\0'; if (-1 == send_cmd(sock, ptrans)) { lerror("send cmd(%s) failed", string_cmd(ptrans->cmd)); goto ERR; } if (-1 == recv_data(sock, ptrans, max_len)) { lerror("recv data failed, sock: %d", sock); goto ERR; } ldebug("data: %s", ptrans->data); ldebug("download file success"); return 0; ERR: if (NULL != ptrans) { free(ptrans); } return -1; }