Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

use terminal to run this run this code. there are 3 files, qotd.c, qotd.h, qotd.txt ---------------------------------------------------------------------------------------------------------- qotd.c ------------------------------------------------------------------------------------------------------------- #include qotd.h static qotd_t *qotd = NULL;

use terminal to run this run this code. there are 3 files, qotd.c, qotd.h, qotd.txt image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

----------------------------------------------------------------------------------------------------------

qotd.c

-------------------------------------------------------------------------------------------------------------

#include "qotd.h"

static qotd_t *qotd = NULL; static size_t count = 0; static char *buffer = NULL;

void freePointer(void *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); }

------------------------------------------------------------------

qotd.h

-------------------------------------------------------------------

#ifndef __QOTD_H #define __QOTD_H

#include #include #include #include #include #include

#define QOTD_FILE "qotd.txt"

#define ERROR_MESSAGE "failed to allocate space for QOTD data. Exiting. "

#define SUCCESS 0 #define ERROR_CONDITION -1

typedef uint16_t buffersize_t;

typedef struct qotd_t {

uint16_t pos; uint16_t len;

} qotd_t;

#define BUFFERSIZE USHRT_MAX

#endif

-------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------

qotd.txt

-----------------------------------------------------------------------------------------------------------------------

Life is what happens when you're busy making other plans. -- John Lennon It is better to light a candle than curse the darkness. -- Eleanor Roosevelt You must be the change you wish to see in the world. -- Gandhi Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. -- Albert Einstein Any sufficiently advanced technology is indistinguishable from magic. -- Arthur C. Clarke

#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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

A class can have multiple constructors with different parameters.

Answered: 1 week ago

Question

What should Meddevco have done to avoid some of its problems?

Answered: 1 week ago

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago