/*pre: takes in int argc and char **argv *post: runs the minishell program * with argc number of command line arguments defined by argv */ int main(int argc, char **argv) { int n; int pid; char *s; char **v; s = (char*)xmalloc(256*sizeof(char)); while(1) { my_str("minishell> "); n = read(0, s, 256); #ifdef DEBUG my_str("n= "); my_int(n); my_char('\n'); #endif s[n - 1] = '\0'; if(n > 1)/*1 character is just a \0 (read in \n user just hit enter)*/ { v = my_str2vect(s); if(my_strcmp(v[0], "cd") == 0) my_chdir(v[1]); else if(my_strcmp(v[0], "exit") == 0) break; else if(v[0] != NULL)/*if not just whitespace, we're going to need to fork*/ { #ifdef DEBUG my_str("command:>"); my_str(v[0]); my_str("<\n"); my_str("going to fork\n"); #endif if((pid = fork()) < 0) my_err("minishell: ERROR forking process!\n"); else if(pid > 0) wait(NULL); else { my_execvp(v[0], v); #ifdef DEBUG my_str("exiting forked process\n"); #endif exit(0);/*for processes that end in error*/ } } #ifdef DEBUG my_str("freeing vector\n"); #endif my_freevect(v); } else if(n < 0) my_str("minishell: ERROR reading command\n"); } free(s); my_str("Thank you for using myminishell, live long and prosper.\n"); return 0; }
/* * server.c * * The server portion of the server. * Run with: './server portNum'. The ideal port range is between * 50000 and 52000, as to not hit any reserved ports. * * Once the server is running, you can press Ctrl + C to disconnect all * clients and stop the server. * * Precondition: A port number is specified, and the port is available * Postcondition: The server is running * * @param argc - The number of command line arguments * @param argv - The vector of command line arguments */ int main(int argc, char** argv) { int port; int pid; int clientfd; int sockfd; int clilen; int bytes_read; struct sockaddr_in serv_addr; struct sockaddr_in cli_addr; char buffer[BUFFER_SIZE]; char** input; signal(SIGINT, bye); if(argc != 2) { my_str("Usage: ./server portNum\n"); exit(1); } if((port = my_atoi(argv[1])) <= 0 || port > 65535) { my_str("Invalid port number!\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); serv_addr.sin_port = htons(port); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; memset(&serv_addr.sin_zero, 0, 8); if(bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) { my_str("Unable to bind socket!\n"); exit(1); } listen(sockfd, 5); clilen = sizeof(cli_addr); while(1) { if((clientfd = accept(sockfd, (struct sockaddr*) &cli_addr, &clilen)) < 0) { my_str("Unable to accept client\n"); exit(1); } if((pid = fork()) < 0) { my_str("fork() critical error!\n"); exit(1); } if(pid == 0) { signal(SIGINT, serv_kill); gl_env.sockfd = clientfd; bytes_read = read(clientfd, buffer, BUFFER_SIZE - 1); buffer[bytes_read] = '\0'; gl_env.username = my_strdup(my_str2vect(buffer)[0]); my_str("***"); my_str(gl_env.username); my_str(" connected!\n"); while(1) { write(clientfd, ".", 1); if((bytes_read = read(clientfd, buffer, BUFFER_SIZE - 1)) < 0) { my_str("Read failed!\n"); exit(1); } input = my_str2vect(buffer); if(my_strcmp(input[0], "/exit") == 0) { my_str("***"); my_str(gl_env.username); my_str(" disconnected\n"); close(clientfd); free(gl_env.username); my_freevect(input); exit(1); } else if(my_strcmp("/nick", input[0]) == 0) { if(input[1]) { bytes_read = 1; if(my_strcmp(input[1], "") == 0 || my_strcmp(input[1], "\t") == 0) { for( ; my_strcmp(input[bytes_read], "") == 0 || my_strcmp(input[bytes_read], "\t") == 0; bytes_read++) ; } my_str("***"); my_str(gl_env.username); my_str(" changed username to "); free(gl_env.username); gl_env.username = my_strdup(input[bytes_read]); my_str(gl_env.username); my_char('\n'); } } else if(my_strcmp("/me", input[0]) == 0) { my_str("***"); my_str(gl_env.username); if(input[1]) { my_str(" "); bytes_read = 1; for( ; input[bytes_read]; bytes_read++) { if(!my_strcmp(input[bytes_read], "") == 0 && !my_strcmp(input[bytes_read], "\t") == 0) { my_str(input[bytes_read]); my_str(" "); } } } my_char('\n'); } else if(my_strcmp(buffer, "") != 0) { my_str(gl_env.username); my_str(": "); my_str(buffer); my_char('\n'); } my_freevect(input); } } } return 0; }
int main(int argc, char** argv){ struct addrinfo *ailist = NULL; struct addrinfo *currai = NULL; struct addrinfo hint; extern int servfd; int clientfd; int err; extern pid_t pid; char* username = NULL; char* input = NULL; char** inputVect = NULL; int strLen; if(argc != 2){ my_str("usage: ./myirc <port>\n"); exit(1); } signal(SIGINT, sig_bye); signal(SIGQUIT, sig_bye); input = xmalloc(BUF_LEN_MAX); hint.ai_flags = AI_PASSIVE; hint.ai_family = AF_INET; hint.ai_socktype = SOCK_STREAM; hint.ai_protocol = 0; hint.ai_addrlen = 0; hint.ai_canonname = NULL; hint.ai_addr = NULL; if((err = getaddrinfo(NULL, argv[1], &hint, &ailist)) != 0){ my_str((char*)gai_strerror(err)); exit(1); } for(currai = ailist; currai != NULL; currai = currai->ai_next){ if((servfd = socket(currai->ai_addr->sa_family, currai->ai_socktype, 0)) >= 0){ if(bind(servfd, currai->ai_addr, currai->ai_addrlen) >= 0){ if(listen(servfd, QUEUELENGTH) >= 0){ while(1){ #ifdef DEBUG my_str("Waiting for client.\n"); #endif clientfd = accept(servfd, NULL, NULL); if((pid = fork()) < 0) my_err("Fork error"); if(pid == 0){ #ifdef DEBUG my_str("Client connected.\n"); #endif while((strLen = recv(clientfd, input, BUF_LEN_MAX, 0)) <= 0) ; my_str(input); input[strLen] = '\0'; inputVect = my_str2vect(input); username = my_strdup(inputVect[0]); my_freevect(inputVect); my_str(" has connected.\n"); } for(;pid == 0;){ strLen = recv(clientfd, input, BUF_LEN_MAX, 0); if(strLen >= 0){ #ifdef DEBUG my_str("Client input read.\n"); #endif input[strLen] = '\0'; inputVect = my_str2vect(input); if(!my_strcmp(inputVect[0], "/exit")){ close(clientfd); my_str(username); my_str(" has disconnected.\n"); exit(0); } else if (!my_strcmp(inputVect[0], "/me")){ my_str(username); my_char(' '); my_str(my_vect2str(&(inputVect[1]))); } else if (!my_strcmp(inputVect[0], "/nick")){ if(inputVect[1]){ my_str(username); my_str(" changed name to: "); free(username); username = my_strdup(inputVect[1]); my_str(username); } } else { my_str(username); my_str(": "); my_str(input); } my_char('\n'); } } } exit(0); } close(servfd); my_err("Listen error"); } close(servfd); my_err("Bind error"); } close(servfd); my_err("Socket error"); } #ifdef DEBUG my_str("EXITING AT END.\n"); #endif exit(1); }