1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
#include <stddef.h>
#include <stdio.h>
///
/// @brief Appends contents of array `from` to array `to`.
/// @pre `limit` != `0`
/// @note No operation is performed for a `limit` of `0`.
/// @remarks Resulting array is NUL-terminated.
/// @param [out] to String to be written to.
/// @param limit Maximum number of bytes that string `to` can store, including NUL.
/// @param [in] from String to be copied from.
/// @returns Size of resulting string (NUL not counted).
///
size_t strscat(char *to, size_t limit, const char *from)
{
size_t s=0;
if (limit != 0)
{
while (to[s] != '\0')
++s;
for (size_t i=0; from[i] != '\0' && s < limit - 1; ++i, ++s)
to[s] = from[i];
to[s] = '\0';
}
return s;
}
typedef struct
{
char *to;
size_t limit;
const char *from;
const char *result;
size_t retval;
} test_t;
static size_t tests_failed;
static void run_test(test_t *t)
{
size_t i=0;
if (t->retval != strscat(t->to, t->limit, t->from))
{
++tests_failed;
return;
}
while (t->result[i] != '\0' || t->to[i] != '\0')
if (t->result[i] != t->to[i])
{
++tests_failed;
break;
}
else
++i;
}
#define RUN_TEST(...) run_test(&(test_t){__VA_ARGS__})
int main(void)
{
RUN_TEST(
.to = (char[15]){"The Cutty"},
.limit = 15,
.from = " Sark is a ship dry-docked in London.",
.result = "The Cutty Sark",
.retval = 14
);
RUN_TEST(
.to = (char[15]){"The Cutty"},
.limit = 0,
.from = "this won't get appended",
.result = "The Cutty",
.retval = 0
);
RUN_TEST(
.to = (char[15]){"The Cutty"},
.limit = 15,
.from = "!",
.result = "The Cutty!",
.retval = 10
);
RUN_TEST(
.to = (char[]){"The Cutty Sark"},
.limit = 3,
.from = "this shouldn't get appended",
.result = "The Cutty Sark",
.retval = 14
);
RUN_TEST(
.to = (char[]){"The Cutty Sark"},
.limit = 1,
.from = "this shouldn't get appended, either",
.result = "The Cutty Sark",
.retval = 14
);
RUN_TEST(
.to = (char[]){""},
.limit = 1,
.from = "this had better not get appended!",
.result = "",
.retval = 0
);
(void)fprintf(stderr, "Number of tests failed: %zu.\n", tests_failed);
}
|