#include "functions.h" // Get stdin line with buffer overrun protection int getstdin(char *prompt, char *buff, size_t sz) { int ch, extra; // Print optional prompt if (prompt != NULL) { printf ("%s", prompt); fflush (stdout); } // Get the intput from stdin if (fgets (buff, sz, stdin) == NULL) { return NO_INPUT; } // If it was too long, there'll be no newline. In that case, we flush // to end of line so that excess doesn't affect the next call. if (buff[strlen(buff) - 1] != '\n') { // strlen of the actually entered line, not the original array size extra = 0; while (((ch = getchar()) != '\n') && (ch != EOF)) { extra = 1; } return (extra == 1) ? TOO_LONG : OK; } // Otherwise remove newline and give string back to caller. buff[strlen(buff) - 1] = '\0'; return OK; } // Append CR-LF to the end of a string (after cleaning up any existing trailing CR or LF) void appendcrlf(char *string) { // Make sure it doesn't already end with CR or LF while (string[strlen(string) - 1] == '\r' || string[strlen(string) - 1] == '\n') { string[strlen(string) - 1] = '\0'; } int startlen = strlen(string); string[startlen] = '\r'; // Add CR string[startlen + 1] = '\n'; // Add LF string[startlen + 2] = '\0'; // Finish with null terminator } // Remove leading colon ':' which is the starting character of a prefix in an IRC message void stripprefix(char *string) { // Make a copy to work with char string2[strlen(string)]; printf("stripprefix(): starting with '%s', strlen: %zd.\n", string, strlen(string)); // Don't bother if this isn't a prefix with a leading ':' if (string[0] != ':') { printf("stripprefix(): no leading ':', returning.\n"); return; } // Copy the old string into a new one, but... for (size_t i = 1; i < strlen(string); i++) { string2[i - 1] = string[i]; } // Copy result back to original string strncpy(string, string2, strlen(string) - 1); // Finish with null terminator string[strlen(string) - 1] = '\0'; printf("stripprefix(): finishing with '%s', strlen: %zd.\n", string, strlen(string)); } // Extract final parameter from IRC message, removing the leading colon ':' // e.g. given this string: // ":irc.tghost.co.uk 332 blabounce #test :This is a test topic!" // We want to end up with: // "This is a test topic!" void extractfinalparameter(char *string) { // Make a copy to work with char string2[strlen(string)]; // Position of colon int colonpos = -1; printf("extractfinalparameter(): starting with '%s', strlen: %zd.\n", string, strlen(string)); // Strip the colon at position 0 if there is one stripprefix(string); // Find the colon for (size_t i = 0; i < strlen(string); i++) { if (string[i] == ':') { printf("Found colon at position %zd!\n", i); colonpos = i; break; } } if (colonpos == -1) { printf("no colon found, returning\n"); return; } // Build a new string starting from the next position after the colon int counter = 0; for (size_t i = colonpos + 1; i < strlen(string); i++) { string2[counter] = string[i]; counter++; } // Copy result back to original string strncpy(string, string2, counter); // Finish with null terminator string[counter] = '\0'; printf("extractfinalparameter(): finishing with '%s', strlen: %zd.\n", string, strlen(string)); } // Extract the IRC nick from a prefix // e.g. given this string: // ":foo!bar@baz" // We want to end up with: // "foo" void extractnickfromprefix(char *string) { // Position of bang int bangpos = -1; printf("extractnickfromprefix(): starting with '%s', strlen: %zd.\n", string, strlen(string)); // Strip the colon at position 0 if there is one stripprefix(string); // Find the bang for (size_t i = 0; i < strlen(string); i++) { if (string[i] == '!') { printf("Found bang at position %zd!\n", i); bangpos = i; break; } } if (bangpos == -1) { printf("no bang found, returning\n"); return; } // Terminate the string at whatever position we found the bang string[bangpos] = '\0'; printf("extractnickfromprefix(): finishing with '%s', strlen: %zd.\n", string, strlen(string)); }