-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug fix and new apps (datetime, weather, ip)
- Loading branch information
Showing
16 changed files
with
665 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
**/bin | ||
**/lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.