Skip to content

Commit

Permalink
refactoring and memory safety
Browse files Browse the repository at this point in the history
  • Loading branch information
alba4k committed Sep 24, 2024
1 parent 6ffb28e commit 5aa7dba
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 46 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.PHONY: build/albafetch

OS := $(shell uname -o 2> /dev/null)
KERNEL := $(shell uname -s 2> /dev/null)

INSTALLPATH := /usr/local/bin
CONFIGPATH := /etc/xdg
Expand All @@ -14,7 +15,7 @@ ifeq ($(OS),Android)
CONFIGPATH := $(PREFIX)/etc
endif

ifeq ($(OS),Darwin)
ifeq ($(KERNEL),Darwin)
INSTALLFLAGS := -m755
CONFIGFLAGS := -m644
INSTALLPATH := $(PREFIX)/bin
Expand Down
9 changes: 3 additions & 6 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ project(
license : 'MIT'
)


src = [
'src/queue.c',
'src/utils.c',
Expand Down Expand Up @@ -54,19 +53,17 @@ if host_machine.system() == 'darwin'
]
endif

src_debug = ['src/debug.c']
src_debug += src
src_debug = src + ['src/debug.c']
src += 'src/main.c'

build_args = [
'-Wall',
'-Wextra',
'-Ofast',
'-std=c99',
'-std=gnu99',
]

debug_args = ['-g']
debug_args += build_args
debug_args = build_args + ['-g']

executable(
meson.project_name(),
Expand Down
4 changes: 2 additions & 2 deletions src/info/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ int cpu(char *dest) {

cpu_info = strstr(cpu_info, "model name");
if(cpu_info == NULL) {
free(buf);
return 1;
free(cpu_info);
}

cpu_info += 13;
Expand All @@ -66,8 +66,8 @@ int cpu(char *dest) {
else {
end = strchr(cpu_info, '\n');
if(end == NULL) {
free(buf);
return 1;
free(cpu_info);
}

*end = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/info/cursor_theme.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int cursor_theme(char *dest){
// cleanup
if(buf[0] != 0) {
if(buf[0] == '\'') {
strcpy(buf, buf+1);
memmove(buf, buf+1, strlen(buf));

char *ptr = strchr(buf, '\'');
if(ptr)
Expand Down
2 changes: 1 addition & 1 deletion src/info/gtk_theme.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int gtk_theme(char *dest){
// cleanup
if(buf[0] != 0) {
if(buf[0] == '\'') {
strcpy(buf, buf+1);
memmove(buf, buf+1, strlen(buf));

char *ptr = strchr(buf, '\'');
if(ptr)
Expand Down
2 changes: 0 additions & 2 deletions src/info/hostname.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "info.h"

#define _GNU_SOURCE

#include <string.h>

#include <unistd.h>
Expand Down
2 changes: 1 addition & 1 deletion src/info/icon_theme.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int icon_theme(char *dest){
// cleanup
if(buf[0] != 0) {
if(buf[0] == '\'') {
strcpy(buf, buf+1);
memmove(buf, buf+1, strlen(buf));

char *ptr = strchr(buf, '\'');
if(ptr)
Expand Down
5 changes: 0 additions & 5 deletions src/info/login_shell.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
#include "info.h"
#include "../utils.h"

#define _GNU_SOURCE

#include <string.h>

#include <stdlib.h>

#ifdef __APPLE__
#include <libgen.h>
#endif // __APPLE__

// get the current login shell
int login_shell(char *dest) {
Expand Down
49 changes: 29 additions & 20 deletions src/info/public_ip.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "info.h"

#define _GNU_SOURCE

#include <string.h>
#include <stdlib.h>

#include <netdb.h>
#include <unistd.h>
#include <sys/socket.h>

// Valgdrind complains about this. Absolutely no clue why it does.

// get the current public ip
int public_ip(char *dest) {
// https://stackoverflow.com/a/65362666 - thanks dbush
Expand All @@ -18,25 +19,39 @@ int public_ip(char *dest) {
hints.ai_protocol = 0;

// using this as it is faster than ident.me
if(getaddrinfo("whatismyip.akamai.com", "80", &hints, &addrs))
if(getaddrinfo("whatismyip.akamai.com", "80", &hints, &addrs)) {
freeaddrinfo(addrs);
return 1;
}

int socket_fd = socket(addrs->ai_family, addrs->ai_socktype, addrs->ai_protocol);
if(socket_fd == -1)
goto error;
if(socket_fd == -1) {
freeaddrinfo(addrs);
return 1;
}

if(connect(socket_fd, addrs->ai_addr, addrs->ai_addrlen) == -1)
goto error;
if(connect(socket_fd, addrs->ai_addr, addrs->ai_addrlen) == -1) {
close(socket_fd);
freeaddrinfo(addrs);
return 1;
}

char cmd[] = "GET / HTTP/1.1\nHost: whatismyip.akamai.com\n\n";
if(send(socket_fd, cmd, strlen(cmd), 0) == -1)
goto error;
if(send(socket_fd, cmd, strlen(cmd), 0) == -1) {
close(socket_fd);
freeaddrinfo(addrs);
return 1;
}

char buf[1024] = {0};
if(recv(socket_fd, buf, sizeof(buf), 0) == -1)
goto error;
char buf[1024] = "";
if(recv(socket_fd, buf, sizeof(buf), 0) == -1) {
close(socket_fd);
freeaddrinfo(addrs);
return 1;
}

close(socket_fd);
freeaddrinfo(addrs);

/* buf should now look like this:
* """
Expand All @@ -50,21 +65,15 @@ int public_ip(char *dest) {
char *start = buf, *end;
end = strchr(start, '\n');
if(strncmp(start, "HTTP/1.1 200 OK", end - start - 1))
goto error;

return 1;

start = strstr(start, "\n\r\n");
if(start == NULL)
goto error;
return 1;

start += 3;

strncpy(dest, start, 256);

freeaddrinfo(addrs);
return 0;

error:
freeaddrinfo(addrs);
return 1;
}
4 changes: 1 addition & 3 deletions src/info/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

#include <stdlib.h>
#include <stdio.h>
#include <libgen.h>
#include <unistd.h>

#ifdef __APPLE__
#include <libgen.h>
#endif // __APPLE__

// get the parent process name (usually the shell)
int shell(char *dest) {
Expand Down
2 changes: 0 additions & 2 deletions src/utils.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "logos.h"
#include "utils.h"

#define _GNU_SOURCE

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down
2 changes: 0 additions & 2 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#ifndef UTILS_H
#define UTILS_H

#define _GNU_SOURCE

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
Expand Down

0 comments on commit 5aa7dba

Please sign in to comment.