Beispiel #1
0
/* Choose */
int
History_Choose()
{
	int    error_code = 0;  /* In unix, a program returns 0 if all is ok. */
	int	   cnt=0;
	char   inputbuffer[BUF_MAX];
	int index=0;
	
	error_code = History_View();
	if(error_code != 0)
	{
		printf("an error occurred in history_view().\n");
		return error_code;
	}

	printf("\nPlease choose a number of command you wish to operate: ");
	if (fgets( inputbuffer, BUF_MAX-1, stdin )) {
		
		index = atoi(inputbuffer);
		printf("command_num: %d\n", command_num);
		
		//param check
		if(command_num < index || index < 0)
		{
			printf("Chosen number is invalid: %d\n", index);
			error_code = 1;
			return error_code;
		}

	} else {
		printf ("Nothing read in.\n");
    }//error fgets()
	
	command_info   *context=NULL;
	command_info   *mydata;
	command_info   *command;

	while(List_next_node( &history, (void*) &context, (void*) &mydata ) && (mydata != NULL))
	{
		if (cnt == index)
		{
			command = (command_info *) malloc( sizeof( command_info ) );
			if (command != NULL) {			
				strcpy(command->name, mydata->name);  
				List_remove_in_context( &history, (void*) &context, (void*) &mydata );
				List_add_head ( &history, (void*) command );
			}
			break;
		}
		cnt++;
	}
	
	//printf ("finish History_Choose().\n");
	return error_code;
}
Beispiel #2
0
/* Add */
int
History_Add(char* inputbuffer)
{
  int    error_code = 0;  /* In unix, a program returns 0 if all is ok. */
  //char   inputbuffer[BUF_MAX];
  command_info  *command;

	/* Put the parameters into a COMMAND and store it in the list of command_info. */
	  command = (command_info *) malloc( sizeof( command_info ) );
	  if (command != NULL) {
	    strncpy( command->name, inputbuffer, BUF_MAX-1 );
	    command->name[BUF_MAX-1] = '\0'; /* Make sure that it is null-terminated. */
	    printf ("Read and stored command, %s\n", command->name);

		//[ADD] functionality
		//if command history contains maximum number of commands, remove the last command
		if (command_num == COM_MAX)
		{
			int cnt=0;
			command_info   *context=NULL;
			command_info   *mydata;

			//search for the last command
			while(List_next_node( &history, (void*) &context, (void*) &mydata ) && (mydata != NULL))
			{
				cnt++;
				//if cnt reaches COM_MAX (=if found the last command), remove the last command
				if(cnt == COM_MAX)
				{
					List_remove_in_context(&history, (void*) &context, (void*) &mydata);
				}
			}
		}	
		
		//add new command to the head of the history list
	    if (List_add_head( &history, (void *)command ) == 0) {
	      printf ("Error in inserting the command into the list.\n");
	    } else {
			//increment # of command
			if(command_num < COM_MAX)
			{
				command_num++;
			}
		}
		
	  } else {
	    printf( "Unable to allocate enough memory.\n");
	  }
	  
	//printf ("finish History_Add().\n");
	return error_code;
}
Beispiel #3
0
/* View */
int
History_View()
{
	int    error_code = 0;  /* In unix, a program returns 0 if all is ok. */
	int cnt=0;
	command_info   *context=NULL;
	command_info   *mydata;

	while(List_next_node( &history, (void*) &context, (void*) &mydata ) && (mydata != NULL))
	{
		printf("%2d: %s", cnt, mydata->name);
		cnt++;
	}
	
	//printf ("finish History_View().\n");
	return error_code;
}
/* The auth thread waits for a user name and pass to get sent to it from the client thread
   which it will then authenticate. It will return a valid authentication string or invalid
   if the user is authenticated or not. */
void *authThread(void *auth)
{
    List_t *userPassList;
    userPassFile *lastNode = NULL;
    userPassFile *userPassNode = NULL;
    userPassList = readInUsersFromFile(((authStruct *)auth)->fileName);
    char messageToSend[CHAR_MAX];
    char username[CHAR_MAX];

    // Wait for username pass to authenticate from another thread
    pthread_t receive;
    char *messageReceived;
    int isAuthed;
    int size;

    isAuthed = 0;

    // Wait to receive a message
    if (receive_message( &receive, &messageReceived, &size) == MSG_OK)
    {
        printf ("Auth received message 1--%s--size %d\n", messageReceived, size );
        // Check if matches list of username and passwords
        // If the head is not null
        if ((userPassList != NULL) && (userPassList->head != NULL))
        {
            // Get the head node of the list
            List_head_info(userPassList, (void *)&userPassNode);
            // Loop through the list until null to check for authorized user:pass
            while (userPassNode != NULL)
            {
                if (strcmp(userPassNode->usernamePassword, messageReceived) == 0)
                {
                    isAuthed = 1;
                }
                // Get next node
                List_next_node(userPassList, (void *)&lastNode, (void *)&userPassNode);
            }

            // Send message back to client thread that auth passed with authentication string
            if (isAuthed == 1)
            {
                // Create message to send back in the form 1:username:password:secretValueTicket
                strcpy (messageToSend, "1:");
                strcat (messageToSend, messageReceived);
                strcat (messageToSend, ":");
                strcat (messageToSend, ((authStruct *)auth)->secretValue);

                // Send message to auth thread
                if (send_message_to_thread( clientThreadRef, messageToSend, strlen(messageToSend) + 1) != MSG_OK)
                {
                    printf( "Auth 1 first failed\n" );
                }
            }
            // Send message back to client thread that it failed auth
            else
            {
                //Get username with regex
                sscanf( messageReceived, "%[^:]", username);

                //Create message to send on failure
                strcpy (messageToSend, "0:");
                strcat (messageToSend, username);
                strcat (messageToSend, ":0");

                // Send message to auth thread
                if (send_message_to_thread( clientThreadRef, messageToSend, strlen(messageToSend) + 1) != MSG_OK)
                {
                    printf( "Auth 0 first failed\n" );
                }
            }
        }
    }
    else
    {
        printf ("first receive failed\n");
    }

    return NULL;
}