#include #include #include int main() { /* Task: * Concatenate World! to hello[32] * Cut of garbage. * Assume "World!" is variable length */ char *world = "World! garbage"; char hello[32] = "Hello "; #ifdef TRY1 // works, but... meh. char buf[32] = ""; char *cutlen = "6"; char fmt[5] = ""; strlcat(fmt, "%.", sizeof(fmt)); strlcat(fmt, cutlen, sizeof(fmt)); strlcat(fmt, "s", sizeof(fmt)); snprintf(buf, sizeof(buf), fmt, world); strlcat(hello, buf, sizeof(hello)); printf("%s\n", hello); #endif #ifdef TRY2 // - copy string // - inject \0 at cutoff point char buf[32] = ""; strlcpy(buf, world, sizeof(buf)); char *ptr = &buf[0] + 7; *ptr = '\0'; strlcat(hello, buf, sizeof(hello)); printf("%s\n", hello); #endif return 0; }