Question
------------------- qotd.c ----------------------- #include qotd.h static qotd_t *qotd = NULL; static size_t count = 0; static char *buffer = NULL; void freePointer(void *ptr) { free(*ptr);
-------------------
qotd.c
-----------------------
#include "qotd.h"
static qotd_t *qotd = NULL; static size_t count = 0; static char *buffer = NULL;
void freePointer(void *ptr) { free(*ptr); /* * * 1. Write a short routine to clear *ptr. * 2. Use freePointer() throughout the code * to remove all memory leaks. * */ }
buffersize_t getBufferSize(ssize_t new_size) {
/* * * Calculate a new buffer size, being * sure to prevent against integer overflow * and/or wrap. * */ return 0; }
int addQuote(uint16_t pos, uint16_t len) { qotd = (qotd_t*)realloc(qotd, sizeof(qotd_t) * (count + 1));
if (qotd == NULL) { return ERROR_CONDITION; }
qotd[count].pos = pos; qotd[count].len = len;
count++;
return SUCCESS; }
void quoteOfTheDay() { char *str = NULL;
srand((unsigned)time(NULL)); uint16_t index = ((rand() % count) + 1) - 1;
str = malloc(qotd[index].len + 1);
if (str == NULL) { fprintf(stderr, "quoteOfTheDay() %s", ERROR_MESSAGE); } else { strncpy(str, (char*)(buffer + qotd[index].pos), qotd[index].len); str[qotd[index].len] = '\0';
printf("%s ", str);
} }
int load(char *file) { FILE *stream; char *line = NULL; size_t length = 0; ssize_t lineSize;
stream = fopen(file, "r"); if (stream == NULL) { return ERROR_CONDITION; }
while ((lineSize = getline(&line, &length, stream)) != -1) { char* pos; buffersize_t bufferSize;
if ((pos = strchr(line, ' ')) != NULL) { *pos = '\0'; } bufferSize = getBufferSize(lineSize);
if (bufferSize == 0) { fclose(stream); fprintf(stderr, "getBufferSize() %s", ERROR_MESSAGE); return ERROR_CONDITION; }
if ((buffer = realloc(buffer, bufferSize * sizeof(char))) == NULL) { fclose(stream); fprintf(stderr, "realloc() %s", ERROR_MESSAGE); return ERROR_CONDITION; }
if (addQuote(strlen(buffer), lineSize - 1) == ERROR_CONDITION) { fclose(stream); fprintf(stderr, "addQuote() %s", ERROR_MESSAGE); return ERROR_CONDITION; }
strncat(buffer, line, lineSize - 1); } fclose(stream);
return strlen(buffer);
}
int main (int argc, char** argv) { if (load(QOTD_FILE) == ERROR_CONDITION) { return ERROR_CONDITION; }
quoteOfTheDay();
freePointer(buffer); freePointer(qotd); }
---------------------------------------------------------------
#include "qotd.h" static qotd_t *qotd = NULL; static size_t count = 0; static char *buffer = NULL; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 void freePointer(void *ptr) { free ( *ptr); /* * 1. Write a short routine to clear *ptr. * 2. Use freePointer() throughout the code to remove all memory leaks. * 15 16 17 } buffersize_t getBufferSize(ssize_t new_size) * 18 19 20 21 22 23 24 25 26 27 28 29 * Calculate a new buffer size, being * sure to prevent against integer overflow * and/or wrap. return 0; 30 31 32 33 34 35 int addQuote(uint16_t pos, uint16_t len) { qotd = (qotd_t*) realloc(qotd, sizeof(qotd_t) * (count + 1)); if (qotd == NULL) { return ERROR_CONDITION; } 36 37 38 39 40 41 42 43 44 45 qotd [count].pos = pos; qotd[count]. len = len; count++; 46 return SUCCESS: qotd [count].pos = pos; qotd [count].len = len; count++; return SUCCESS; } void quoteOfTheDay() { char *str = NULL; srand( (unsigned)time(NULL)); uint16_t index = ((rand() % count) + 1) - 1; str = malloc(qotd[index]. len + 1); 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 if (str == NULL) { fprintf(stderr, "quoteOfTheDay ( ) %s", ERROR_MESSAGE); } else { strncpy(str, (char*) (buffer + qotd[index].pos), qotd[index]. len); str[qotd[index]. len] = '\0'; printf("%s ", str); } } int load(char *file) { FILE *stream; char *line = NULL; size_t length = 0; ssize_t lineSize; stream = fopen(file, "r"); if (stream == NULL) { return ERROR_CONDITION; } bila meci - culinalcina clanatha -tamil 1 11 LLL 82 83 return ERROR_CONDITION; } 84 85 86 while ((lineSize = getline(&line, &length, stream)) != -1) { char* pos; buffersize_t bufferSize; 87 88 89 90 91 if ((pos = strchr(line, ' ')) != NULL) { 92 *pos = '\0'; } 93 94 95 bufferSize = getBufferSize(lineSize); if (bufferSize == 0) { fclose(stream); fprintf(stderr, "getBufferSize() %s", ERROR_MESSAGE); return ERROR_CONDITION; } if ((buffer = realloc(buffer, bufferSize * sizeof(char))) == NULL) { fclose(stream); fprintf(stderr, "realloc() %s", ERROR_MESSAGE); return ERROR_CONDITION; } 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 if (addQuote(strlen(buffer), lineSize - 1) == ERROR_CONDITION) { fclose(stream); fprintf(stderr, "addQuote() %s", ERROR_MESSAGE); return ERROR_CONDITION; } strncat(buffer, line, lineSize - 1); } fclose(stream); return strlen(buffer); } } 124 125 126 127 128 129 int main (int argc, char** argv) { if (load (QOTD_FILE) ERROR_CONDITION) { return ERROR_CONDITION; } 130 131 132 133 134 135 quoteOfTheDay(); 136 freePointer(buffer); freePointer(qotd); } 137 138 139 140 #include "qotd.h" static qotd_t *qotd = NULL; static size_t count = 0; static char *buffer = NULL; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 void freePointer(void *ptr) { free ( *ptr); /* * 1. Write a short routine to clear *ptr. * 2. Use freePointer() throughout the code to remove all memory leaks. * 15 16 17 } buffersize_t getBufferSize(ssize_t new_size) * 18 19 20 21 22 23 24 25 26 27 28 29 * Calculate a new buffer size, being * sure to prevent against integer overflow * and/or wrap. return 0; 30 31 32 33 34 35 int addQuote(uint16_t pos, uint16_t len) { qotd = (qotd_t*) realloc(qotd, sizeof(qotd_t) * (count + 1)); if (qotd == NULL) { return ERROR_CONDITION; } 36 37 38 39 40 41 42 43 44 45 qotd [count].pos = pos; qotd[count]. len = len; count++; 46 return SUCCESS: qotd [count].pos = pos; qotd [count].len = len; count++; return SUCCESS; } void quoteOfTheDay() { char *str = NULL; srand( (unsigned)time(NULL)); uint16_t index = ((rand() % count) + 1) - 1; str = malloc(qotd[index]. len + 1); 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 if (str == NULL) { fprintf(stderr, "quoteOfTheDay ( ) %s", ERROR_MESSAGE); } else { strncpy(str, (char*) (buffer + qotd[index].pos), qotd[index]. len); str[qotd[index]. len] = '\0'; printf("%s ", str); } } int load(char *file) { FILE *stream; char *line = NULL; size_t length = 0; ssize_t lineSize; stream = fopen(file, "r"); if (stream == NULL) { return ERROR_CONDITION; } bila meci - culinalcina clanatha -tamil 1 11 LLL 82 83 return ERROR_CONDITION; } 84 85 86 while ((lineSize = getline(&line, &length, stream)) != -1) { char* pos; buffersize_t bufferSize; 87 88 89 90 91 if ((pos = strchr(line, ' ')) != NULL) { 92 *pos = '\0'; } 93 94 95 bufferSize = getBufferSize(lineSize); if (bufferSize == 0) { fclose(stream); fprintf(stderr, "getBufferSize() %s", ERROR_MESSAGE); return ERROR_CONDITION; } if ((buffer = realloc(buffer, bufferSize * sizeof(char))) == NULL) { fclose(stream); fprintf(stderr, "realloc() %s", ERROR_MESSAGE); return ERROR_CONDITION; } 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 if (addQuote(strlen(buffer), lineSize - 1) == ERROR_CONDITION) { fclose(stream); fprintf(stderr, "addQuote() %s", ERROR_MESSAGE); return ERROR_CONDITION; } strncat(buffer, line, lineSize - 1); } fclose(stream); return strlen(buffer); } } 124 125 126 127 128 129 int main (int argc, char** argv) { if (load (QOTD_FILE) ERROR_CONDITION) { return ERROR_CONDITION; } 130 131 132 133 134 135 quoteOfTheDay(); 136 freePointer(buffer); freePointer(qotd); } 137 138 139 140
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started