int close(int fd) { task_t* curr = task_with_pid(getpid()); fd_entry ent = curr->fd_table[fd]; if (fd_empty(ent)) { return -1; } int ret = -1; switch (ent.type) { case STD_TYPE: ret = -1; break; case FILE_TYPE: fclose((FILE*)ent.payload); ret = 0; break; case PIPE_TYPE: default: ret = pipe_close(fd); break; } fd_remove(curr, fd); return ret; }
/* Quit TEG */ TEG_STATUS token_exit( int fd ) { PSPLAYER pJ; PLAY_DEBUG("token_exit\n"); if( player_whoisfd( fd, &pJ ) == TEG_STATUS_SUCCESS ) player_del_hard( pJ ); else fd_remove(fd); return TEG_STATUS_CONNCLOSED; }
static int fd_close_remove(struct rtems_bsd_program_control *prog_ctrl, int fd) { int rv = -1; rv = fd_remove(prog_ctrl, fd); if (rv == 0) { rv = close(fd); } return rv; }
int main(){ FDict *fd = fd_new(); fd_insert(fd, 102, 19); fd_insert(fd, 10, 2); fd_insert(fd, 89, 2); fd_insert(fd, 350, 2); fd_insert(fd, 5, 2); fd_each(fd, pn); puts("~~~~~~~~~~~~~~~~~~~~~~~~~~"); fd_remove(fd, 5); fd_each(fd, pn); return 0; }
int close(int fd) { File* f = fd_get(fd); if (f == NULL) { return EOF; } fd_remove(fd); f->Close(); delete f; return 0; }
/* Read the file descriptor and call the apropiate function */ TEG_STATUS play_teg( int fd ) { int i,j; PARSER p; char str[PROT_MAX_LEN]; DELIM igualador={ '=', '=', '=' }; DELIM separador={ ';', ';', ';' }; p.igualador = &igualador; p.separador = &separador; str[0]=0; if( g_game.fog_of_war ) g_game.player_fow = NULL; j=net_readline( fd, str, PROT_MAX_LEN ); if( j<1 ) { PSPLAYER pJ; if( player_whoisfd( fd, &pJ ) == TEG_STATUS_SUCCESS ) player_del_hard( pJ ); else fd_remove(fd); return TEG_STATUS_CONNCLOSED; } if( g_game.fog_of_war ) { if( player_whoisfd( fd, &g_game.player_fow ) != TEG_STATUS_SUCCESS ) g_game.player_fow = NULL; } p.data = str; do { if( (i=parser_call( &p )) ) { if( token_lookup( fd,&p ) == TEG_STATUS_CONNCLOSED ) { return TEG_STATUS_CONNCLOSED; } } } while( i && p.hay_otro); return TEG_STATUS_SUCCESS; }
/* Protocol version */ STATIC TEG_STATUS token_pversion( int fd, char *str ) { PSPLAYER pJ; PARSER p; DELIM igualador={ ':', ':', ':' }; DELIM separador={ ',', ',', ',' }; int hi,lo; PLAY_DEBUG("token_pversion()\n"); if( strlen(str)==0 ) goto error; p.igualador = &igualador; p.separador = &separador; p.data = str; if( parser_call( &p ) && p.hay_otro ) { hi = atoi( p.token ); } else goto error; if( parser_call( &p ) && !p.hay_otro ) { lo = atoi( p.token ); } else goto error; net_printf(fd,"%s=%i,%i\n", TOKEN_PVERSION, PROTOCOL_HIVER,PROTOCOL_LOVER); if( hi != PROTOCOL_HIVER ) { con_text_out(M_ERR,_("Client with incompatible protocol version (server:%d , client:%d)\n"),PROTOCOL_HIVER,hi); if( player_whoisfd( fd, &pJ ) == TEG_STATUS_SUCCESS ) player_del_hard( pJ ); else fd_remove(fd); return TEG_STATUS_CONNCLOSED; } return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_PVERSION"\n"); return TEG_STATUS_PARSEERROR; }
int close_conn(int sock) { if (close(sock) < 0) { perror("close_conn error - close failed"); return -1; } fd_remove(sock); if (delete_near(sock) < 0) { fprintf(stderr, "close_conn error - socket not found in near list\n"); return -1; } set_near(); nsock --; return 0; }
/* Creates a Player */ STATIC TEG_STATUS token_playerid( int fd, char *str ) { PARSER p; DELIM igualador={ ':', ':', ':' }; DELIM separador={ ',', ',', ',' }; SPLAYER j, *pJ; char c[TEG_MAX_PLAYERS]; char colores[100]; int i; int reconnect = FALSE; PLAY_DEBUG("token_playerid( fd=%d)\n",fd); /* si existe entonces da error, porque no tiene que existir */ if( player_whoisfd(fd, &pJ ) == TEG_STATUS_SUCCESS ) goto error; if( strlen(str)==0 ) goto error; p.igualador = &igualador; p.separador = &separador; p.data = str; memset( &j, 0, sizeof(SPLAYER)); /* averigua el name */ if( parser_call( &p ) && p.hay_otro ) { #ifdef WITH_GGZ if(g_server.with_ggz) { if( ggz_server_find_ggzname(fd,j.name,sizeof(j.name)-1) != TEG_STATUS_SUCCESS ) { player_fillname( &j, p.token ); } } else #endif /* WITH_GGZ */ player_fillname( &j, p.token ); } else goto error; if( parser_call( &p ) && p.hay_otro ) { j.is_player = atoi( p.token ); } else goto error; if( parser_call( &p ) && !p.hay_otro ) { j.human = atoi( p.token ); } else goto error; if( j.is_player ) { if( JUEGO_EMPEZADO ) { if( ! (reconnect = player_is_disconnected(&j)) ) { net_print(fd,TOKEN_GAMEINPROGRESS"\n"); fd_remove(fd); return TEG_STATUS_CONNCLOSED; } } if( reconnect ) pJ = player_return_disconnected( &j ); else pJ = player_ins_player( &j ); } else pJ = player_ins_ro( &j ); if( pJ == NULL ) { net_print(fd,TOKEN_SERVERFULL "\n"); fd_remove(fd); return TEG_STATUS_CONNCLOSED; } pJ->fd = fd; aux_find_inaddr( pJ ); if( reconnect ) { pJ->estado = pJ->status_before_discon; net_printf(fd,"%s=%s,%d,%d\n", TOKEN_RECONNECT, pJ->name,pJ->numjug,pJ->color); con_text_out(M_INF,_("Player %s(%d) is re-connected from %s\n"),pJ->name,pJ->numjug,pJ->addr); } else { colores_libres( c ); memset(colores,0,sizeof(colores)); for(i=0;i<TEG_MAX_PLAYERS;i++) { char buf[100]; sprintf( buf, ",%d",c[i] ); strncat( colores, buf, sizeof(colores)-1 ); } net_printf(fd,"%s=%s,%d%s\n", TOKEN_PLAYERID, pJ->name,pJ->numjug,colores); con_text_out(M_INF,_("Player %s(%d) is connected from %s\n"),pJ->name,pJ->numjug,pJ->addr); } return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_PLAYERID"\n"); return TEG_STATUS_PARSEERROR; }
int fd_add_index(task_t* task, fd_entry entry, int index) { fd_remove(task, index); task->fd_table[index] = entry; return index; }