Skip to content

Commit

Permalink
Bug fix and new apps (datetime, weather, ip)
Browse files Browse the repository at this point in the history
  • Loading branch information
konect-V committed Jun 10, 2024
1 parent b453143 commit c76475a
Show file tree
Hide file tree
Showing 16 changed files with 665 additions and 161 deletions.
12 changes: 11 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,17 @@
"curl_addrinfo.h": "c",
"curl_printf.h": "c",
"connect.h": "c",
"sendf.h": "c"
"sendf.h": "c",
"telnet.h": "c",
"iovec.h": "c",
"cf-socket.h": "c",
"select.h": "c",
"url.h": "c",
"sockaddr.h": "c",
"inet_ntop.h": "c",
"inet_pton.h": "c",
"progress.h": "c",
"curl_memory.h": "c"
},
"files.exclude": {
"bundled": true,
Expand Down
50 changes: 49 additions & 1 deletion bootstrap/sys-apps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,55 @@ packages:
- '-r'
- '@THIS_SOURCE_DIR@/target/@OPTION:arch_name@/bin/.'
- '@SOURCE_ROOT@/target/boot_disk_kot_mount/.'
- name: kot
- name: weather
source:
subdir: 'sources/core/apps'
tools_required:
- host-gcc
pkgs_required:
- mlibc
- curl
build:
- args:
- 'make'
- '-C'
- '@THIS_SOURCE_DIR@/target/@OPTION:arch_name@'
- 'build'
environ:
CC: "@OPTION:cc@"
CXX: "@OPTION:cxx@"
LD: "@OPTION:ld@"
ASMC: "@OPTION:asmc@"
- args:
- 'cp'
- '-r'
- '@THIS_SOURCE_DIR@/target/@OPTION:arch_name@/bin/.'
- '@SOURCE_ROOT@/target/boot_disk_kot_mount/.'
- name: ip
source:
subdir: 'sources/core/apps'
tools_required:
- host-gcc
pkgs_required:
- mlibc
- curl
build:
- args:
- 'make'
- '-C'
- '@THIS_SOURCE_DIR@/target/@OPTION:arch_name@'
- 'build'
environ:
CC: "@OPTION:cc@"
CXX: "@OPTION:cxx@"
LD: "@OPTION:ld@"
ASMC: "@OPTION:asmc@"
- args:
- 'cp'
- '-r'
- '@THIS_SOURCE_DIR@/target/@OPTION:arch_name@/bin/.'
- '@SOURCE_ROOT@/target/boot_disk_kot_mount/.'
- name: datetime
source:
subdir: 'sources/core/apps'
tools_required:
Expand Down
76 changes: 76 additions & 0 deletions sources/core/apps/datetime/source/core/core.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp){
size_t real_size = size * nmemb;
char *buf = (char*)userp;
memcpy(&(buf[strlen(buf)]), contents, real_size);
return real_size;
}

void print_help(){
printf("Usage: ./program [--help | --credits | --utc]\n");
printf("If no argument is provided, the program will fetch and display the current date and time in your timezone.\n");
}

void print_credits(){
printf("A big thank you to worldtimeapi.org for providing the date and time data.\n");
}

void fetch_time(const char *url){
CURL *curl;
CURLcode res;
char read_buffer[1024];
memset(read_buffer, 0, sizeof(read_buffer));

curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl){
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, read_buffer);
curl_easy_setopt(curl, CURLOPT_CAINFO, "/usr/etc/ssl/cert.pem");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
res = curl_easy_perform(curl);

if(res != CURLE_OK){
printf("Error : curl_%s\n", curl_easy_strerror(res));
}else{
char* datetime = strstr(read_buffer, "\"datetime\":\"");
if (datetime) {
datetime += 12;
char *end = strstr(datetime, "\"");
if (end) {
*end = '\0';
printf("Current date and time: %s\n", datetime);
}
}
}

curl_easy_cleanup(curl);
}
curl_global_cleanup();
}

int main(int argc, char *argv[]){
const char *url = "http://worldtimeapi.org/api/ip";

if (argc == 2) {
if (strcmp(argv[1], "--help") == 0) {
print_help();
return 0;
}
else if (strcmp(argv[1], "--credits") == 0) {
print_credits();
return 0;
}
else if (strcmp(argv[1], "--utc") == 0) {
url = "http://worldtimeapi.org/api/timezone/UTC";
}
}

fetch_time(url);
return 0;
}
39 changes: 39 additions & 0 deletions sources/core/apps/datetime/target/amd64/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
cyan = /bin/echo -e "\x1b[36m\#\# $1\x1b[0m"

# Project Root
override HOME = ../..

# Project Resources
SYSROOT = $(HOME)/../../../../sysroot
INCLUDE = $(SYSROOT)/usr/include
LIBRARIES = $(SYSROOT)/usr/lib
SOURCE = $(HOME)/source
TOOLS = $(HOME)/../../tools
BIN = bin/usr/bin
LIB = lib

# Tools Config
CFLAGS =

LDFLAGS = -Wall \
-lc

# Recursive Wild Card
rwildcard = $(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d))

# Source Files
C_SRC = $(call rwildcard,$(SOURCE),*.c)

OBJS = $(patsubst $(SOURCE)/%.c,$(LIB)/%_c.o,$(C_SRC))

# Target
$(LIB)/%_c.o: $(SOURCE)/%.c
@ mkdir -m 777 -p $(@D)
@ $(call cyan,"$(subst ../,,$^)")
@ $(CC) $(CFLAGS) -c $^ -o $@

link:
@ mkdir -m 777 -p $(BIN)
@ $(CC) $(LDFLAGS) -o $(BIN)/datetime $(OBJS) $(LIBRARIES)/libcurl.a $(LIBRARIES)/libtls.a $(LIBRARIES)/libssl.a

build: $(OBJS) link
58 changes: 58 additions & 0 deletions sources/core/apps/ip/source/core/core.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp){
size_t real_size = size * nmemb;
char *buf = (char*)userp;
memcpy(&(buf[strlen(buf)]), contents, real_size);
return real_size;
}

void print_help(){
printf("Usage: ./program [--help | --credits]\n");
printf("If no argument is provided, the program will fetch and display your IP address.\n");
}

void print_credits(){
printf("A big thank you to ifconfig.me for providing the IP data.\n");
}

int main(int argc, char *argv[]){
CURL *curl;
CURLcode res;
char read_buffer[1024];
memset(read_buffer, 0, sizeof(read_buffer));

if(argc == 2){
if(strcmp(argv[1], "--help") == 0){
print_help();
return 0;
}else if (strcmp(argv[1], "--credits") == 0){
print_credits();
return 0;
}
}

curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl){
curl_easy_setopt(curl, CURLOPT_URL, "ifconfig.me");
curl_easy_setopt(curl, CURLOPT_CAINFO, "/usr/etc/ssl/cert.pem");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, read_buffer);
res = curl_easy_perform(curl);

if(res != CURLE_OK){
printf("Error : curl_%s\n", curl_easy_strerror(res));
}else{
printf("IP address: %s\n", read_buffer);
}

curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
2 changes: 2 additions & 0 deletions sources/core/apps/ip/target/amd64/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/bin
**/lib
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ $(LIB)/%_c.o: $(SOURCE)/%.c

link:
@ mkdir -m 777 -p $(BIN)
@ $(CC) $(LDFLAGS) -o $(BIN)/kot $(OBJS) $(LIBRARIES)/libcurl.a $(LIBRARIES)/libtls.a $(LIBRARIES)/libssl.a
@ $(CC) $(LDFLAGS) -o $(BIN)/ip $(OBJS) $(LIBRARIES)/libcurl.a $(LIBRARIES)/libtls.a $(LIBRARIES)/libssl.a

build: $(OBJS) link
Loading

0 comments on commit c76475a

Please sign in to comment.