int bind_socket(void) { int sock; int ret; struct sockaddr_nl nl; int sndbuf = 32768; int rcvbuf = 1048576; sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); if (sock == -1) error_quit("socket"); ret = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf)); if (ret == -1) error_quit("setsockopt[SO_SNDBUF]"); ret = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)); if (ret == -1) error_quit("setsockopt[SO_RCVBUF]"); memset(&nl, 0, sizeof(nl)); nl.nl_family = AF_NETLINK; nl.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE; ret = bind(sock, (struct sockaddr *)&nl, sizeof(nl)); if (ret == -1) error_quit("bind"); return sock; }
int main(int ac, char **av) { t_env env; if (!(isatty(0))) error_quit("Invalid stdin, please run from terminal"); g_env = &env; env.old_width = 0; env.old_height = 0; env.list_size = ac - 1; env.items = NULL; get_tty_fd(&env); build_list(&env, ac, av); env.curr = env.items; init_signals(); tgetent(0, getenv("TERM")); if (!(env.caps = malloc(sizeof(*env.caps)))) error_quit("Failed to malloc env caps"); init_caps(env.caps); key_codes_init(&env); terminal_catch(); env.fd = 2; while (1) { draw_list(&env); read_stdin(&env); } }
unsigned int deal_storage_class_specifier(int node_index, unsigned int value) { if(-1==node_index) { node_index=my_state.node_number; } struct node_my * node_ptr=my_state.node_table[node_index]; if(82!=node_ptr->node_type) //STORAGE_CLASS_SPECIFIER_TYPE(82) { snprintf(my_state.error_str, sizeof(my_state.error_str), "lib/symbol/smbl_function_declaration.c: deal_storage_classs_specifier function wrong! The node's type is not storage_class_specifier(82)\n"); my_state.error_int=30203; error_quit(); return 0; } if(value!=0) { snprintf(my_state.error_str, sizeof(my_state.error_str), "lib/symbol/smbl_function_declaration.c: deal_storage_class_specifier two storage_class_specifiers!\n"); my_state.error_int=30204; error_quit(); return 0; } unsigned int out_value=0; out_value=(unsigned int)node_ptr->node_inrule_num+1; return out_value; }
unsigned int deal_function_specifier(int node_index, unsigned int value) { node_index=(-1==node_index)?my_state.node_number:node_index; struct node_my * node_ptr=my_state.node_table[node_index]; unsigned int out_value=value; int in_value=node_ptr->node_inrule_num; if(43!=node_ptr->node_type) //function_specifier_type(43) { snprintf(my_state.error_str, sizeof(my_state.error_str), "lib/symbol/smbl_function_declaration.c: deal_type_qualifier: node's type is not function_specifier_type(43)\n"); my_state.error_int=30221; error_quit(); return 0; } switch(in_value) { case 0: //INILINE out_value=out_value|0x00000001; break; case 1: //NORETURN out_value=out_value|0x00000002; break; default: snprintf(my_state.error_str, sizeof(my_state.error_str), "lib/symbol/smbl_function_declaration.c: deal_function_specifier node's node_inrule_num is INLINE(0) or NORETURN(1)\n"); my_state.error_int=30222; error_quit(); return 0; break; } return out_value; }
JBDATA * classify_components (int num_input_files, char **input_files, double thresh, double weight) { /* JBCLASSER* classer = jbCorrelationInit(JB_CONN_COMPS, 9999, 9999, thresh, weight); */ JBCLASSER *classer = jbCorrelationInitWithoutComponents (JB_CONN_COMPS, 9999, 9999, thresh, weight); if (classer == NULL) { error_quit ("Unable to create leptonica JBCLASSER."); } int i; for (i = 0; i < num_input_files; i++) { PIX *page = pixRead (input_files[i]); if (page == NULL) { printf ("Problem with page %s\n", input_files[i]); error_quit ("Unable to read Page"); } if (pixGetDepth (page) != 1) { printf ("Input file %s is not 1bpp\n", input_files[i]); error_quit ("Only 1bpp (black and white) images currently supported."); } if (jbAddPage (classer, page) == 1) { printf ("Problem with page %s\n", input_files[i]); error_quit ("Unable to add page to JBCLASSIFIER."); } pixDestroy (&page); } /* This is the part we have to add stuff to save each glyph separately */ JBDATA *data = jbDataSave (classer); if (data == NULL) { error_quit ("Unable to create the leptonica JBDATA."); } jbClasserDestroy (&classer); return data; }
struct symbol_my* add_symbol(char *input_yytext_ptr, int line_num, int symbol_type) { //printf("in add_symbol start\n"); if(my_state.symbol_table[my_state.symbol_number]==0x00) { //printf("in add_symbol 1\n"); my_state.symbol_table[my_state.symbol_number]=(struct symbol_my *)malloc(sizeof(struct symbol_my)); //printf("in add_symbol 2\n"); if(my_state.symbol_table[my_state.symbol_number]==0x00) { snprintf(my_state.error_str, sizeof(my_state.error_str), "lib/symbol/symbol.c add_symbol function wrong! malloc error!"); my_state.error_int=30101; error_quit(); return 0; } //my_state.symbol_number+=1; } else { snprintf(my_state.error_str, sizeof(my_state.error_str), "lib/symbol/symbol.c add_symbol function wrong! symbol_number error!\n"); my_state.error_int=30102; error_quit(); return 0; } //printf("in add_symbol 3\n"); int symbol_index=my_state.symbol_number; //printf("in add_symbol 14\n"); //printf("symbol_index:%d my_state.symbol_table[symbol_index]:%x\n", symbol_index, my_state.symbol_table[symbol_index]); my_state.symbol_table[symbol_index]->smbl_id=my_state.symbol_number; //printf("in add_symbol 24\n"); char *tmp=malloc(str_len(input_yytext_ptr)+1); //printf("in add_symbol 34\n"); str_copy(input_yytext_ptr, tmp, str_len(input_yytext_ptr)); //printf("in add_symbol 44\n"); my_state.symbol_table[symbol_index]->smbl_name=tmp; //printf("in add_symbol 54\n"); my_state.symbol_table[symbol_index]->smbl_name_len=str_len(input_yytext_ptr); //printf("in add_symbol 64\n"); my_state.symbol_table[symbol_index]->smbl_type=symbol_type; my_state.symbol_table[symbol_index]->smbl_line_num=line_num; my_state.symbol_table[symbol_index]->smbl_in_which_node=my_state.node_number; my_state.symbol_number+=1; //printf("in add_symbol end\n"); return my_state.symbol_table[symbol_index]; }
void set_nonblock(int fd) { int res; res = fcntl(fd, F_GETFL, 0); if (res == -1) error_quit("fcntl[F_GETFL]"); res = fcntl(fd, F_SETFL, res | O_NONBLOCK); if (res == -1) error_quit("fcntl[F_SETFL]"); }
char *load_file_loul(char *name, t_directory *dir) { char *tmp; if (name[0] == '/') return (ft_strdup(name)); if (!(tmp = ft_strjoin(dir->path, "/"))) error_quit("Failed to malloc string"); if (!(tmp = ft_strjoin_free1(tmp, name))) error_quit("Failed to malloc string"); return (tmp); }
int main(int argc, char **argv) { int sock; fd_set rset, wset; sock = bind_socket(); set_nonblock(STDIN_FILENO); set_nonblock(sock); FD_ZERO(&rset); FD_ZERO(&wset); FD_SET(sock, &wset); for (;;) { int res; ssize_t n; FD_SET(STDIN_FILENO, &rset); FD_SET(sock, &rset); res = select(sock + 1, &rset, &wset, NULL, NULL); if (res == -1) error_quit("select"); if (FD_ISSET(STDIN_FILENO, &rset)) { char buf[1]; n = read(STDIN_FILENO, buf, 1); if (n == -1 && errno != EWOULDBLOCK) error_quit("read"); else if (n == 0) { close(sock); exit(0); /* got EOF from erlang */ } } if (FD_ISSET(sock, &wset)) { FD_CLR(sock, &wset); request_dump(sock); read_routes(sock); } if (FD_ISSET(sock, &rset)) read_routes(sock); } return 0; }
// sendPacket ( sizeof( data ), type 값 , data 의 주소값 ); void AsynchronousClientClass::sendPacket(const BYTE data_size, const BYTE type, BYTE* data_start_pointer) { // 실제 최대 버퍼 사이즈 보다 데이터 길이가 커지면 안된다. if (MAX_BUF_SIZE < (data_size + 2)) { #ifdef _DEBUG // 아래와 같은 에러가 발생하게 된다면, 버퍼 사이즈를 건드리기 보다 실제 데이터 크기를 압축해 줄여 보낼 수 있도록 하자 printf("[ code LINE %d ] [ code FUNCTION %s ] SendPacket class ERROR :: data size overed MAX_BUF_SIZE\n", __LINE__, __FUNCTION__); #endif } else { // 패킷 안의 실제 내용 생성 m_sendbuf[0] = data_size + 2; m_sendbuf[1] = type; if (nullptr != data_start_pointer) { memcpy(&m_sendbuf[2], data_start_pointer, m_sendbuf[0]); } m_wsa_sendbuf.len = m_sendbuf[0]; DWORD ioByteSize; m_retval = WSASend(m_sock, &m_wsa_sendbuf, 1, &ioByteSize, 0, NULL, NULL); if (SOCKET_ERROR == m_retval) { // 비동기 소켓이라 그냥 리턴, 검사 해주어야 함 if (WSAGetLastError() != WSAEWOULDBLOCK) { int err_no = WSAGetLastError(); error_quit(L"sendPacket()", err_no); } } } }
int main (int argc, char *argv[]) { struct args *args = parse_args (argc, argv); validate_args (args); JBDATA *data = classify_components (args->num_input_files, args->input_files, args->thresh, args->weight); /* Render output of leptonica's classifier, if requested */ if (args->debug_render_pages) { int i; PIXA *pa = jbDataRender (data, 0); for (i = 0; i < pa->n; i++) { PIX *pix = pa->pix[i]; char filename[512]; sprintf (filename, "rendered_%05d.png", i); pixWrite (filename, pix, IFF_PNG); } } struct mapping *maps = NULL; int num_fonts = register_mappings (data, &maps); char *tmpdirname = NULL; if (!args->debug_skip_font_gen) { tmpdirname = generate_fonts (data, maps, num_fonts, args->debug_tmpdir); } generate_pdf (args->outname, tmpdirname, num_fonts, args->num_input_files, data, maps, args->debug_draw_borders); /* clean up tmpdir */ if (!args->debug_no_clean_tmpdir) { /* This may not be Windows compatible */ if (nftw (tmpdirname, delete_file, 64, FTW_DEPTH | FTW_PHYS) == -1) { error_quit ("Failed to clean up tmpdir."); } } if (args->debug_tmpdir == NULL) { free (tmpdirname); } free (maps); jbDataDestroy (&data); free (args->input_files); free (args); return 0; }
static void read_stdin(t_env *env) { char buffer[20]; int rd; ft_memset(buffer, 0, 20); rd = read(0, buffer, 20); if (rd == 1 && buffer[0] == 27) error_quit(NULL); else if (rd == 1 && buffer[0] == 10) return_selected(env); else if ((rd == 1 && buffer[0] == 127) || !ft_strcmp(buffer, env->key_code_delete)) delete_current(env); else if (rd == 1 && buffer[0] == ' ') { if (env->curr) env->curr->item->selected = !env->curr->item->selected; } else if (!ft_strcmp(buffer, env->key_code_down)) move_down(env); else if (!ft_strcmp(buffer, env->key_code_up)) move_up(env); else if (!ft_strcmp(buffer, env->key_code_left)) move_left(env); else if (!ft_strcmp(buffer, env->key_code_right)) move_right(env); }
void parse(t_env *env) { t_parser p; int rd; p.is_start = 0; p.is_end = 0; p.link_part = 0; while ((rd = get_next_line(0, &(p.line))) == 1) { if (p.line[0] != '#' || (p.line[1] && p.line[1] == '#')) { if (env->ants == -1) { if (valid_int(p.line)) env->ants = ft_atoi(p.line); else return ; } else if (!parse_line(env, &p)) return ; } add_file_line(env, p.line); } if (rd == -1) error_quit("Error while reading stdin"); }
int parse_room(t_env *env, t_parser *p, char **split) { t_room_list *lst; t_room *room; if (!is_nbr(split[1]) || !is_nbr(split[2]) || p->link_part) return (0); lst = env->rooms; while (lst) { if (!ft_strcmp(lst->room->name, split[0])) return (0); lst = lst->next; } if (!(lst = malloc(sizeof(*lst)))) error_quit("Failed to malloc room list"); room = room_create(); room->name = split[0]; lst->room = room; lst->next = env->rooms; env->rooms = lst; if (!check_start_end(env, room, p)) return (0); return (1); }
unsigned int deal_type_qualifier(int node_index, unsigned int value) { node_index=(-1==node_index)?my_state.node_number:node_index; struct node_my * node_ptr=my_state.node_table[node_index]; unsigned int out_value=value; int in_value=node_ptr->node_inrule_num; if(94!=node_ptr->node_type) //TYPE_QUALIFIER_TYPE(94) { snprintf(my_state.error_str, sizeof(my_state.error_str), "lib/symbol/smbl_function_declaration.c: deal_type_qualifier: node's type is not TYPE_QUALIFIER(94)\n"); my_state.error_int=30221; error_quit(); return 0; } switch(in_value) { case 0: //const out_value=out_value|0x00000001; break; case 1: //restrict out_value=out_value|0x00000002; break; case 2: //volatile out_value=out_value|0x00000004; break; case 3: //atomic out_value=out_value|0x00000008; break; } return out_value; }
void pdf_error_handler (HPDF_STATUS error_no, HPDF_STATUS detail_no, void *user_data) { fprintf (stderr, "libharu error: error_no=%04X, detail_no=%d\n", (unsigned int) error_no, (int) detail_no); error_quit ("PDF Generation problem."); }
int main(int argc,char **argv) { int sock_fd; struct sockaddr_in client_addr; char buf[20]; if(argc!=3) error_quit("Argument"); if((sock_fd=socket(AF_INET,SOCK_STREAM,0))==-1) error_quit("socket"); init_data_with_client(&client_addr,atoi(argv[2]),argv[1]); if(connect(sock_fd,(struct sockaddr *)&client_addr,sizeof(struct sockaddr))==-1) error_quit("connect"); if(recv(sock_fd,buf,sizeof(buf),0)>0) set_local_time(buf); else printf("Recv Error!\n"); printf("Close With %s. . .\n",argv[1]); close_with_safe(sock_fd); return 0; }
static int check_link(t_env *env, t_parser *p) { char **splitted; if (!(splitted = ft_strsplit(p->line, '-'))) error_quit("Failed to malloc split"); if (splitted[0] && splitted[1] && !splitted[2]) return (parse_link(env, p, splitted)); else if (splitted[0] && splitted[1]) return (0); return (-1); }
void * malloc_guarded (size_t size) { void *vptr = malloc (size); if (vptr == NULL) { error_quit ("Out of memory."); } return vptr; }
int delete_file (const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { int ret = remove (path); if (ret == -1) { error_quit ("Could not remove temp file."); } return ret; }
int main(int argc, char **argv) { t_world *world; if (argc != 2) error_quit("Invalid parameters.\nUsage:\n./fdf <map_file>"); if (!(world = malloc(sizeof(*world)))) error_quit("Failed to malloc world struct"); world_init(world); window_init(world); load_map(world, argv[1]); world_init_positions(world); get_min_max(world); mlx_do_key_autorepeatoff(world->window->mlx); mlx_hook(world->window->mlx_window, 2, 1, &key_press_listener, world); mlx_hook(world->window->mlx_window, 3, 2, &key_release_listener, world); mlx_loop_hook(world->window->mlx, &loop_listener, world); //mlx_key_hook(world->window->mlx_window, &key_listener, world); //mlx_expose_hook(world->window->mlx_window, &expose_listener, world); mlx_loop(world->window->mlx); return (0); }
static void dup_tab(t_env *env) { int i; if (!(env->sorted = malloc(sizeof(*env->sorted) * env->stack_a_size))) error_quit("Failed to malloc sorted tab"); i = 0; while (i < env->stack_a_size) { env->sorted[i] = env->stack_a[i]; i++; } env->sorted_size = env->stack_a_size; }
void env_init(t_env *env) { if (!(env->position = malloc(sizeof(*env->position)))) error_quit("Failed to malloc position struct"); env->position->x = 3; env->position->y = 2; if (!(env->direction = malloc(sizeof(*env->direction)))) error_quit("Failed to malloc rotation struct"); env->direction->x = -1; env->direction->y = 0; if (!(env->window = malloc(sizeof(*env->window)))) error_quit("Failed to malloc window struct"); if (!(env->map = malloc(sizeof(*env->window)))) error_quit("Failed to malloc map struct"); env->last_time = time_elapse(); env->tmp_fps = 0; env->fps = 0; env->plane_x = 0; env->plane_y = 1; env->left = 0; env->right = 0; env->up = 0; env->down = 0; }
void read_routes(int sock) { int ret; struct sockaddr_nl nl; struct iovec iov; struct msghdr msg; struct nlmsghdr *p; char buf[BUFSIZE]; msg.msg_name = &nl; msg.msg_namelen = sizeof(nl); msg.msg_iov = &iov; msg.msg_iovlen = 1; memset(&nl, 0, sizeof(nl)); nl.nl_family = AF_NETLINK; nl.nl_pid = 0; nl.nl_groups = 0; iov.iov_base = buf; iov.iov_len = BUFSIZE; ret = recvmsg(sock, &msg, 0); if (ret == -1) { if (errno == EINTR || errno == EAGAIN || errno == ENOBUFS) return; error_quit("recvmsg"); } if (ret == 0) error_quit("recvmsg: EOF"); if (msg.msg_namelen != sizeof(nl)) error_quit("bad message namelen"); p = (struct nlmsghdr *)buf; while (ret >= sizeof(*p)) { int len = p->nlmsg_len; if (len < sizeof(*p) || len > ret) { if (msg.msg_flags & MSG_TRUNC) error_quit("truncated message"); error_quit("malformed message"); } if (p->nlmsg_type != NLMSG_DONE) notify(&nl, p); ret -= NLMSG_ALIGN(len); p = (struct nlmsghdr*)((char*)p + NLMSG_ALIGN(len)); } if (msg.msg_flags & MSG_TRUNC) return; if (ret != 0) error_quit("unexpected remaining bytes"); }
// 서버가 살아있는지, 응답은 하는지에 대한 기본 테스트 함수. 3 Bytes 를 보내며, 기본 타입은 TEST, 데이터는 1을 보낸다. void AsynchronousClientClass::sendPacket_TEST() { m_sendbuf[0] = 3; m_sendbuf[1] = TEST; m_sendbuf[2] = 1; // true m_wsa_sendbuf.len = m_sendbuf[0]; DWORD ioByteSize; m_retval = WSASend(m_sock, &m_wsa_sendbuf, 1, &ioByteSize, 0, NULL, NULL); //int retval = send(sock, reinterpret_cast<char*>(&sendbuf), sendbuf[0], 0); if (SOCKET_ERROR == m_retval) { // 비동기 소켓이라 그냥 리턴, 검사 해주어야 함 if (WSAGetLastError() != WSAEWOULDBLOCK) { int err_no = WSAGetLastError(); error_quit(L"sendPacket_TEST()", err_no); } } }
int deal_declarator(int node_index) { if(-1==node_index) { node_index=my_state.node_number-1; } struct node_my* node_ptr=my_state.node_table[node_index]; if(23!=node_ptr->node_type) //declarator_type(23) { snprintf(my_state.error_str, sizeof(my_state.error_str), "lib/symbol/smbl_function_definition.c:smbl_function_defintion: node is not declarator_type(23)\n"); my_state.error_int=30201; error_quit(); } int pointer_1=-1; struct node_my * pointer_1_ptr=NULL; int direct_declarator_2=-1; struct node_my * direct_declarator_2_ptr=NULL; switch(node_ptr->node_inrule_num) { case 0: pointer_1=node_ptr->node_first_child; pointer_1_ptr=my_state.node_table[pointer_1]; direct_declarator_2=pointer_1_ptr->node_right_brother; direct_declarator_2_ptr=my_state.node_table[direct_declarator_2]; printf("not ready yet\n"); break; case 1: direct_declarator_2=node_ptr->node_first_child; direct_declarator_2_ptr=my_state.node_table[direct_declarator_2]; break; default: break; } return 0; }
void init_caps(t_caps *caps) { if (!(caps->underline_start = tgetstr("us", 0))) error_quit("Failed to load us cpa"); if (!(caps->underline_end = tgetstr("ue", 0))) error_quit("Failed to load ue cap"); if (!(caps->highlight_start = tgetstr("so", 0))) error_quit("Failed to load so cap"); if (!(caps->highlight_end = tgetstr("se", 0))) error_quit("Failed to load se cap"); if (!(caps->bold_start = tgetstr("md", 0))) error_quit("Failed to load md cap"); if (!(caps->bold_end = tgetstr("me", 0))) error_quit("Failed to load me cap"); if (!(caps->move = tgetstr("cm", 0))) error_quit("Failed to load cm cap"); }
int validate_args (const struct args *args) { int i; if (args->num_input_files <= 0 || args->input_files == NULL) { error_quit ("No input files specified."); } if (args->outname == NULL) { error_quit ("No output file specified."); } /* Check that all input files exist */ for (i = 0; i < args->num_input_files; i++) { if (!file_exists (args->input_files[i])) { printf ("Can't read %s.\n", args->input_files[i]); error_quit ("Input file doesn't exist."); } } /* Check thresh and weight in valid range thresh (value for correlation score: in [0.4 - 0.98]) weightfactor (corrects thresh for thick characters [0.0 - 1.0]) */ if (args->thresh < 0.4 || args->thresh > 0.98) { error_quit ("Threshold parameter must be in the range [0.4 - 0.98]"); } if (args->weight < 0.0 || args->weight > 1.0) { error_quit ("Weight must be in range [0.0 - 1.0]"); } /* Confirm overwriting if outname exists */ if (file_exists (args->outname)) { char c = 'n'; printf ("Output file %s already exists. Overwrite? (y/N) ", args->outname); scanf ("%c", &c); if (c != 'Y' && c != 'y') { error_quit ("Output file exists."); } } return 0; }
void strtovni (char * str, u_int8_t * vni) { u_int32_t vni32; char buf[9]; if (snprintf (buf, sizeof (buf), "0x%s", str) < 0) error_quit ("invalid VNI \"%s\"", str); vni32 = strtol (buf, NULL, 0); if (vni32 == LONG_MAX || vni32 == LONG_MIN) err (EXIT_FAILURE, "invalid VNI %u", vni32); memcpy (vni, ((char *)&vni32) + 2, 1); memcpy (vni + 1, ((char *)&vni32) + 1, 1); memcpy (vni + 2, ((char *)&vni32), 1); return; }
int main( int argc, char* argv[] ){ unsigned index; //loop indicies unsigned array_size; double *A; if( argc != num_expected_args ){ printf("Usage: ./sort <size of array to sort>\n"); exit(-1); } array_size = atoi(argv[1]); printf("Generating array...\n"); A = (double*) malloc( sizeof(double) * array_size ); for( index = 0; index < array_size; index++ ){ A[index] = (double) rand(); } printf("Sorting array...\n"); quicksort( A, 0, array_size); #ifdef VERIFY_CORRECT printf("Verifying array is sorted...\n"); for( index = 0; index < (array_size - 1); index++) if( !(A[index] <= A[index + 1]) ){ //Array is not sorted error_quit(A, array_size, index); } #endif printf("Sort done!\n"); return 0; }