void add(struct oper op, char finish_rem, int curline) { if (finish_rem == 'f') { if (op.type == LEFT) pusho(&o, op); else if (sizeo(&o) == 0) pusho(&o, op); else { while (sizeo(&o) > 0 && topo(&o).type != LEFT && priority[topo(&o).type] >= priority[op.type]) { struct command* newcom = (struct command*)malloc(sizeof(struct command)); newcom->input = newcom->output = NULL; newcom->type = types[topo(&o).type]; if (sizec(&s) < 2) printError(curline); newcom->u.command[1] = topc(&s); popc(&s); newcom->u.command[0] = topc(&s); popc(&s); pushc(&s, newcom); popo(&o); } if (op.type == RIGHT && topo(&o).type == LEFT) popo(&o); else pusho(&o, op); if (op.type == RIGHT) { struct command* ncom = (struct command*)malloc(sizeof(struct command)); ncom->input = ncom->output = NULL; ncom->type = SUBSHELL_COMMAND; ncom->u.subshell_command = topc(&s); popc(&s); pushc(&s, ncom); } } } else if (finish_rem == 't') { while (sizeo(&o) > 0) { struct command* newcom = (struct command*)malloc(sizeof(struct command)); newcom->input = newcom->output = NULL; newcom->type = types[topo(&o).type]; if (sizec(&s) < 2) printError(curline); newcom->u.command[1] = topc(&s); popc(&s); newcom->u.command[0] = topc(&s); popc(&s); pushc(&s, newcom); popo(&o); } } }
char * sock_ntop(const struct sockaddr *sa, socklen_t salen) { static char str[128]; switch(sa->sa_family){ case AF_INET: { struct sockaddr_in * sin= (struct sockaddr_in *)sa; if(inet_ntop(AF_INET, &sin->sin_addr, str ,sizeof(str)) == NULL) return NULL; return str; } #ifdef IPV6 case AF_INET6:{ struct sockaddr_in6*sin6 = (struct sockaddr_in6 *) sa; if(inet_ntop(AF_INET6, &sin6->sin6_addr, str , sizeof(str)) == NULL) return NULL; return str; } #endif #ifdef AF_UNIX case AF_UNIX : { struct sockaddr_un * unp = (struct sockaddr_un *) sa ; if( unp->sun_path[0] == 0) strcpy(str,"(no pathname bound)"); else snprintf(str,sizeof(str), "%s", unp->sun_path); return str; } #endif #ifdef HAVE_SOCKADDR_dL_STRUCT case AF_LINK :{ struct sockaddr_dl * sdl = (struct sockaddr_dl * )sa; if (sdl->sdl_nllen > 0) snprintf(str,sizeof(str), "%s", sdl_sdl->Nnlen, &sdl->sdl_data[0]); else snprintf(str,sizeo(str),"AF_LINK,index=%d",sdl->sdl_index); return str; } #endif default: snprintf(str, sizeof(str), "sock_ntop_host: unknow AF_XXX:%d, len : %d", sa->sa_family,salen); return str; } return NULL; }
int main(int argc, char* argv[]) { int peerfd = socket(AF_INET, SOCK_STREAM, 0); if(peerfd == -1) { ERR_EXIT("socket"); } struct sockaddr_in client_addr; memset(client_addr, 0, sizeo(client_addr)); client_addr.sin_family = AF_INET; client_addr.sin_addr.s_addr = inet_addr("192.168.175.128"); client_addr.sin_port = htons(9999); socklen_t sock_len = sizeof(client_addr); if(-1 == connect(sockdf, (struct sockaddr&)*client_addr, sock_len)) { ERR_EXIT("connect"); } do_service(peerfd); }