Skip to content

Commit

Permalink
Merge branch 'proh14:main' into cat
Browse files Browse the repository at this point in the history
  • Loading branch information
SzAkos04 authored Apr 20, 2024
2 parents 6e95c6f + 6c35700 commit ec2968a
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
on:
workflow_dispatch:
pull_request:
branches: [ main ]
push:
branches: [ main ]

jobs:
check_build:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/flake-check.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
on:
workflow_dispatch:
pull_request:
branches: [ main ]
push:
branches: [ main ]
paths:
- "**.nix"
- "**.lock"
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/fmt-check.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
on:
workflow_dispatch:
pull_request:
branches: [ main ]
push:
branches: [ main ]
paths:
- "**.c"
- "**.h"
Expand All @@ -26,4 +29,4 @@ jobs:
| xargs \
| tee /dev/stderr \
| wc -c)
[ $out -ge 1 ] && exit 1 || exit 0
[ $out -gt 1 ] && exit 1 || exit 0
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ result
# config
.cache
compile_commands.json
.pre-commit-config.yaml
2 changes: 1 addition & 1 deletion commons.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CFLAGS := @$/compile_flags.txt

CFLAGS += -ffunction-sections -fdata-sections
CFLAGS += -Wp,-U_FORTIFY_SOURCE
CFLAGS += -iquote src
CFLAGS += -iquote $/src

LDFLAGS := -fwhole-program -flto
LDFLAGS += -Wl,--gc-sections
Expand Down
82 changes: 81 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 26 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
pre-commit-hooks = {
url = "github:cachix/git-hooks.nix";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};
};

outputs = {
self,
nixpkgs,
flake-utils,
pre-commit-hooks,
}:
flake-utils.lib.eachSystem [
"aarch64-darwin"
Expand All @@ -16,11 +24,27 @@
"x86_64-linux"
] (system: let
pkgs = nixpkgs.legacyPackages.${system};
in {
in rec {
formatter = pkgs.alejandra;

checks = let
pre-commit-check = pre-commit-hooks.lib.${system}.run {
src = ./.;
hooks = {
alejandra.enable = true;
clang-format.enable = true;
};
};
in
if (builtins.elem system flake-utils.lib.defaultSystems)
then {inherit pre-commit-check;}
else {};

devShells.default = pkgs.mkShell {
# inputsFrom = pkgs.lib.attrsets.attrValues packages;
inherit (checks.pre-commit-check) shellHook;

hardeningDisable = ["fortify"];
inputsFrom = pkgs.lib.attrsets.attrValues packages;
packages = with pkgs; [
bear
python3Packages.compiledb
Expand Down
5 changes: 1 addition & 4 deletions src/echo/echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
#define VERSION "1.0.0"
#define AUTHOR "Hoorad Farrokh (proh14)"

#define print_version() \
do { \
printf("%s\nversion: %s\nby: %s\n", NAME, VERSION, AUTHOR); \
} while (0)
#include "version_info.h"

int isoctal(int c) { return (c >= '0' && c <= '7'); }

Expand Down
96 changes: 96 additions & 0 deletions src/getopt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "./getopt.h"
#include <stdio.h>
#include <stdlib.h>

char *optarg = NULL;
int optind = 1, opterr = 0, optopt = '\0';

static void getopt_printerr(const char *msg) {
if (opterr) {
fprintf(stderr, "%s", msg);
}
}

static int getopt_in(char d, const char *str) {
int i = 0;
while (str[i] != '\0') {
if (d == str[i] && str[i] != ':') {
return i;
}
i++;
}
return 0;
}

static void getopt_exchange(char *argv[], int i, int j) {
char *tmp = argv[i];
argv[i] = argv[j];
argv[j] = tmp;
}

int getopt(int argc, char **argv, const char *optstring) {
int c;
static char *nextchar = NULL;
static int coropt = 1;
if (!coropt) {
coropt = 1;
}

if (coropt >= argc || argv[coropt] == NULL) {
return -1;
}

while (argv[coropt] && argv[coropt][0] != '-') {
coropt++;
}

if (nextchar == NULL || *nextchar == '\0') {
if (coropt >= argc) {
return -1;
}
nextchar = &argv[coropt][1];
}

int idx;
if (!((idx = getopt_in(*nextchar, optstring)) >= 0)) {
getopt_printerr("invalid option\n");
optopt = *nextchar;
return '?';
}

c = *nextchar++;
if (*nextchar == '\0') {
coropt++;
nextchar = NULL;
}

if (optstring[idx + 1] != ':') {
coropt--;
optind++;
goto exit;
}

if (nextchar != NULL && *nextchar != '\0') {
coropt++;
}

if (coropt >= argc || argv[coropt][0] == '-') {
getopt_printerr("option requires an argument\n");
optopt = *nextchar;
return '?';
}

optarg = argv[coropt];

exit: {
int i = coropt;
int j = coropt - 1;
while (j >= 0 && argv[j][0] != '-') {
getopt_exchange(argv, i, j);
i--;
j--;
}
}
optind++;
return c;
}
23 changes: 23 additions & 0 deletions src/getopt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Reimplementation of GNU getopt
#ifndef _GETOPT_H_
#define _GETOPT_H_

struct option {
const char *name;
int has_arg;
int *flag;
int val;
};

extern char *optarg;
extern int optind, opterr, optopt;

int getopt(int argc, char **argv, const char *optstring);

int getopt_long(int argc, char *const argv[], const char *optstring,
const struct option *longopts, int *longindex);

int getopt_long_only(int argc, char *const argv[], const char *optstring,
const struct option *longopts, int *longindex);

#endif // _GETOPT_H_
11 changes: 5 additions & 6 deletions src/ls/ls_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
#define VERSION "1.0.0"
#define AUTHOR "Yohann Boniface (Sigmanificient)"

#define print_version() \
do { \
printf("%s\nversion: %s\nby: %s\n", NAME, VERSION, AUTHOR); \
} while (0)
#include "version_info.h"

static const char FLAGLIST[] = "alRdrt";
static char DEFAULT_LOCATION[] = ".";
Expand Down Expand Up @@ -62,8 +59,10 @@ int main(int argc, char **argv) {
int err = 0;

for (int i = 0; argv[i] != NULL; i++)
if (!strcmp(argv[i], "--version"))
return printf(VERSION), EXIT_SUCCESS;
if (!strcmp(argv[i], "--version")) {
print_version();
return EXIT_SUCCESS;
}
flags = compose_flaglist(argc, argv);
db.entries = malloc(db.size * sizeof(*db.entries));
if (db.entries == NULL)
Expand Down
Loading

0 comments on commit ec2968a

Please sign in to comment.