examples/register: replace getline() with fgets()

getline(3) doesn't exist on all systems.
This commit is contained in:
Dmitry Podgorny
2020-05-31 22:05:21 +03:00
parent a65dd1059d
commit cba21b5598

View File

@@ -46,8 +46,8 @@ iq_reg_send_form(xmpp_reg_t *reg, xmpp_conn_t *conn, xmpp_stanza_t *stanza)
xmpp_stanza_t *text; xmpp_stanza_t *text;
xmpp_stanza_t *iq; xmpp_stanza_t *iq;
const char *name; const char *name;
ssize_t rc; size_t len;
size_t size; char buf[256];
char *s; char *s;
query = xmpp_stanza_get_child_by_name(stanza, "query"); query = xmpp_stanza_get_child_by_name(stanza, "query");
@@ -68,22 +68,24 @@ iq_reg_send_form(xmpp_reg_t *reg, xmpp_conn_t *conn, xmpp_stanza_t *stanza)
xmpp_free(ctx, s); xmpp_free(ctx, s);
} else { } else {
printf("%s: ", name); printf("%s: ", name);
s = NULL; s = fgets(buf, sizeof(buf), stdin);
size = 0; if (s != NULL) {
rc = getline(&s, &size, stdin); len = strlen(s);
if (rc > 0 && s[rc - 1] == '\n') if (len > 0 && s[len - 1] == '\n') {
s[rc - 1] = '\0'; s[len - 1] = '\0';
if (rc > 0 && strlen(s) > 0) { --len;
elem = xmpp_stanza_new(ctx); }
text = xmpp_stanza_new(ctx); if (len > 0) {
xmpp_stanza_set_text(text, s); elem = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(elem, name); text = xmpp_stanza_new(ctx);
xmpp_stanza_add_child(elem, text); xmpp_stanza_set_text(text, s);
xmpp_stanza_add_child(query, elem); xmpp_stanza_set_name(elem, name);
xmpp_stanza_release(text); xmpp_stanza_add_child(elem, text);
xmpp_stanza_release(elem); xmpp_stanza_add_child(query, elem);
xmpp_stanza_release(text);
xmpp_stanza_release(elem);
}
} }
free(s);
} }
next = xmpp_stanza_get_next(next); next = xmpp_stanza_get_next(next);
} }