Skip to content

Commit

Permalink
Merge pull request #17 from jmattaa/working
Browse files Browse the repository at this point in the history
Add github actions and remove magic numbers
  • Loading branch information
jmattaa authored Nov 5, 2024
2 parents 5b81302 + df62f75 commit cedb065
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 39 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/check-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: clang-format Check
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
clang-format:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install clang-format
run: sudo apt-get install -y clang-format

- name: Check code formatting
id: check_formatting
run: ./scripts/clang-check-format.sh
shell: bash
22 changes: 22 additions & 0 deletions .github/workflows/makefile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Makefile CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Run make
run: make

- name: Clean
run: make clean

4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ debug: $(TARGET)

release: CFLAGS += $(CFLAGS_RELEASE)
release: LDFLAGS += $(LDFLAGS_RELEASE)
release: clean
release: $(TARGET)

$(TARGET): $(OBJS) | $(BIN_DIR)
Expand All @@ -46,10 +47,11 @@ clean:
rm -rf $(OBJ_DIR) $(BIN_DIR)

format:
clang-format -i $(PROJECT_DIR)/*.c $(PROJECT_DIR)/include/*.h
clang-format -i $(SRCS) $(PROJECT_DIR)/include/**.h

install: release
install -m 755 $(TARGET) /usr/local/bin
make clean

uninstall:
rm -f /usr/local/bin/$(notdir $(TARGET))
Expand Down
8 changes: 4 additions & 4 deletions include/git/lgit.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@

#include "../../include/utils.h"

#include <stdlib.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct
{
char *name;
unsigned tracked:1;
unsigned tracked : 1;

mode_t mode;
mode_t mode;
} lgit_entry;

typedef struct
typedef struct
{
char **lgit_entry;
} lgit_entries;
Expand Down
2 changes: 1 addition & 1 deletion include/git/lgit_utils.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef LASER_GIT_UTILS_H
#define LASER_GIT_UTILS_H

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

uint32_t lgit_utils_hexToUint32(const char *hex);
uint32_t lgit_utils_binToUint32(const char *bin);
Expand Down
8 changes: 5 additions & 3 deletions include/laser.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
#define LASER_LASER_H

#include "colors.h"
#include "utils.h"
#include "sort.h"
#include "utils.h"
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>

#define MAX_ENTRIES 1024
#define PATH_MAX 1024

void laser_list_directory(laser_opts opts, int depth);
void laser_print_entry(const char *name, const char *color, char *indent, int depth, int is_last);
void laser_print_entry(const char *name, const char *color, char *indent,
int depth, int is_last);

#endif
7 changes: 4 additions & 3 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>

typedef struct laser_opts
{
Expand All @@ -24,7 +24,8 @@ typedef struct laser_opts

laser_opts laser_utils_parsecmd(int argc, char **argv);
//void laser_utils_format_date(time_t time, char *buffer, size_t buffer_size);
char **laser_utils_grow_strarray(char **array, size_t *alloc_size, size_t count);
char **laser_utils_grow_strarray(char **array, size_t *alloc_size,
size_t count);
int laser_string_in_sorted_array(char *target, char **array, int size);
int laser_cmp_string(const void *a, const void *b, const void *arg);
void laser_swap(void *a, void *b, size_t size);
Expand Down
8 changes: 8 additions & 0 deletions scripts/clang-check-format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
files=$(find . -name '*.c' -o -name '*.h')
for file in $files; do
clang-format -style=file $file | diff $file - >/dev/null
if [ $? -ne 0 ]; then \
echo "::error file=$file::Code not formatted according to clang-format"
exit 1
fi
done
3 changes: 1 addition & 2 deletions src/filetypes/archives.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ const struct laser_magicnumber laser_archiveformats[] = {
{(unsigned char[]){0x42, 0x5A, 0x68}, 3}, // BZIP2
{(unsigned char[]){0xFD, 0x37, 0x7A, 0x58, 0x5A}, 5}, // XZ
// ADD MORE ARCHIVES
{NULL, 0}
};
{NULL, 0}};
3 changes: 2 additions & 1 deletion src/filetypes/checktype.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ int laser_checktype(const char *filename,
while (formats[i].magic_size != 0)
{
if (bytesRead >= formats[i].magic_size &&
memcmp(buffer, formats[i].magic, formats[i].magic_size) == 0)
memcmp(buffer, formats[i].magic, formats[i].magic_size) ==
0) ///asdjaklsdjalksdj
return 1;
i++;
}
Expand Down
3 changes: 1 addition & 2 deletions src/filetypes/medias.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ const struct laser_magicnumber laser_mediaformats[] = {
{(unsigned char[]){0x66, 0x74, 0x79, 0x70, 0x4D, 0x53, 0x4E, 0x56},
3}, // MP4
// ADD MORE MEDIA TYPES
{NULL, 0}
};
{NULL, 0}};
29 changes: 13 additions & 16 deletions src/git/lgit.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

char **lgit_parseGitignore(const char *dir, int *count)
{
size_t filename_len =
strlen(dir) + strlen("/.gitignore") +
1;

size_t filename_len = strlen(dir) + strlen("/.gitignore") + 1;
char *filename = malloc(filename_len);

snprintf(filename, filename_len, "%s/.gitignore", dir);
Expand Down Expand Up @@ -69,51 +66,51 @@ char **lgit_parseGitignore(const char *dir, int *count)
// lgit_entries *lgit_getGitEntries(laser_opts opts)
// {
// lgit_entries *entries = malloc(sizeof(lgit_entries));
//
//
// lgit_parseGit(opts.parentDir);
//
//
// return entries;
// }
//
//
// void lgit_parseGit(char *dir)
// {
// // TODO: check if git dir exists!!
// char *index_file = malloc(strlen(dir) + strlen("/.git/index") + 1);
//
//
// snprintf(index_file, strlen(dir) + strlen("/.git/index") + 1,
// "%s/.git/index", dir);
//
//
// size_t idxfsize;
// char *idx_bincontent = lgit_utils_readbin(index_file, &idxfsize);
// char *idx_file_as_hex = lgit_utils_binToHex(idx_bincontent, idxfsize);
//
//
// // https://git-scm.com/docs/index-format
// // HEADER
// // first 4 bytes DIRC
// char HEADER_SIGNATURE[4] = {'D', 'I', 'R', 'C'};
//
//
// if (strcmp(lgit_utils_getChars(idx_bincontent, 0, 4 * 8),
// HEADER_SIGNATURE) != 0)
// {
// fprintf(stderr, "lsr: %s: file is not formatted right!!\n", index_file);
// return;
// }
//
//
// // next 4 bytes: version
// // 4 bytes = 8 hex nums
// uint32_t version =
// lgit_utils_hexToUint32(lgit_utils_getChars(idx_file_as_hex, 8, 8));
//
//
// // then 32 bit num for index entries
// // 32 bit 4 byte
// uint32_t entries_num =
// lgit_utils_hexToUint32(lgit_utils_getChars(idx_file_as_hex, 16, 8));
//
//
// printf("version: %u, entries_count: %u\n", version, entries_num);
// }
//
//
// void lgit_entries_free(lgit_entries *lgit)
// {
// free(lgit);
// }
//
//
14 changes: 8 additions & 6 deletions src/laser.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "git/lgit.h"
#include "utils.h"

#define BRANCH_SIZE 8

char *strip_parent_dir(const char *full_path, const char *parent_dir)
{
size_t parent_len = strlen(parent_dir);
Expand All @@ -22,8 +24,8 @@ int laser_cmp_dirent(const void *a, const void *b, const void *arg)
struct dirent *dirent_a = *(struct dirent **)a;
struct dirent *dirent_b = *(struct dirent **)b;

char path_a[1024];
char path_b[1024];
char path_a[PATH_MAX];
char path_b[PATH_MAX];
snprintf(path_a, sizeof(path_a), "%s/%s", dir_path, dirent_a->d_name);
snprintf(path_b, sizeof(path_b), "%s/%s", dir_path, dirent_b->d_name);

Expand All @@ -48,7 +50,7 @@ int laser_cmp_dirent(const void *a, const void *b, const void *arg)
void laser_print_entry(const char *name, const char *color, char *indent,
int depth, int is_last)
{
char branch[8] = "";
char branch[BRANCH_SIZE] = "";
if (depth > 0)
strcpy(branch, is_last ? "└─ " : "├─ ");

Expand All @@ -70,7 +72,7 @@ void laser_process_entries(laser_opts opts, int depth, char *indent,
struct dirent **entries = malloc(sizeof(struct dirent *));
int entry_count = 0;

char full_path[1024];
char full_path[PATH_MAX];
while ((entry = readdir(dir)) != NULL)
{
snprintf(full_path, sizeof(full_path), "%s/%s", opts.dir,
Expand Down Expand Up @@ -138,13 +140,13 @@ void laser_process_entries(laser_opts opts, int depth, char *indent,
{
if (S_ISLNK(file_stat.st_mode) && opts.show_symlinks)
{
char symlink_target[1024];
char symlink_target[PATH_MAX];
int len = readlink(full_path, symlink_target,
sizeof(symlink_target) - 1);
if (len != -1)
{
symlink_target[len] = '\0';
char res_string[2048];
char res_string[PATH_MAX * 2 + 4]; // 4 is " -> "
snprintf(res_string, sizeof(res_string), "%s -> %s",
entries[i]->d_name, symlink_target);
laser_print_entry(res_string, SYMLINK_COLOR, indent, depth,
Expand Down

0 comments on commit cedb065

Please sign in to comment.