Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
src/main.c
Go to the documentation of this file.
1/**
2 * @file main.c
3 * @brief Entry point for the application. Parses command-line arguments
4 * and either starts the HTTP server or fetches and displays a GitHub
5 * user.
6 */
7
8#include "server.h"
9#include "user.h"
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13
14/**
15 * @brief Main function that initializes and runs the application.
16 *
17 * Supported command-line arguments:
18 * - `--user <username>` : Specify the GitHub username to fetch.
19 * - `--user=<username>` : Alternative way to specify the GitHub username.
20 * - `--server` : Run the HTTP server instead of fetching a user.
21 * - `--port=<port>` : Specify the port for the HTTP server (default: 1234).
22 *
23 * @param argc Argument count.
24 * @param argv Argument vector.
25 * @return int Exit status code (0 for success, nonzero for failure).
26 */
27int main(int argc, char **argv) {
28 const char *username = "izelnakri";
29 int port = 1234;
30 int run_server = 0;
31
32 for (int i = 1; i < argc; i++) {
33 if (strcmp(argv[i], "--user") == 0 && i + 1 < argc) {
34 username = argv[i + 1];
35 i++;
36 } else if (strncmp(argv[i], "--user=", 7) == 0) {
37 username = argv[i] + 7;
38 } else if (strcmp(argv[i], "--server") == 0) {
39 run_server = 1;
40 } else if (strncmp(argv[i], "--port=", 7) == 0) {
41 char *endptr = NULL;
42 port = (int)strtol(argv[i] + 7, &endptr, 10);
43 if (endptr == argv[i] + 7 || *endptr != '\0') {
44 (void)fprintf(stderr, "Invalid port number: %s\n", argv[i] + 7);
45 exit(EXIT_FAILURE);
46 }
47 }
48 }
49
50 if (run_server) {
52 } else {
53 User user = fetch_github_user(username);
54 print_github_user(&user);
55 }
56
57 return 0;
58}
#define NULL
Definition gmacros.h:924
int main(void)
Definition sanitycheckc.c:1
void start_http_server(int port)
Starts an HTTP server on the specified port.
Definition server.c:59
Declaration of the HTTP server functions.
Structure representing a GitHub user.
Definition user.h:9
void print_github_user(const User *user)
Print a User's information to the console.
Definition user.c:230
User fetch_github_user(const char *username)
Fetch a GitHub user's information via the GitHub API.
Definition user.c:130