3 ms·
This is a safe, if slow, alternative for strncpy, but it does not safely replace strncat. The C standard does not define the behavior if the code "sprintf(buf,
by Hello71 8y ago
This is a safe, if slow, alternative for strncpy, but it does not safely replace strncat. The C standard does not define the behavior if the code "sprintf(buf, "%s some further text", buf);" is used.
- dmitrygr 8y ago#define strncat(a, b, maxlen) snprintf(a + strlen(a), maxlen - strlen(a), "%s", b)
- kazinator 8y agoToo many evaluations of a. If you have snprintf, you probably have inline functions. One day someone will do something like strncat(a += previous_delta, ...).
- dmitrygr 8y agoOPTION 1 (c): char* mystrncat(char *a, const char *b, size_t maxlen){ snprintf(a + strlen(a), maxlen - strlen(a), "%s", b); return a; } OPTION 2 (gccism): #define strncat(_a, b, maxlen) ({char* a = _a; snprintf(a + strlen(a), maxlen - strlen(a), "%s", b); _a;})
- vortico 8y agoRight, don't use `buf` as an input and output with `snprintf`. A lot of people use `str(n)cat` and `str(n)cpy` together like char *a; char *b; char *c; strcpy(c, a); strcat(c, b); and I meant that `snprintf` can replace that. But if you actually only have two strings, one with a larger buffer than what it contains, and one to be concatinated, then you can't use `snprintf` like that.
- legulere 8y agoStrncpy gives back the length copied which you can use to append more text: char buf[1024]; size_t pos = 0; int ret = snprintf(buf, sizeof(buf), src1); if(ret >= 0) { pos += ret } else { error() } ret = snprintf(buf + pos, sizeof(buf) - pos, src2); ...