error:
if (output) {
g_string_free(output, TRUE);
+ output = NULL;
}
end:
at += end_pos;
- /* :// */
+ /* `://` */
if (strncmp(at, "://", 3) != 0) {
if (error_buf) {
snprintf(error_buf, error_buf_size,
goto error;
}
+ /* Skip `://` */
at += 3;
/* Hostname */
}
if (at[end_pos] == '\0') {
+ /* Relay daemon hostname and ports provided only */
goto end;
}
at += end_pos;
- /* /host/ */
+ /* `/host/` */
if (strncmp(at, "/host/", 6) != 0) {
if (error_buf) {
snprintf(error_buf, error_buf_size,
}
if (at[end_pos] == '\0') {
- goto end;
+ if (error_buf) {
+ snprintf(error_buf, error_buf_size,
+ "Missing `/` after target hostname (`%s`)",
+ parts.target_hostname->str);
+ }
+
+ goto error;
}
+ /* Skip `/` */
at += end_pos + 1;
/* Session name */
norm_path = NULL;
}
end:
- if (tmp) {
- free(tmp);
- }
+ free(tmp);
return norm_path;
}
#else
g_strfreev(lines);
}
- if (line_words) {
- g_strfreev(line_words);
- }
+ BT_ASSERT(!line_words);
if (tmp_line) {
g_string_free(tmp_line, TRUE);
return ret;
}
#endif /* __MINGW32__ */
+
+BT_HIDDEN
+int bt_common_g_string_append_printf(GString *str, const char *fmt, ...)
+{
+ va_list ap;
+ gsize len, allocated_len, available_len;
+ int print_len;
+
+ /* str->len excludes \0. */
+ len = str->len;
+ /* Explicitly exclude \0. */
+ allocated_len = str->allocated_len - 1;
+ available_len = allocated_len - len;
+
+ str->len = allocated_len;
+ va_start(ap, fmt);
+ print_len = vsnprintf(str->str + len, available_len + 1, fmt, ap);
+ va_end(ap);
+ if (print_len < 0) {
+ return print_len;
+ }
+ if (G_UNLIKELY(available_len < print_len)) {
+ /* Resize. */
+ g_string_set_size(str, len + print_len);
+ va_start(ap, fmt);
+ print_len = vsprintf(str->str + len, fmt, ap);
+ va_end(ap);
+ } else {
+ str->len = len + print_len;
+ }
+ return print_len;
+}
+
+BT_HIDDEN
+int bt_common_append_file_content_to_g_string(GString *str, FILE *fp)
+{
+ const size_t chunk_size = 4096;
+ int ret = 0;
+ char *buf;
+ size_t read_len;
+ gsize orig_len = str->len;
+
+ BT_ASSERT(str);
+ BT_ASSERT(fp);
+ buf = g_malloc(chunk_size);
+ if (!buf) {
+ ret = -1;
+ goto end;
+ }
+
+ while (true) {
+ if (ferror(fp)) {
+ ret = -1;
+ goto end;
+ }
+
+ if (feof(fp)) {
+ break;
+ }
+
+ read_len = fread(buf, 1, chunk_size, fp);
+ g_string_append_len(str, buf, read_len);
+ }
+
+end:
+ if (ret) {
+ /* Remove what was appended */
+ g_string_truncate(str, orig_len);
+ }
+
+ g_free(buf);
+ return ret;
+}