Esempio n. 1
0
//Functions related to find/replace : Start
void executeFindAndReplace(){
	FILE * file;
	char nameOfFile[256];
	char textToFind[256];
	char textToReplace[256];
	char buffer[256];
	printf("File Name\n");
	writeToLog("File Name");
	scanf("%s", nameOfFile);
	printFileContents(nameOfFile);
	file = fopen(nameOfFile, "r");
	if(!file){
		printf("The file you entered does not exist or is empty!\n");
		writeToLog("The file you entered does not exist or is empty!");
		fclose(file);
		displayMainMenu();
		return;
	}
	printf("Enter text to find.\n");
	writeToLog("Enter text to find.");
	scanf("%s", textToFind);
	writeToLog(textToFind);
	printf("Enter text to replace.\n");
	writeToLog("Enter text to replace.");
	scanf("%s", textToReplace);
	writeToLog(textToReplace);
	while(fgets(buffer, 256, (FILE*) file)){
		replace(buffer, textToFind, textToReplace);
	}
	fclose(file);
	displayMainMenu();	
}
Esempio n. 2
0
int main (int argc, char **argv){

	int option; //for getopts

	//go through the arguments and check if user has entered the help string
	//if they have, then print help sequence and exit
	char helpString[7] = "--help";
	for (int j=1; j<argc; j++) {

		if ( strcmp(helpString, argv[j]) == 0){
			printHelp();
				

				exit (1);
		}
	}
	
	//Use getopts to parse for arguments. Increment appropriate flags
	while ((option = getopt(argc, argv, "nm:M:")) != -1){
		switch (option){
			case 'n':
				nflag++; 
				break;
			case 'm':
				if (!Mfirst && !mfirst){ //if the user hasn't entered an m or M flag yet
					mfirst++; //-m came first
				}
				MTotalFlag++;
				mflag++;
				createEncryptionMap(optarg);
				break;
			case 'M':
				if (!Mfirst && !mfirst){ //if user hasn't entered m or M flag yet
					Mfirst++; //then -M came first
				}
				Mflag++;
				MTotalFlag++;
				createEncryptionMap(optarg);
				break;
			default:
				printf("Argument Error\n");
				break; 
		}
	}

	//parse arguments for the echo line command. If the user doesn't provie any arguments,
	//provides the - or -n - argument, echo back the user input until they quit
	int i;
	int skipNextFile = 0;
	if (argc==1 || (argc==2 && argv[1][0] == '-' && !argv[1][1]) || (argc == 3 && argv[1][0] == '-' && argv[1][1] == 'n' && argv[2][0] == '-' && !argv[2][1])){
		printf("Printing mode activated. Enter 'quit' to quit\n");
		char userInput[MAX_STRING_SIZE]; 
		int lineCount  = 1;
		while (1){
			fgets(userInput,MAX_STRING_SIZE,stdin);
			if (strcmp(userInput,"quit\n")==0){
				printf("Received exit string. Exiting program\n");
				exit(0);
			}
			else if (nflag){
				printf("%d. ", lineCount);
				printf("%s", userInput);
				lineCount++;
			}
			else{
				printf("%s", userInput);
			}
		}
	}

	/*Deal with the -m and -M flags. Since the file after the flag is used
	to make the encryption map separate function, skip the arguments that 
	come after the -m or -M flags. */
	for (i=1; i<argc; i++) {
		if (skipNextFile){
			skipNextFile = 0; 
		}

			//if argument is -m or -M
			else if ( argv[i][0] == '-' && (argv[i][1] == 'm' || argv[i][1] == 'M')){
					skipNextFile = 1;
					if (!argv[i+2]){ //check if user hasn't given an argument after the mapfile
						printf("dog.c needs an argument after the mapfile\n");
						exit(0);
					} 

			}
			else if (argv[i][0] == '-' && argv[i][1] == 'n')
			{
				// printf("Argument is -n \n");	
			}
			else{
				printFileContents(argv[i]);
				}
		}
	}
Esempio n. 3
0
void server(unsigned short port)
{
    int status, sockfd, clientfd, listen_success;
    struct sockaddr_in server;
    struct sockaddr_storage their_addr;
    socklen_t addr_size;
    char ipstr[INET6_ADDRSTRLEN];

    memset(&server, 0, sizeof(server));
    sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sockfd == -1)
    {
        perror("Error establishing socket\n");
        return;
    }

    server.sin_family = AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port = htons(port);

    int bind_status = bind(sockfd, (struct sockaddr *) &server, sizeof(server));
    if (bind_status < 0)
    {
        perror("Error binding to socket\n");
        return;
    }


    struct sockaddr_in sockin;
    socklen_t socklen = sizeof(sockin);
    getsockname(sockfd, (struct sockaddr *) &sockin, &socklen);


    printf("ss %s %d\n", getCurrentIP(), ntohs(sockin.sin_port));

    addr_size = sizeof(their_addr);
    while (1)
    {

        listen_success = listen(sockfd, 10);
        if (listen_success == -1)
        {
            perror("Unable to listen to socket\n");
            return;
        }

        clientfd = accept(sockfd, (struct sockaddr *) &their_addr, &addr_size);

        char *rec_buffer = calloc(500, sizeof(char));
        uint32_t file_length = 1;
        uint32_t total_bytes_received = 0;

        int recv_status;
        unsigned int msg_length;

        /* First packet received is the url */
        recv_status = recv(clientfd, rec_buffer, 500, 0);
        memcpy(&msg_length, rec_buffer + 4, 2);
        msg_length = ntohs(msg_length);
        char *url = calloc(msg_length, sizeof(char));
        memcpy(url, rec_buffer + 6, msg_length);
        memset(rec_buffer, 0, strlen(rec_buffer));
        char *ack = "Ack";


        /* send ack to add temporary to delay */
        if (send(clientfd, ack, strlen(ack), 0) < 0)
        {
//		int send_status = send(clientfd, ack, strlen(ack), 0);
//		while (send_status < 0) {
//			send_status = send(clientfd, ack, strlen(ack), 0);
//		}

            printf("Send failed\n");
        }


        FILE *ofp = fopen("chainlist.txt", "wb");

        while (total_bytes_received < file_length)
        {
            msg_length = 0;
            recv_status = recv(clientfd, rec_buffer, 500, 0);
            if (recv_status < 0)
            { perror("Error: receive failed\n"); }

            memcpy(&file_length, rec_buffer + 0, 4);
            file_length = ntohl(file_length);
            memcpy(&msg_length, rec_buffer + 4, 2);
            msg_length = ntohs(msg_length);

            total_bytes_received += msg_length;

            if (DEBUG) printf("File Length: %zu\n", file_length);
            if (DEBUG)printf("Message Length: %u\n", msg_length);
            if (DEBUG)printf("Bytes Received: %d\n", total_bytes_received);

            char *data = calloc(msg_length, sizeof(char));
            memcpy(data, rec_buffer + 6, msg_length);
            if (DEBUG)printf("Message is: %s\n", data);
            fprintf(ofp, data);

            free(data);
            memset(rec_buffer, 0, 500);
            /* send ack to add temporary to delay */
            if (send(clientfd, ack, strlen(ack), 0) < 0)
            {
                int send_status = send(clientfd, ack, strlen(ack), 0);
                while (send_status < 0)
                {
                    send_status = send(clientfd, ack, strlen(ack), 0);
                }

            }
        }
        fclose(ofp);


        char *next_ss = getNextSteppingStone();
        if (next_ss == NULL)
        {
            printf("Request: %s\n", url);
            printf("chainlist is empty\n");
            printf("issuing wget for file %s\n", url);
            char cmd_buf[1024];
            snprintf(cmd_buf, sizeof(cmd_buf), "wget %s -q -O download_file", url);
            system(cmd_buf);

            printf("File received\n");
            printf("Relaying file\n");

            char *tmp_buffer = calloc(100, sizeof(char));

            FILE *file;
            file = fopen("download_file", "rb");

            uint32_t fileLength = getFileLength("download_file");
            int packet_counter = 0;

            ///////////////// check size //////////////
            if (fileLength < 401)
            {
                //printf("File length under 400bytes\n");
                char *content = (char *) calloc(406, sizeof(char));

                uint16_t packetLength = fileLength;
                uint32_t fileLengthSend = htonl(fileLength);
                uint16_t packetLengthSend = htons(packetLength);

                memcpy(content + 0, &fileLengthSend, 4);
                memcpy(content + 4, &packetLengthSend, 2);
                char ch;
                int counter = 0;

                while ((ch = fgetc(file)) != EOF)
                {
                    content[counter + 6] = ch;
                    counter++;
                }

                if (send(sockfd, content, 6 + fileLength, 0) < 0)
                {
                    printf("Send failed\n");
                    abort();
                }
                recv_status = recv(clientfd, tmp_buffer, 500, 0);
                packet_counter++;

                free(content);
            }
            else
            {

                /*Find out how many times file needs to be split*/
                int numberOfParts = fileLength / 400;
                /*Figure out last part of files size*/
                int sizeOfLastPart = fileLength % 400;
                /*Loop through the file the number of times it is split*/
                int i = 0;
                for (i; i <= numberOfParts; i++)
                {
                    //if(DEBUG) printf("In iteration %d of the loop for breaking up packets\n", i);
                    char *content;
                    /*Last part of the file.  Could be smaller than 400 bytes*/
                    if (numberOfParts == i)
                    {
                        if (DEBUG) printf("Sending last part of file\n");
                        content = (char *) calloc(6 + sizeOfLastPart, sizeof(char));
                        /*Go to right position in file*/
                        fseek(file, i * 400, SEEK_SET);

                        /*Set variables for packet header*/
                        uint16_t packetLength = 6 + sizeOfLastPart;
                        uint32_t fileLengthSend = htonl(fileLength);
                        uint16_t packetLengthSend = htons(packetLength);

                        /*Fill buffer with correct information*/
                        memcpy(content + 0, &fileLengthSend, 4);
                        memcpy(content + 4, &packetLengthSend, 2);

                        /*Write chars into buffer*/
                        int j = 0;
                        for (j; j < sizeOfLastPart; j++)
                        {
                            char ch;
                            ch = fgetc(file);
                            content[6 + j] = ch;
                        }

                        if (send(clientfd, content, 6 + sizeOfLastPart, 0) < 0)
                        {
                            printf("Send failed\n");
                            abort();
                        }
                        packet_counter++;

                        recv_status = recv(clientfd, tmp_buffer, 500, 0);

                        //printf("send in loop iteration %d is:\n", i);
//						char c;
//						int k = 0;
//						for(k = 0; k < sizeOfLastPart + 6; k++)
//						{
//							printf("%c", content[k]);
//						}


                        free(content);
                    }
                        /*Middle of file where the parts are still 400 bytes*/
                    else
                    {
                        //if(DEBUG) printf("Sending middle parts of file\n");
                        char *content_buffer = calloc(406, sizeof(char));
                        /*Go to right position in file*/
                        fseek(file, i * 400, SEEK_SET);

                        /*Set variables for packet header*/
                        uint16_t packetLength = 400;
                        uint32_t fileLengthSend = htonl(fileLength);
                        uint16_t packetLengthSend = htons(packetLength);

                        /*Fill buffer with correct information*/
                        memcpy(content_buffer + 0, &fileLengthSend, 4);
                        memcpy(content_buffer + 4, &packetLengthSend, 2);

                        /*Write chars into buffer*/
                        int j = 0;
                        for (j; j < 400; j++)
                        {
                            char ch;
                            ch = fgetc(file);
                            content_buffer[6 + j] = ch;
                        }


                        if (send(clientfd, content_buffer, 406, 0) < 0)
                        {
                            printf("Send failed\n");
                            abort();
                        }

                        packet_counter++;
                        recv_status = recv(clientfd, tmp_buffer, 500, 0);

                        //printf("send in loop iteration %d is:\n", i);
                        free(content_buffer);

                    }
                }
            }

            system("rm download_file");
            system("rm chainlist.txt");
        } else
        {

            FILE *readFileContentsPointer = fopen("chainlist.txt", "r");
            printf("Request: %s\n", url);
            printf("chainlist is\n");
            printFileContents(readFileContentsPointer);
            fclose(readFileContentsPointer);
            client(next_ss, url);
            uint32_t bytes_sent = 0;
            uint32_t num_406b_packets = outBuff_size / 406;
            int recv_ack;

            int packet_counter = 0;


            char *send_buffer = calloc(406, sizeof(char));
            char *ack_buffer = calloc(500, sizeof(char));
            int i = 0;
            for (i; i < num_406b_packets; i++)
            {
                memcpy(send_buffer, outBuff + bytes_sent, 406);
                if (send(clientfd, send_buffer, 406, 0) < 0)
                {
                    printf("Send failed\n");
                }
                bytes_sent += 406;
                memset(send_buffer, 0, 406);
                packet_counter++;
                recv_ack = recv(clientfd, ack_buffer, 500, 0);
            }

            if (outBuff_size % 406 > 0)
            {
                uint32_t remainder_bytes = outBuff_size % 406;
                memcpy(send_buffer, outBuff + bytes_sent, remainder_bytes);
//			printf("Sending a remainder packet with size: %u\n", remainder_bytes);
                if (send(clientfd, send_buffer, remainder_bytes, 0) < 0)
                {
                    printf("Send failed\n");
                }
                recv_ack = recv(clientfd, ack_buffer, 500, 0);
                packet_counter++;
            }

        }
        printf("Goodbye!\n");

    }
}