mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 01:18:00 +00:00
3396c97da9
Now that the new CMake build system is encouraging us to lay out the code like a set of libraries, it seems like a good idea to make them look more _like_ libraries, by putting things into separate modules as far as possible. This fixes several previous annoyances in which you had to link against some object in order to get a function you needed, but that object also contained other functions you didn't need which included link-time symbol references you didn't want to have to deal with. The usual offender was subsidiary supporting programs including misc.c for some innocuous function and then finding they had to deal with the requirements of buildinfo(). This big reorganisation introduces three new subdirectories called 'utils', one at the top level and one in each platform subdir. In each case, the directory contains basically the same files that were previously placed in the 'utils' build-time library, except that the ones that were extremely miscellaneous (misc.c, utils.c, uxmisc.c, winmisc.c, winmiscs.c, winutils.c) have been split up into much smaller pieces.
52 lines
1.4 KiB
C
52 lines
1.4 KiB
C
/*
|
|
* Trim square brackets off the outside of an IPv6 address literal.
|
|
* Leave all other strings unchanged. Returns a fresh dynamically
|
|
* allocated string.
|
|
*/
|
|
|
|
#include <ctype.h>
|
|
#include <string.h>
|
|
|
|
#include "defs.h"
|
|
#include "misc.h"
|
|
|
|
char *host_strduptrim(const char *s)
|
|
{
|
|
if (s[0] == '[') {
|
|
const char *p = s+1;
|
|
int colons = 0;
|
|
while (*p && *p != ']') {
|
|
if (isxdigit((unsigned char)*p))
|
|
/* OK */;
|
|
else if (*p == ':')
|
|
colons++;
|
|
else
|
|
break;
|
|
p++;
|
|
}
|
|
if (*p == '%') {
|
|
/*
|
|
* This delimiter character introduces an RFC 4007 scope
|
|
* id suffix (e.g. suffixing the address literal with
|
|
* %eth1 or %2 or some such). There's no syntax
|
|
* specification for the scope id, so just accept anything
|
|
* except the closing ].
|
|
*/
|
|
p += strcspn(p, "]");
|
|
}
|
|
if (*p == ']' && !p[1] && colons > 1) {
|
|
/*
|
|
* This looks like an IPv6 address literal (hex digits and
|
|
* at least two colons, plus optional scope id, contained
|
|
* in square brackets). Trim off the brackets.
|
|
*/
|
|
return dupprintf("%.*s", (int)(p - (s+1)), s+1);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Any other shape of string is simply duplicated.
|
|
*/
|
|
return dupstr(s);
|
|
}
|