modified strndupcat, added more comments

This commit is contained in:
Fufu Fang 2019-04-21 11:10:18 +01:00
parent e5c77f3680
commit b1971a7c3f
3 changed files with 21 additions and 10 deletions

View File

@ -19,8 +19,17 @@
*
* More information regarding block size can be found at:
* https://wiki.vuze.com/w/Torrent_Piece_Size
*
* Note that at the current configuration, a 16GiB file uses 128MiB of memory to
* store the bitmap, but hey, I have 16GiB on my computer!
*/
#define DATA_BLK_SZ 131072
/**
* \brief the maximum length of a path
* \details This corresponds the maximum path length under Ext4. If you need
* longer path, then fuck you.
*/
#define MAX_PATH_LEN 4096
/**
@ -399,9 +408,3 @@ long Data_write(const Cache *cf, long offset, long len,
}
return byte_written;
}

View File

@ -6,9 +6,18 @@
char *strndupcat(const char *a, const char *b, int n)
{
int na = strnlen(a, n/2);
int nb = strnlen(b, n/2);
int na = strnlen(a, n);
int nb = strnlen(b, n);
int nc = na + nb + 1;
if (nc > n) {
fprintf(stderr,
"strndupcat(): resulting string length exceeds maximum limit!\n");
/*
* It is better to crash the program here, then corrupting the cache
* folder
*/
exit(EXIT_FAILURE);
}
char *c = calloc(nc, sizeof(char));
if (!c) {
fprintf(stderr, "strndupcat(): calloc failure!\n");
@ -16,5 +25,6 @@ char *strndupcat(const char *a, const char *b, int n)
}
strncpy(c, a, na);
strncat(c, b, nb);
c[nc-1] = '\0';
return c;
}

View File

@ -10,8 +10,6 @@
* \brief strndup with concatenation
* \details This function concatenate string a and string b together, and put
* the result in a new string.
* \note The maximum length of string a and string b is half the length of the
* maximum length of the output string.
* \param[in] a the first string
* \param[in] b the second string
* \param[n] c the maximum length of the output string