Skip to content

Commit

Permalink
Merge branch 'proh14:main' into date
Browse files Browse the repository at this point in the history
  • Loading branch information
Eike-Flath authored Apr 24, 2024
2 parents 42370e1 + 7072fc1 commit 0a5be13
Show file tree
Hide file tree
Showing 17 changed files with 493 additions and 134 deletions.
28 changes: 28 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This file contains a list of commits that are not likely what you
# are looking for in a blame, such as mass reformatting or renaming.
# You can set this file as a default ignore file for blame by running
# the following command.
#
# $ git config blame.ignoreRevsFile .git-blame-ignore-revs
#
# To temporarily not use this file add
# --ignore-revs-file=""
# to your blame command.
#
# The ignoreRevsFile can't be set globally due to blame failing if the file isn't present.
# To not have to set the option in every repository it is needed in,
# save the following script in your path with the name "git-bblame"
# now you can run
# $ git bblame $FILE
# to use the .git-blame-ignore-revs file if it is present.
#
# #!/usr/bin/env bash
# repo_root=$(git rev-parse --show-toplevel)
# if [[ -e $repo_root/.git-blame-ignore-revs ]]; then
# git blame --ignore-revs-file="$repo_root/.git-blame-ignore-revs" $@
# else
# git blame $@
# fi

# Move to global directory
6bfb6b9faa005659944cfbb580f6fb34871b5493
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ src/rm/rm
src/rmdir/rmdir
src/sh/sh
src/sleep/sleep
src/cksum/cksum

bin/

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ BINS += mv
BINS += chmod
BINS += sh
BINS += sleep
BINS += cksum

BINARIES := $(foreach b, $(BINS), src/$b/$b)
BINS-COPY := $(foreach b, $(BINS), bin/$b)
Expand Down
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/global

LDFLAGS := -fwhole-program -flto
LDFLAGS += -Wl,--gc-sections
Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
value = build-single-bin name;
})
(attrNames (pkgs.lib.filterAttrs
(n: v: v == "directory")
(n: v: v == "directory" && n != "global")
(readDir ./src)))));
});
}
5 changes: 5 additions & 0 deletions src/cksum/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
OUT := cksum

SRC := cksum.c

include ../shared.mk
1 change: 1 addition & 0 deletions src/cksum/cksum.1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\" TODO
149 changes: 149 additions & 0 deletions src/cksum/cksum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NAME "cksum (canoutils)"
#define VERSION "0.0.1"
#define AUTHOR "cospplredman"

#include "cgetopt.h"
#include "version_info.h"

static const char usage[] = {"Usage: cksum [Option]... [File]...\n"
" --version version information\n"
" --help display this help and exit\n"
" --raw output raw crc\n"
" --tag default output style\n"};

// flags
static int output_format = 't';

static struct option long_options[] = {
{"version", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{"raw", no_argument, NULL, 'r'},
{"tag", no_argument, NULL, 't'},
};

static const uint32_t crc32_ = 0x04c11db7;

static uint32_t crc32(FILE *file, size_t *octets) {
uint32_t remainder = 0;
uint8_t difference = 0;

size_t length = 0;

/* calculate crc of file contents */
int cur_char;
while ((cur_char = getc(file)) != EOF) {
remainder = (remainder << 8) ^ (difference & 0xff) ^ cur_char;
difference = 0;
for (int i = 31; i > 23; i--) {
if (remainder & (1 << i)) {
remainder ^= (1 << i) | (crc32_ >> (32 - i));
difference ^= crc32_ << (i - 24);
}
}
length++;
}

*octets = length;

/* calculate crc of file contents + length of files in bytes */
while (length) {
cur_char = (length & 0xff);
length >>= 8;

remainder = (remainder << 8) ^ (difference & 0xff) ^ cur_char;
difference = 0;
for (int i = 31; i > 23; i--) {
if (remainder & (1 << i)) {
remainder ^= (1 << i) | (crc32_ >> (32 - i));
difference ^= crc32_ << (i - 24);
}
}
}

/* work through the remaining 3 bytes */
for (int j = 0; j < 3; j++) {
remainder = (remainder << 8) ^ (difference & 0xff);
difference = 0;
for (int i = 31; i > 23; i--) {
if (remainder & (1 << i)) {
remainder ^= (1 << i) | (crc32_ >> (32 - i));
difference ^= crc32_ << (i - 24);
}
}
}

return ~((remainder << 8) | (difference & 0xff));
}

static void crc_print(char *filename, uint32_t crc, size_t octets) {
switch (output_format) {
case 't':
if (filename == NULL)
printf("%u %lu\n", crc, octets);
else
printf("%u %lu %s\n", crc, octets, filename);
break;
case 'r':
printf("%c%c%c%c", (crc >> 24) & 255, (crc >> 16) & 255, (crc >> 8) & 255,
crc & 255);
break;
}
}

int main(int argc, char **argv) {

int opt, option_index;
while ((opt = getopt_long(argc, argv, "", long_options, &option_index)) !=
-1) {
switch (opt) {
case 0:
break;
case 'v':
print_version();
return EXIT_SUCCESS;
break;
case 'h':
printf("%s\n", usage);
return EXIT_SUCCESS;
case 'r':
output_format = 'r';
break;
case 't':
output_format = 't';
break;
}
}

argc -= optind;
argv += optind;

int index = 0;
size_t octets;
uint32_t crc;

if (argc < 1) {
crc = crc32(stdin, &octets);
crc_print(NULL, crc, octets);
return EXIT_SUCCESS;
}

for (; index < argc; index++) {
FILE *file = fopen(argv[index], "r+");

if (file == NULL) {
fprintf(stderr, "cksum: %s: %s\n", argv[index], strerror(errno));
continue;
}

crc = crc32(file, &octets);
crc_print(argv[index], crc, octets);

fclose(file);
}
}
96 changes: 0 additions & 96 deletions src/getopt.c

This file was deleted.

Loading

0 comments on commit 0a5be13

Please sign in to comment.