27 (void)fprintf(stderr,
"Memory allocation failed\n");
37 if (buf && buf->
data) {
53 if (buf->
capacity >= required_size) {
58 size_t new_capacity = buf->
capacity * 2;
59 while (new_capacity < required_size) {
63 if (new_capacity < buf->capacity) {
64 (void)fprintf(stderr,
"Buffer capacity overflow\n");
71 (void)fprintf(stderr,
"Memory allocation failed for buffer\n");
91 size_t total_size = size * nmemb;
95 if (total_size / size != nmemb) {
96 (void)fprintf(stderr,
"Integer overflow in size calculation\n");
102 (void)fprintf(stderr,
"Buffer size would overflow\n");
112 if (total_size > 0) {
113 memmove(buf->
data + buf->
size, contents,
116 buf->
size += total_size;
135 if (!username || *username ==
'\0') {
136 (void)fprintf(stderr,
"Invalid username\n");
143 size_t username_len = strnlen(username, 100);
147 char *url =
malloc(url_size);
149 (void)fprintf(stderr,
"Memory allocation failed for URL\n");
155 snprintf(url, url_size,
"https://api.github.com/users/%s", username);
156 if (ret < 0 || (
size_t)ret >= url_size) {
157 (void)fprintf(stderr,
"Error: URL was truncated or snprintf failed\n");
165 (void)fprintf(stderr,
"curl init failed\n");
191 char *buffer_data = buffer.
data;
194 (void)fprintf(stderr,
"JSON parse error: %s\n",
error.text);
232 (void)fprintf(stderr,
"NULL user pointer\n");
CURL_EXTERN const char * curl_easy_strerror(CURLcode)
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl)
CURL_EXTERN CURL * curl_easy_init(void)
#define json_is_string(json)
const char * json_string_value(const json_t *string)
json_t * json_object_get(const json_t *object, const char *key) JANSSON_ATTRS((warn_unused_result))
json_t * json_loads(const char *input, size_t flags, json_error_t *error) JANSSON_ATTRS((warn_unused_result))
static JSON_INLINE void json_decref(json_t *json)
static void error(LoadState *S, const char *why)
Buffer structure to hold incoming HTTP response data.
Structure representing a GitHub user.
const char * name
Full name.
const char * login
GitHub username.
const char * location
User's location.
const char * company
Company name.
#define curl_easy_setopt(handle, option, value)
int buffer_ensure_capacity(Buffer *buf, size_t required_size)
Ensure that a Buffer has enough capacity for new data.
void buffer_init(Buffer *buf)
Initialize a Buffer structure.
void buffer_cleanup(Buffer *buf)
Free the memory held by a Buffer.
void print_github_user(const User *user)
Print a User's information to the console.
User fetch_github_user(const char *username)
Fetch a GitHub user's information via the GitHub API.
static size_t write_callback(const void *contents, size_t size, size_t nmemb, void *userp)
Callback for libcurl to write response data into a Buffer.