Exemple #1
0
int main(int argc, char **argv)
{
    int n;
    int fd;
    int i;
    char **vect;
    char buffer[BUFF_SIZE];
    char *san;

    while(1)
    {
        my_str("[--MyMiniShell--]$ ");
        if((n = read(1, buffer, BUFF_SIZE-1)) < 0)
        {
            my_str("Something bad happened.\n");
            exit(0);
        }
        else
        {
            san = my_stripbuffer(buffer);
            vect = my_str2vect(san);

            if(my_strcmp(vect[0], "cd") == 0)
            {
                if((n = chdir(vect[1])) < 0)
                {
                    my_str("Execution of cd failed.\n");
                }
            }
            else if(my_strcmp(vect[0], "exit") == 0)
            {
                my_str("Goodbye!\n");
                exit(0);
            }
            else
            {
                if((fd = fork()) < 0)
                {
                    my_str("Fork Fail");
                    exit(0);
                }
                if(fd > 0)
                {
                    wait();
                }
                else
                {
                    if((n = execvp(vect[0], vect)) < 0)
                    {
                        my_str("Execution of ");
                        my_str(vect[0]);
                        my_str(" failed.\n");
                        exit(0);
                    }
                }
            }
        }
    }
}
Exemple #2
0
/*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;
}
Exemple #3
0
void testMY_STR2VECT(void){
    char* str = NULL;
    char** strVect = NULL;
    int i;

    if(temp_file != NULL){
        str = my_strdup("testing was successful");
        strVect = my_str2vect(str);
        CU_ASSERT_STRING_EQUAL(strVect[0], "testing");
        CU_ASSERT_STRING_EQUAL(strVect[1], "was");
        CU_ASSERT_STRING_EQUAL(strVect[2], "successful");
        CU_ASSERT(strVect[3] == NULL);
        for(i = 0; strVect[i]; ++i){
            free(strVect[i]);
            strVect[i] = NULL;
        }
        free(strVect);
        strVect = NULL;
        strVect = my_str2vect(NULL);
        CU_ASSERT(strVect == NULL);
        free(str);
    }
}
Exemple #4
0
int main(int argc, char **argv)
{
    test_my_char();
    test_my_str();
    test_my_int();
    test_my_alpha();
    test_my_digits();
    test_my_strindex();
    test_my_strlen();
    test_my_revstr();
    my_num_base(123, "01");
    my_char('\n');
    my_num_base(123, "0123456789ABCDEF");
    my_char('\n');
    my_num_base(100, "0123456789");
    my_char('\n');
    test_my_strrindex();
    my_str("-->");
    my_str(my_vect2str(&argv[1]));
    my_str("<--\n");
    my_str("-->");
    my_printvect(&argv[1]);
    my_str("<--\n");
    my_printvect(my_str2vect(my_strdup("hello  world      bro")));
    my_char('\n');
    my_int(my_atoi("100"));
    my_char('\n');
    my_int(my_atoi("-100"));
    my_char('\n');
    my_int(my_atoi(""));
    my_char('\n');
    my_int(my_atoi("\n\n\n"));
    my_char('\n');

    return 0;
}
Exemple #5
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;
}
Exemple #6
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);
}
Exemple #7
0
int main(int argc, char *argv[])
{
    int listener, newsockfd, portno, i, j, bytesRead;
    fd_set master, readfd;
    struct s_node *userList = NULL;
    struct user *newUser = NULL;

    socklen_t clilen;

    char buffer[256];
    char *messageSending;

    struct sockaddr_in serv_addr, cli_addr;
    int n, maxFileDescriptors = 0;

    char **parsedInput;

    if (argc < 2) 
    {
        my_str("Usage: /server [port]\n");
        exit(1);
    }

    listener = socket(AF_INET, SOCK_STREAM, 0);

    if (listener < 0) 
    {
       my_str("ERROR opening socket");
       exit(1);
    }

    my_bzero((char *) &serv_addr, sizeof(serv_addr));

    portno = my_atoi(argv[1]);

    serv_addr.sin_family = AF_INET; 
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
    my_memset(&(serv_addr.sin_zero), '\0', 8);

    if (bind(listener, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
    {
        my_str("Binding failed.\n");
        exit(1);
    }

    listen(listener,5);

    FD_ZERO(&master);
    FD_ZERO(&readfd);

    FD_SET(listener, &master);
    maxFileDescriptors = listener;
    my_str("Server is now online!\n\n");
    while(1)
    {
        readfd = master;

        if (select(maxFileDescriptors + 1, &readfd, NULL, NULL, NULL) < 0)
            error("select() failed!\n");

        for(i = 3; i <= maxFileDescriptors; i++)
        {
            if(FD_ISSET(i, &readfd))
            {
                if(i == listener) /* we got a new connection */
                {
                	my_str("Got a new connection!\n");
                    clilen = sizeof(cli_addr);
                    newsockfd = accept(listener, 
                                (struct sockaddr *) &cli_addr, 
                                &clilen);

                    if (newsockfd < 0) 
                        error("ERROR on accept");

                    /* read in username */
                    my_bzero(buffer, 256);
                    bytesRead = read(newsockfd, buffer, 255);
                    if (bytesRead > 0) /* was valid username */
                    {
                    	newUser = (struct user *)malloc(sizeof(struct user));
                    	newUser->fd = newsockfd;
                    	newUser->nick = my_strconcat(buffer, ""); /* hacky lmao */
                    	add_node(new_node(newUser, NULL, NULL), &userList);
                    	my_str("New user entry: ");
                    	my_str(buffer);
                    	my_char('\n');
                        messageSending = my_strconcat(buffer, " has connected.");
                        for(j = 3; j <= maxFileDescriptors; j++)
                        {
                            if(FD_ISSET(j, &master))
                            {
                                if(j != listener && j != i)
                                {
                                    if (write(j, messageSending, my_strlen(messageSending)) < 0)
                                        error("write() error!\n");
                                }
                            }
                        }
                    }
                    else
                    	my_str("no nickname received...\n");

                    FD_SET(newsockfd, &master);
                    if (newsockfd > maxFileDescriptors)
                        maxFileDescriptors = newsockfd;
                }
                else /* we got text */
                {
                    bytesRead = read(i, buffer, 255);
                    messageSending = "";
                    if (bytesRead > 1) /* got a message, not just a newline */
                    {
                    	buffer[bytesRead] = '\0';
                    	if (buffer[0] == '/') /* special command */
                    	{
                    		parsedInput = my_str2vect(buffer);

                    		if (my_strcmp(parsedInput[0], "/nick") == 0)
                    		{
                    			if (parsedInput[1] != NULL)
                    			{
                    				messageSending = my_strconcat(getUserNick(userList, i), " changed their nick to ");
                    				removeUser(&userList, i);
                    				newUser = (struct user *)malloc(sizeof(struct user));
                    				newUser->fd = i;
                    				newUser->nick = parsedInput[1];
                    				add_node(new_node(newUser, NULL, NULL), &userList);
                    				messageSending = my_strconcat(messageSending, getUserNick(userList, i));
                    			}
                    		}
                    		else if (my_strcmp(parsedInput[0], "/me") == 0)
                    		{
                    			messageSending = my_strconcat(getUserNick(userList, i), " ");
                  				messageSending = my_strconcat(messageSending, my_vect2str(&parsedInput[1]));
                    		}else if (my_strcmp(parsedInput[0], "/exit") == 0)
                            {
                                messageSending = "";
                            }else /* invalid command */
                            {
                                if (write(i, "Invalid command.", my_strlen("Invalid command.")) < 0)
                                    error("wrote() error!\n");
                            }
                    	}
                    	else /* normal message */
                    	{
	                        messageSending = my_strconcat(getUserNick(userList, i), ": ");
	                        messageSending = my_strconcat(messageSending, buffer);

                    	}

                    	if (my_strcmp(messageSending, "") != 0) /* if wrote message to send, send it */
                    	{
	                        my_str(messageSending);
	                        my_char('\n');
                    		for(j = 3; j <= maxFileDescriptors; j++)
	                        {
	                            if(FD_ISSET(j, &master))
	                            {
	                                if(j != listener && j != i)
	                                {
	                                    if (write(j, messageSending, my_strlen(messageSending)) < 0)
	                                        error("write() error!\n");
	                                }
	                            }
	                        }
	                    }
                    }
                    else if (bytesRead == 0) /* we closed the file descriptor on client */
                    {
                        messageSending = my_strconcat("SERVER: ", getUserNick(userList, i));
                        messageSending = my_strconcat(messageSending, " disconnected.\n");
                        my_str(messageSending);
                        removeUser(&userList, i);

                        for(j = 3; j <= maxFileDescriptors; j++)
                        {
                            if(FD_ISSET(j, &master))
                            {
                                if(j != listener && j != i)
                                {
                                    /* change to "SERVER: /nick disconnected" */
                                    if (write(j, messageSending, my_strlen(messageSending)) < 0)
                                        error("write() error!\n");
                                }
                            }
                        }

                        close(i);
                        FD_CLR(i, &master);
                        continue;
                    }
                    else if (bytesRead < 0)
                        error("Read error...\n");
                }
            }




        }
    }
    return 0; 
}