I am trying to implement an eqivilent version of perl's chomp()
function in C and I have come across a corner case where a string literal passed as the argument will cause a segmentation fault (rightfully so).
Example chomp("some literal string\n");
Is there a defined way in C99 to detect wether or not my function was passed a string literal so that I can return
without attempting to NUL it out?
char* chomp(char *s)
{
char *temp = s;
if (s && *s)
{
s += strlen(s) - 1;
if (*s == '\n')
{
*s = '\0';
}
}
return temp;
}