Commit 7747254c authored by Niels Möller's avatar Niels Möller
Browse files

Moved functions format_cstring and make_cstring here.

Rev: src/format.c:1.19
Rev: src/format.h:1.14
parent 7cd1a123
...@@ -416,3 +416,37 @@ void ssh_vformat_write(char *f, UINT32 size, UINT8 *buffer, va_list args) ...@@ -416,3 +416,37 @@ void ssh_vformat_write(char *f, UINT32 size, UINT8 *buffer, va_list args)
assert(buffer == start + size); assert(buffer == start + size);
} }
/* These functions add an extra NUL-character at the end of the string
* (not included in the length), to make it possible to pass the
* string directly to C library functions. */
struct lsh_string *format_cstring(char *s)
{
if (s)
{
struct lsh_string *res = ssh_format("%lz%c", s, 0);
res->length--;
return res;
}
return NULL;
}
struct lsh_string *make_cstring(struct lsh_string *s, int free)
{
struct lsh_string *res;
if (memchr(s->data, '\0', s->length))
{
if (free)
lsh_string_free(s);
return 0;
}
res = ssh_format("%lS%c", s, 0);
res->length--;
if (free)
lsh_string_free(s);
return res;
}
...@@ -77,4 +77,7 @@ void ssh_vformat_write(char *format, UINT32 length, UINT8 *buffer, va_list args) ...@@ -77,4 +77,7 @@ void ssh_vformat_write(char *format, UINT32 length, UINT8 *buffer, va_list args)
/* Short cut */ /* Short cut */
#define lsh_string_dup(s) (ssh_format("%lS", (s))) #define lsh_string_dup(s) (ssh_format("%lS", (s)))
struct lsh_string *format_cstring(char *s);
struct lsh_string *make_cstring(struct lsh_string *s, int free);
#endif /* LSH_FORMAT_H_INCLUDED */ #endif /* LSH_FORMAT_H_INCLUDED */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment