int main(){ char* source = "abc"; char* destination=malloc((strlen(source)+1)*sizeof(char)); destination=mystrcpy(destination,source); printf("%d \n",myStrlen(destination)); printf("%s \n",myStrdup(source)); }
char * getNonTagFromString(const char *s) { memBuf_t buf; strToMemBuf(s, &buf); return myStrdup(getNonTag(&buf)); }
/* * Create a membuf from a string. */ memBuf_t * strToMemBuf(const char *s, memBuf_t *mp) { mp->timeToFirstByte = time(NULL); mp->memory = myStrdup(s); mp->readptr = mp->memory; mp->size = s ? strlen(s) : 0; return mp; }
/** * Write all user-specified option values to a buffer. * The buffer is dynamically allocated and must be freed by the caller. * * @param table pointer to the option table * @return pointer to text buffer */ char * logOptionValues(const optionTable_t *table) { char *res = myStrdup("\tspecified options or config values:\n"); char *tmp; char buf[1024]; const optionTable_t *tableptr; for(tableptr = table; NULL != tableptr->value; ++tableptr) { if(tableptr->isSet) { if(tableptr->logging == LOG_NORMAL) { switch(tableptr->type) { case OPTION_STRING: case OPTION_SPECSTR: sprintf(buf, "\t %2d x %.15s(%.15s) = \"%.900s\"\n", tableptr->isSet, nullEmptyStr(tableptr->configname), nullEmptyStr(tableptr->optionname), nullStr(*(char**)(tableptr->value)) ); break; case OPTION_INT: case OPTION_SPECINT: case OPTION_BOOL: case OPTION_BOOL_NEG: sprintf(buf, "\t %2d x %.15s(%.15s) = %d\n", tableptr->isSet, nullEmptyStr(tableptr->configname), nullEmptyStr(tableptr->optionname), *(int*)(tableptr->value) ); break; default: sprintf(buf, "\t %2d x %.15s(%.15s) = (unknown type)\n", tableptr->isSet, nullEmptyStr(tableptr->configname), nullEmptyStr(tableptr->optionname) ); break; } } else { sprintf(buf, "\t %2d x %.15s(%.15s) = ***\n", tableptr->isSet, nullEmptyStr(tableptr->configname), nullEmptyStr(tableptr->optionname) ); } tmp = myStrdup2(res, buf); free(res); res = tmp; } } return res; }
char * getNthNonTagFromString(const char *s, int n) { memBuf_t buf; int i; strToMemBuf(s, &buf); for (i = 1; i < n; i++) getNonTag(&buf); return myStrdup(getNonTag(&buf)); }
/* * parseStringValue(): parse a string value, call checking func if specified * * returns: 0 = OK, else error */ static int parseStringValue(const char *name, const char *value, optionTable_t *tableptr, const char *filename, const char *line) { if (tableptr->checkfunc) { /* Check value with specific check function. * Check function is responsible for allocating/freeing values */ if ((*tableptr->checkfunc)(value, tableptr, filename, line)) return 1; } else { free(*(char**)(tableptr->value)); *(char**)(tableptr->value) = myStrdup(value); } tableptr->isSet++; log(("string value for %s is \"%s\"\n", name, nullStr(*(char**)(tableptr->value)))); return 0; }
/*This function takes a pointer to an ordered set and a new string, and inserts the string into the tree pointed to in the ordered set*/ void orderedSetInsert(struct orderedSet *s, const char *newElem) { char* insertElem; /*variable to store input string*/ insertElem = myStrdup(newElem); /*malloc space for input string*/ treeInsert(&(s->set), insertElem); /*insert new string into tree pointed to in orderedSet s*/ }