diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /rushs/tinyprintf | |
add: graphs et rushs
Diffstat (limited to 'rushs/tinyprintf')
192 files changed, 6435 insertions, 0 deletions
diff --git a/rushs/tinyprintf/80cols/80cols.sh b/rushs/tinyprintf/80cols/80cols.sh new file mode 100755 index 0000000..d66cf9b --- /dev/null +++ b/rushs/tinyprintf/80cols/80cols.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +[ $# -ne 1 ] && exit 1 +[ -f $1 ] || exit 1 + +while IFS='' read -r line; do + var=$(printf '%s' "$line" | wc -m) + [ "$var" -ge 80 ] && printf '%s\n' "$line" +done < "$1" + +exit 0 diff --git a/rushs/tinyprintf/80cols_grep/80cols.grep b/rushs/tinyprintf/80cols_grep/80cols.grep new file mode 100644 index 0000000..1fe0c1f --- /dev/null +++ b/rushs/tinyprintf/80cols_grep/80cols.grep @@ -0,0 +1 @@ +.\{80,\} diff --git a/rushs/tinyprintf/a.out b/rushs/tinyprintf/a.out Binary files differnew file mode 100755 index 0000000..33e84ed --- /dev/null +++ b/rushs/tinyprintf/a.out diff --git a/rushs/tinyprintf/add_int_ptr/add_int_ptr.c b/rushs/tinyprintf/add_int_ptr/add_int_ptr.c new file mode 100644 index 0000000..ad48639 --- /dev/null +++ b/rushs/tinyprintf/add_int_ptr/add_int_ptr.c @@ -0,0 +1,7 @@ +int *add_int_ptr(int *a, int *b) +{ + if (!a || !b) + return a; + *a += *b; + return a; +} diff --git a/rushs/tinyprintf/alphabet/alphabet.c b/rushs/tinyprintf/alphabet/alphabet.c new file mode 100644 index 0000000..9496c66 --- /dev/null +++ b/rushs/tinyprintf/alphabet/alphabet.c @@ -0,0 +1,13 @@ +#include <stdio.h> + +int main(void) +{ + for (char i = 'a'; i < 'z'; i++) + { + putchar(i); + putchar(' '); + } + putchar('z'); + putchar('\n'); + return 0; +} diff --git a/rushs/tinyprintf/alphanum/alphanum.sh b/rushs/tinyprintf/alphanum/alphanum.sh new file mode 100755 index 0000000..e4c5aee --- /dev/null +++ b/rushs/tinyprintf/alphanum/alphanum.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +letters() +{ + grepped=$(echo "$1" | grep -E "^[a-zA-Z]+$") + [ "$1" = "$grepped" ] && return 0 || return 1 +} + +empty() +{ + if echo "$1" | grep -qE '^[[:space:]]+$'; then + return 0 + fi + return 1 +} + +digit() +{ + grepped=$(echo "$1" | grep -E "^[0-9]$") + [ "$1" = "$grepped" ] && return 0 || return 1 +} + +number() +{ + grepped=$(echo "$1" | grep -E "^[1-9][0-9]+$") + [ "$1" = "$grepped" ] && return 0 || return 1 +} + +alph() +{ + grepped=$(echo "$1" | grep -E "^[a-zA-Z0-9]+$") + [ "$1" = "$grepped" ] && return 0 || return 1 +} + +while IFS='' read -r str; do + if [ -z "$str" ]; then + echo it is empty + continue + elif letters "$str"; then + echo "it is a word" + elif number "$str"; then + echo "it is a number" + elif digit "$str"; then + echo "it is a digit" + elif empty "$str"; then + echo "it is empty" + elif alph "$str"; then + echo "it is an alphanum" + else + echo "it is too complicated" + exit + fi +done diff --git a/rushs/tinyprintf/array_max_min/array_max_min.c b/rushs/tinyprintf/array_max_min/array_max_min.c new file mode 100644 index 0000000..8b2d3a5 --- /dev/null +++ b/rushs/tinyprintf/array_max_min/array_max_min.c @@ -0,0 +1,23 @@ +#include <stddef.h> +#include <stdio.h> + +void array_max_min(int tab[], size_t len, int *max, int *min) +{ + if (tab && len) + { + *min = tab[0]; + *max = tab[0]; + + for (size_t i = 0; i < len; i++) + { + if (tab[i] > *max) + { + *max = tab[i]; + } + else if (tab[i] < *min) + { + *min = tab[i]; + } + } + } +} diff --git a/rushs/tinyprintf/ascii_carousel/rot_x.c b/rushs/tinyprintf/ascii_carousel/rot_x.c new file mode 100644 index 0000000..667106d --- /dev/null +++ b/rushs/tinyprintf/ascii_carousel/rot_x.c @@ -0,0 +1,26 @@ +#include <stddef.h> + +void rot_x(char *s, int x) +{ + if (s == NULL) + { + return; + } + + if (x < 0) + { + x = 26 + x; + } + + for (size_t i = 0; s[i]; i++) + { + if (s[i] >= 'a' && s[i] <= 'z') + { + s[i] = ((s[i] - 'a') + x) % 26 + 'a'; + } + else if (s[i] >= 'A' && s[i] <= 'Z') + { + s[i] = ((s[i] - 'A') + x) % 26 + 'A'; + } + } +} diff --git a/rushs/tinyprintf/ascii_house/ascii_house.sh b/rushs/tinyprintf/ascii_house/ascii_house.sh new file mode 100755 index 0000000..83d907e --- /dev/null +++ b/rushs/tinyprintf/ascii_house/ascii_house.sh @@ -0,0 +1,9 @@ +#!/bin/sh + + +echo ' /\' +echo ' / \' +echo -n "/____\\ \`" && echo "'\`" +echo -n "| | \`" && echo "'''\`" +echo "| | \`|\`" +echo '|_/\_|___|__' diff --git a/rushs/tinyprintf/assignment_operator/assignment_operator.c b/rushs/tinyprintf/assignment_operator/assignment_operator.c new file mode 100644 index 0000000..cae560f --- /dev/null +++ b/rushs/tinyprintf/assignment_operator/assignment_operator.c @@ -0,0 +1,37 @@ +void plus_equal(int *a, int *b) +{ + if (!a || !b) + { + return; + } + *a = *a + *b; +} + +void minus_equal(int *a, int *b) +{ + if (!a || !b) + { + return; + } + *a = *a - *b; +} + +void mult_equal(int *a, int *b) +{ + if (!a || !b) + { + return; + } + *a = *a * *b; +} + +int div_equal(int *a, int *b) +{ + if (!a || !b || *b == 0) + { + return 0; + } + int res = *a % *b; + *a = *a / *b; + return res; +} diff --git a/rushs/tinyprintf/binary_search_ptr/bsearch.c b/rushs/tinyprintf/binary_search_ptr/bsearch.c new file mode 100644 index 0000000..bdc189c --- /dev/null +++ b/rushs/tinyprintf/binary_search_ptr/bsearch.c @@ -0,0 +1,38 @@ +#include "bsearch.h" + +#include <stddef.h> + +int *binary_search(int *begin, int *end, int elt) +{ + if (begin == end) + { + return begin; + } + if (begin > end) + { + if (elt > *begin) + { + return begin + 1; + } + return begin; + } + + size_t m = (end - begin) / 2; + + if (begin[m] == elt) + { + return begin + m; + } + + if (begin[m] > elt) + { + return binary_search(begin, begin + m, elt); + } + + if (m == 0) + { + m++; + } + + return binary_search(begin + m, end, elt); +} diff --git a/rushs/tinyprintf/binary_search_ptr/bsearch.h b/rushs/tinyprintf/binary_search_ptr/bsearch.h new file mode 100644 index 0000000..e011744 --- /dev/null +++ b/rushs/tinyprintf/binary_search_ptr/bsearch.h @@ -0,0 +1,16 @@ +#ifndef BSEARCH_H_ +#define BSEARCH_H_ + +/* +** Search `elt` in the memory range of [`begin` - `end`[. +** `begin` is a pointer to the first element. +** `end` is a pointer **AFTER** the last element. +** The elements in the range [`begin` - `end`[ are sorted in ascending order. +** If the range is empty, `begin` == `end`. +** `begin` and `end` can't be `NULL`. +** Returns a pointer to the element if found, or a pointer to the memory +** location where the element could be inserted to keep the array sorted. +*/ +int *binary_search(int *begin, int *end, int elt); + +#endif /* !BSEARCH_H_ */ diff --git a/rushs/tinyprintf/binary_tree_dynamic/Makefile b/rushs/tinyprintf/binary_tree_dynamic/Makefile new file mode 100644 index 0000000..7fa9879 --- /dev/null +++ b/rushs/tinyprintf/binary_tree_dynamic/Makefile @@ -0,0 +1,15 @@ +CC = gcc +CFLAGS = -Wall -Werror -Wvla -Wextra -std=c99 -pedantic + +SRC = binary_tree.c binary_tree_print.c +OBJ = $(SRC:.c=.o) + +.PHONY: library clean + +library: $(OBJ) + ar csr libbinary_tree.a $(OBJ) + +$(OBJ): $(SRC) + +clean: + $(RM) libbinary_tree.a $(OBJ) diff --git a/rushs/tinyprintf/binary_tree_dynamic/binary_tree.c b/rushs/tinyprintf/binary_tree_dynamic/binary_tree.c new file mode 100644 index 0000000..6d99e99 --- /dev/null +++ b/rushs/tinyprintf/binary_tree_dynamic/binary_tree.c @@ -0,0 +1,95 @@ +#include "binary_tree.h" + +#include <stddef.h> +#include <stdio.h> + +int size(const struct binary_tree *tree) +{ + if (tree == NULL) + return 0; + + return 1 + size(tree->left) + size(tree->right); +} + +static int max(int a, int b) +{ + if (a > b) + return a; + return b; +} + +int height(const struct binary_tree *tree) +{ + if (tree == NULL) + { + return -1; + } + + return 1 + max(height(tree->left), height(tree->right)); +} + +int is_perfect(const struct binary_tree *tree) +{ + if (tree == NULL) + return 1; + return height(tree->left) == height(tree->right) && is_perfect(tree->right) + && is_perfect(tree->right); +} + +int is_complete(const struct binary_tree *tree) +{ + if (tree == NULL) + { + return 1; + } + + int hg = height(tree->left); + int hd = height(tree->right); + + if (hg - hd != 0 && hg - hd != 1) + { + return 0; + } + + return is_complete(tree->left) && is_complete(tree->right); +} + +int is_degenerate(const struct binary_tree *tree) +{ + if (tree == NULL) + { + return 1; + } + + if (tree->left && tree->right) + { + return 0; + } + return is_degenerate(tree->left) && is_degenerate(tree->right); +} + +int is_full(const struct binary_tree *tree) +{ + if (tree == NULL) + return 1; + if ((tree->left && !tree->right) || (!tree->left && tree->right)) + return 0; + return is_full(tree->right) && is_full(tree->left); +} + +static int is_bzt(const struct binary_tree *tree, int min, int max) +{ + if (tree == NULL) + return 1; + if (tree->data > max || tree->data <= min) + return 0; + return is_bzt(tree->left, min, tree->data) + && is_bzt(tree->right, tree->data, max); +} + +int is_bst(const struct binary_tree *tree) +{ + if (tree == NULL) + return 1; + return is_bzt(tree, -2147483647, 2147483647); +} diff --git a/rushs/tinyprintf/binary_tree_dynamic/binary_tree.h b/rushs/tinyprintf/binary_tree_dynamic/binary_tree.h new file mode 100644 index 0000000..a08e4ef --- /dev/null +++ b/rushs/tinyprintf/binary_tree_dynamic/binary_tree.h @@ -0,0 +1,22 @@ +#ifndef BINARY_TREE_H +#define BINARY_TREE_H + +struct binary_tree +{ + int data; + struct binary_tree *left; + struct binary_tree *right; +}; + +int size(const struct binary_tree *tree); +int height(const struct binary_tree *tree); +void dfs_print_prefix(const struct binary_tree *tree); +void dfs_print_infix(const struct binary_tree *tree); +void dfs_print_postfix(const struct binary_tree *tree); +int is_perfect(const struct binary_tree *tree); +int is_complete(const struct binary_tree *tree); +int is_degenerate(const struct binary_tree *tree); +int is_full(const struct binary_tree *tree); +int is_bst(const struct binary_tree *tree); + +#endif /* !BINARY_TREE_H */ diff --git a/rushs/tinyprintf/binary_tree_dynamic/binary_tree_print.c b/rushs/tinyprintf/binary_tree_dynamic/binary_tree_print.c new file mode 100644 index 0000000..bce0f77 --- /dev/null +++ b/rushs/tinyprintf/binary_tree_dynamic/binary_tree_print.c @@ -0,0 +1,40 @@ +#include <stddef.h> +#include <stdio.h> + +#include "binary_tree.h" + +void dfs_print_prefix(const struct binary_tree *tree) +{ + if (tree == NULL) + { + return; + } + + printf("%d ", tree->data); + dfs_print_prefix(tree->left); + dfs_print_prefix(tree->right); +} + +void dfs_print_infix(const struct binary_tree *tree) +{ + if (tree == NULL) + { + return; + } + + dfs_print_infix(tree->left); + printf("%d ", tree->data); + dfs_print_infix(tree->right); +} + +void dfs_print_postfix(const struct binary_tree *tree) +{ + if (tree == NULL) + { + return; + } + + dfs_print_postfix(tree->left); + dfs_print_postfix(tree->right); + printf("%d ", tree->data); +} diff --git a/rushs/tinyprintf/bit_rotation/rol.c b/rushs/tinyprintf/bit_rotation/rol.c new file mode 100644 index 0000000..151ebb5 --- /dev/null +++ b/rushs/tinyprintf/bit_rotation/rol.c @@ -0,0 +1,5 @@ +unsigned char rol(unsigned char value, unsigned char roll) +{ + roll %= sizeof(unsigned char) * 8; + return (value << roll) | (value >> (8 * sizeof(unsigned char) - roll)); +} diff --git a/rushs/tinyprintf/bubble_sort/bubble_sort.c b/rushs/tinyprintf/bubble_sort/bubble_sort.c new file mode 100644 index 0000000..13d2ba3 --- /dev/null +++ b/rushs/tinyprintf/bubble_sort/bubble_sort.c @@ -0,0 +1,25 @@ +#include "bubble_sort.h" + +void bubble_sort(int array[], size_t size) +{ + if (!array || size == 0) + { + return; + } + + int mod; + do + { + mod = 0; + for (size_t i = 0; i < size - 1; i++) + { + if (array[i] > array[i + 1]) + { + mod = 1; + int tmp = array[i]; + array[i] = array[i + 1]; + array[i + 1] = tmp; + } + } + } while (mod); +} diff --git a/rushs/tinyprintf/bubble_sort/bubble_sort.h b/rushs/tinyprintf/bubble_sort/bubble_sort.h new file mode 100644 index 0000000..a33d531 --- /dev/null +++ b/rushs/tinyprintf/bubble_sort/bubble_sort.h @@ -0,0 +1,8 @@ +#ifndef BUBBLE_SORT_H +#define BUBBLE_SORT_H + +#include <stddef.h> + +void bubble_sort(int array[], size_t size); + +#endif /* !BUBBLE_SORT_H */ diff --git a/rushs/tinyprintf/check_alphabet/check_alphabet.c b/rushs/tinyprintf/check_alphabet/check_alphabet.c new file mode 100644 index 0000000..fc540b4 --- /dev/null +++ b/rushs/tinyprintf/check_alphabet/check_alphabet.c @@ -0,0 +1,25 @@ +#include "check_alphabet.h" + +#include <stddef.h> + +int check_alphabet(const char *str, const char *alphabet) +{ + if (alphabet == NULL || *alphabet == '\0') + { + return 1; + } + + for (int i = 0; alphabet[i]; i++) + { + int j; + for (j = 0; str[j] && str[j] != alphabet[i]; j++) + { + continue; + } + if (str[j] == '\0') + { + return 0; + } + } + return 1; +} diff --git a/rushs/tinyprintf/check_alphabet/check_alphabet.h b/rushs/tinyprintf/check_alphabet/check_alphabet.h new file mode 100644 index 0000000..667a20f --- /dev/null +++ b/rushs/tinyprintf/check_alphabet/check_alphabet.h @@ -0,0 +1,6 @@ +#ifndef CHECK_ALPHABET_H +#define CHECK_ALPHABET_H + +int check_alphabet(const char *str, const char *alphabet); + +#endif /* !CHECK_ALPHABET_H */ diff --git a/rushs/tinyprintf/clang-format/.clang-format b/rushs/tinyprintf/clang-format/.clang-format new file mode 100644 index 0000000..7ed8115 --- /dev/null +++ b/rushs/tinyprintf/clang-format/.clang-format @@ -0,0 +1,79 @@ +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Right +AlignOperands: false +AlignTrailingComments: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: Yes +BinPackArguments: true +BinPackParameters: true +BreakBeforeBraces: Custom +BraceWrapping: + AfterEnum: true + AfterClass: true + AfterControlStatement: true + AfterFunction: true + AfterNamespace: true + AfterStruct: true + AfterUnion: true + AfterExternBlock: true + BeforeCatch: true + BeforeElse: true + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: false +BreakBeforeBinaryOperators: NonAssignment +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeComma +BreakInheritanceList: BeforeComma +BreakStringLiterals: true +ColumnLimit: 80 +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +Cpp11BracedListStyle: false +DerivePointerAlignment: false +FixNamespaceComments: true +ForEachMacros: ['ILIST_FOREACH', 'ILIST_FOREACH_ENTRY'] +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '<.*>' + Priority: 1 + - Regex: '.*' + Priority: 2 +IndentCaseLabels: false +IndentPPDirectives: AfterHash +IndentWidth: 4 +IndentWrappedFunctionNames: false +KeepEmptyLinesAtTheStartOfBlocks: false +Language: Cpp +NamespaceIndentation: All +PointerAlignment: Right +ReflowComments: true +SortIncludes: true +SortUsingDeclarations: false +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +TabWidth: 4 +UseTab: Never diff --git a/rushs/tinyprintf/clang-format/zaza.c b/rushs/tinyprintf/clang-format/zaza.c new file mode 100644 index 0000000..a6eec9a --- /dev/null +++ b/rushs/tinyprintf/clang-format/zaza.c @@ -0,0 +1,78 @@ +#include <stdio.h> +#include <stdlib.h> +#define ZAZA_SIZE 4 + +static char get_zaza_char(size_t id) +{ + const int zaza[ZAZA_SIZE] = { + 10 * 10 + 2 * 10 + 2 * 10 / 10, + 10 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 + + 10 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 + + 10 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 + + 10 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 + + * 1 + + 10 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 + + 10 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 + + 10 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 + + 10 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 + + 10 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 + + 10 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 + - (1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 + + 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 + + 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1), + 122, + (38943 * 43 - 84393 / 34583 + + ) % 10 + + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + + 1 + 1 + 1 + 1 + 1 + }; + switch (id * 2 + 4 - 2 + id * 4 - 2 + -5 * id) + + { + case (0): + return (zaza[0]); + case (1000 - 100 - 4 * 100 + 10 * 100 - 14 * 100 - (10 * 10 - 1)): + return (zaza[1]); + case (1 << 11 >> 3 << 5 >> 2 << 1 << 1 << 1 >> 1 >> 1 >> 1 << 5 >> 4 << 1 + >> 1 >> 2 >> 9): + return zaza[2]; + case (3 ^ 100) - (100 ^ 100) - (99 - 99) - (98 - 98) - (97 - 97) - (96 - 96) + - (95 - 95) - (94 - 94) - (93 - 93) - (92 - 92) - (91 - 91) - (90 - 90) + - (89 - 89) - (88 - 88) - (87 - 87) - (86 - 86) - (85 - 85) - (84 - 84) + - (83 - 83) - (82 - 82) - (81 - 81) - (80 - 80) - (79 - 79) - (78 - 78) + - (77 - 77) - (76 - 76) - (75 - 75) - (74 - 74) - (73 - 73) - (72 - 72) + - (71 - 71) - (70 - 70) - + + (69 - 69) - (68 - 68) - (67 - 67) - (66 - 66) - (65 - 65) - (64 - 64) + - (63 - 63) - (62 - 62) - (61 - 61) - (60 - 60) - (59 - 59) - (58 - 58) + - (57 - 57) - (56 - 56) - (55 - 55) - (54 - 54) - (53 - 53) - (52 - 52) + - (51 - 51) - 100 - (50 - 50) - (49 - 49) - (48 - 48) - (47 - 47) + - (46 - 46) - (45 - 45) - (44 - 44) - (43 - 43) - (42 - 42) - (41 - 41) + - (40 - 40) - (39 - 39) - (38 - 38) - (37 - 37) + + - (36 - 36) - (35 - 35) - (34 - 34) - (33 - 33) - (32 - 32) - (31 - 31) + - (30 - 30) - (29 - 29) - (28 - 28) - (27 - 27) - (26 - 26) - (25 - 25) + - (24 - 24) - (23 - 23) - (22 - 22) - (21 - 21) - (20 - 20) - (19 - 19) + - (18 - 18) - (17 - 17) - (16 - 16) - (15 - 15) - (14 - 14) - (13 - 13) + - (12 - 12) - (11 - 11) - (10 - 10) - (9 - 9) - (8 - 8) - (7 - 7) + - (6 - 6) - (5 - 5) - (4 - 4) - (3 - 3) - (2 - 2) - (1 - 1): + return (zaza[3]); + default: + return (0); + } +} +int main(void) +{ + for (size_t i = 0; i < ZAZA_SIZE; i += (i % 2 == 2 - 2) ? 1 % 2 : 1 % 2) + { + putchar(get_zaza_char(i)); + } + putchar('\n'); + return (0); +} diff --git a/rushs/tinyprintf/create_files/create_files.sh b/rushs/tinyprintf/create_files/create_files.sh new file mode 100755 index 0000000..28dba00 --- /dev/null +++ b/rushs/tinyprintf/create_files/create_files.sh @@ -0,0 +1,34 @@ +#!/bin/sh + +touch ' ' +chmod 644 ' ' + +touch '\' +chmod 644 '\' + +touch -- -- +chmod 644 -- -- + +touch '|' +chmod 644 '|' + +touch '"' +chmod 644 '"' + +touch "'" +chmod 644 "'" + +touch -- --\$i*\'\"\\ +chmod 644 -- --\$i*\'\"\\ + +touch '# Exams are fun!' +chmod 644 '# Exams are fun!' + +touch ";\`kill -9 0\`" +chmod 644 ";\`kill -9 0\`" + +path="1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44/45/46/47/48/49/50" + +mkdir -p "$path" +touch $path/farfaraway +chmod 644 $path/farfaraway diff --git a/rushs/tinyprintf/cut_csv/test.csv b/rushs/tinyprintf/cut_csv/test.csv new file mode 100644 index 0000000..d88282b --- /dev/null +++ b/rushs/tinyprintf/cut_csv/test.csv @@ -0,0 +1,15 @@ +James;Lebron;LABron;Lakers; +Davis;Anthony;Unibrow;Pelicans; +Mitchell;Donovan;Spida;Jazz; +Harden;James;TheBeard;Rockets; +Cousins;DeMarcus;Boogie;Wariors; +Embiid;Joel;TheProcess;76Sixers; +Bryant;Kobe;BlackMamba;Lakers; +Jordan;Michael;AirJordan;Bulls; +Johnson;Earvin;Magic;Lakers; +Howard;Dwight;Superman;Wizards; +Westbrook;Russel;MrTripleDouble;Thunder; +Durant;Kevin;KD;Wariors; +George;Paul;PG-13;Thunder; +Leonard;Kawhi;TheKlaw;Raptors; +Irving;Kyrie;UncleDrew;Celtics; diff --git a/rushs/tinyprintf/cut_csv/with_cut.sh b/rushs/tinyprintf/cut_csv/with_cut.sh new file mode 100755 index 0000000..9618f00 --- /dev/null +++ b/rushs/tinyprintf/cut_csv/with_cut.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +[ $# -ne 2 ] && exit 1 + +if ! [ -f "$1" ]; then + exit 1 +fi + +if ! [ "$2" -eq "$2" ] 2> /dev/null; then + exit 1 +fi + +[ "$2" -lt 0 ] && exit 1 + +if [ "$2" -gt "$(wc -l < "$1")" ]; then + exit 1 +fi + +line=$(head --lines="$2" "$1" 2> /dev/null | tail -n 1 | cut -d ';' -f 2-3 || exit 1) +c1=$(echo "$line" | cut -d ';' -f 1) +c2=$(echo "$line" | cut -d ';' -f 2) + +echo "$c1 is $c2" diff --git a/rushs/tinyprintf/cut_csv/with_sed.sh b/rushs/tinyprintf/cut_csv/with_sed.sh new file mode 100755 index 0000000..fb5e1f8 --- /dev/null +++ b/rushs/tinyprintf/cut_csv/with_sed.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +[ $# -ne 2 ] && exit 1 + +if ! [ -f "$1" ]; then + exit 1 +fi + +if ! [ "$2" -eq "$2" ] 2>/dev/null; then + exit 1 +fi + +[ "$2" -lt 0 ] && exit 1 + +if [ "$2" -gt $(wc -l < "$1") ]; then + exit 1 +fi + +i=1 + +while IFS= read -r line; do + [ "$i" -eq "$2" ] && echo "$line" | sed 's/^.*;\(.*\);\(.*\);.*;$/\1 is \2/' + i=$(($i + 1)) +done < "$1" diff --git a/rushs/tinyprintf/digit/digit.c b/rushs/tinyprintf/digit/digit.c new file mode 100644 index 0000000..5646b13 --- /dev/null +++ b/rushs/tinyprintf/digit/digit.c @@ -0,0 +1,13 @@ +unsigned int digit(int n, int k) +{ + if (n <= 0 || k <= 0) + { + return 0; + } + + for (int i = 0; i < k - 1; i++) + { + n /= 10; + } + return n % 10; +} diff --git a/rushs/tinyprintf/display_square/display_square.c b/rushs/tinyprintf/display_square/display_square.c new file mode 100644 index 0000000..9e834d7 --- /dev/null +++ b/rushs/tinyprintf/display_square/display_square.c @@ -0,0 +1,44 @@ +#include <stdio.h> + +void display_square(int width) +{ + if (width <= 0) + { + return; + } + + if (width % 2 == 0) + { + width++; + } + + if (width == 1) + { + putchar('*'); + putchar('\n'); + return; + } + + for (int i = 0; i < width; i++) + { + putchar('*'); + } + putchar('\n'); + + for (int i = 0; i < ((width - 3) / 2); i++) + { + putchar('*'); + for (int j = 0; j < width - 2; j++) + { + putchar(' '); + } + putchar('*'); + putchar('\n'); + } + + for (int i = 0; i < width; i++) + { + putchar('*'); + } + putchar('\n'); +} diff --git a/rushs/tinyprintf/dlist/Makefile b/rushs/tinyprintf/dlist/Makefile new file mode 100644 index 0000000..1251967 --- /dev/null +++ b/rushs/tinyprintf/dlist/Makefile @@ -0,0 +1,15 @@ +CC = gcc +CFLAGS = -std=c99 -pedantic -Werror -Wall -Wextra -Wvla + +SRC = dlist-1.c dlist-2.c dlist-3.c dlist-4.c +OBJ = $(SRC:.c=.o) + +.PHONY: library clean + +library: $(OBJ) + ar csr libdlist.a $(OBJ) + +clean: $(OBJ) + $(RM) *.a $(OBJ) + +$(OBJ): $(SRC) diff --git a/rushs/tinyprintf/dlist/dlist-1.c b/rushs/tinyprintf/dlist/dlist-1.c new file mode 100644 index 0000000..443ebca --- /dev/null +++ b/rushs/tinyprintf/dlist/dlist-1.c @@ -0,0 +1,78 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "dlist.h" + +struct dlist *dlist_init(void) +{ + struct dlist *res = malloc(sizeof(struct dlist)); + if (res == NULL) + return NULL; + + res->size = 0; + res->head = NULL; + res->tail = NULL; + return res; +} + +int dlist_push_front(struct dlist *list, int element) +{ + if (element < 0) + return 0; + struct dlist_item *new = malloc(sizeof(struct dlist_item)); + if (new == NULL) + return 0; + + new->data = element; + new->next = list->head; + new->prev = NULL; + + if (list->size == 0) + list->tail = new; + else + list->head->prev = new; + + list->head = new; + list->size++; + + return 1; +} + +void dlist_print(const struct dlist *list) +{ + if (list->size == 0) + return; + + for (struct dlist_item *i = list->head; i != list->tail; i = i->next) + { + printf("%d\n", i->data); + } + printf("%d\n", list->tail->data); +} + +int dlist_push_back(struct dlist *list, int element) +{ + if (element < 0) + return 0; + struct dlist_item *new = malloc(sizeof(struct dlist_item)); + if (new == NULL) + return 0; + + new->data = element; + new->prev = list->tail; + new->next = NULL; + + if (list->size == 0) + list->head = new; + else + list->tail->next = new; + list->tail = new; + list->size++; + + return 1; +} + +size_t dlist_size(const struct dlist *list) +{ + return list->size; +} diff --git a/rushs/tinyprintf/dlist/dlist-2.c b/rushs/tinyprintf/dlist/dlist-2.c new file mode 100644 index 0000000..5ccdaa3 --- /dev/null +++ b/rushs/tinyprintf/dlist/dlist-2.c @@ -0,0 +1,113 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "dlist.h" + +int dlist_get(const struct dlist *list, size_t index) +{ + if (index >= list->size) + return -1; + + struct dlist_item *i; + for (i = list->head; index; index--, i = i->next) + { + continue; + } + + return i->data; +} + +int dlist_insert_at(struct dlist *list, int element, size_t index) +{ + if (index > list->size || element < 0) + return 0; + else if (index == list->size) + dlist_push_back(list, element); + else if (index == 0) + dlist_push_front(list, element); + else + { + struct dlist_item *new = malloc(sizeof(struct dlist_item)); + if (new == NULL) + return 0; + new->data = element; + + struct dlist_item *i; + for (i = list->head; index - 1; index--, i = i->next) + continue; + new->next = i->next; + i->next->prev = new; + i->next = new; + new->prev = i; + list->size++; + } + return 1; +} + +int dlist_find(const struct dlist *list, int element) +{ + int index = 0; + struct dlist_item *i; + for (i = list->head; i && i->data != element; index++, i = i->next) + continue; + if (!i) + return -1; + return index; +} + +int dlist_remove_at(struct dlist *list, size_t index) +{ + if (index >= list->size) + return -1; + int res; + struct dlist_item *item; + if (list->size == 1) + { + item = list->head; + res = list->head->data; + list->head = NULL; + list->tail = NULL; + } + else if (index == 0) + { + item = list->head; + res = item->data; + item->next->prev = NULL; + list->head = item->next; + } + else if (index == list->size - 1) + { + item = list->tail; + res = item->data; + item->prev->next = NULL; + list->tail = item->prev; + } + else + { + for (item = list->head; index; index--, item = item->next) + continue; + + res = item->data; + item->prev->next = item->next; + item->next->prev = item->prev; + } + + free(item); + list->size--; + return res; +} + +void dlist_clear(struct dlist *list) +{ + if (list->size == 0) + return; + for (struct dlist_item *i = list->head; i;) + { + struct dlist_item *tmp = i->next; + free(i); + i = tmp; + } + list->size = 0; + list->head = NULL; + list->tail = NULL; +} diff --git a/rushs/tinyprintf/dlist/dlist-3.c b/rushs/tinyprintf/dlist/dlist-3.c new file mode 100644 index 0000000..22b4b52 --- /dev/null +++ b/rushs/tinyprintf/dlist/dlist-3.c @@ -0,0 +1,83 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "dlist.h" + +void dlist_map_square(struct dlist *list) +{ + for (struct dlist_item *i = list->head; i; i = i->next) + { + i->data *= i->data; + } +} + +void dlist_reverse(struct dlist *list) +{ + struct dlist_item *tmp; + for (struct dlist_item *i = list->head; i; i = i->prev) + { + tmp = i->next; + i->next = i->prev; + i->prev = tmp; + } + tmp = list->tail; + list->tail = list->head; + list->head = tmp; +} + +struct dlist *dlist_split_at(struct dlist *list, size_t index) +{ + if (list->size == 0) + return dlist_init(); + if (index >= list->size) + { + return NULL; + } + + struct dlist *new = dlist_init(); + if (new == NULL) + return NULL; + new->size = list->size - index; + list->size = index; + + struct dlist_item *i; + for (i = list->head; index; index--, i = i->next) + { + continue; + } + + new->head = i; + new->tail = list->tail; + list->tail = i->prev; + if (i->prev) + { + i->prev->next = NULL; + } + else + { + list->head = NULL; + } + i->prev = NULL; + return new; +} + +void dlist_concat(struct dlist *list1, struct dlist *list2) +{ + if (list2->head == NULL) + return; + if (list1->tail == NULL) + { + list1->head = list2->head; + list1->tail = list2->tail; + list1->size = list2->size; + return; + } + + list1->tail->next = list2->head; + list2->head->prev = list1->tail; + list1->tail = list2->tail; + list1->size += list2->size; + list2->size = 0; + list2->head = NULL; + list2->tail = NULL; +} diff --git a/rushs/tinyprintf/dlist/dlist-4.c b/rushs/tinyprintf/dlist/dlist-4.c new file mode 100644 index 0000000..9ed7aaa --- /dev/null +++ b/rushs/tinyprintf/dlist/dlist-4.c @@ -0,0 +1,97 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "dlist.h" + +size_t max(size_t a, size_t b) +{ + if (a >= b) + { + return a; + } + return b; +} + +size_t min(size_t a, size_t b) +{ + if (a <= b) + { + return a; + } + return b; +} + +size_t min_3(size_t a, size_t b, size_t c) +{ + if (a <= b) + { + if (a <= c) + { + return a; + } + return c; + } + else + { + if (b <= c) + { + return b; + } + return c; + } +} + +size_t my_strlen(const int *s) +{ + size_t i; + for (i = 0; s[i] != -1; i++) + { + continue; + } + return i; +} + +size_t levenshtein(const int *s1, const int *s2) +{ + size_t l1 = my_strlen(s1); + size_t l2 = my_strlen(s2); + if (min(l1, l2) == 0) + { + return max(l1, l2); + } + + if (s1[0] == s2[0]) + { + return levenshtein(s1 + 1, s2 + 1); + } + + size_t lev1 = levenshtein(s1 + 1, s2); + size_t lev2 = levenshtein(s1, s2 + 1); + size_t lev3 = levenshtein(s1 + 1, s2 + 1); + + return 1 + min_3(lev1, lev2, lev3); +} + +int *to_str(const struct dlist *l) +{ + int *res = malloc((l->size + 1) * sizeof(int)); + res[l->size] = -1; + size_t j = 0; + for (struct dlist_item *i = l->head; i; i = i->next, j++) + { + res[j] = i->data; + } + return res; +} + +unsigned int dlist_levenshtein(const struct dlist *list1, + const struct dlist *list2) +{ + int *l1 = to_str(list1); + int *l2 = to_str(list2); + + unsigned int res = levenshtein(l1, l2); + free(l1); + free(l2); + return res; +} diff --git a/rushs/tinyprintf/dlist/dlist.h b/rushs/tinyprintf/dlist/dlist.h new file mode 100644 index 0000000..97cde1a --- /dev/null +++ b/rushs/tinyprintf/dlist/dlist.h @@ -0,0 +1,44 @@ +#ifndef DLIST_H +#define DLIST_H + +#include <stddef.h> + +struct dlist_item +{ + int data; + struct dlist_item *next; + struct dlist_item *prev; +}; + +struct dlist +{ + size_t size; + struct dlist_item *head; + struct dlist_item *tail; +}; + +// Threshold 1 +struct dlist *dlist_init(void); +int dlist_push_front(struct dlist *list, int element); +void dlist_print(const struct dlist *list); +int dlist_push_back(struct dlist *list, int element); +size_t dlist_size(const struct dlist *list); + +// Threshold 2 +int dlist_get(const struct dlist *list, size_t index); +int dlist_insert_at(struct dlist *list, int element, size_t index); +int dlist_find(const struct dlist *list, int element); +int dlist_remove_at(struct dlist *list, size_t index); +void dlist_clear(struct dlist *list); + +// Threshold 3 +void dlist_map_square(struct dlist *list); +void dlist_reverse(struct dlist *list); +struct dlist *dlist_split_at(struct dlist *list, size_t index); +void dlist_concat(struct dlist *list1, struct dlist *list2); + +// Threshold 4 +unsigned int dlist_levenshtein(const struct dlist *list1, + const struct dlist *list2); + +#endif /* !DLIST_H */ diff --git a/rushs/tinyprintf/element_count/element_count.c b/rushs/tinyprintf/element_count/element_count.c new file mode 100644 index 0000000..cec47ae --- /dev/null +++ b/rushs/tinyprintf/element_count/element_count.c @@ -0,0 +1,10 @@ +#include "element_count.h" + +size_t element_count(int *begin, int *end) +{ + if (!begin || !end || begin >= end) + { + return 0; + } + return end - begin; +} diff --git a/rushs/tinyprintf/element_count/element_count.h b/rushs/tinyprintf/element_count/element_count.h new file mode 100644 index 0000000..57412ed --- /dev/null +++ b/rushs/tinyprintf/element_count/element_count.h @@ -0,0 +1,8 @@ +#ifndef ELEMENT_COUNT_H +#define ELEMENT_COUNT_H + +#include <stddef.h> + +size_t element_count(int *begin, int *end); + +#endif /* !ELEMENT_COUNT_H */ diff --git a/rushs/tinyprintf/evalexpr/Makefile b/rushs/tinyprintf/evalexpr/Makefile new file mode 100644 index 0000000..280f19c --- /dev/null +++ b/rushs/tinyprintf/evalexpr/Makefile @@ -0,0 +1,23 @@ +CC = gcc +CFLAGS = -Wall -Werror -Wextra -pedantic -std=c99 -Wvla +AFLAGS = -fsanitize=address + +SRC=src/stack.c src/evalrpn.c src/shunting_yard.c src/fifo_access.c src/fifo_setup_destroy.c src/evalexpr.c +SRC_TEST=tests/unit_tests.c +#OBJ=src/stack.o src/evalrpn.o +OBJ=$(SRC:.c=.o) +#OBJ_TEST=$(SRC_TEST:.c=.o) + +all: $(OBJ) + $(CC) -o evalexpr $(OBJ) + +$(OBJ): $(SRC) + +check: #$(OBJ) $(OBJ_TEST) +# $(CC) $(CFLAGS) -o evaltest $(OBJ) $(OBJ_TEST) -lcriterion + tests/tests.sh + +.PHONY: clean + +clean: + rm $(OBJ) evalexpr diff --git a/rushs/tinyprintf/evalexpr/src/evalexpr.c b/rushs/tinyprintf/evalexpr/src/evalexpr.c new file mode 100644 index 0000000..5012355 --- /dev/null +++ b/rushs/tinyprintf/evalexpr/src/evalexpr.c @@ -0,0 +1,93 @@ +#include "evalexpr.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define BUFFER_SIZE 10 + +char *get_input(void) +{ + size_t r; + char buf[BUFFER_SIZE]; + size_t size = 0; + char *expr = malloc(size * sizeof(char) + 1); + if (expr == NULL) + return NULL; + expr[0] = '\0'; + do + { + r = fread(buf, sizeof(char), BUFFER_SIZE - 1, stdin); + if (r != 0) + { + buf[r] = '\0'; + size += r; + char *tmp = realloc(expr, size * sizeof(char) + 1); + if (tmp == NULL) + { + free(expr); + return NULL; + } + expr = tmp; + strcat(expr, buf); + } + } while (r != 0); + if (size == 0) + { + free(expr); + return NULL; + } + if (expr[size - 1] == '\n') + expr[size - 1] = '\0'; + return expr; +} + +void cleanup(char *expr, char *rpn) +{ + free(expr); + free(rpn); +} + +int main(int argc, char **argv) +{ + if ((argc == 2 && strcmp(argv[1], "-rpn") != 0) || (argc > 2)) + return 4; + char *expr = get_input(); + if (expr == NULL) + return 4; + if (argc == 1) + { + char *rpn; + int e = shunting_yard(expr, &rpn); + if (e != 0) + return e; + // call shunting yard + int retval; + e = evalrpn(rpn, &retval); + if (e != 0) + { + cleanup(expr, rpn); + return e; + } + // FREE RPN + printf("%d\n", retval); + cleanup(expr, rpn); + // return + return 0; + } + + if (argc == 2 && strcmp(argv[1], "-rpn") == 0) + { + // call rpn eval + int retval; + int e = evalrpn(expr, &retval); + printf("%d\n", retval); + // return + free(expr); + if (e != 0) + return e; + return 0; + } + + return 4; +} diff --git a/rushs/tinyprintf/evalexpr/src/evalexpr.h b/rushs/tinyprintf/evalexpr/src/evalexpr.h new file mode 100644 index 0000000..d440ae1 --- /dev/null +++ b/rushs/tinyprintf/evalexpr/src/evalexpr.h @@ -0,0 +1,43 @@ +#ifndef EVALEXPR_H +#define EVALEXPR_H + +#include <stddef.h> + +#include "fifo.h" +#include "stack.h" +#include "stack_struct.h" + +enum token_type +{ + NUMBER = 0, + SUB = 1, + ADD = 1, + MOD = 2, + DIV = 2, + MUL = 2, + EXP = 3, + UN_PLUS = 4, + UN_MINUS = 4, + PAR_LEFT = 5, + PAR_RIGHT = 5, + WRONG_TOKEN = -1, +}; + +struct token +{ + enum token_type type; + int value; +}; + +// evalrpn +int parse_number(const char *expr, size_t *offset); +int get_operands(struct stack **s, int *op1, int *op2); +int my_pow(int a, int b); +int evalrpn(const char *expr, int *retval); + +// shunting yard +int opcmp(struct token *op1, struct token *op2); +enum token_type get_type(char op); +int shunting_yard(const char *expr, char **rpn); + +#endif /* ! EVALEXPR_H */ diff --git a/rushs/tinyprintf/evalexpr/src/evalrpn.c b/rushs/tinyprintf/evalexpr/src/evalrpn.c new file mode 100644 index 0000000..db493eb --- /dev/null +++ b/rushs/tinyprintf/evalexpr/src/evalrpn.c @@ -0,0 +1,145 @@ +#include <ctype.h> + +#include "evalexpr.h" + +int parse_number(const char *expr, size_t *offset) +{ + for (*offset = 0; + expr[*offset] && expr[*offset] >= '0' && expr[*offset] <= '9'; + (*offset)++) + { + continue; + } + (*offset)--; + + int res = 0; + int pow = 1; + for (size_t n = 0; n <= *offset; n++, pow *= 10) + { + res += (expr[*offset - n] - '0') * pow; + } + + return res; +} + +int get_operands(struct stack **s, int *op1, int *op2) +{ + if (*s == NULL) + { + return 2; + } + *op1 = stack_peek(*s); + *s = stack_pop(*s); + if (*s == NULL) + { + return 2; + } + *op2 = stack_peek(*s); + *s = stack_pop(*s); + return 0; +} + +// Computes a to the bth +int my_pow(int a, int b) +{ + if (b == 0) + { + return 1; + } + if (a == 0) + { + return 0; + } + + int res = 1; + int c = b / 2; + while (c != 0) + { + res *= a * a; + c /= 2; + } + if (b % 2 != 0) + res *= a; + return res; +} + +int dispatch(const char c, struct stack **s, int op1, int op2) +{ + switch (c) + { + case '*': + *s = stack_push(*s, op1 * op2); + break; + case '+': + *s = stack_push(*s, op1 + op2); + break; + case '-': + *s = stack_push(*s, op2 - op1); + break; + case '/': + if (op1 == 0) + { + return 3; + } + *s = stack_push(*s, op2 / op1); + break; + case '%': + if (op1 == 0) + { + return 3; + } + *s = stack_push(*s, op2 % op1); + break; + case '^': + if (op1 < 0) + { + return 3; + } + *s = stack_push(*s, my_pow(op2, op1)); + break; + default: + return 1; + } + return 0; +} + +int evalrpn(const char *expr, int *retval) +{ + struct stack *s = NULL; + + for (size_t i = 0; expr[i]; i++) + { + if (expr[i] >= '0' && expr[i] <= '9') + { + size_t offset; + int val = parse_number(expr + i, &offset); + s = stack_push(s, val); + i += offset; + } + else if (isspace(expr[i])) + continue; + else + { + int op1; + int op2; + if (get_operands(&s, &op1, &op2) == 2) + { + return 2; + } + + int d = dispatch(expr[i], &s, op1, op2); + if (d != 0) + { + return d; + } + } + } + + if (s == NULL) + { + return 0; + } + *retval = stack_peek(s); + s = stack_pop(s); + return (!s) ? 0 : 2; +} diff --git a/rushs/tinyprintf/evalexpr/src/fifo.h b/rushs/tinyprintf/evalexpr/src/fifo.h new file mode 100644 index 0000000..b330eac --- /dev/null +++ b/rushs/tinyprintf/evalexpr/src/fifo.h @@ -0,0 +1,29 @@ +#ifndef FIFO_H +#define FIFO_H + +#include <stddef.h> + +#include "evalexpr.h" + +struct list +{ + struct token *data; + struct list *next; +}; + +struct fifo +{ + struct list *head; + struct list *tail; + size_t size; +}; + +struct fifo *fifo_init(void); +size_t fifo_size(struct fifo *fifo); +void fifo_push(struct fifo *fifo, struct token *elt); +struct token *fifo_head(struct fifo *fifo); +void fifo_pop(struct fifo *fifo); +void fifo_clear(struct fifo *fifo); +void fifo_destroy(struct fifo *fifo); + +#endif /* !FIFO_H */ diff --git a/rushs/tinyprintf/evalexpr/src/fifo_access.c b/rushs/tinyprintf/evalexpr/src/fifo_access.c new file mode 100644 index 0000000..1986a09 --- /dev/null +++ b/rushs/tinyprintf/evalexpr/src/fifo_access.c @@ -0,0 +1,61 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "fifo.h" + +size_t fifo_size(struct fifo *fifo) +{ + return fifo->size; +} + +void fifo_push(struct fifo *fifo, struct token *elt) +{ + struct list *new = malloc(sizeof(struct list)); + if (new == NULL) + { + return; + } + new->data = elt; + + if (fifo_size(fifo) == 0) + { + fifo->head = new; + } + new->next = NULL; + if (fifo_size(fifo) != 0) + { + fifo->tail->next = new; + } + fifo->tail = new; + + fifo->size++; +} + +struct token *fifo_head(struct fifo *fifo) +{ + return fifo->head->data; +} + +void fifo_pop(struct fifo *fifo) +{ + if (fifo_size(fifo) == 0) + { + return; + } + if (fifo_size(fifo) == 1) + { + free(fifo->head->data); + free(fifo->head); + fifo->head = NULL; + fifo->tail = NULL; + fifo->size = 0; + return; + } + + struct list *tmp = fifo->head->next; + free(fifo->head->data); + free(fifo->head); + fifo->head = tmp; + + fifo->size--; +} diff --git a/rushs/tinyprintf/evalexpr/src/fifo_setup_destroy.c b/rushs/tinyprintf/evalexpr/src/fifo_setup_destroy.c new file mode 100644 index 0000000..0f99ad0 --- /dev/null +++ b/rushs/tinyprintf/evalexpr/src/fifo_setup_destroy.c @@ -0,0 +1,44 @@ +#include <stdlib.h> + +#include "fifo.h" + +struct fifo *fifo_init(void) +{ + struct fifo *f = malloc(sizeof(struct fifo)); + if (f == NULL) + { + return NULL; + } + + f->size = 0; + f->head = NULL; + f->tail = NULL; + + return f; +} + +void fifo_clear(struct fifo *fifo) +{ + for (struct list *l = fifo->head; l != fifo->tail;) + { + struct list *tmp = l->next; + free(l->data); + free(l); + l = tmp; + } + if (fifo->tail) + { + free(fifo->tail->data); + free(fifo->tail); + } + + fifo->head = NULL; + fifo->tail = NULL; + fifo->size = 0; +} + +void fifo_destroy(struct fifo *fifo) +{ + fifo_clear(fifo); + free(fifo); +} diff --git a/rushs/tinyprintf/evalexpr/src/shunting_yard.c b/rushs/tinyprintf/evalexpr/src/shunting_yard.c new file mode 100644 index 0000000..2db5fc8 --- /dev/null +++ b/rushs/tinyprintf/evalexpr/src/shunting_yard.c @@ -0,0 +1,199 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "evalexpr.h" + +enum assoc +{ + NONE, + RIGHT, + LEFT, +}; + +int opcmp(struct token *op1, struct token *op2) +{ + return op1->type - op2->type; +} + +enum assoc get_assoc(struct token *op) +{ + if (op->type == EXP) + { + return RIGHT; + } + return LEFT; +} + +enum token_type get_type(char op) +{ + switch (op) + { + case '*': + return MUL; + case '/': + return DIV; + case '-': + return SUB; + case '+': + return ADD; + case '%': + return MOD; + case '^': + return EXP; + case '(': + return PAR_LEFT; + case ')': + return PAR_RIGHT; + default: + return WRONG_TOKEN; + } +} + +char *fifo_to_expr(struct fifo *output, size_t size_offset) +{ + char *res = malloc((2 * fifo_size(output) + size_offset) * sizeof(char)); + if (!res) + { + fifo_destroy(output); + return NULL; + } + res[2 * fifo_size(output) + size_offset - 1] = '\0'; + size_t i = 0; + while (fifo_size(output) > 0) + { + if (fifo_head(output)->type == NUMBER) + { + i += sprintf(res + i, "%d", fifo_head(output)->value) - 1; + } + else + { + res[i] = fifo_head(output)->value; + } + fifo_pop(output); + i++; + if (fifo_size(output) != 0) + { + res[i] = ' '; + i++; + } + } + fifo_destroy(output); + return res; +} + +int handle_rpar(struct tstack **opstack, struct fifo *output, struct token **t) +{ + while (*opstack) + { + struct token *o2 = tstack_peek(*opstack); + if (o2->value == '(') + { + free(o2); + break; + } + *opstack = tstack_pop(*opstack); + fifo_push(output, o2); + } + if (*opstack == NULL) + { + free(*t); + fifo_destroy(output); + return 2; + } + free(*t); + *opstack = tstack_pop(*opstack); + return 0; +} + +int empty_opstack(struct tstack **opstack, struct fifo *output) +{ + while (*opstack) + { + struct token *t = tstack_peek(*opstack); + if (t->value == '(' || t->value == ')') + { + fifo_destroy(output); + return 1; + } + *opstack = tstack_pop(*opstack); + fifo_push(output, t); + } + return 0; +} + +void pop_ops(struct tstack **opstack, struct fifo *output, struct token *t) +{ + while (*opstack) + { + struct token *o2 = tstack_peek(*opstack); + if (!(o2->value != '(' + && (opcmp(t, o2) < 0 + || (opcmp(t, o2) == 0 && get_assoc(t) == LEFT)))) + { + break; + } + + *opstack = tstack_pop(*opstack); + fifo_push(output, o2); + } +} + +void handle_numbers(struct fifo *output, size_t *i, int *size_offset, + const char *expr) +{ + size_t offset; + int val = parse_number(expr + *i, &offset); + struct token *t = malloc(sizeof(struct token)); + t->type = NUMBER; + t->value = val; + fifo_push(output, t); + *i += offset; + *size_offset += offset; +} + +int shunting_yard(const char *expr, char **rpn) +{ + struct fifo *output = fifo_init(); + struct tstack *opstack = NULL; + int size_offset = 0; + for (size_t i = 0; expr[i]; i++) + { + if (expr[i] >= '0' && expr[i] <= '9') + { + handle_numbers(output, &i, &size_offset, expr); + } + else if (expr[i] != ' ') + { + struct token *t = malloc(sizeof(struct token)); + t->value = expr[i]; + if ((t->type = get_type(expr[i])) == WRONG_TOKEN) + { + free(t); + return 1; + } + if (t->value == '(') + { + opstack = tstack_push(opstack, t); + continue; + } + else if (t->value == ')') + { + if (handle_rpar(&opstack, output, &t) != 0) + return 2; + continue; + } + pop_ops(&opstack, output, t); + opstack = tstack_push(opstack, t); + } + } + + if (empty_opstack(&opstack, output) != 0) + { + return 1; + } + *rpn = fifo_to_expr(output, size_offset); + if (!*rpn) + return 4; + + return 0; +} diff --git a/rushs/tinyprintf/evalexpr/src/stack.c b/rushs/tinyprintf/evalexpr/src/stack.c new file mode 100644 index 0000000..14f659e --- /dev/null +++ b/rushs/tinyprintf/evalexpr/src/stack.c @@ -0,0 +1,57 @@ +#include "stack.h" + +#include <stdlib.h> + +struct stack *stack_push(struct stack *s, int e) +{ + struct stack *new = malloc(sizeof(struct stack)); + new->data = e; + new->next = NULL; + + new->next = s; + return new; +} + +struct stack *stack_pop(struct stack *s) +{ + if (s == NULL) + { + return NULL; + } + + struct stack *res = s->next; + free(s); + return res; +} + +int stack_peek(struct stack *s) +{ + return s->data; +} + +struct tstack *tstack_push(struct tstack *s, struct token *e) +{ + struct tstack *new = malloc(sizeof(struct tstack)); + new->token = e; + new->next = NULL; + + new->next = s; + return new; +} + +struct tstack *tstack_pop(struct tstack *s) +{ + if (s == NULL) + { + return NULL; + } + + struct tstack *res = s->next; + free(s); + return res; +} + +struct token *tstack_peek(struct tstack *s) +{ + return s->token; +} diff --git a/rushs/tinyprintf/evalexpr/src/stack.h b/rushs/tinyprintf/evalexpr/src/stack.h new file mode 100644 index 0000000..d08e465 --- /dev/null +++ b/rushs/tinyprintf/evalexpr/src/stack.h @@ -0,0 +1,20 @@ +#ifndef STACK_H +#define STACK_H + +#include "evalexpr.h" +#include "stack_struct.h" + +struct tstack +{ + struct token *token; + struct tstack *next; +}; + +struct stack *stack_push(struct stack *s, int e); +struct stack *stack_pop(struct stack *s); +int stack_peek(struct stack *s); + +struct tstack *tstack_push(struct tstack *s, struct token *e); +struct tstack *tstack_pop(struct tstack *s); +struct token *tstack_peek(struct tstack *s); +#endif /* !STACK_H */ diff --git a/rushs/tinyprintf/evalexpr/src/stack_struct.h b/rushs/tinyprintf/evalexpr/src/stack_struct.h new file mode 100644 index 0000000..105cd5d --- /dev/null +++ b/rushs/tinyprintf/evalexpr/src/stack_struct.h @@ -0,0 +1,10 @@ +#ifndef STACK_STRUCT_H +#define STACK_STRUCT_H + +struct stack +{ + int data; + struct stack *next; +}; + +#endif /* ! STACK_STRUCT_H */ diff --git a/rushs/tinyprintf/evalexpr/tests/tests.sh b/rushs/tinyprintf/evalexpr/tests/tests.sh new file mode 100755 index 0000000..920f09b --- /dev/null +++ b/rushs/tinyprintf/evalexpr/tests/tests.sh @@ -0,0 +1,82 @@ +#!/bin/sh + +REF_OUT="ref.out" +TEST_OUT="test.out" + +testrpn() +{ + echo "$2" > "$REF_OUT" + echo "Evaluating '$1' in RPN notation..." + echo "$1" | ./evalexpr -rpn > "$TEST_OUT" + diff "$REF_OUT" "$TEST_OUT" && echo "Success" +} + +testeval() +{ + echo "$1" | bc 2> /dev/null > "$REF_OUT" + echo "Evaluating '$1' in standard notation..." + echo "$1" | ./evalexpr > "$TEST_OUT" + diff "$REF_OUT" "$TEST_OUT" && echo "Success" +} + +testerror() +{ + echo "Testing error code '$2'..." + echo "$1" | ./evalexpr + error="$(echo $?)" + [ "$2" -eq "$error" ] && echo "Succesful failure" || echo "Wrong error $error" +} + +clean() +{ + rm "$REF_OUT" "$TEST_OUT" +} + +# RPN + +echo "Tests for RPN:" +echo "======" + +testrpn "1 1 +" 2 +testrpn "5 2 2 ^ 3 + *" 35 +testrpn "10 6 9 3 + 0 11 - * / * 17 + 5 +" 22 +testrpn "3 4 5 * 3 + -" "-20" +testrpn "3 2 % 9 3 1 2 + * / -" 0 + +echo +echo "=============================================" +echo + +# Standard + +echo "Tests for standard notation:" +echo "======" + +testeval "1 + 1" +testeval " 1 + 1 +1 " +testeval "2 * 2" +testeval "5 * (2 + 4)" +testeval "5 * (2 % 4)" +testeval " 5 *(2 ^4) " +testeval " 5 *(2 ^4 " + +echo +echo "=============================================" +echo + +# Errors + +echo "Error tests:" +echo "======" + +testerror "" 0 +testerror "a+1" 1 +testerror "1%0" 3 + +echo "Testing error code '4'..." +./evalexpr --toto 2> /dev/null +echo $? + +# Cleanup + +clean diff --git a/rushs/tinyprintf/evalexpr/tests/unit_tests.c b/rushs/tinyprintf/evalexpr/tests/unit_tests.c new file mode 100644 index 0000000..ed445a0 --- /dev/null +++ b/rushs/tinyprintf/evalexpr/tests/unit_tests.c @@ -0,0 +1,208 @@ +#include <criterion/criterion.h> +#include <criterion/assert.h> +#include <stddef.h> +#include <string.h> + +#include "../src/evalexpr.h" + +TestSuite(parse_number); + +Test(parse_number, parse_42) +{ + size_t o; + int actual = parse_number("42", &o); + cr_expect(actual == 42, "Attendu : %d, renvoyé : %d", 42, actual); + cr_expect(o == 1, "Décalage attendu : %d, renvoyé : %zu", 1, o); +} + +Test(parse_number, parse_4) +{ + size_t o; + int actual = parse_number("4", &o); + cr_expect(actual == 4, "Attendu : %d, renvoyé : %d", 4, actual); + cr_expect(o == 0, "Décalage attendu : %d, renvoyé : %zu", 0, o); +} + +TestSuite(my_pow); + +Test(my_pow, my_pow00) +{ + int actual = my_pow(0, 0); + cr_expect(actual == 1, "Attendu : %d, renvoyé : %d", 1, actual); +} + +Test(my_pow, my_pown0) +{ + int actual = my_pow(50, 0); + cr_expect(actual == 1, "Attendu : %d, renvoyé : %d", 1, actual); +} +Test(my_pow, my_pow0n) +{ + int actual = my_pow(0, 42); + cr_expect(actual == 0, "Attendu : %d, renvoyé : %d", 0, actual); +} +Test(my_pow, my_powab) +{ + int actual = my_pow(3,3); + cr_expect(actual == 27, "Attendu : %d, renvoyé : %d", 27, actual); +} +Test(my_pow, my_powab_2) +{ + int actual = my_pow(4, 2); + cr_expect(actual == 16, "Attendu : %d, renvoyé : %d", 16, actual); +} +Test(my_pow, my_powab_3) +{ + int actual = my_pow(10, 3); + cr_expect(actual == 1000, "Attendu : %d, renvoyé : %d", 1000, actual); +} +Test(my_pow, my_pow1n) +{ + int actual = my_pow(1, 50); + cr_expect(actual == 1, "Attendu : %d, renvoyé : %d", 1, actual); +} + +TestSuite(RPN); + +Test(RPN, evalrpn_easiest) +{ + const char test[] = "1 1 +"; + int expected = 0; + int retval; + int res = 2; + int actual = evalrpn(test, &retval); + cr_expect(actual == expected, "%s => Retour attendu : %d, renvoyé : %d", test, expected, actual); + cr_expect(retval == res, "%s => Résultat attendu : %d, reçu : %d", test, res, retval); +} +Test(RPN, evalrpn_35) +{ + const char test[] = "5 2 2 ^ 3 + *"; + int expected = 0; + int retval; + int res = 35; + int actual = evalrpn(test, &retval); + cr_expect(actual == expected, "%s => Retour attendu : %d, renvoyé : %d", test, expected, actual); + cr_expect(retval == res, "%s => Résultat attendu : %d, reçu : %d", test, res, retval); +} +Test(RPN, evalrpn_22) +{ + const char test[] = "10 6 9 3 + 0 11 - * / * 17 + 5 +"; + int expected = 0; + int retval; + int res = 22; + int actual = evalrpn(test, &retval); + cr_expect(actual == expected, "%s => Retour attendu : %d, renvoyé : %d", test, expected, actual); + cr_expect(retval == res, "%s => Résultat attendu : %d, reçu : %d", test, res, retval); +} +Test(RPN, evalrpn_minus20) +{ + const char test[] = "3 4 5 * 3 + -"; + int expected = 0; + int retval; + int res = -20; + int actual = evalrpn(test, &retval); + cr_expect(actual == expected, "%s => Retour attendu : %d, renvoyé : %d", test, expected, actual); + cr_expect(retval == res, "%s => Résultat attendu : %d, reçu : %d", test, res, retval); +} +Test(RPN, evalrpn_zero) +{ + const char test[] = "3 2 % 9 3 1 2 + * / -"; + int expected = 0; + int retval; + int res = 0; + int actual = evalrpn(test, &retval); + cr_expect(actual == expected, "%s => Retour attendu : %d, renvoyé : %d", test, expected, actual); + cr_expect(retval == res, "%s => Résultat attendu : %d, reçu : %d", test, res, retval); +} + +TestSuite(Precedence); + +Test(Precedence, parenthesis_above_all) +{ + struct token pleft = { PAR_LEFT, '(' }; + struct token pright = { PAR_RIGHT, ')' }; + struct token unplus = { UN_PLUS, '+' }; + struct token exp = { EXP, '^' }; + struct token mul = { MUL, '*' }; + struct token minus = { SUB, '-' }; + int eq = opcmp(&pleft, &pright); + int sup = opcmp(&pleft, &unplus); + int inf = opcmp(&unplus, &pright); + int parftw = opcmp(&pleft, &exp); + int par4ever = opcmp(&pright, &mul); + int paragain = opcmp(&pright, &minus); + cr_expect(eq == 0, "Wrong order (equal)"); + cr_expect(sup > 0, "Wrong order (>)"); + cr_expect(inf < 0, "Wrong order (<)"); + cr_expect(parftw > 0, "Wrong order (>)"); + cr_expect(par4ever > 0, "Wrong order (>)"); + cr_expect(paragain > 0, "Wrong order (>)"); +} + +Test(Precedence, other_precedence_tests) +{ + struct token exp = { EXP, '^' }; + struct token mul = { MUL, '*' }; + struct token unplus = { UN_PLUS, '+' }; + struct token minus = { SUB, '-' }; + struct token plus = { ADD, '+' }; + int eq = opcmp(&minus, &plus); + int sup = opcmp(&exp, &mul); + int inf = opcmp(&plus, &unplus); + + cr_expect(eq == 0, "Wrong order (equal)"); + cr_expect(sup > 0, "Wrong order (>)"); + cr_expect(inf < 0, "Wrong order (<)"); +} + +TestSuite(ShuntingTests); + +Test(ShuntingTests, shunt_simplest) +{ + char *rpn; + const char *expr = "1 + 1"; + int actual = shunting_yard(expr, &rpn); + cr_expect(actual == 0, "Expected shunting_yard return value %d, got %d", 0, actual); + cr_expect(strcmp(rpn, "1 1 +") == 0, "Expected '1 1 +', got %s", rpn); + free(rpn); +} + +Test(ShuntingTests, shunt_nico) +{ + char *rpn; + const char *expr = "1 + 1 + 1"; + int actual = shunting_yard(expr, &rpn); + cr_expect(actual == 0, "Expected shunting_yard return value %d, got %d", 0, actual); + cr_expect(strcmp(rpn, "1 1 + 1 +") == 0, "Expected '1 1 + 1 +', got %s", rpn); + free(rpn); +} + +Test(ShuntingTests, shunt_harderdaddy) +{ + char *rpn; + const char *expr = "5*(2^2+3)"; + int actual = shunting_yard(expr, &rpn); + cr_expect(actual == 0, "Expected shunting_yard return value %d, got %d", 0, actual); + cr_expect(strcmp(rpn, "5 2 2 ^ 3 + *") == 0, "Expected '5 2 2 ^ 3 + *', got %s", rpn); + free(rpn); +} + +Test(ShuntingTests, shunt_numbers) +{ + char *rpn; + const char *expr = "42 + 50"; + int actual = shunting_yard(expr, &rpn); + cr_expect(actual == 0, "Expected shunting_yard return value %d, got %d", 0, actual); + cr_expect(strcmp(rpn, "42 50 +") == 0, "Expected '42 50 +', got %s", rpn); + free(rpn); +} + +Test(ShuntingTests, shunt_mod) +{ + char *rpn; + const char *expr = "42 % 50"; + int actual = shunting_yard(expr, &rpn); + cr_expect(actual == 0, "Expected shunting_yard return value %d, got %d", 0, actual); + cr_expect(strcmp(rpn, "42 50 %") == 0, "Expected '42 50 +', got %s", rpn); + free(rpn); +} diff --git a/rushs/tinyprintf/fact/fact.c b/rushs/tinyprintf/fact/fact.c new file mode 100644 index 0000000..1440c94 --- /dev/null +++ b/rushs/tinyprintf/fact/fact.c @@ -0,0 +1,8 @@ +unsigned long fact(unsigned n) +{ + if (n == 0) + { + return 1; + } + return n * fact(n - 1); +} diff --git a/rushs/tinyprintf/facto/facto.sh b/rushs/tinyprintf/facto/facto.sh new file mode 100755 index 0000000..350973a --- /dev/null +++ b/rushs/tinyprintf/facto/facto.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +[ $# -ne 1 ] && exit 1 +i=$1 +res=1 +while [ "$i" -gt 0 ]; do + res=$(($res * $i)) + i=$(($i - 1)) +done +echo "$res" diff --git a/rushs/tinyprintf/fibo/fibo.c b/rushs/tinyprintf/fibo/fibo.c new file mode 100644 index 0000000..99c0d79 --- /dev/null +++ b/rushs/tinyprintf/fibo/fibo.c @@ -0,0 +1,8 @@ +unsigned long fibonacci(unsigned long n) +{ + if (n == 0 || n == 1) + { + return n; + } + return fibonacci(n - 1) + fibonacci(n - 2); +} diff --git a/rushs/tinyprintf/fibo_iter/fibo_iter.c b/rushs/tinyprintf/fibo_iter/fibo_iter.c new file mode 100644 index 0000000..36ebf46 --- /dev/null +++ b/rushs/tinyprintf/fibo_iter/fibo_iter.c @@ -0,0 +1,21 @@ +#include <stdio.h> + +unsigned long fibo_iter(unsigned long n) +{ + if (n == 0) + { + return 0; + } + unsigned long prev = 1; + unsigned long pprev = 0; + unsigned long i; + + for (i = 1; i < n; i++) + { + unsigned long next = pprev + prev; + pprev = prev; + prev = next; + } + + return prev; +} diff --git a/rushs/tinyprintf/fifo/Makefile b/rushs/tinyprintf/fifo/Makefile new file mode 100644 index 0000000..e5c9374 --- /dev/null +++ b/rushs/tinyprintf/fifo/Makefile @@ -0,0 +1,16 @@ +CC=gcc +CFLAGS=-std=c99 -Wall -Wextra -Wvla -Werror -pedantic + +.PHONY: library clean + +library: fifo_access.o fifo_setup_destroy.o + ar csr libfifo.a $^ + +fifo_access.o: fifo_access.c + $(CC) $(CFLAGS) -c -o fifo_access.o fifo_access.c + +fifo_setup_destroy.o: fifo_setup_destroy.c + $(CC) $(CFLAGS) -c -o fifo_setup_destroy.o fifo_setup_destroy.c + +clean: + rm *.o libfifo.a diff --git a/rushs/tinyprintf/fifo/fifo.h b/rushs/tinyprintf/fifo/fifo.h new file mode 100644 index 0000000..c4b0a6f --- /dev/null +++ b/rushs/tinyprintf/fifo/fifo.h @@ -0,0 +1,28 @@ +#ifndef FIFO_H +#define FIFO_H + +#include <stddef.h> + +struct list +{ + int data; + struct list *next; +}; + +struct fifo +{ + struct list *head; + struct list *tail; + size_t size; +}; + +struct fifo *fifo_init(void); +size_t fifo_size(struct fifo *fifo); +void fifo_push(struct fifo *fifo, int elt); +int fifo_head(struct fifo *fifo); +void fifo_pop(struct fifo *fifo); +void fifo_clear(struct fifo *fifo); +void fifo_destroy(struct fifo *fifo); +void fifo_print(const struct fifo *fifo); + +#endif /* !FIFO_H */ diff --git a/rushs/tinyprintf/fifo/fifo_access.c b/rushs/tinyprintf/fifo/fifo_access.c new file mode 100644 index 0000000..5d31586 --- /dev/null +++ b/rushs/tinyprintf/fifo/fifo_access.c @@ -0,0 +1,66 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "fifo.h" + +size_t fifo_size(struct fifo *fifo) +{ + return fifo->size; +} + +void fifo_push(struct fifo *fifo, int elt) +{ + struct list *new = malloc(sizeof(struct list)); + if (new == NULL) + { + return; + } + new->data = elt; + + if (fifo_size(fifo) == 0) + { + fifo->head = new; + } + new->next = NULL; + if (fifo_size(fifo) != 0) + { + fifo->tail->next = new; + } + fifo->tail = new; + + fifo->size++; +} + +int fifo_head(struct fifo *fifo) +{ + return fifo->head->data; +} + +void fifo_pop(struct fifo *fifo) +{ + if (fifo_size(fifo) == 0) + { + return; + } + if (fifo_size(fifo) == 1) + { + free(fifo->head); + fifo->head = NULL; + fifo->tail = NULL; + return; + } + + struct list *tmp = fifo->head->next; + free(fifo->head); + fifo->head = tmp; + + fifo->size--; +} + +void fifo_print(const struct fifo *fifo) +{ + for (struct list *l = fifo->head; l; l = l->next) + { + printf("%d\n", l->data); + } +} diff --git a/rushs/tinyprintf/fifo/fifo_setup_destroy.c b/rushs/tinyprintf/fifo/fifo_setup_destroy.c new file mode 100644 index 0000000..80820e1 --- /dev/null +++ b/rushs/tinyprintf/fifo/fifo_setup_destroy.c @@ -0,0 +1,39 @@ +#include <stdlib.h> + +#include "fifo.h" + +struct fifo *fifo_init(void) +{ + struct fifo *f = malloc(sizeof(struct fifo)); + if (f == NULL) + { + return NULL; + } + + f->size = 0; + f->head = NULL; + f->tail = NULL; + + return f; +} + +void fifo_clear(struct fifo *fifo) +{ + for (struct list *l = fifo->head; l != fifo->tail;) + { + struct list *tmp = l->next; + free(l); + l = tmp; + } + free(fifo->tail); + + fifo->head = NULL; + fifo->tail = NULL; + fifo->size = 0; +} + +void fifo_destroy(struct fifo *fifo) +{ + fifo_clear(fifo); + free(fifo); +} diff --git a/rushs/tinyprintf/find_ascii/find_ascii.sh b/rushs/tinyprintf/find_ascii/find_ascii.sh new file mode 100755 index 0000000..db31722 --- /dev/null +++ b/rushs/tinyprintf/find_ascii/find_ascii.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +for filename in $(ls "$1"); do + echo $1/$filename | file -f - | grep ASCII +done diff --git a/rushs/tinyprintf/freq_analysis/freq_analysis.c b/rushs/tinyprintf/freq_analysis/freq_analysis.c new file mode 100644 index 0000000..c60d7a6 --- /dev/null +++ b/rushs/tinyprintf/freq_analysis/freq_analysis.c @@ -0,0 +1,28 @@ +#include <stdio.h> + +int find_rank(int h[26], int min) +{ + int res = 0; + for (int i = 0; i < 26; i++) + { + res += h[i] > h[min] || (h[i] == h[min] && i < min); + } + return res; +} + +void freq_analysis(const char text[], const char table[]) +{ + int h[26] = { 0 }; + for (int i = 0; text[i]; i++) + { + h[text[i] - 'A']++; + } + + for (int j = 0; j < 26; j++) + { + if (h[j] != 0) + { + printf("%c %c\n", j + 'A', table[find_rank(h, j)]); + } + } +} diff --git a/rushs/tinyprintf/functional_programming/foldl.c b/rushs/tinyprintf/functional_programming/foldl.c new file mode 100644 index 0000000..ac222a7 --- /dev/null +++ b/rushs/tinyprintf/functional_programming/foldl.c @@ -0,0 +1,11 @@ +#include "functional_programming.h" + +int foldl(int *array, size_t len, int (*func)(int, int)) +{ + int acc = 0; + for (size_t i = 0; i < len; i++) + { + acc = (*func)(acc, array[i]); + } + return acc; +} diff --git a/rushs/tinyprintf/functional_programming/foldr.c b/rushs/tinyprintf/functional_programming/foldr.c new file mode 100644 index 0000000..c232410 --- /dev/null +++ b/rushs/tinyprintf/functional_programming/foldr.c @@ -0,0 +1,10 @@ +#include "functional_programming.h" + +int foldr(int *array, size_t len, int (*func)(int, int)) +{ + if (len == 1) + { + return (*func)(array[0], 0); + } + return (*func)(array[0], foldr(array + 1, len - 1, func)); +} diff --git a/rushs/tinyprintf/functional_programming/functional_programming.h b/rushs/tinyprintf/functional_programming/functional_programming.h new file mode 100644 index 0000000..429b13c --- /dev/null +++ b/rushs/tinyprintf/functional_programming/functional_programming.h @@ -0,0 +1,10 @@ +#ifndef FUNCTIONAL_PROGRAMMING_H +#define FUNCTIONAL_PROGRAMMING_H + +#include <stddef.h> + +void map(int *array, size_t len, void (*func)(int *)); +int foldr(int *array, size_t len, int (*func)(int, int)); +int foldl(int *array, size_t len, int (*func)(int, int)); + +#endif /* !FUNCTIONAL_PROGRAMMING_H */ diff --git a/rushs/tinyprintf/functional_programming/map.c b/rushs/tinyprintf/functional_programming/map.c new file mode 100644 index 0000000..311c39c --- /dev/null +++ b/rushs/tinyprintf/functional_programming/map.c @@ -0,0 +1,9 @@ +#include "functional_programming.h" + +void map(int *array, size_t len, void (*func)(int *)) +{ + for (size_t i = 0; i < len; i++) + { + (*func)(array + i); + } +} diff --git a/rushs/tinyprintf/generate_files/generate_files.sh b/rushs/tinyprintf/generate_files/generate_files.sh new file mode 100755 index 0000000..cf2ba0a --- /dev/null +++ b/rushs/tinyprintf/generate_files/generate_files.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +FILENAME="default" +NUMBER="1" +EXTENSION="txt" + +while [ $# -gt 0 ]; do + case "$1" in + '-f' | '--filename') + shift + FILENAME="$1" + shift + ;; + '-n' | '--number') + shift + NUMBER="$1" + shift + ;; + '-e' | '--extension') + shift + EXTENSION="$1" + shift + ;; + *) + exit 1 + ;; + esac +done + +for i in $(seq 1 $NUMBER); do + touch -- "$FILENAME-$i.$EXTENSION" +done diff --git a/rushs/tinyprintf/generic_void_list/list.c b/rushs/tinyprintf/generic_void_list/list.c new file mode 100644 index 0000000..20ecfa8 --- /dev/null +++ b/rushs/tinyprintf/generic_void_list/list.c @@ -0,0 +1,36 @@ +#include "list.h" + +#include <stdlib.h> +#include <string.h> + +struct list *list_prepend(struct list *list, const void *value, + size_t data_size) +{ + struct list *new = malloc(sizeof(struct list)); + new->next = list; + new->data = malloc(sizeof(void *)); + memcpy(new->data, value, data_size); + return new; +} + +size_t list_length(struct list *list) +{ + size_t res = 0; + while (list) + { + res++; + list = list->next; + } + return res; +} + +void list_destroy(struct list *list) +{ + while (list) + { + struct list *tmp = list->next; + free(list->data); + free(list); + list = tmp; + } +} diff --git a/rushs/tinyprintf/generic_void_list/list.h b/rushs/tinyprintf/generic_void_list/list.h new file mode 100644 index 0000000..a1bc035 --- /dev/null +++ b/rushs/tinyprintf/generic_void_list/list.h @@ -0,0 +1,31 @@ +#ifndef LIST_H +#define LIST_H + +#include <stddef.h> + +struct list +{ + void *data; + struct list *next; +}; + +/* +** Insert a node containing `value` at the beginning of the list. +** Return `NULL` if an error occurred. +*/ +struct list *list_prepend(struct list *list, const void *value, + size_t data_size); + +/* +** Return the length of the list. +** Return `0` if the list is empty. +*/ +size_t list_length(struct list *list); + +/* +** Release the memory used by the list. +** Does nothing if `list` is `NULL`. +*/ +void list_destroy(struct list *list); + +#endif /* !LIST_H */ diff --git a/rushs/tinyprintf/glob_easy/glob_easy.sh b/rushs/tinyprintf/glob_easy/glob_easy.sh new file mode 100755 index 0000000..b6ae028 --- /dev/null +++ b/rushs/tinyprintf/glob_easy/glob_easy.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +echo $1/*.[A-Za-z][A-Za-z] diff --git a/rushs/tinyprintf/glob_remove_shell/glob_remove_shell.sh b/rushs/tinyprintf/glob_remove_shell/glob_remove_shell.sh new file mode 100755 index 0000000..c2e7ff7 --- /dev/null +++ b/rushs/tinyprintf/glob_remove_shell/glob_remove_shell.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +fmt=*.${1:-"txt"} + +for file in $(echo "$fmt"); do + [ -e "$file" ] && rm "$file" || exit 1 +done diff --git a/rushs/tinyprintf/grade/grade.c b/rushs/tinyprintf/grade/grade.c new file mode 100644 index 0000000..caecc82 --- /dev/null +++ b/rushs/tinyprintf/grade/grade.c @@ -0,0 +1,29 @@ +#include <stdio.h> + +void grade(char g) +{ + switch (g) + { + case 'A': + puts("Excellent"); + break; + case 'B': + puts("Good"); + break; + case 'C': + puts("Not so bad"); + break; + case 'D': + puts("Could be worse"); + break; + case 'E': + puts("Maybe next time"); + break; + case 'F': + puts("No comment"); + break; + default: + puts("Call a wild ACU"); + break; + } +} diff --git a/rushs/tinyprintf/greatest_divisor/greatest_divisor.c b/rushs/tinyprintf/greatest_divisor/greatest_divisor.c new file mode 100644 index 0000000..4c8efef --- /dev/null +++ b/rushs/tinyprintf/greatest_divisor/greatest_divisor.c @@ -0,0 +1,15 @@ +unsigned int greatest_divisor(unsigned int n) +{ + if (n == 0 || n == 1) + { + return 1; + } + + int i; + for (i = n / 2; i > 0 && n % i; i--) + { + continue; + } + + return i; +} diff --git a/rushs/tinyprintf/hacker_news/input.html b/rushs/tinyprintf/hacker_news/input.html new file mode 100644 index 0000000..54d338d --- /dev/null +++ b/rushs/tinyprintf/hacker_news/input.html @@ -0,0 +1,147 @@ +<html op="news" class=" lddwcbt idc0_332" lang="en"><head> +<meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta name="referrer" content="origin"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" type="text/css" href="02_files/news.css"> + <link rel="shortcut icon" href="https://news.ycombinator.com/favicon.ico"> + <link rel="alternate" type="application/rss+xml" title="RSS" href="https://news.ycombinator.com/rss"> + <title>Hacker News</title></head><body><center><table id="hnmain" width="85%" cellspacing="0" cellpadding="0" border="0" bgcolor="#f6f6ef"> + <tbody><tr><td bgcolor="#ff6600"><table style="padding:2px" width="100%" cellspacing="0" cellpadding="0" border="0"><tbody><tr><td style="width:18px;padding-right:4px"><a href="https://news.ycombinator.com/"><img src="02_files/y18.gif" style="border:1px white solid;" width="18" height="18"></a></td> + <td style="line-height:12pt; height:10px;"><span class="pagetop"><b class="hnname"><a href="https://news.ycombinator.com/news">Hacker News</a></b> + <a href="https://news.ycombinator.com/newest">new</a> | <a href="https://news.ycombinator.com/front">past</a> | <a href="https://news.ycombinator.com/newcomments">comments</a> | <a href="https://news.ycombinator.com/ask">ask</a> | <a href="https://news.ycombinator.com/show">show</a> | <a href="https://news.ycombinator.com/jobs">jobs</a> | <a href="https://news.ycombinator.com/submit">submit</a> </span></td><td style="text-align:right;padding-right:4px;"><span class="pagetop"> + <a href="https://news.ycombinator.com/login?goto=news%3Fp%3D2">login</a> + </span></td> + </tr></tbody></table></td></tr> +<tr id="pagespace" title="" style="height:10px"></tr><tr><td><table class="itemlist" cellspacing="0" cellpadding="0" border="0"> + <tbody><tr class="athing" id="28943869"> + <td class="title" valign="top" align="right"><span class="rank">31.</span></td> <td class="votelinks" valign="top"><center><a id="up_28943869" href="https://news.ycombinator.com/vote?id=28943869&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://apnews.com/article/technology-business-arts-and-entertainment-be48d7582fdd5604664fff33ed81ca80" class="titlelink">Sinclair Broadcast Group identifies data breach</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=apnews.com"><span class="sitestr">apnews.com</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28943869">8 points</span> by <a href="https://news.ycombinator.com/user?id=xojoc" class="hnuser">xojoc</a> <span class="age" title="2021-10-21T13:12:05"><a href="https://news.ycombinator.com/item?id=28943869">1 hour ago</a></span> <span id="unv_28943869"></span> | <a href="https://news.ycombinator.com/hide?id=28943869&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28943869">1 comment</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28926775"> + <td class="title" valign="top" align="right"><span class="rank">32.</span></td> <td class="votelinks" valign="top"><center><a id="up_28926775" href="https://news.ycombinator.com/vote?id=28926775&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://www.earlevel.com/main/2002/08/31/a-gentle-introduction-to-the-fft/" class="titlelink">A gentle introduction to the FFT (2002)</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=earlevel.com"><span class="sitestr">earlevel.com</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28926775">71 points</span> by <a href="https://news.ycombinator.com/user?id=tigerlily" class="hnuser">tigerlily</a> <span class="age" title="2021-10-20T04:31:20"><a href="https://news.ycombinator.com/item?id=28926775">12 hours ago</a></span> <span id="unv_28926775"></span> | <a href="https://news.ycombinator.com/hide?id=28926775&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28926775">14 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28930157"> + <td class="title" valign="top" align="right"><span class="rank">33.</span></td> <td class="votelinks" valign="top"><center><a id="up_28930157" href="https://news.ycombinator.com/vote?id=28930157&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://thebrowser.com/notes/jon-ingold/" class="titlelink">Jon Ingold on translating archeology into video games</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=thebrowser.com"><span class="sitestr">thebrowser.com</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28930157">36 points</span> by <a href="https://news.ycombinator.com/user?id=nupitalnumber" class="hnuser">nupitalnumber</a> <span class="age" title="2021-10-20T13:04:53"><a href="https://news.ycombinator.com/item?id=28930157">9 hours ago</a></span> <span id="unv_28930157"></span> | <a href="https://news.ycombinator.com/hide?id=28930157&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28930157">4 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28943611"> + <td class="title" valign="top" align="right"><span class="rank">34.</span></td> <td class="votelinks" valign="top"><center><a id="up_28943611" href="https://news.ycombinator.com/vote?id=28943611&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://news.ycombinator.com/item?id=28943611" class="titlelink">Ask HN: How to Sell a Website</a></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28943611">7 points</span> by <a href="https://news.ycombinator.com/user?id=legrisch" class="hnuser">legrisch</a> <span class="age" title="2021-10-21T12:45:35"><a href="https://news.ycombinator.com/item?id=28943611">1 hour ago</a></span> <span id="unv_28943611"></span> | <a href="https://news.ycombinator.com/hide?id=28943611&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28943611">3 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28933391"> + <td class="title" valign="top" align="right"><span class="rank">35.</span></td> <td class="votelinks" valign="top"><center><a id="up_28933391" href="https://news.ycombinator.com/vote?id=28933391&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://iconmap.io/blog" class="titlelink">We analyzed 425k favicons</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=iconmap.io"><span class="sitestr">iconmap.io</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28933391">493 points</span> by <a href="https://news.ycombinator.com/user?id=gurgeous" class="hnuser">gurgeous</a> <span class="age" title="2021-10-20T17:29:03"><a href="https://news.ycombinator.com/item?id=28933391">20 hours ago</a></span> <span id="unv_28933391"></span> | <a href="https://news.ycombinator.com/hide?id=28933391&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28933391">119 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28938551"> + <td class="title" valign="top" align="right"><span class="rank">36.</span></td> <td class="votelinks" valign="top"><center><a id="up_28938551" href="https://news.ycombinator.com/vote?id=28938551&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://www.science.org/content/article/machu-picchu-was-built-over-major-fault-zones-now-researchers-think-they-know-why" class="titlelink">Machu Picchu was built over major fault zones (2019)</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=science.org"><span class="sitestr">science.org</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28938551">98 points</span> by <a href="https://news.ycombinator.com/user?id=Anon84" class="hnuser">Anon84</a> <span class="age" title="2021-10-21T00:07:54"><a href="https://news.ycombinator.com/item?id=28938551">14 hours ago</a></span> <span id="unv_28938551"></span> | <a href="https://news.ycombinator.com/hide?id=28938551&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28938551">41 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28940507"> + <td class="title" valign="top" align="right"><span class="rank">37.</span></td> <td class="votelinks" valign="top"><center><a id="up_28940507" href="https://news.ycombinator.com/vote?id=28940507&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://brave.com/wp-content/uploads/2021/03/goggles.pdf" class="titlelink">Goggles: Democracy dies in darkness, and so does the Web [pdf]</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=brave.com"><span class="sitestr">brave.com</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28940507">114 points</span> by <a href="https://news.ycombinator.com/user?id=InvaderFizz" class="hnuser">InvaderFizz</a> <span class="age" title="2021-10-21T05:03:56"><a href="https://news.ycombinator.com/item?id=28940507">9 hours ago</a></span> <span id="unv_28940507"></span> | <a href="https://news.ycombinator.com/hide?id=28940507&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28940507">119 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28933663"> + <td class="title" valign="top" align="right"><span class="rank">38.</span></td> <td class="votelinks" valign="top"><center><a id="up_28933663" href="https://news.ycombinator.com/vote?id=28933663&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://browser.geekbench.com/v5/cpu/10496766" class="titlelink">Apple M1 Max Geekbench Score</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=geekbench.com"><span class="sitestr">geekbench.com</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28933663">467 points</span> by <a href="https://news.ycombinator.com/user?id=mv9" class="hnuser">mv9</a> <span class="age" title="2021-10-20T17:50:17"><a href="https://news.ycombinator.com/item?id=28933663">20 hours ago</a></span> <span id="unv_28933663"></span> | <a href="https://news.ycombinator.com/hide?id=28933663&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28933663">771 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28933981"> + <td class="title" valign="top" align="right"><span class="rank">39.</span></td> <td class="votelinks" valign="top"><center><a id="up_28933981" href="https://news.ycombinator.com/vote?id=28933981&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://www.commerce.gov/news/press-releases/2021/10/commerce-tightens-export-controls-items-used-surveillance-private" class="titlelink">U.S. tightens export controls on items used in surveillance of private citizens</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=commerce.gov"><span class="sitestr">commerce.gov</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28933981">275 points</span> by <a href="https://news.ycombinator.com/user?id=transpute" class="hnuser">transpute</a> <span class="age" title="2021-10-20T18:14:33"><a href="https://news.ycombinator.com/item?id=28933981">20 hours ago</a></span> <span id="unv_28933981"></span> | <a href="https://news.ycombinator.com/hide?id=28933981&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28933981">161 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28943645"> + <td class="title" valign="top" align="right"><span class="rank">40.</span></td> <td class="votelinks" valign="top"><center><a id="up_28943645" href="https://news.ycombinator.com/vote?id=28943645&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://www.wsj.com/articles/wework-set-to-go-public-via-spac-deal-two-years-after-failed-ipo-11634808600" class="titlelink" rel="nofollow">WeWork is trying to go public – again</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=wsj.com"><span class="sitestr">wsj.com</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28943645">8 points</span> by <a href="https://news.ycombinator.com/user?id=collegeburner" class="hnuser">collegeburner</a> <span class="age" title="2021-10-21T12:49:02"><a href="https://news.ycombinator.com/item?id=28943645">1 hour ago</a></span> <span id="unv_28943645"></span> | <a href="https://news.ycombinator.com/hide?id=28943645&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28943645">2 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28934624"> + <td class="title" valign="top" align="right"><span class="rank">41.</span></td> <td class="votelinks" valign="top"><center><a id="up_28934624" href="https://news.ycombinator.com/vote?id=28934624&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://www.copetti.org/writings/consoles/playstation-3/" class="titlelink">Playstation 3 Architecture</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=copetti.org"><span class="sitestr">copetti.org</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28934624">362 points</span> by <a href="https://news.ycombinator.com/user?id=bangonkeyboard" class="hnuser">bangonkeyboard</a> <span class="age" title="2021-10-20T18:56:53"><a href="https://news.ycombinator.com/item?id=28934624">19 hours ago</a></span> <span id="unv_28934624"></span> | <a href="https://news.ycombinator.com/hide?id=28934624&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28934624">81 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28929639"> + <td class="title" valign="top" align="right"><span class="rank">42.</span></td> <td class="votelinks" valign="top"><center><a id="up_28929639" href="https://news.ycombinator.com/vote?id=28929639&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://owlpal.substack.com/p/about-that-time-i-had-an-outburst" class="titlelink">About that time I had an outburst during the Y Combinator Interview</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=owlpal.substack.com"><span class="sitestr">owlpal.substack.com</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28929639">498 points</span> by <a href="https://news.ycombinator.com/user?id=curiousowl" class="hnuser">curiousowl</a> <span class="age" title="2021-10-20T12:03:25"><a href="https://news.ycombinator.com/item?id=28929639">1 day ago</a></span> <span id="unv_28929639"></span> | <a href="https://news.ycombinator.com/hide?id=28929639&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28929639">242 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28938378"> + <td class="title" valign="top" align="right"><span class="rank">43.</span></td> <td class="votelinks" valign="top"><center><a id="up_28938378" href="https://news.ycombinator.com/vote?id=28938378&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://www.guernicamag.com/one-mans-pest/" class="titlelink" rel="nofollow">One Man's Pest</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=guernicamag.com"><span class="sitestr">guernicamag.com</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28938378">8 points</span> by <a href="https://news.ycombinator.com/user?id=yimby" class="hnuser">yimby</a> <span class="age" title="2021-10-20T23:47:13"><a href="https://news.ycombinator.com/item?id=28938378">6 hours ago</a></span> <span id="unv_28938378"></span> | <a href="https://news.ycombinator.com/hide?id=28938378&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28938378">discuss</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28926582"> + <td class="title" valign="top" align="right"><span class="rank">44.</span></td> <td class="votelinks" valign="top"><center><a id="up_28926582" href="https://news.ycombinator.com/vote?id=28926582&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://brave.com/search-and-web-discovery/" class="titlelink">Brave Search replaces Google as default search engine in the Brave browser</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=brave.com"><span class="sitestr">brave.com</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28926582">723 points</span> by <a href="https://news.ycombinator.com/user?id=skellertor" class="hnuser">skellertor</a> <span class="age" title="2021-10-20T03:56:51"><a href="https://news.ycombinator.com/item?id=28926582">1 day ago</a></span> <span id="unv_28926582"></span> | <a href="https://news.ycombinator.com/hide?id=28926582&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28926582">528 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28943866"> + <td class="title" valign="top" align="right"><span class="rank">45.</span></td> <td class="votelinks" valign="top"><center><a id="up_28943866" href="https://news.ycombinator.com/vote?id=28943866&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://twitter.com//rashiq/status/1319346264992026624" class="titlelink" rel="nofollow">I reverse engineered mcdonald's internal API</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=twitter.com/rashiq"><span class="sitestr">twitter.com/rashiq</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28943866">3 points</span> by <a href="https://news.ycombinator.com/user?id=graderjs" class="hnuser">graderjs</a> <span class="age" title="2021-10-21T13:11:46"><a href="https://news.ycombinator.com/item?id=28943866">1 hour ago</a></span> <span id="unv_28943866"></span> | <a href="https://news.ycombinator.com/hide?id=28943866&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28943866">discuss</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28936324"> + <td class="title" valign="top" align="right"><span class="rank">46.</span></td> <td class="votelinks" valign="top"><center><a id="up_28936324" href="https://news.ycombinator.com/vote?id=28936324&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://www.science.org/doi/10.1126/scisignal.abc4764" class="titlelink">Injury response to DNA damage in live tumor cells promotes antitumor immunity</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=science.org"><span class="sitestr">science.org</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28936324">109 points</span> by <a href="https://news.ycombinator.com/user?id=bcaulfield" class="hnuser">bcaulfield</a> <span class="age" title="2021-10-20T20:48:40"><a href="https://news.ycombinator.com/item?id=28936324">17 hours ago</a></span> <span id="unv_28936324"></span> | <a href="https://news.ycombinator.com/hide?id=28936324&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28936324">5 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28940197"> + <td class="title" valign="top" align="right"><span class="rank">47.</span></td> <td class="votelinks" valign="top"><center><a id="up_28940197" href="https://news.ycombinator.com/vote?id=28940197&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://www.cowin.gov.in/" class="titlelink">India Counting Down to 1B Doses</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=cowin.gov.in"><span class="sitestr">cowin.gov.in</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28940197">105 points</span> by <a href="https://news.ycombinator.com/user?id=neelkadia" class="hnuser">neelkadia</a> <span class="age" title="2021-10-21T04:17:41"><a href="https://news.ycombinator.com/item?id=28940197">10 hours ago</a></span> <span id="unv_28940197"></span> | <a href="https://news.ycombinator.com/hide?id=28940197&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28940197">78 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28943066"> + <td class="title" valign="top" align="right"><span class="rank">48.</span></td> <td class="votelinks" valign="top"><center><a id="up_28943066" href="https://news.ycombinator.com/vote?id=28943066&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://slate.com/technology/2021/10/tether-crypto-danger-ben-mckenzie.html" class="titlelink" rel="nofollow">Time to get worried about Tether, the “stablecoin” at center of cryptocurrency</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=slate.com"><span class="sitestr">slate.com</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28943066">6 points</span> by <a href="https://news.ycombinator.com/user?id=RickJWagner" class="hnuser">RickJWagner</a> <span class="age" title="2021-10-21T11:45:09"><a href="https://news.ycombinator.com/item?id=28943066">2 hours ago</a></span> <span id="unv_28943066"></span> | <a href="https://news.ycombinator.com/hide?id=28943066&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28943066">5 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28940258"> + <td class="title" valign="top" align="right"><span class="rank">49.</span></td> <td class="votelinks" valign="top"><center><a id="up_28940258" href="https://news.ycombinator.com/vote?id=28940258&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://www.alifewithoutlimits.com.au/the-history-of-surveying/" class="titlelink">The History of Surveying</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=alifewithoutlimits.com.au"><span class="sitestr">alifewithoutlimits.com.au</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28940258">20 points</span> by <a href="https://news.ycombinator.com/user?id=barbazoo" class="hnuser">barbazoo</a> <span class="age" title="2021-10-21T04:27:11"><a href="https://news.ycombinator.com/item?id=28940258">9 hours ago</a></span> <span id="unv_28940258"></span> | <a href="https://news.ycombinator.com/hide?id=28940258&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28940258">9 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28921244"> + <td class="title" valign="top" align="right"><span class="rank">50.</span></td> <td class="votelinks" valign="top"><center><a id="up_28921244" href="https://news.ycombinator.com/vote?id=28921244&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://www.npr.org/2021/10/19/1047303559/fda-hearing-aid-prescription-over-the-counter" class="titlelink">The FDA wants you to be able to buy a hearing aid without a prescription</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=npr.org"><span class="sitestr">npr.org</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28921244">722 points</span> by <a href="https://news.ycombinator.com/user?id=cf100clunk" class="hnuser">cf100clunk</a> <span class="age" title="2021-10-19T17:58:42"><a href="https://news.ycombinator.com/item?id=28921244">1 day ago</a></span> <span id="unv_28921244"></span> | <a href="https://news.ycombinator.com/hide?id=28921244&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28921244">441 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28942594"> + <td class="title" valign="top" align="right"><span class="rank">51.</span></td> <td class="votelinks" valign="top"><center><a id="up_28942594" href="https://news.ycombinator.com/vote?id=28942594&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://www.pcgamer.com/windows-11-pcs-can-hobble-gaming-performance/" class="titlelink">Windows 11 will hobble gaming performance by default on some prebuilt PCs</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=pcgamer.com"><span class="sitestr">pcgamer.com</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28942594">13 points</span> by <a href="https://news.ycombinator.com/user?id=DeathArrow" class="hnuser">DeathArrow</a> <span class="age" title="2021-10-21T10:33:39"><a href="https://news.ycombinator.com/item?id=28942594">3 hours ago</a></span> <span id="unv_28942594"></span> | <a href="https://news.ycombinator.com/hide?id=28942594&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28942594">3 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28940705"> + <td class="title" valign="top" align="right"><span class="rank">52.</span></td> <td class="votelinks" valign="top"><center><a id="up_28940705" href="https://news.ycombinator.com/vote?id=28940705&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://www.gimp.org/news/2021/10/20/gimp-2-99-8-released/" class="titlelink">Development version: GIMP 2.99.8 Released</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=gimp.org"><span class="sitestr">gimp.org</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28940705">19 points</span> by <a href="https://news.ycombinator.com/user?id=pauloxnet" class="hnuser">pauloxnet</a> <span class="age" title="2021-10-21T05:33:20"><a href="https://news.ycombinator.com/item?id=28940705">8 hours ago</a></span> <span id="unv_28940705"></span> | <a href="https://news.ycombinator.com/hide?id=28940705&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28940705">2 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28903383"> + <td class="title" valign="top" align="right"><span class="rank">53.</span></td> <td class="votelinks" valign="top"><center><a id="up_28903383" href="https://news.ycombinator.com/vote?id=28903383&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://mikolaj-kaminski.com/jetbrains-rider-docker-compose-unicodedecodeerror-issue-fix/" class="titlelink">I couldn't debug the code because of my name</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=mikolaj-kaminski.com"><span class="sitestr">mikolaj-kaminski.com</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28903383">247 points</span> by <a href="https://news.ycombinator.com/user?id=mikasjp" class="hnuser">mikasjp</a> <span class="age" title="2021-10-18T08:40:39"><a href="https://news.ycombinator.com/item?id=28903383">23 hours ago</a></span> <span id="unv_28903383"></span> | <a href="https://news.ycombinator.com/hide?id=28903383&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28903383">268 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28937484"> + <td class="title" valign="top" align="right"><span class="rank">54.</span></td> <td class="votelinks" valign="top"><center><a id="up_28937484" href="https://news.ycombinator.com/vote?id=28937484&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://calbryant.uk/blog/10-ways-to-get-the-best-out-of-openscad/" class="titlelink">Getting the best out of OpenSCAD</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=calbryant.uk"><span class="sitestr">calbryant.uk</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28937484">109 points</span> by <a href="https://news.ycombinator.com/user?id=naggie" class="hnuser">naggie</a> <span class="age" title="2021-10-20T22:13:20"><a href="https://news.ycombinator.com/item?id=28937484">16 hours ago</a></span> <span id="unv_28937484"></span> | <a href="https://news.ycombinator.com/hide?id=28937484&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28937484">52 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28921083"> + <td class="title" valign="top" align="right"><span class="rank">55.</span></td> <td class="votelinks" valign="top"><center><a id="up_28921083" href="https://news.ycombinator.com/vote?id=28921083&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://spectrum.ieee.org/recycled-batteries-good-as-newly-mined" class="titlelink">Study: Recycled Lithium Batteries as Good as Newly Mined</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=ieee.org"><span class="sitestr">ieee.org</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28921083">623 points</span> by <a href="https://news.ycombinator.com/user?id=mpweiher" class="hnuser">mpweiher</a> <span class="age" title="2021-10-19T17:45:07"><a href="https://news.ycombinator.com/item?id=28921083">1 day ago</a></span> <span id="unv_28921083"></span> | <a href="https://news.ycombinator.com/hide?id=28921083&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28921083">179 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28934715"> + <td class="title" valign="top" align="right"><span class="rank">56.</span></td> <td class="votelinks" valign="top"><center><a id="up_28934715" href="https://news.ycombinator.com/vote?id=28934715&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://americasfuture.org/eliminating-gifted-programs-wont-make-education-fair/" class="titlelink">Eliminating gifted programs won’t make education fair</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=americasfuture.org"><span class="sitestr">americasfuture.org</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28934715">180 points</span> by <a href="https://news.ycombinator.com/user?id=paulpauper" class="hnuser">paulpauper</a> <span class="age" title="2021-10-20T19:03:44"><a href="https://news.ycombinator.com/item?id=28934715">19 hours ago</a></span> <span id="unv_28934715"></span> | <a href="https://news.ycombinator.com/hide?id=28934715&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28934715">400 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28939407"> + <td class="title" valign="top" align="right"><span class="rank">57.</span></td> <td class="votelinks" valign="top"><center><a id="up_28939407" href="https://news.ycombinator.com/vote?id=28939407&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://github.com/ToshioCP/Gtk4-tutorial/blob/main/Readme.md" class="titlelink">Gtk4 Tutorial</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=github.com/toshiocp"><span class="sitestr">github.com/toshiocp</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28939407">53 points</span> by <a href="https://news.ycombinator.com/user?id=marcodiego" class="hnuser">marcodiego</a> <span class="age" title="2021-10-21T01:59:18"><a href="https://news.ycombinator.com/item?id=28939407">12 hours ago</a></span> <span id="unv_28939407"></span> | <a href="https://news.ycombinator.com/hide?id=28939407&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28939407">73 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28940334"> + <td class="title" valign="top" align="right"><span class="rank">58.</span></td> <td class="votelinks" valign="top"><center><a id="up_28940334" href="https://news.ycombinator.com/vote?id=28940334&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://obua.com/publications/cosmo-id/3/" class="titlelink" rel="nofollow">Cosmopolitan Identifiers</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=obua.com"><span class="sitestr">obua.com</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28940334">10 points</span> by <a href="https://news.ycombinator.com/user?id=BeefySwain" class="hnuser">BeefySwain</a> <span class="age" title="2021-10-21T04:39:41"><a href="https://news.ycombinator.com/item?id=28940334">9 hours ago</a></span> <span id="unv_28940334"></span> | <a href="https://news.ycombinator.com/hide?id=28940334&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28940334">2 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28929840"> + <td class="title" valign="top" align="right"><span class="rank">59.</span></td> <td class="votelinks" valign="top"><center><a id="up_28929840" href="https://news.ycombinator.com/vote?id=28929840&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://madned.substack.com/p/a-talk-with-computer-gaming-pioneer" class="titlelink">A talk with Walter Bright about Empire</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=madned.substack.com"><span class="sitestr">madned.substack.com</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28929840">218 points</span> by <a href="https://news.ycombinator.com/user?id=mad_ned" class="hnuser">mad_ned</a> <span class="age" title="2021-10-20T12:29:16"><a href="https://news.ycombinator.com/item?id=28929840">1 day ago</a></span> <span id="unv_28929840"></span> | <a href="https://news.ycombinator.com/hide?id=28929840&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28929840">86 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="athing" id="28934833"> + <td class="title" valign="top" align="right"><span class="rank">60.</span></td> <td class="votelinks" valign="top"><center><a id="up_28934833" href="https://news.ycombinator.com/vote?id=28934833&how=up&goto=news%3Fp%3D2"><div class="votearrow" title="upvote"></div></a></center></td><td class="title"><a href="https://www.youtube.com/watch?v=NjrYk546uBA" class="titlelink">Bioelektryczność – Polish Robotics (1968) [video]</a><span class="sitebit comhead"> (<a href="https://news.ycombinator.com/from?site=youtube.com"><span class="sitestr">youtube.com</span></a>)</span></td></tr><tr><td colspan="2"></td><td class="subtext"> + <span class="score" id="score_28934833">123 points</span> by <a href="https://news.ycombinator.com/user?id=danielEM" class="hnuser">danielEM</a> <span class="age" title="2021-10-20T19:11:59"><a href="https://news.ycombinator.com/item?id=28934833">19 hours ago</a></span> <span id="unv_28934833"></span> | <a href="https://news.ycombinator.com/hide?id=28934833&goto=news%3Fp%3D2">hide</a> | <a href="https://news.ycombinator.com/item?id=28934833">27 comments</a> </td></tr> + <tr class="spacer" style="height:5px"></tr> + <tr class="morespace" style="height:10px"></tr><tr><td colspan="2"></td><td class="title"><a href="https://news.ycombinator.com/news?p=3" class="morelink" rel="next">More</a></td></tr> + </tbody></table> +</td></tr> +<tr><td><img src="02_files/s.gif" width="0" height="10"><table width="100%" cellspacing="0" cellpadding="1"><tbody><tr><td bgcolor="#ff6600"></td></tr></tbody></table><br><center><span class="yclinks"><a href="https://news.ycombinator.com/newsguidelines.html">Guidelines</a> + | <a href="https://news.ycombinator.com/newsfaq.html">FAQ</a> + | <a href="https://news.ycombinator.com/lists">Lists</a> + | <a href="https://github.com/HackerNews/API">API</a> + | <a href="https://news.ycombinator.com/security.html">Security</a> + | <a href="http://www.ycombinator.com/legal/">Legal</a> + | <a href="http://www.ycombinator.com/apply/">Apply to YC</a> + | <a href="mailto:hn@ycombinator.com">Contact</a></span><br><br><form method="get" action="//hn.algolia.com/">Search: + <input type="text" name="q" size="17" autocorrect="off" spellcheck="false" autocapitalize="none" autocomplete="false"></form> + </center></td></tr> + </tbody></table></center><script type="text/javascript" src="02_files/hn.js"></script> +</body></html>
\ No newline at end of file diff --git a/rushs/tinyprintf/hacker_news/news.sed b/rushs/tinyprintf/hacker_news/news.sed new file mode 100644 index 0000000..91b76d6 --- /dev/null +++ b/rushs/tinyprintf/hacker_news/news.sed @@ -0,0 +1 @@ +s/^.*"\(https\?:\/\/.*\)" class="titlelink"\( rel="nofollow"\)\?>\([^<]*\).*$/**\3**\n\1\n/p diff --git a/rushs/tinyprintf/hacker_news/output.txt b/rushs/tinyprintf/hacker_news/output.txt new file mode 100644 index 0000000..68cc714 --- /dev/null +++ b/rushs/tinyprintf/hacker_news/output.txt @@ -0,0 +1,90 @@ +**Sinclair Broadcast Group identifies data breach** +https://apnews.com/article/technology-business-arts-and-entertainment-be48d7582fdd5604664fff33ed81ca80 + +**A gentle introduction to the FFT (2002)** +https://www.earlevel.com/main/2002/08/31/a-gentle-introduction-to-the-fft/ + +**Jon Ingold on translating archeology into video games** +https://thebrowser.com/notes/jon-ingold/ + +**Ask HN: How to Sell a Website** +https://news.ycombinator.com/item?id=28943611 + +**We analyzed 425k favicons** +https://iconmap.io/blog + +**Machu Picchu was built over major fault zones (2019)** +https://www.science.org/content/article/machu-picchu-was-built-over-major-fault-zones-now-researchers-think-they-know-why + +**Goggles: Democracy dies in darkness, and so does the Web [pdf]** +https://brave.com/wp-content/uploads/2021/03/goggles.pdf + +**Apple M1 Max Geekbench Score** +https://browser.geekbench.com/v5/cpu/10496766 + +**U.S. tightens export controls on items used in surveillance of private citizens** +https://www.commerce.gov/news/press-releases/2021/10/commerce-tightens-export-controls-items-used-surveillance-private + +**WeWork is trying to go public – again** +https://www.wsj.com/articles/wework-set-to-go-public-via-spac-deal-two-years-after-failed-ipo-11634808600 + +**Playstation 3 Architecture** +https://www.copetti.org/writings/consoles/playstation-3/ + +**About that time I had an outburst during the Y Combinator Interview** +https://owlpal.substack.com/p/about-that-time-i-had-an-outburst + +**One Man's Pest** +https://www.guernicamag.com/one-mans-pest/ + +**Brave Search replaces Google as default search engine in the Brave browser** +https://brave.com/search-and-web-discovery/ + +**I reverse engineered mcdonald's internal API** +https://twitter.com//rashiq/status/1319346264992026624 + +**Injury response to DNA damage in live tumor cells promotes antitumor immunity** +https://www.science.org/doi/10.1126/scisignal.abc4764 + +**India Counting Down to 1B Doses** +https://www.cowin.gov.in/ + +**Time to get worried about Tether, the “stablecoin” at center of cryptocurrency** +https://slate.com/technology/2021/10/tether-crypto-danger-ben-mckenzie.html + +**The History of Surveying** +https://www.alifewithoutlimits.com.au/the-history-of-surveying/ + +**The FDA wants you to be able to buy a hearing aid without a prescription** +https://www.npr.org/2021/10/19/1047303559/fda-hearing-aid-prescription-over-the-counter + +**Windows 11 will hobble gaming performance by default on some prebuilt PCs** +https://www.pcgamer.com/windows-11-pcs-can-hobble-gaming-performance/ + +**Development version: GIMP 2.99.8 Released** +https://www.gimp.org/news/2021/10/20/gimp-2-99-8-released/ + +**I couldn't debug the code because of my name** +https://mikolaj-kaminski.com/jetbrains-rider-docker-compose-unicodedecodeerror-issue-fix/ + +**Getting the best out of OpenSCAD** +https://calbryant.uk/blog/10-ways-to-get-the-best-out-of-openscad/ + +**Study: Recycled Lithium Batteries as Good as Newly Mined** +https://spectrum.ieee.org/recycled-batteries-good-as-newly-mined + +**Eliminating gifted programs won’t make education fair** +https://americasfuture.org/eliminating-gifted-programs-wont-make-education-fair/ + +**Gtk4 Tutorial** +https://github.com/ToshioCP/Gtk4-tutorial/blob/main/Readme.md + +**Cosmopolitan Identifiers** +https://obua.com/publications/cosmo-id/3/ + +**A talk with Walter Bright about Empire** +https://madned.substack.com/p/a-talk-with-computer-gaming-pioneer + +**Bioelektryczność – Polish Robotics (1968) [video]** +https://www.youtube.com/watch?v=NjrYk546uBA + diff --git a/rushs/tinyprintf/handling_complex/complex.c b/rushs/tinyprintf/handling_complex/complex.c new file mode 100644 index 0000000..79a10be --- /dev/null +++ b/rushs/tinyprintf/handling_complex/complex.c @@ -0,0 +1,51 @@ +#include "complex.h" + +#include <stdio.h> + +void print_complex(struct complex a) +{ + printf("complex(%.2f ", a.real); + + if (a.img < 0) + { + printf("- %.2fi", -a.img); + } + else + { + printf("+ %.2fi", a.img); + } + printf(")\n"); +} + +struct complex neg_complex(struct complex a) +{ + struct complex z = { -a.real, -a.img }; + return z; +} + +struct complex add_complex(struct complex a, struct complex b) +{ + struct complex z = { a.real + b.real, a.img + b.img }; + return z; +} + +struct complex sub_complex(struct complex a, struct complex b) +{ + return add_complex(a, neg_complex(b)); +} + +struct complex mul_complex(struct complex a, struct complex b) +{ + struct complex z = { a.real * b.real - a.img * b.img, + a.real * b.img + a.img * b.real }; + return z; +} + +struct complex div_complex(struct complex a, struct complex b) +{ + struct complex z = { + (a.real * b.real + a.img * b.img) / (b.real * b.real + b.img * b.img), + (a.img * b.real - a.real * b.img) / (b.real * b.real + b.img * b.img) + }; + return z; +} diff --git a/rushs/tinyprintf/handling_complex/complex.h b/rushs/tinyprintf/handling_complex/complex.h new file mode 100644 index 0000000..c562810 --- /dev/null +++ b/rushs/tinyprintf/handling_complex/complex.h @@ -0,0 +1,20 @@ +#ifndef COMPLEX_H +#define COMPLEX_H + +struct complex +{ + float real; + float img; +}; + +// Print +void print_complex(struct complex a); + +// Operations +struct complex neg_complex(struct complex a); +struct complex add_complex(struct complex a, struct complex b); +struct complex sub_complex(struct complex a, struct complex b); +struct complex mul_complex(struct complex a, struct complex b); +struct complex div_complex(struct complex a, struct complex b); + +#endif /* !COMPLEX_H */ diff --git a/rushs/tinyprintf/hanoi/hanoi.c b/rushs/tinyprintf/hanoi/hanoi.c new file mode 100644 index 0000000..6ac2b71 --- /dev/null +++ b/rushs/tinyprintf/hanoi/hanoi.c @@ -0,0 +1,31 @@ +#include <stdio.h> + +void move(unsigned src, unsigned spare, unsigned dst, unsigned n) +{ + if (n == 0) + { + return; + } + + move(src, dst, spare, n - 1); + + printf("%u->%u\n", src, dst); + + move(spare, src, dst, n - 1); +} + +void hanoi(unsigned n) +{ + if (n == 0) + { + return; + } + + move(1, 2, 3, n); +} + +int main(void) +{ + hanoi(3); + return 0; +} diff --git a/rushs/tinyprintf/hash_map/hash.c b/rushs/tinyprintf/hash_map/hash.c new file mode 100644 index 0000000..434616f --- /dev/null +++ b/rushs/tinyprintf/hash_map/hash.c @@ -0,0 +1,13 @@ +#include <stddef.h> + +size_t hash(const char *key) +{ + size_t i = 0; + size_t hash = 0; + + for (i = 0; key[i] != '\0'; ++i) + hash += key[i]; + hash += i; + + return hash; +} diff --git a/rushs/tinyprintf/hash_map/hash_map.c b/rushs/tinyprintf/hash_map/hash_map.c new file mode 100644 index 0000000..4690e8b --- /dev/null +++ b/rushs/tinyprintf/hash_map/hash_map.c @@ -0,0 +1,177 @@ +#include "hash_map.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +struct hash_map *hash_map_init(size_t size) +{ + struct hash_map *new; + if ((new = malloc(sizeof(struct hash_map))) == NULL) + { + return NULL; + } + if ((new->data = malloc(size * sizeof(struct pair_list *))) == NULL) + { + return NULL; + } + new->size = size; + + for (size_t i = 0; i < size; i++) + { + new->data[i] = NULL; + } + + return new; +} + +bool hash_map_insert(struct hash_map *hash_map, const char *key, char *value, + bool *updated) +{ + if (hash_map == NULL || updated == NULL || hash_map->size == 0) + { + return false; + } + *updated = false; + + size_t h = hash(key); + if (h >= hash_map->size) + { + h %= hash_map->size; + } + + struct pair_list *new = malloc(sizeof(struct pair_list)); + if (new == NULL) + { + return false; + } + + for (struct pair_list *p = hash_map->data[h]; p; p = p->next) + { + if (strcmp(p->key, key) == 0) + { + p->value = value; + *updated = true; + free(new); + return true; + } + } + + new->next = hash_map->data[h]; + new->key = key; + new->value = value; + hash_map->data[h] = new; + return true; +} + +void hash_map_free(struct hash_map *hash_map) +{ + if (hash_map == NULL) + { + return; + } + + if (hash_map->data == NULL) + { + free(hash_map); + return; + } + + for (size_t i = 0; i < hash_map->size; i++) + { + while (hash_map->data[i]) + { + struct pair_list *tmp = hash_map->data[i]->next; + free(hash_map->data[i]); + hash_map->data[i] = tmp; + } + } + + free(hash_map->data); + free(hash_map); +} + +void hash_map_dump(struct hash_map *hash_map) +{ + if (hash_map == NULL || hash_map->data == NULL) + { + return; + } + + for (size_t i = 0; i < hash_map->size; i++) + { + if (hash_map->data[i] != NULL) + { + printf("%s: %s", hash_map->data[i]->key, hash_map->data[i]->value); + for (struct pair_list *p = hash_map->data[i]->next; p; p = p->next) + { + printf(", %s: %s", p->key, p->value); + } + printf("\n"); + } + } +} + +const char *hash_map_get(const struct hash_map *hash_map, const char *key) +{ + if (hash_map == NULL || hash_map->data == NULL || hash_map->size == 0) + { + return NULL; + } + + size_t h = hash(key); + if (h >= hash_map->size) + { + h %= hash_map->size; + } + struct pair_list *p; + for (p = hash_map->data[h]; p && strcmp(p->key, key) != 0; p = p->next) + { + continue; + } + + if (p) + { + return p->value; + } + return NULL; +} + +bool hash_map_remove(struct hash_map *hash_map, const char *key) +{ + if (hash_map == NULL || hash_map->data == NULL || hash_map->size == 0) + { + return false; + } + + size_t h = hash(key); + if (h >= hash_map->size) + { + h %= hash_map->size; + } + if (hash_map->data[h] == NULL) + { + return false; + } + + if (strcmp(hash_map->data[h]->key, key) == 0) + { + struct pair_list *tmp = hash_map->data[h]->next; + free(hash_map->data[h]); + hash_map->data[h] = tmp; + return true; + } + + struct pair_list *p; + for (p = hash_map->data[h]; p->next; p = p->next) + { + if (strcmp(p->next->key, key) == 0) + { + struct pair_list *tmp = p->next->next; + free(p->next); + p->next = tmp; + return true; + } + } + return false; +} diff --git a/rushs/tinyprintf/hash_map/hash_map.h b/rushs/tinyprintf/hash_map/hash_map.h new file mode 100644 index 0000000..c731eab --- /dev/null +++ b/rushs/tinyprintf/hash_map/hash_map.h @@ -0,0 +1,29 @@ +#ifndef HASH_MAP_H +#define HASH_MAP_H + +#include <stdbool.h> +#include <stddef.h> + +struct pair_list +{ + const char *key; + char *value; + struct pair_list *next; +}; + +struct hash_map +{ + struct pair_list **data; + size_t size; +}; + +size_t hash(const char *str); +struct hash_map *hash_map_init(size_t size); +bool hash_map_insert(struct hash_map *hash_map, const char *key, char *value, + bool *updated); +void hash_map_free(struct hash_map *hash_map); +void hash_map_dump(struct hash_map *hash_map); +const char *hash_map_get(const struct hash_map *hash_map, const char *key); +bool hash_map_remove(struct hash_map *hash_map, const char *key); + +#endif /* ! HASH_MAP_H */ diff --git a/rushs/tinyprintf/heap/Makefile b/rushs/tinyprintf/heap/Makefile new file mode 100644 index 0000000..2ed972b --- /dev/null +++ b/rushs/tinyprintf/heap/Makefile @@ -0,0 +1,14 @@ +CC = gcc +CFLAGS = -Wall -Werror -Wvla -Wextra -std=c99 -pedantic +SRC = add.c del.c print.c pop.c create.c +OBJ = $(SRC:.c=.o) + +.PHONY: library clean + +library: $(OBJ) + ar csr libheap.a $(OBJ) + +$(OBJ): $(SRC) + +clean: + $(RM) libheap.a $(OBJ) diff --git a/rushs/tinyprintf/heap/add.c b/rushs/tinyprintf/heap/add.c new file mode 100644 index 0000000..78a4db8 --- /dev/null +++ b/rushs/tinyprintf/heap/add.c @@ -0,0 +1,39 @@ +#include <stdlib.h> + +#include "heap.h" + +void heapify(int arr[], int n, int i) +{ + if (i == 0) + return; + int parent = (i - 1) / 2; + if (parent >= 0) + { + if (arr[i] > arr[parent]) + { + int tmp = arr[i]; + arr[i] = arr[parent]; + arr[parent] = tmp; + heapify(arr, n, parent); + } + } +} + +void add(struct heap *heap, int val) +{ + if (heap->size == heap->capacity) + { + heap->array = realloc(heap->array, heap->capacity * 2 * sizeof(int)); + if (heap->array == NULL) + { + free(heap); + return; + } + heap->capacity *= 2; + } + + heap->array[heap->size] = val; + heap->size++; + + heapify(heap->array, heap->size, heap->size - 1); +} diff --git a/rushs/tinyprintf/heap/create.c b/rushs/tinyprintf/heap/create.c new file mode 100644 index 0000000..f0675ad --- /dev/null +++ b/rushs/tinyprintf/heap/create.c @@ -0,0 +1,19 @@ +#include <stdlib.h> + +#include "heap.h" + +struct heap *create_heap(void) +{ + struct heap *res = malloc(sizeof(struct heap)); + if (res == NULL) + return NULL; + res->size = 0; + res->capacity = 8; + res->array = malloc(8 * sizeof(int)); + if (res->array == NULL) + { + free(res); + return NULL; + } + return res; +} diff --git a/rushs/tinyprintf/heap/del.c b/rushs/tinyprintf/heap/del.c new file mode 100644 index 0000000..4d2ae35 --- /dev/null +++ b/rushs/tinyprintf/heap/del.c @@ -0,0 +1,9 @@ +#include <stdlib.h> + +#include "heap.h" + +void delete_heap(struct heap *heap) +{ + free(heap->array); + free(heap); +} diff --git a/rushs/tinyprintf/heap/heap.h b/rushs/tinyprintf/heap/heap.h new file mode 100644 index 0000000..085f436 --- /dev/null +++ b/rushs/tinyprintf/heap/heap.h @@ -0,0 +1,20 @@ +#ifndef HEAP_H +#define HEAP_H + +// size_t +#include <stddef.h> + +struct heap +{ + size_t size; + size_t capacity; + int *array; +}; + +struct heap *create_heap(void); +void add(struct heap *heap, int val); +int pop(struct heap *heap); +void delete_heap(struct heap *heap); +void print_heap(const struct heap *heap); + +#endif /* !HEAP_H */ diff --git a/rushs/tinyprintf/heap/pop.c b/rushs/tinyprintf/heap/pop.c new file mode 100644 index 0000000..55e063f --- /dev/null +++ b/rushs/tinyprintf/heap/pop.c @@ -0,0 +1,49 @@ +#include <assert.h> +#include <stdlib.h> + +#include "heap.h" + +void heapify2(struct heap *h, size_t i) +{ + size_t max = i; + if (2 * i + 1 < h->size && h->array[2 * i + 1] > h->array[max]) + max = 2 * i + 1; + if (2 * i + 2 < h->size && h->array[2 * i + 2] > h->array[max]) + max = 2 * i + 2; + if (max != i) + { + int tmp = h->array[i]; + h->array[i] = h->array[max]; + h->array[max] = tmp; + heapify2(h, max); + } +} + +int pop(struct heap *heap) +{ + assert(heap->size != 0); + if (heap->size == 1) + { + heap->size--; + return heap->array[0]; + } + else + { + int res = heap->array[0]; + heap->array[0] = heap->array[heap->size - 1]; + heap->size--; + heapify2(heap, 0); + if (heap->size < heap->capacity / 2 && heap->capacity > 8) + { + heap->array = + realloc(heap->array, (heap->capacity / 2) * sizeof(int)); + if (heap->array == NULL) + { + free(heap); + return -1; + } + heap->capacity /= 2; + } + return res; + } +} diff --git a/rushs/tinyprintf/heap/print.c b/rushs/tinyprintf/heap/print.c new file mode 100644 index 0000000..f5bbe95 --- /dev/null +++ b/rushs/tinyprintf/heap/print.c @@ -0,0 +1,27 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "heap.h" + +void print_rec(const struct heap *h, size_t i, int root) +{ + if (i >= h->size) + return; + if (!root) + printf(" "); + else + root = 0; + printf("%d", h->array[i]); + if (i == h->size - 1) + { + return; + } + print_rec(h, i * 2 + 1, root); + print_rec(h, i * 2 + 2, root); +} + +void print_heap(const struct heap *heap) +{ + print_rec(heap, 0, 1); + printf("\n"); +} diff --git a/rushs/tinyprintf/hello_friends/hello.c b/rushs/tinyprintf/hello_friends/hello.c new file mode 100644 index 0000000..63df2f1 --- /dev/null +++ b/rushs/tinyprintf/hello_friends/hello.c @@ -0,0 +1,13 @@ +#include <stdio.h> + +int main(int argc, char **argv) +{ + if (argc == 1) + printf("Hello World!\n"); + else + for (int i = 1; i < argc; i++) + { + printf("Hello %s!\n", argv[i]); + } + return 0; +} diff --git a/rushs/tinyprintf/hello_world/hello.c b/rushs/tinyprintf/hello_world/hello.c new file mode 100644 index 0000000..0681c18 --- /dev/null +++ b/rushs/tinyprintf/hello_world/hello.c @@ -0,0 +1,7 @@ +#include <stdio.h> + +int main(void) +{ + puts("Hello World!"); + return 0; +} diff --git a/rushs/tinyprintf/hello_world_shebang/hello.sh b/rushs/tinyprintf/hello_world_shebang/hello.sh new file mode 100755 index 0000000..8dc4f64 --- /dev/null +++ b/rushs/tinyprintf/hello_world_shebang/hello.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +echo "Hello World!" diff --git a/rushs/tinyprintf/hill_array/hill_array.c b/rushs/tinyprintf/hill_array/hill_array.c new file mode 100644 index 0000000..14d3a85 --- /dev/null +++ b/rushs/tinyprintf/hill_array/hill_array.c @@ -0,0 +1,43 @@ +#include "hill_array.h" + +int top_of_the_hill(int tab[], size_t len) +{ + if (len == 0) + { + return -1; + } + + int top = 0; + size_t i = 0; + + while (i + 1 < len && tab[i] <= tab[i + 1]) + { + if (tab[i] < 0 || tab[i + 1] < 0) + { + return -1; + } + if (tab[i] != tab[i + 1]) + { + top = i + 1; + } + i++; + } + + while (i + 1 < len && tab[i] >= tab[i + 1]) + { + if (tab[i] < 0 || tab[i + 1] < 0) + { + return -1; + } + i++; + } + + if (i + 1 == len) + { + return top; + } + else + { + return -1; + } +} diff --git a/rushs/tinyprintf/hill_array/hill_array.h b/rushs/tinyprintf/hill_array/hill_array.h new file mode 100644 index 0000000..3152c19 --- /dev/null +++ b/rushs/tinyprintf/hill_array/hill_array.h @@ -0,0 +1,8 @@ +#ifndef HILL_ARRAY_H +#define HILL_ARRAY_H + +#include <stddef.h> + +int top_of_the_hill(int tab[], size_t len); + +#endif /* !HILL_ARRAY_H */ diff --git a/rushs/tinyprintf/insertion_sort/insertion_sort.c b/rushs/tinyprintf/insertion_sort/insertion_sort.c new file mode 100644 index 0000000..2edd195 --- /dev/null +++ b/rushs/tinyprintf/insertion_sort/insertion_sort.c @@ -0,0 +1,20 @@ +#include "insertion_sort.h" + +#include <stddef.h> + +void insertion_sort(void **array, f_cmp comp) +{ + if (array == NULL || *array == NULL) + { + return; + } + for (int i = 1; array[i]; i++) + { + for (int j = i; j > 0 && comp(array[j - 1], array[j]) > 0; j--) + { + void *tmp = array[j]; + array[j] = array[j - 1]; + array[j - 1] = tmp; + } + } +} diff --git a/rushs/tinyprintf/insertion_sort/insertion_sort.h b/rushs/tinyprintf/insertion_sort/insertion_sort.h new file mode 100644 index 0000000..a7ba674 --- /dev/null +++ b/rushs/tinyprintf/insertion_sort/insertion_sort.h @@ -0,0 +1,8 @@ +#ifndef INSERTION_SORT_H +#define INSERTION_SORT_H + +typedef int (*f_cmp)(const void *, const void *); + +void insertion_sort(void **array, f_cmp comp); + +#endif /* ! INSERTION_SORT_H */ diff --git a/rushs/tinyprintf/inside/inside.sh b/rushs/tinyprintf/inside/inside.sh new file mode 100755 index 0000000..c6872fa --- /dev/null +++ b/rushs/tinyprintf/inside/inside.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +if [ $# -ne 1 ]; then + echo Sorry, expected 1 argument but $# were passed + exit 1 +fi + +if [ -f $1 ]; then + cat $1 + exit 0 +else + echo "$1: + is not a valid file" + exit 2 +fi diff --git a/rushs/tinyprintf/inside_noif/inside_noif.sh b/rushs/tinyprintf/inside_noif/inside_noif.sh new file mode 100755 index 0000000..d4ed8c9 --- /dev/null +++ b/rushs/tinyprintf/inside_noif/inside_noif.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +[ $# -ne 1 ] && echo Sorry, expected 1 argument but $# were passed && exit 1 + +[ -f $1 ] && cat $1 && exit 0 || echo "$1: + is not a valid file" && exit 2 diff --git a/rushs/tinyprintf/int_palindrome/int_palindrome.c b/rushs/tinyprintf/int_palindrome/int_palindrome.c new file mode 100644 index 0000000..6d6847f --- /dev/null +++ b/rushs/tinyprintf/int_palindrome/int_palindrome.c @@ -0,0 +1,18 @@ +int int_palindrome(int n) +{ + if (n < 0) + { + return 0; + } + + int reversed = 0; + int m = n; + + while (m > 0) + { + reversed = reversed * 10 + m % 10; + m /= 10; + } + + return n == reversed; +} diff --git a/rushs/tinyprintf/int_sqrt/int_sqrt.c b/rushs/tinyprintf/int_sqrt/int_sqrt.c new file mode 100644 index 0000000..4b2e5db --- /dev/null +++ b/rushs/tinyprintf/int_sqrt/int_sqrt.c @@ -0,0 +1,20 @@ +int int_sqrt(int n) +{ + if (n < 0) + { + return -1; + } + + if (n == 0 || n == 1) + { + return n; + } + + int i; + for (i = 1; i * i < n; i++) + { + continue; + } + + return i - (i * i != n); +} diff --git a/rushs/tinyprintf/io_count_words/count_words.c b/rushs/tinyprintf/io_count_words/count_words.c new file mode 100644 index 0000000..8b8c9a5 --- /dev/null +++ b/rushs/tinyprintf/io_count_words/count_words.c @@ -0,0 +1,33 @@ +#include <stdio.h> + +int count_words(const char *file_in) +{ + if (file_in == NULL) + { + return -1; + } + + FILE *f = fopen(file_in, "r"); + if (f == NULL) + { + return -1; + } + + int word = 0; + int count = 0; + int c; + while ((c = fgetc(f)) != EOF) + { + if ((c == ' ' || c == '\n' || c == '\t') && word == 1) + { + word = 0; + } + if (c != ' ' && c != '\n' && c != '\t' && word == 0) + { + word = 1; + count++; + } + } + + return count; +} diff --git a/rushs/tinyprintf/io_merge_files/merge_files.c b/rushs/tinyprintf/io_merge_files/merge_files.c new file mode 100644 index 0000000..26ac9cf --- /dev/null +++ b/rushs/tinyprintf/io_merge_files/merge_files.c @@ -0,0 +1,31 @@ +#define _POSIX_C_SOURCE 200809L + +#include <stdio.h> + +int merge_files(const char *file_1, const char *file_2) +{ + FILE *a = fopen(file_1, "a"); + if (a == NULL) + { + return -1; + } + FILE *r = fopen(file_2, "r"); + if (r == NULL) + { + return -1; + } + + int c; + while ((c = fgetc(r)) != EOF) + { + if (fputc(c, a) == EOF) + { + return -1; + } + } + + fclose(a); + fclose(r); + + return 0; +} diff --git a/rushs/tinyprintf/io_replace_line/replace_line.c b/rushs/tinyprintf/io_replace_line/replace_line.c new file mode 100644 index 0000000..7fd0e2a --- /dev/null +++ b/rushs/tinyprintf/io_replace_line/replace_line.c @@ -0,0 +1,50 @@ +#define _POSIX_C_SOURCE 200809L + +#include <stdio.h> +#include <stdlib.h> + +int replace_line(const char *file_in, const char *file_out, const char *content, + int n) +{ + FILE *a = fopen(file_out, "w"); + if (a == NULL) + { + return -1; + } + FILE *r = fopen(file_in, "r"); + if (r == NULL) + { + return -1; + } + + char *buf = NULL; + ssize_t e; + int l = 0; + size_t count = 0; + while ((e = getline(&buf, &count, r)) != 0 && e != -1) + { + if (l == n) + { + if (fputs(content, a) == EOF) + { + free(buf); + return -1; + } + } + else + { + if (fputs(buf, a) == EOF) + { + free(buf); + return -1; + } + } + l++; + } + + fclose(a); + fclose(r); + free(buf); + + return 0; +} diff --git a/rushs/tinyprintf/levenshtein/levenshtein.c b/rushs/tinyprintf/levenshtein/levenshtein.c new file mode 100644 index 0000000..4da9397 --- /dev/null +++ b/rushs/tinyprintf/levenshtein/levenshtein.c @@ -0,0 +1,72 @@ +#include "levenshtein.h" + +#include <stdio.h> + +size_t max(size_t a, size_t b) +{ + if (a >= b) + { + return a; + } + return b; +} + +size_t min(size_t a, size_t b) +{ + if (a <= b) + { + return a; + } + return b; +} + +size_t min_3(size_t a, size_t b, size_t c) +{ + if (a <= b) + { + if (a <= c) + { + return a; + } + return c; + } + else + { + if (b <= c) + { + return b; + } + return c; + } +} + +size_t my_strlen(const char *s) +{ + size_t i; + for (i = 0; s[i]; i++) + { + continue; + } + return i; +} + +size_t levenshtein(const char *s1, const char *s2) +{ + size_t l1 = my_strlen(s1); + size_t l2 = my_strlen(s2); + if (min(l1, l2) == 0) + { + return max(l1, l2); + } + + if (s1[0] == s2[0]) + { + return levenshtein(s1 + 1, s2 + 1); + } + + size_t lev1 = levenshtein(s1 + 1, s2); + size_t lev2 = levenshtein(s1, s2 + 1); + size_t lev3 = levenshtein(s1 + 1, s2 + 1); + + return 1 + min_3(lev1, lev2, lev3); +} diff --git a/rushs/tinyprintf/levenshtein/levenshtein.h b/rushs/tinyprintf/levenshtein/levenshtein.h new file mode 100644 index 0000000..70a5a7b --- /dev/null +++ b/rushs/tinyprintf/levenshtein/levenshtein.h @@ -0,0 +1,8 @@ +#ifndef LEVENSHTEIN_H +#define LEVENSHTEIN_H + +#include <stddef.h> + +size_t levenshtein(const char *s1, const char *s2); + +#endif /* !LEVENSHTEIN_H */ diff --git a/rushs/tinyprintf/main.c b/rushs/tinyprintf/main.c new file mode 100644 index 0000000..4062426 --- /dev/null +++ b/rushs/tinyprintf/main.c @@ -0,0 +1,15 @@ +#include <stdio.h> +#include <stdlib.h> + +//#include "traffic_lights/traffic_lights.h" +unsigned char rol(unsigned char value, unsigned char roll); + +int main(void) +{ + unsigned char l1 = 0b01010101; + unsigned char l2 = 3; + + printf("l1: %b", rol(l1, l2)); + + return 0; +} diff --git a/rushs/tinyprintf/my_abs/my_abs.c b/rushs/tinyprintf/my_abs/my_abs.c new file mode 100644 index 0000000..fc89d2f --- /dev/null +++ b/rushs/tinyprintf/my_abs/my_abs.c @@ -0,0 +1,11 @@ +int my_abs(int n) +{ + if (n < 0) + { + return -n; + } + else + { + return n; + } +} diff --git a/rushs/tinyprintf/my_atoi/my_atoi.c b/rushs/tinyprintf/my_atoi/my_atoi.c new file mode 100644 index 0000000..ca185a5 --- /dev/null +++ b/rushs/tinyprintf/my_atoi/my_atoi.c @@ -0,0 +1,61 @@ +#include "my_atoi.h" + +int my_atoi(const char *str) +{ + int res = 0; + + // str error check + if (str == NULL || *str == '0') + { + return 0; + } + + // trim whitespaces + for (; *str && *str == ' '; str++) + { + continue; + } + + // move to end of str + size_t l; + for (l = 0; str[l]; l++) + { + continue; + } + l--; + + // prepare for calculations + int factor = 1; + + // actual conversion of up to the second element of str (potential sign) + for (; l > 0; l--) + { + char val = str[l]; + if (val < '0' || val > '9') + { + return 0; + } + val -= '0'; + res += val * factor; + + factor *= 10; + } + + // l should be 0 by now + if (str[l] == '-') + { + return -res; + } + else if (str[l] != '+') + { + int val = str[l]; + if (val < '0' || val > '9') + { + return 0; + } + val -= '0'; + return res + val * factor; + } + + return res; +} diff --git a/rushs/tinyprintf/my_atoi/my_atoi.h b/rushs/tinyprintf/my_atoi/my_atoi.h new file mode 100644 index 0000000..b520d09 --- /dev/null +++ b/rushs/tinyprintf/my_atoi/my_atoi.h @@ -0,0 +1,8 @@ +#ifndef MY_ATOI_H +#define MY_ATOI_H + +#include <stddef.h> + +int my_atoi(const char *str); + +#endif /* ! MY_ATOI_H */ diff --git a/rushs/tinyprintf/my_atoi_base/my_atoi_base.c b/rushs/tinyprintf/my_atoi_base/my_atoi_base.c new file mode 100644 index 0000000..46b4560 --- /dev/null +++ b/rushs/tinyprintf/my_atoi_base/my_atoi_base.c @@ -0,0 +1,86 @@ +#include "my_atoi_base.h" + +int val_in_base(char c, const char *base) +{ + size_t i; + for (i = 0; base[i] && base[i] != c; i++) + { + continue; + } + + if (base[i]) + { + return i; + } + + return -1; +} + +int base_size(const char *base) +{ + int res; + for (res = 0; base[res]; res++) + { + continue; + } + return res; +} + +int my_atoi_base(const char *str, const char *base) +{ + int res = 0; + + // str error check + if (str == NULL || *str == '0') + { + return 0; + } + + // trim whitespaces + for (; *str && *str == ' '; str++) + { + continue; + } + + // move to end of str + size_t l; + for (l = 0; str[l]; l++) + { + continue; + } + l--; + + // prepare for calculations + int b = base_size(base); + int factor = 1; + + // actual conversion of up to the second element of str (potential sign) + for (; l > 0; l--) + { + int val = val_in_base(str[l], base); + if (val == -1) + { + return 0; + } + res += val * factor; + + factor *= b; + } + + // l should be 0 by now + if (str[l] == '-') + { + return -res; + } + else if (str[l] != '+') + { + int val = val_in_base(str[l], base); + if (val == -1) + { + return 0; + } + return res + val * factor; + } + + return res; +} diff --git a/rushs/tinyprintf/my_atoi_base/my_atoi_base.h b/rushs/tinyprintf/my_atoi_base/my_atoi_base.h new file mode 100644 index 0000000..296ae23 --- /dev/null +++ b/rushs/tinyprintf/my_atoi_base/my_atoi_base.h @@ -0,0 +1,8 @@ +#ifndef MY_ATOI_BASE_H +#define MY_ATOI_BASE_H + +#include <stddef.h> + +int my_atoi_base(const char *str, const char *base); + +#endif /* ! MY_ATOI_BASE_H */ diff --git a/rushs/tinyprintf/my_bc/my_bc.sh b/rushs/tinyprintf/my_bc/my_bc.sh new file mode 100755 index 0000000..f675838 --- /dev/null +++ b/rushs/tinyprintf/my_bc/my_bc.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +[ -n "$1" ] && echo "$(($1))" && exit 0 + +while IFS='' read -r line; do + [ -z "$line" ] && exit 0 || echo "$(($line))" +done diff --git a/rushs/tinyprintf/my_c_tail/main.c b/rushs/tinyprintf/my_c_tail/main.c new file mode 100644 index 0000000..ba33337 --- /dev/null +++ b/rushs/tinyprintf/my_c_tail/main.c @@ -0,0 +1,10 @@ +#include <stdlib.h> + +#include "my_c_tail.h" + +int main(int argc, char *argv[]) +{ + if (argc > 1) + stdintail(atoi(argv[1])); + return 0; +} diff --git a/rushs/tinyprintf/my_c_tail/my_c_tail.c b/rushs/tinyprintf/my_c_tail/my_c_tail.c new file mode 100644 index 0000000..790240c --- /dev/null +++ b/rushs/tinyprintf/my_c_tail/my_c_tail.c @@ -0,0 +1,46 @@ +#include "my_c_tail.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+
+void stdintail(unsigned int n)
+{
+ char **lines = calloc(2000, sizeof(char *));
+ lines[0] = malloc(350 * sizeof(char));
+ size_t m = 0;
+ char c;
+ size_t i = 0;
+ while (read(STDIN_FILENO, &c, 1))
+ {
+ if (c == '\n')
+ {
+ lines[m][i] = '\0';
+ lines[++m] = malloc(350 * sizeof(char));
+ i = 0;
+ }
+ else
+ {
+ lines[m][i++] = c;
+ }
+ }
+
+ size_t j;
+ if (m > n)
+ {
+ for (size_t i = 0; i < m - n; i++)
+ free(lines[i]);
+ j = m - n;
+ }
+ else
+ j = 0;
+
+ for (; j < m; j++)
+ {
+ for (size_t i = 0; lines[j][i]; i++)
+ write(STDOUT_FILENO, &(lines[j][i]), 1);
+ write(STDOUT_FILENO, "\n", 1);
+ free(lines[j]);
+ }
+ free(lines[m]);
+ free(lines);
+}
diff --git a/rushs/tinyprintf/my_c_tail/my_c_tail.h b/rushs/tinyprintf/my_c_tail/my_c_tail.h new file mode 100644 index 0000000..172c844 --- /dev/null +++ b/rushs/tinyprintf/my_c_tail/my_c_tail.h @@ -0,0 +1,6 @@ +#ifndef MY_C_TAIL_H +#define MY_C_TAIL_H + +void stdintail(unsigned int n); + +#endif // MY_C_TAIL_H diff --git a/rushs/tinyprintf/my_calloc/my_calloc.c b/rushs/tinyprintf/my_calloc/my_calloc.c new file mode 100644 index 0000000..5a2f7f2 --- /dev/null +++ b/rushs/tinyprintf/my_calloc/my_calloc.c @@ -0,0 +1,11 @@ +#include <stdlib.h> + +void *my_calloc(size_t n, size_t size) +{ + char *res = malloc(n * size); + for (size_t i = 0; i < n * size; i++) + { + res[i] = 0; + } + return res; +} diff --git a/rushs/tinyprintf/my_calloc/my_calloc.h b/rushs/tinyprintf/my_calloc/my_calloc.h new file mode 100644 index 0000000..44bf9a2 --- /dev/null +++ b/rushs/tinyprintf/my_calloc/my_calloc.h @@ -0,0 +1,8 @@ +#ifndef MY_CALLOC_H +#define MY_CALLOC_H + +#include <stdlib.h> + +void *my_calloc(size_t n, size_t size); + +#endif /* ! MY_CALLOC_H */ diff --git a/rushs/tinyprintf/my_file/my_file.sh b/rushs/tinyprintf/my_file/my_file.sh new file mode 100755 index 0000000..93c0c20 --- /dev/null +++ b/rushs/tinyprintf/my_file/my_file.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +for arg; do + if [ -f "$arg" ]; then + echo $arg: file + elif [ -d "$arg" ]; then + echo $arg: directory + else + echo $arg: unknown + fi +done diff --git a/rushs/tinyprintf/my_first_variable/create.sh b/rushs/tinyprintf/my_first_variable/create.sh new file mode 100755 index 0000000..d9264db --- /dev/null +++ b/rushs/tinyprintf/my_first_variable/create.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +my_local_frais="Javotte" + +echo My frais is $my_local_frais diff --git a/rushs/tinyprintf/my_first_variable/edit.sh b/rushs/tinyprintf/my_first_variable/edit.sh new file mode 100755 index 0000000..e47e0c3 --- /dev/null +++ b/rushs/tinyprintf/my_first_variable/edit.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +my_local_frais="Javotte" + +echo My frais is $my_local_frais + +my_local_frais="Pulpa" + +echo My frais is now $my_local_frais diff --git a/rushs/tinyprintf/my_first_variable/use.sh b/rushs/tinyprintf/my_first_variable/use.sh new file mode 100755 index 0000000..f9d462e --- /dev/null +++ b/rushs/tinyprintf/my_first_variable/use.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +echo "My frais is $MY_ENV_FRAIS" diff --git a/rushs/tinyprintf/my_itoa/my_itoa.c b/rushs/tinyprintf/my_itoa/my_itoa.c new file mode 100644 index 0000000..cbb6f73 --- /dev/null +++ b/rushs/tinyprintf/my_itoa/my_itoa.c @@ -0,0 +1,38 @@ +#include "my_itoa.h" + +char *my_itoa(int value, char *s) +{ + if (value == 0) + { + s[0] = '0'; + s[1] = '\0'; + return s; + } + char *head = s; + if (value < 0) + { + s[0] = '-'; + s++; + value = -value; + } + + // count numbers + int t = value; + int n = 0; + while (t > 0) + { + t /= 10; + n++; + } + + // n = number count + s[n] = '\0'; + n--; + for (; n >= 0; n--) + { + s[n] = value % 10 + '0'; + value /= 10; + } + + return head; +} diff --git a/rushs/tinyprintf/my_itoa/my_itoa.h b/rushs/tinyprintf/my_itoa/my_itoa.h new file mode 100644 index 0000000..8e84c72 --- /dev/null +++ b/rushs/tinyprintf/my_itoa/my_itoa.h @@ -0,0 +1,6 @@ +#ifndef MY_ITOA_H +#define MY_ITOA_H + +char *my_itoa(int value, char *s); + +#endif /* ! MY_ITOA_H */ diff --git a/rushs/tinyprintf/my_itoa_base/my_itoa_base.c b/rushs/tinyprintf/my_itoa_base/my_itoa_base.c new file mode 100644 index 0000000..29b3042 --- /dev/null +++ b/rushs/tinyprintf/my_itoa_base/my_itoa_base.c @@ -0,0 +1,49 @@ +#include "my_itoa_base.h" + +int base_count(const char *base) +{ + int i; + for (i = 0; base[i]; i++) + { + continue; + } + return i; +} + +char *my_itoa_base(int n, char *s, const char *base) +{ + if (n == 0) + { + s[0] = base[0]; + s[1] = '\0'; + return s; + } + char *head = s; + if (n < 0) + { + s[0] = '-'; + s++; + n = -n; + } + + // count numbers + int t = n; + int m = 0; + int b = base_count(base); + while (t > 0) + { + t /= b; + m++; + } + + // n = number count + s[m] = '\0'; + m--; + for (; m >= 0; m--) + { + s[m] = base[n % b]; + n /= b; + } + + return head; +} diff --git a/rushs/tinyprintf/my_itoa_base/my_itoa_base.h b/rushs/tinyprintf/my_itoa_base/my_itoa_base.h new file mode 100644 index 0000000..0be6314 --- /dev/null +++ b/rushs/tinyprintf/my_itoa_base/my_itoa_base.h @@ -0,0 +1,6 @@ +#ifndef MY_ITOA_BASE_H +#define MY_ITOA_BASE_H + +char *my_itoa_base(int n, char *s, const char *base); + +#endif /* ! MY_ITOA_BASE_H */ diff --git a/rushs/tinyprintf/my_memcmp/my_memcmp.c b/rushs/tinyprintf/my_memcmp/my_memcmp.c new file mode 100644 index 0000000..d498360 --- /dev/null +++ b/rushs/tinyprintf/my_memcmp/my_memcmp.c @@ -0,0 +1,18 @@ +#include "my_memcmp.h" + +int my_memcmp(const void *s1, const void *s2, size_t num) +{ + if (num == 0) + { + return 0; + } + const unsigned char *a = s1; + const unsigned char *b = s2; + + for (; num - 1 && *a == *b; a++, b++, num--) + { + continue; + } + + return *a - *b; +} diff --git a/rushs/tinyprintf/my_memcmp/my_memcmp.h b/rushs/tinyprintf/my_memcmp/my_memcmp.h new file mode 100644 index 0000000..d17cbe6 --- /dev/null +++ b/rushs/tinyprintf/my_memcmp/my_memcmp.h @@ -0,0 +1,8 @@ +#ifndef MY_MEMCMP_H +#define MY_MEMCMP_H + +#include <stddef.h> + +int my_memcmp(const void *s1, const void *s2, size_t num); + +#endif /* !MY_MEMCMP_H */ diff --git a/rushs/tinyprintf/my_memcpy/my_memcpy.c b/rushs/tinyprintf/my_memcpy/my_memcpy.c new file mode 100644 index 0000000..a6a48d4 --- /dev/null +++ b/rushs/tinyprintf/my_memcpy/my_memcpy.c @@ -0,0 +1,28 @@ +#include "my_memcpy.h" + +void *my_memcpy(void *dest, const void *source, size_t num) +{ + char *d = dest; + const char *s = source; + if (dest > source) // reverse array + { + size_t l = num; + for (; l > 0; l--) + { + d[l - 1] = s[l - 1]; + } + } + else + { + for (size_t i = 0; i < num; i++) + { + d[i] = s[i]; + } + } + return dest; +} + +int main(void) +{ + return 0; +} diff --git a/rushs/tinyprintf/my_memcpy/my_memcpy.h b/rushs/tinyprintf/my_memcpy/my_memcpy.h new file mode 100644 index 0000000..bc1b926 --- /dev/null +++ b/rushs/tinyprintf/my_memcpy/my_memcpy.h @@ -0,0 +1,8 @@ +#ifndef MY_MEMCPY_H +#define MY_MEMCPY_H + +#include <stddef.h> + +void *my_memcpy(void *dest, const void *source, size_t num); + +#endif /* ! MY_MEMCPY_H */ diff --git a/rushs/tinyprintf/my_memmove/my_memmove.c b/rushs/tinyprintf/my_memmove/my_memmove.c new file mode 100644 index 0000000..bb360a5 --- /dev/null +++ b/rushs/tinyprintf/my_memmove/my_memmove.c @@ -0,0 +1,23 @@ +#include "my_memmove.h" + +void *my_memmove(void *dest, const void *src, size_t n) +{ + char *d = dest; + const char *s = src; + if (dest > src) // reverse array + { + size_t l = n; + for (; l > 0; l--) + { + d[l - 1] = s[l - 1]; + } + } + else + { + for (size_t i = 0; i < n; i++) + { + d[i] = s[i]; + } + } + return dest; +} diff --git a/rushs/tinyprintf/my_memmove/my_memmove.h b/rushs/tinyprintf/my_memmove/my_memmove.h new file mode 100644 index 0000000..cb253b7 --- /dev/null +++ b/rushs/tinyprintf/my_memmove/my_memmove.h @@ -0,0 +1,8 @@ +#ifndef MY_MEMMOVE_H +#define MY_MEMMOVE_H + +#include <stddef.h> + +void *my_memmove(void *dest, const void *src, size_t n); + +#endif /* ! MY_MEMMOVE_H */ diff --git a/rushs/tinyprintf/my_memset/my_memset.c b/rushs/tinyprintf/my_memset/my_memset.c new file mode 100644 index 0000000..243a5ac --- /dev/null +++ b/rushs/tinyprintf/my_memset/my_memset.c @@ -0,0 +1,12 @@ +#include "my_memset.h" + +void *my_memset(void *s, int c, size_t n) +{ + unsigned char *t = s; + for (size_t i = 0; i < n; i++) + { + t[i] = c; + } + + return t; +} diff --git a/rushs/tinyprintf/my_memset/my_memset.h b/rushs/tinyprintf/my_memset/my_memset.h new file mode 100644 index 0000000..e5ed0f0 --- /dev/null +++ b/rushs/tinyprintf/my_memset/my_memset.h @@ -0,0 +1,6 @@ +#ifndef MY_MEMSET_H +#define MY_MEMSET_H + +#include <stddef.h> + +#endif /* ! MY_MEMSET_H */ diff --git a/rushs/tinyprintf/my_pow/my_pow.c b/rushs/tinyprintf/my_pow/my_pow.c new file mode 100644 index 0000000..f529d87 --- /dev/null +++ b/rushs/tinyprintf/my_pow/my_pow.c @@ -0,0 +1,13 @@ +int my_pow(int a, int b) +{ + if (!a) + return b == 0; + int res = 1; + for (int i = 0; i < b / 2; i++) + { + res *= a * a; + } + if (b % 2) + res *= a; + return res; +} diff --git a/rushs/tinyprintf/my_round/my_round.c b/rushs/tinyprintf/my_round/my_round.c new file mode 100644 index 0000000..324bc1d --- /dev/null +++ b/rushs/tinyprintf/my_round/my_round.c @@ -0,0 +1,6 @@ +int my_round(float n) +{ + if (n < 0) + return n - 0.5; + return n + 0.5; +} diff --git a/rushs/tinyprintf/my_strcmp/my_strcmp.c b/rushs/tinyprintf/my_strcmp/my_strcmp.c new file mode 100644 index 0000000..d3ef3e3 --- /dev/null +++ b/rushs/tinyprintf/my_strcmp/my_strcmp.c @@ -0,0 +1,11 @@ +#include "my_strcmp.h" + +int my_strcmp(const char *s1, const char *s2) +{ + for (; *s1 && *s1 == *s2; s1++, s2++) + { + continue; + } + + return *s1 - *s2; +} diff --git a/rushs/tinyprintf/my_strcmp/my_strcmp.h b/rushs/tinyprintf/my_strcmp/my_strcmp.h new file mode 100644 index 0000000..d89a00b --- /dev/null +++ b/rushs/tinyprintf/my_strcmp/my_strcmp.h @@ -0,0 +1,6 @@ +#ifndef MY_STRCMP_H +#define MY_STRCMP_H + +#include <stddef.h> + +#endif /* ! MY_STRCMP_H */ diff --git a/rushs/tinyprintf/my_strcpy/my_strcpy.c b/rushs/tinyprintf/my_strcpy/my_strcpy.c new file mode 100644 index 0000000..69ad5ee --- /dev/null +++ b/rushs/tinyprintf/my_strcpy/my_strcpy.c @@ -0,0 +1,13 @@ +#include <stdlib.h> + +char *my_strcpy(char *dest, const char *source) +{ + size_t i; + for (i = 0; source[i]; i++) + { + dest[i] = source[i]; + } + dest[i] = '\0'; + + return dest; +} diff --git a/rushs/tinyprintf/my_strlen/my_strlen.c b/rushs/tinyprintf/my_strlen/my_strlen.c new file mode 100644 index 0000000..ec80d0b --- /dev/null +++ b/rushs/tinyprintf/my_strlen/my_strlen.c @@ -0,0 +1,12 @@ +#include "my_strlen.h" + +size_t my_strlen(const char *s) +{ + size_t i; + for (i = 0; s[i]; i++) + { + continue; + } + + return i; +} diff --git a/rushs/tinyprintf/my_strlen/my_strlen.h b/rushs/tinyprintf/my_strlen/my_strlen.h new file mode 100644 index 0000000..02806cc --- /dev/null +++ b/rushs/tinyprintf/my_strlen/my_strlen.h @@ -0,0 +1,6 @@ +#ifndef MY_STRLEN_H +#define MY_STRLEN_H + +#include <stddef.h> + +#endif /* ! MY_STRLEN_H */ diff --git a/rushs/tinyprintf/my_strlowcase/my_strlowcase.c b/rushs/tinyprintf/my_strlowcase/my_strlowcase.c new file mode 100644 index 0000000..e52ea32 --- /dev/null +++ b/rushs/tinyprintf/my_strlowcase/my_strlowcase.c @@ -0,0 +1,13 @@ +#include "my_strlowcase.h" + +void my_strlowcase(char *s) +{ + size_t i; + for (i = 0; s[i]; i++) + { + if (s[i] >= 'A' && s[i] <= 'Z') + { + s[i] += ('a' - 'A'); + } + } +} diff --git a/rushs/tinyprintf/my_strlowcase/my_strlowcase.h b/rushs/tinyprintf/my_strlowcase/my_strlowcase.h new file mode 100644 index 0000000..d4996b8 --- /dev/null +++ b/rushs/tinyprintf/my_strlowcase/my_strlowcase.h @@ -0,0 +1,8 @@ +#ifndef MY_STRLOWCASE_H +#define MY_STRLOWCASE_H + +#include <stddef.h> + +void my_strlowcase(char *str); + +#endif /* ! MY_STRLOWCASE_H */ diff --git a/rushs/tinyprintf/my_strspn/my_strspn.c b/rushs/tinyprintf/my_strspn/my_strspn.c new file mode 100644 index 0000000..18bba0f --- /dev/null +++ b/rushs/tinyprintf/my_strspn/my_strspn.c @@ -0,0 +1,26 @@ +#include "my_strspn.h" + +int is_in(char c, const char *accept) +{ + for (; *accept && *accept != c; accept++) + { + continue; + } + return *accept != '\0'; +} + +size_t my_strspn(const char *s, const char *accept) +{ + if (s == NULL || *s == '\0') + { + return 0; + } + + size_t res; + for (res = 0; *s && is_in(*s, accept) != 0; res++, s++) + { + continue; + } + + return res; +} diff --git a/rushs/tinyprintf/my_strspn/my_strspn.h b/rushs/tinyprintf/my_strspn/my_strspn.h new file mode 100644 index 0000000..f2d7759 --- /dev/null +++ b/rushs/tinyprintf/my_strspn/my_strspn.h @@ -0,0 +1,8 @@ +#ifndef MY_STRSPN_H +#define MY_STRSPN_H + +#include <stddef.h> + +size_t my_strspn(const char *s, const char *accept); + +#endif /* ! MY_STRSPN_H */ diff --git a/rushs/tinyprintf/my_strstr/my_strstr.c b/rushs/tinyprintf/my_strstr/my_strstr.c new file mode 100644 index 0000000..36ac439 --- /dev/null +++ b/rushs/tinyprintf/my_strstr/my_strstr.c @@ -0,0 +1,34 @@ +#include "my_strstr.h" + +#include <stddef.h> + +int my_strstr(const char *haystack, const char *needle) +{ + if (needle == NULL || *needle == '\0') + { + return 0; + } + + for (int i = 0; haystack[i]; i++) + { + if (haystack[i] == needle[0]) + { + int j; + for (j = 0; + haystack[i + j] && needle[j] && needle[j] == haystack[i + j]; + j++) + { + continue; + } + if (needle[j] == '\0') + { + return i; + } + if (haystack[i + j] == '\0') + { + return -1; + } + } + } + return -1; +} diff --git a/rushs/tinyprintf/my_strstr/my_strstr.h b/rushs/tinyprintf/my_strstr/my_strstr.h new file mode 100644 index 0000000..1b734b2 --- /dev/null +++ b/rushs/tinyprintf/my_strstr/my_strstr.h @@ -0,0 +1,6 @@ +#ifndef MY_STRSTR_H +#define MY_STRSTR_H + +int my_strstr(const char *haystack, const char *needle); + +#endif /* ! MY_STRSTR_H */ diff --git a/rushs/tinyprintf/my_strtok_r/my_strtok_r.c b/rushs/tinyprintf/my_strtok_r/my_strtok_r.c new file mode 100644 index 0000000..ec052b7 --- /dev/null +++ b/rushs/tinyprintf/my_strtok_r/my_strtok_r.c @@ -0,0 +1,51 @@ +#include "my_strtok_r.h" + +#include <stddef.h> + +static int is_delim(char c, const char *delims) +{ + for (const char *d = delims; *d; d++) + { + if (*d == c) + return 1; + } + return 0; +} + +char *my_strtok_r(char *str, const char *delim, char **saveptr) +{ + if (str == NULL) + { + if (*saveptr == NULL) + { + return NULL; + } + str = *saveptr; + } + + size_t i = 0; + while (str[i] != '\0' && is_delim(str[i], delim)) + { + i++; + } + if (str[i] == '\0') + { + *saveptr = NULL; + return NULL; + } + + char *res = str + i; + + while (str[i] != '\0' && !is_delim(str[i], delim)) + { + i++; + } + if (str[i] == '\0') + { + *saveptr = NULL; + return res; + } + *saveptr = str + i + 1; + str[i] = '\0'; + return res; +} diff --git a/rushs/tinyprintf/my_strtok_r/my_strtok_r.h b/rushs/tinyprintf/my_strtok_r/my_strtok_r.h new file mode 100644 index 0000000..5603729 --- /dev/null +++ b/rushs/tinyprintf/my_strtok_r/my_strtok_r.h @@ -0,0 +1,6 @@ +#ifndef MY_STRTOK_R_H +#define MY_STRTOK_R_H + +char *my_strtok_r(char *str, const char *delim, char **saveptr); + +#endif /* ! MY_STRTOK_R_H */ diff --git a/rushs/tinyprintf/null_terminated_arrays/null_terminated_arrays.c b/rushs/tinyprintf/null_terminated_arrays/null_terminated_arrays.c new file mode 100644 index 0000000..32d2a17 --- /dev/null +++ b/rushs/tinyprintf/null_terminated_arrays/null_terminated_arrays.c @@ -0,0 +1,50 @@ +#include "null_terminated_arrays.h" + +#include <assert.h> +#include <stddef.h> +#include <stdio.h> + +void reverse_array(const char **arr) +{ + const char **p; + for (p = arr; *p; p++) + { + continue; + } + p--; + + while (p > arr) + { + const char *tmp = *p; + *p = *arr; + *arr = tmp; + arr++; + p--; + } +} + +void reverse_matrix(const char ***matrix) +{ + const char ***p; + for (p = matrix; *p; p++) + { + continue; + } + p--; + + while (p > matrix) + { + reverse_array(*p); + reverse_array(*matrix); + const char **tmp = *p; + *p = *matrix; + *matrix = tmp; + matrix++; + p--; + } + + if (p == matrix) + { + reverse_array(*matrix); + } +} diff --git a/rushs/tinyprintf/null_terminated_arrays/null_terminated_arrays.h b/rushs/tinyprintf/null_terminated_arrays/null_terminated_arrays.h new file mode 100644 index 0000000..31fccc5 --- /dev/null +++ b/rushs/tinyprintf/null_terminated_arrays/null_terminated_arrays.h @@ -0,0 +1,6 @@ +#ifndef NULL_TERMINATED_ARRAYS_H_ +#define NULL_TERMINATED_ARRAYS_H_ + +void reverse_matrix(const char ***matrix); + +#endif /* !NULL_TERMINATED_ARRAYS_H_ */ diff --git a/rushs/tinyprintf/number_digits_rec/number_digits_rec.c b/rushs/tinyprintf/number_digits_rec/number_digits_rec.c new file mode 100644 index 0000000..94de296 --- /dev/null +++ b/rushs/tinyprintf/number_digits_rec/number_digits_rec.c @@ -0,0 +1,8 @@ +unsigned int number_digits_rec(unsigned int n) +{ + if (n / 10 == 0) + { + return 1; + } + return 1 + number_digits_rec(n / 10); +} diff --git a/rushs/tinyprintf/palindrome/palindrome.c b/rushs/tinyprintf/palindrome/palindrome.c new file mode 100644 index 0000000..2ecacfd --- /dev/null +++ b/rushs/tinyprintf/palindrome/palindrome.c @@ -0,0 +1,47 @@ +#include "palindrome.h" + +#include <stddef.h> + +int palindrome(const char *s) +{ + if (s == NULL) + { + return 0; + } + + if (*s == '\0') + { + return 1; + } + + const char *p = s; + while (*p) + { + p++; + } + p--; + + while (p > s) + { + while ((*p < '0' || (*p > '9' && *p < 'A') || (*p > 'Z' && *p < 'a') + || *p > 'z') + && p > s) + { + p--; + } + while ((*s < '0' || (*s > '9' && *s < 'A') || (*s > 'Z' && *s < 'a') + || *s > 'z') + && p > s) + { + s++; + } + if (*p != *s) + { + return 0; + } + p--; + s++; + } + + return 1; +} diff --git a/rushs/tinyprintf/palindrome/palindrome.h b/rushs/tinyprintf/palindrome/palindrome.h new file mode 100644 index 0000000..8595911 --- /dev/null +++ b/rushs/tinyprintf/palindrome/palindrome.h @@ -0,0 +1,6 @@ +#ifndef PALINDROME_H +#define PALINDROME_H + +int palindrome(const char *s); + +#endif /* !PALINDROME_H */ diff --git a/rushs/tinyprintf/pine/pine.c b/rushs/tinyprintf/pine/pine.c new file mode 100644 index 0000000..9d48761 --- /dev/null +++ b/rushs/tinyprintf/pine/pine.c @@ -0,0 +1,36 @@ +#include <stdio.h> + +int pine(unsigned n) +{ + if (n < 3) + { + return 1; + } + + for (unsigned i = 0; i < n; i++) + { + for (unsigned j = 0; j < n - i - 1; j++) + { + putchar(' '); + } + + for (unsigned j = 0; j < 2 * i + 1; j++) + { + putchar('*'); + } + + putchar('\n'); + } + + for (unsigned i = 0; i < n / 2; i++) + { + for (unsigned j = 0; j < n - 1; j++) + { + putchar(' '); + } + putchar('*'); + putchar('\n'); + } + + return 0; +} diff --git a/rushs/tinyprintf/pointer_swap/pointer_swap.c b/rushs/tinyprintf/pointer_swap/pointer_swap.c new file mode 100644 index 0000000..32ceb84 --- /dev/null +++ b/rushs/tinyprintf/pointer_swap/pointer_swap.c @@ -0,0 +1,6 @@ +void pointer_swap(int **a, int **b) +{ + int *tmp = *a; + *a = *b; + *b = tmp; +} diff --git a/rushs/tinyprintf/prototypes/prototypes.sh b/rushs/tinyprintf/prototypes/prototypes.sh new file mode 100755 index 0000000..3c80468 --- /dev/null +++ b/rushs/tinyprintf/prototypes/prototypes.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +sed -En 's/^(([a-zA-Z]* ){0,2}([a-zA-Z]*) *\**([a-z][a-z0-9_]*)\((([a-zA-Z]* )?([a-zA-Z]* )?([a-z][a-z0-9_]* *)(\**[a-z][a-z0-9_]*|[a-z][a-z0-9_]*\[([0-9][0-9]*)?\])(, ([a-zA-Z]* )?([a-zA-Z]* )?([a-z][a-z0-9_]* *)(\**[a-z][a-z0-9_]*|[a-z][a-z0-9_]*\[([0-9][0-9]*)?\])){0,3}|void)\))$/\1;/p' "$1" diff --git a/rushs/tinyprintf/quick_sort/quick_sort.c b/rushs/tinyprintf/quick_sort/quick_sort.c new file mode 100644 index 0000000..6c61fc3 --- /dev/null +++ b/rushs/tinyprintf/quick_sort/quick_sort.c @@ -0,0 +1,18 @@ +#include <stddef.h> + +void quicksort(int *tab, size_t len) +{ + if (tab == NULL) + { + return; + } + for (size_t i = 1; i < len; i++) + { + for (size_t j = i; j > 0 && tab[j - 1] > tab[j]; j--) + { + int tmp = tab[j]; + tab[j] = tab[j - 1]; + tab[j - 1] = tmp; + } + } +} diff --git a/rushs/tinyprintf/quick_sort/quick_sort_example.c b/rushs/tinyprintf/quick_sort/quick_sort_example.c new file mode 100644 index 0000000..2a5228f --- /dev/null +++ b/rushs/tinyprintf/quick_sort/quick_sort_example.c @@ -0,0 +1,19 @@ +#include <stdio.h> + +void quicksort(int *tab, int len); + +int main(void) +{ + unsigned i = 0; + int tab[] = { 10, 11, 2, 3, 8, 5, 7, 6, 26, 30, 2, 1, 17, 13, 14 }; + + unsigned size = sizeof(tab) / sizeof(int); + + quicksort(tab, size); + + for (; i < size - 1; ++i) + printf("%d ", tab[i]); + printf("%d\n", tab[i]); + + return 0; +} diff --git a/rushs/tinyprintf/repeat/repeat.c b/rushs/tinyprintf/repeat/repeat.c new file mode 100644 index 0000000..06d0b43 --- /dev/null +++ b/rushs/tinyprintf/repeat/repeat.c @@ -0,0 +1,11 @@ +#include <stdio.h> + +int main(int argc, char **argv) +{ + if (argc != 3) + return 1; + + for (int i = 0; i < argv[2][0] - '0'; i++) + puts(argv[1]); + return 0; +} diff --git a/rushs/tinyprintf/right_tarball/my_tarball.tar.gz b/rushs/tinyprintf/right_tarball/my_tarball.tar.gz Binary files differnew file mode 100644 index 0000000..eb6acfc --- /dev/null +++ b/rushs/tinyprintf/right_tarball/my_tarball.tar.gz diff --git a/rushs/tinyprintf/rotx/rotx.c b/rushs/tinyprintf/rotx/rotx.c new file mode 100644 index 0000000..a2cb820 --- /dev/null +++ b/rushs/tinyprintf/rotx/rotx.c @@ -0,0 +1,59 @@ +#include <stdlib.h> +#include <unistd.h> + +#define BUFFER_SIZE 10 + +int main(int argc, char **argv) +{ + if (argc != 2) + { + return 0; + } + + int rot = atoi(argv[1]); + int rod = rot; + if (rot < 0) + { + rot = (rot % 26) + 26; + rod = (rod % 10) + 10; + } + + char buf[BUFFER_SIZE]; + ssize_t r; + + while ((r = read(STDIN_FILENO, buf, BUFFER_SIZE))) + { + if (r == -1) + { + return 1; + } + + for (ssize_t i = 0; i < r; i++) + { + if (buf[i] >= 'a' && buf[i] <= 'z') + { + buf[i] = ((buf[i] - 'a') + rot) % 26 + 'a'; + } + else if (buf[i] >= 'A' && buf[i] <= 'Z') + { + buf[i] = ((buf[i] - 'A') + rot) % 26 + 'A'; + } + else if (buf[i] >= '0' && buf[i] <= '9') + { + buf[i] = ((buf[i] - '0') + rod) % 10 + '0'; + } + } + + ssize_t w = write(STDOUT_FILENO, buf, r); + while (w != r) + { + w += write(STDOUT_FILENO, buf, r); + if (w == -1) + { + return 1; + } + } + } + + return 0; +} diff --git a/rushs/tinyprintf/sed_trailing_whitespaces/whitespaces.sed b/rushs/tinyprintf/sed_trailing_whitespaces/whitespaces.sed new file mode 100644 index 0000000..46b7017 --- /dev/null +++ b/rushs/tinyprintf/sed_trailing_whitespaces/whitespaces.sed @@ -0,0 +1 @@ +s/[ \t]*$// diff --git a/rushs/tinyprintf/selection_sort/selection_sort.c b/rushs/tinyprintf/selection_sort/selection_sort.c new file mode 100644 index 0000000..98adc7e --- /dev/null +++ b/rushs/tinyprintf/selection_sort/selection_sort.c @@ -0,0 +1,30 @@ +#include <stddef.h> + +void swap(int *a, int *b) +{ + int tmp = *a; + *a = *b; + *b = tmp; +} + +unsigned array_min(const int arr[], unsigned start, unsigned size) +{ + unsigned min = start; + for (; start < size; start++) + { + if (arr[min] > arr[start]) + { + min = start; + } + } + return min; +} + +void selection_sort(int arr[], unsigned size) +{ + for (size_t i = 0; i < size; i++) + { + unsigned j = array_min(arr, i, size); + swap(&(arr[i]), &(arr[j])); + } +} diff --git a/rushs/tinyprintf/seq/seq.sh b/rushs/tinyprintf/seq/seq.sh new file mode 100755 index 0000000..9721432 --- /dev/null +++ b/rushs/tinyprintf/seq/seq.sh @@ -0,0 +1,33 @@ +#!/bin/sh + +if [ $# -ne 3 ]; then + echo "Usage: ./seq.sh FIRST INCREMENT LAST" 1>&2 + exit 1 +fi + +if [ "$2" -eq 0 ]; then + exit 1 +fi + +if [ "$1" -eq "$3" ]; then + echo "$1" + exit 0 +fi + +if [ "$1" -lt "$3" ]; then + [ 0 -gt "$2" ] && exit 1 + i="$1" + while [ "$3" -ge "$i" ]; do + echo "$i" + i=$(($i + $2)) + done + exit 0 +fi + +[ "$2" -gt 0 ] && exit 1 +i="$1" +while [ "$i" -ge "$3" ]; do + echo "$i" + i=$(($i + $2)) +done +exit 0 diff --git a/rushs/tinyprintf/sieve_eratosthenes_advanced/Makefile b/rushs/tinyprintf/sieve_eratosthenes_advanced/Makefile new file mode 100644 index 0000000..c7e35f9 --- /dev/null +++ b/rushs/tinyprintf/sieve_eratosthenes_advanced/Makefile @@ -0,0 +1,13 @@ +CC=gcc +CFLAGS=-std=c99 -Wall -Wextra -Werror -Wvla -pedantic +LDLIBS= + +all: sieve.o + +sieve.o: sieve.c + $(CC) $(CFLAGS) -c -o sieve.o sieve.c + +.PHONY: clean + +clean: + rm sieve.o diff --git a/rushs/tinyprintf/sieve_eratosthenes_advanced/sieve.c b/rushs/tinyprintf/sieve_eratosthenes_advanced/sieve.c new file mode 100644 index 0000000..7dd4816 --- /dev/null +++ b/rushs/tinyprintf/sieve_eratosthenes_advanced/sieve.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <stdlib.h> + +void sieve(int n) +{ + if (n <= 2) + { + return; + } + + // Generate array + int *a = calloc(n, sizeof(int)); + int count = 0; + + // Actual sieve and count + for (int i = 2; i < n; i++) + { + if (a[i] == 0) + { + for (int k = 2 * i; k < n; k += i) + { + a[k] = 1; + } + } + } + + for (int i = 2; i < n; i++) + { + if (a[i] == 0) + { + count++; + } + } + + // Print the count + printf("%d\n", count); + + free(a); +} diff --git a/rushs/tinyprintf/simple_fnmatch/simple_fnmatch.c b/rushs/tinyprintf/simple_fnmatch/simple_fnmatch.c new file mode 100644 index 0000000..d40353f --- /dev/null +++ b/rushs/tinyprintf/simple_fnmatch/simple_fnmatch.c @@ -0,0 +1,46 @@ +#include "simple_fnmatch.h" + +int simple_fnmatch(const char *pattern, const char *string) +{ + if (!pattern || !string) + return FNM_NOMATCH; + if (*pattern == '*' && pattern[1] == '\0') + return 0; + while (*pattern && *string) + { + if (*pattern == '?') + { + pattern++; + string++; + } + else if (*pattern == '\\') + { + pattern++; + if (!pattern || *pattern != *string) + return FNM_NOMATCH; + string++; + pattern++; + } + else if (*pattern == '*') + { + pattern++; + while (*string && simple_fnmatch(pattern, string)) + string++; + if (*string) + return 0; + } + else if (*pattern != *string) + return FNM_NOMATCH; + else + { + string++; + pattern++; + } + } + + if (*pattern == '*' && pattern[1] == '\0') + return 0; + if (*string || *pattern) + return FNM_NOMATCH; + return 0; +} diff --git a/rushs/tinyprintf/simple_fnmatch/simple_fnmatch.h b/rushs/tinyprintf/simple_fnmatch/simple_fnmatch.h new file mode 100644 index 0000000..e1ae166 --- /dev/null +++ b/rushs/tinyprintf/simple_fnmatch/simple_fnmatch.h @@ -0,0 +1,8 @@ +#ifndef SIMPLE_FNMATCH_H +#define SIMPLE_FNMATCH_H + +#define FNM_NOMATCH 1 + +int simple_fnmatch(const char *pattern, const char *string); + +#endif /* !SIMPLE_FNMATCH_H */ diff --git a/rushs/tinyprintf/stack/stack.c b/rushs/tinyprintf/stack/stack.c new file mode 100644 index 0000000..0498abc --- /dev/null +++ b/rushs/tinyprintf/stack/stack.c @@ -0,0 +1,30 @@ +#include "stack.h" + +#include <stdlib.h> + +struct stack *stack_push(struct stack *s, int e) +{ + struct stack *new = malloc(sizeof(struct stack)); + new->data = e; + new->next = NULL; + + new->next = s; + return new; +} + +struct stack *stack_pop(struct stack *s) +{ + if (s == NULL) + { + return NULL; + } + + struct stack *res = s->next; + free(s); + return res; +} + +int stack_peek(struct stack *s) +{ + return s->data; +} diff --git a/rushs/tinyprintf/stack/stack.h b/rushs/tinyprintf/stack/stack.h new file mode 100644 index 0000000..bd5dd24 --- /dev/null +++ b/rushs/tinyprintf/stack/stack.h @@ -0,0 +1,14 @@ +#ifndef STACK_H +#define STACK_H + +struct stack +{ + int data; + struct stack *next; +}; + +struct stack *stack_push(struct stack *s, int e); +struct stack *stack_pop(struct stack *s); +int stack_peek(struct stack *s); + +#endif /* !STACK_H */ diff --git a/rushs/tinyprintf/str_revert/str_revert.c b/rushs/tinyprintf/str_revert/str_revert.c new file mode 100644 index 0000000..31f7f3d --- /dev/null +++ b/rushs/tinyprintf/str_revert/str_revert.c @@ -0,0 +1,25 @@ +#include "str_revert.h" + +#include <stddef.h> + +void str_revert(char str[]) +{ + if (*str == '\0') + { + return; + } + + size_t len = 0; + for (; str[len]; len++) + { + continue; + } + len--; + + for (size_t i = 0; i <= len / 2; i++) + { + char tmp = str[i]; + str[i] = str[len - i]; + str[len - i] = tmp; + } +} diff --git a/rushs/tinyprintf/str_revert/str_revert.h b/rushs/tinyprintf/str_revert/str_revert.h new file mode 100644 index 0000000..daa23d4 --- /dev/null +++ b/rushs/tinyprintf/str_revert/str_revert.h @@ -0,0 +1,6 @@ +#ifndef STR_REVERT_H +#define STR_REVERT_H + +void str_revert(char str[]); + +#endif /* ! STR_REVERT_H */ diff --git a/rushs/tinyprintf/test.txt b/rushs/tinyprintf/test.txt new file mode 100644 index 0000000..958cdbc --- /dev/null +++ b/rushs/tinyprintf/test.txt @@ -0,0 +1,3 @@ +This is a short line. +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +This is another short line. diff --git a/rushs/tinyprintf/test_a_bit/is_set.c b/rushs/tinyprintf/test_a_bit/is_set.c new file mode 100644 index 0000000..38fccf8 --- /dev/null +++ b/rushs/tinyprintf/test_a_bit/is_set.c @@ -0,0 +1,6 @@ +#include "is_set.h" + +unsigned int is_set(unsigned int value, unsigned char n) +{ + return (value & (1 << (n - 1))) != 0; +} diff --git a/rushs/tinyprintf/test_a_bit/is_set.h b/rushs/tinyprintf/test_a_bit/is_set.h new file mode 100644 index 0000000..8f2fd9a --- /dev/null +++ b/rushs/tinyprintf/test_a_bit/is_set.h @@ -0,0 +1,6 @@ +#ifndef IS_SET_H +#define IS_SET_H + +unsigned int is_set(unsigned int value, unsigned char n); + +#endif /* ! IS_SET_H */ diff --git a/rushs/tinyprintf/test_a_bit/test.c b/rushs/tinyprintf/test_a_bit/test.c new file mode 100644 index 0000000..e3403e4 --- /dev/null +++ b/rushs/tinyprintf/test_a_bit/test.c @@ -0,0 +1,11 @@ +#include <stdio.h> + +#include "is_set.h" + +int main(void) +{ + printf("%d\n", is_set(24, 4)); + printf("%d\n", is_set(24, 3)); + + return 0; +} diff --git a/rushs/tinyprintf/tinylibstream/Makefile b/rushs/tinyprintf/tinylibstream/Makefile new file mode 100644 index 0000000..b060495 --- /dev/null +++ b/rushs/tinyprintf/tinylibstream/Makefile @@ -0,0 +1,13 @@ +CC = gcc +CFLAGS = -std=c99 -pedantic -Werror -Wall -Wextra -Wvla + +.PHONY: library clean + +library: src/tinylibstream.o + ar csr libstream.a src/tinylibstream.o + +clean: + rm libstream.a src/tinylibstream.o + +check: + $(CC) $(CFLAGS) -lcriterion src/tinylibstream.c tests/tests.c diff --git a/rushs/tinyprintf/tinylibstream/include/libstream.h b/rushs/tinyprintf/tinylibstream/include/libstream.h new file mode 100644 index 0000000..459432d --- /dev/null +++ b/rushs/tinyprintf/tinylibstream/include/libstream.h @@ -0,0 +1,167 @@ +#ifndef LIBSTREAM_H +#define LIBSTREAM_H + +#include <fcntl.h> +#include <stdbool.h> +#include <stddef.h> +#include <unistd.h> + +/* +** /!\ DO NOT MODIFY THIS FILE, AS IT WILL BE OVERRIDDEN DURING CORRECTION. /!\ +** +** You can add your own functions declarations to OTHER HEADER FILES. +*/ + +/* the value returned when end of file is reached */ +#define LBS_EOF (-1) + +/* the size of the buffer */ +#define LBS_BUFFER_SIZE 32 + +/* +** Describes the current operation: +** - if reading, the buffer contains read-buffered data +** - if writing, the buffer contains write-buffered data +*/ +enum stream_io_operation +{ + STREAM_READING = 0, + STREAM_WRITING, +}; + +/* +** Controls when to flush the buffer: +** - when unbuffered, flush every time a character is written +** - when buffered, flush when the buffer is full +** - when line buffered, flush when the buffer is full or when a \n +** character is written +*/ +enum stream_buffering +{ + STREAM_UNBUFFERED = 0, + STREAM_LINE_BUFFERED, + STREAM_BUFFERED, +}; + +struct stream +{ + /* the flags passed to open */ + int flags; + + /* + ** Initially, this variable is 0. + ** When a function such as fgetc fails, it is set to 1 to indicate + ** something went wrong. This is useful to make the difference between + ** reaching the end of file and read errors while using fgetc and + ** some others. + ** It is often referred to as the error indicator. + */ + int error; + + /* the file descriptor, as returned by open(2) */ + int fd; + + /* + ** the kind of data stored by the buffer. + ** The default value shouldn't matter. + */ + enum stream_io_operation io_operation; + + /* + ** defines when to flush **output**. + ** This field does not control input buffering (which is always fully + ** buffered). + ** + ** The default value is LINE_BUFFERED if isatty(fd), BUFFERED otherwise. + */ + enum stream_buffering buffering_mode; + + /* the amount of used bytes in the buffer */ + size_t buffered_size; + + /* + ** /!\ This field only makes sense when io_operation is STREAM_READING /!\ + ** the amount of data already read from the buffer by the user. + */ + size_t already_read; + + /* + ** buffer + ** --------------> + ** +==============+====================+---------------------+ + ** | already_read | remaining_buffered | unused_buffer_space | + ** +==============+====================+---------------------+ + ** \_______________________________/ + ** buffered_size + ** + ** /!\ The buffer can contain either read-buffered or write-buffered data, + ** depending on the value of io_operation /!\ + */ + char buffer[LBS_BUFFER_SIZE]; +}; + +/* +** These functions are defined in a header for optimization reasons: +** each .c file that includes this header will get its own copy of the +** function's code, thus easily make optimizations. +** +** ``static`` means each compilation unit (.c file) will have its own copy +** of the function without them clashing. +** +** ``inline`` means the content of the function should be "copy pasted" +** where it's called. It also tells the compiler not to complain when the +** function isn't used. +** +** They're just like a macro, except the type of arguments is checked. +*/ + +static inline size_t stream_remaining_buffered(struct stream *stream) +{ + return stream->buffered_size - stream->already_read; +} + +static inline size_t stream_unused_buffer_space(struct stream *stream) +{ + return sizeof(stream->buffer) - stream->buffered_size; +} + +static inline bool stream_readable(struct stream *stream) +{ + int access_mode = stream->flags & O_ACCMODE; + if (access_mode == O_RDWR) + return true; + return access_mode == O_RDONLY; +} + +static inline bool stream_writable(struct stream *stream) +{ + int access_mode = stream->flags & O_ACCMODE; + if (access_mode == O_RDWR) + return true; + return access_mode == O_WRONLY; +} + +static inline int lbs_ferror(struct stream *stream) +{ + return stream->error; +} + +static inline void lbs_clearerr(struct stream *stream) +{ + stream->error = 0; +} + +static inline void lbs_setbufmode(struct stream *stream, + enum stream_buffering mode) +{ + stream->buffering_mode = mode; +} + +struct stream *lbs_fopen(const char *path, const char *mode); +struct stream *lbs_fdopen(int fd, const char *mode); +int lbs_fflush(struct stream *stream); +int lbs_fclose(struct stream *stream); +int lbs_fputc(int c, struct stream *stream); +int lbs_fgetc(struct stream *stream); + +#endif /* !LIBSTREAM_H */ diff --git a/rushs/tinyprintf/tinylibstream/src/tinylibstream.c b/rushs/tinyprintf/tinylibstream/src/tinylibstream.c new file mode 100644 index 0000000..dca1c01 --- /dev/null +++ b/rushs/tinyprintf/tinylibstream/src/tinylibstream.c @@ -0,0 +1,221 @@ +#include <stdlib.h> +#include <string.h> + +#include "../include/libstream.h" + +int get_flags(const char *mode) +{ + int flags; + if (strcmp(mode, "r") == 0) + { + flags = O_RDONLY; + } + else if (strcmp(mode, "r+") == 0) + { + flags = O_RDWR; + } + else if (strcmp(mode, "w") == 0) + { + flags = O_WRONLY | O_TRUNC | O_CREAT; + } + else + { + flags = O_RDWR | O_TRUNC | O_CREAT; + } + + return flags; +} + +struct stream *lbs_fopen(const char *path, const char *mode) +{ + int fd = open(path, get_flags(mode)); + + return lbs_fdopen(fd, mode); +} + +struct stream *lbs_fdopen(int fd, const char *mode) +{ + if (fd == -1) + { + return NULL; + } + + struct stream *s = malloc(sizeof(struct stream)); + if (s == NULL) + { + return NULL; + } + + s->flags = get_flags(mode); + s->error = 0; + s->fd = fd; + if (isatty(fd)) + { + s->buffering_mode = STREAM_LINE_BUFFERED; + } + else + { + s->buffering_mode = STREAM_BUFFERED; + } + s->buffered_size = 0; + s->already_read = 0; + + return s; +} + +int lbs_fflush(struct stream *stream) +{ + if (stream == NULL || stream->buffered_size == 0) + { + return 0; + } + + if (stream->io_operation == STREAM_READING) + { + if (!stream_readable(stream)) + { + stream->error = 1; + return LBS_EOF; + } + if (stream_remaining_buffered(stream) != 0 + && lseek(stream->fd, -stream_remaining_buffered(stream), SEEK_CUR) + == -1) + { + stream->error = 1; + return LBS_EOF; + } + stream->buffered_size = 0; + stream->already_read = 0; + } + else + { + if (!stream_writable(stream)) + { + stream->error = 1; + return LBS_EOF; + } + ssize_t w; + if ((w = write(stream->fd, stream->buffer, stream->buffered_size)) + == -1) + { + stream->error = 1; + return LBS_EOF; + } + stream->buffered_size = 0; + stream->already_read = 0; + } + return 0; +} + +int lbs_fclose(struct stream *stream) +{ + if (stream == NULL) + { + return 1; + } + + lbs_fflush(stream); + if (close(stream->fd) == -1) + { + return 1; + } + + free(stream); + + return 0; +} + +int lbs_fputc(int c, struct stream *stream) +{ + if (!stream_writable(stream)) + { + stream->error = 1; + return -1; + } + + if (stream->io_operation == STREAM_READING) + { + if (lbs_fflush(stream) != 0) + { + return -1; + } + stream->buffered_size = 0; + stream->already_read = 0; + } + stream->io_operation = STREAM_WRITING; + + if (stream_unused_buffer_space(stream) == 0 + || stream->buffering_mode == STREAM_UNBUFFERED) + { + if (lbs_fflush(stream) != 0) + { + return -1; + } + } + + stream->buffer[stream->buffered_size] = c; + stream->buffered_size++; + if (stream_unused_buffer_space(stream) == 0 + || stream->buffering_mode == STREAM_UNBUFFERED + || (stream->buffering_mode == STREAM_LINE_BUFFERED && c == '\n')) + { + if (lbs_fflush(stream) != 0) + { + return -1; + } + } + + return c; +} + +int refill_buffer(struct stream *stream) +{ + stream->already_read = 0; + ssize_t r; + if ((r = read(stream->fd, stream->buffer, LBS_BUFFER_SIZE)) == -1) + { + stream->error = 1; + return -1; + } + if (r == 0) + { + return -1; + } + stream->buffered_size = r; + return r; +} + +int lbs_fgetc(struct stream *stream) +{ + if (!stream_readable(stream)) + { + stream->error = 1; + return -1; + } + if (stream->io_operation == STREAM_WRITING) + { + if (lbs_fflush(stream) != 0) + { + stream->error = 1; + return -1; + } + stream->already_read = 0; + } + stream->io_operation = STREAM_READING; + + if (stream_remaining_buffered(stream) == 0) + { + int r; + if ((r = refill_buffer(stream)) == -1) + { + stream->error = 1; + return -1; + } + } + + int res = stream->buffer[stream->already_read++]; + + unsigned char c = res; + + return c; +} diff --git a/rushs/tinyprintf/tinylibstream/stdin_buffering_test.c b/rushs/tinyprintf/tinylibstream/stdin_buffering_test.c new file mode 100644 index 0000000..6d3361b --- /dev/null +++ b/rushs/tinyprintf/tinylibstream/stdin_buffering_test.c @@ -0,0 +1,70 @@ +#define _GNU_SOURCE +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +struct slow_cookie +{ + int fd; +}; + +static ssize_t slow_read(void *vcookie, char *buf, size_t count) +{ + struct slow_cookie *cookie = vcookie; + usleep(500000); // sleep for half of a second + + ssize_t res; + + while ((res = read(cookie->fd, buf, count)) < 0) + if (errno != EINTR && errno != EAGAIN) + break; + + return res; +} + +static int slow_close(void *vcookie) +{ + struct slow_cookie *cookie = vcookie; + return close(cookie->fd); +} + +cookie_io_functions_t slow_stdio = { + .read = slow_read, + .close = slow_close, +}; + +int main(void) +{ + /* setup a custom stdin stream with a slow read function */ + struct slow_cookie cookie = { + .fd = STDIN_FILENO, + }; + + FILE *input_stream = fopencookie(&cookie, "r", slow_stdio); + + /* change the buffer size to the one given by stdbuf. + ** it doesn't work out of the box as stdbuf only does + ** this for the already existing stdin stream. + */ + char *buffer = NULL; + char *buffer_size = getenv("_STDBUF_I"); + if (buffer_size) + { + size_t size = atoi(buffer_size); + buffer = malloc(size); + setvbuf(input_stream, buffer, _IOFBF, size); + } + + /* forward all characters from stdin to stdout */ + int c; + while ((c = fgetc(input_stream)) != EOF) + { + fputc(c, stdout); + fflush(stdout); + } + + fclose(input_stream); + free(buffer); + return 0; +} diff --git a/rushs/tinyprintf/tinylibstream/stdout_buffering_test.c b/rushs/tinyprintf/tinylibstream/stdout_buffering_test.c new file mode 100644 index 0000000..45c0a83 --- /dev/null +++ b/rushs/tinyprintf/tinylibstream/stdout_buffering_test.c @@ -0,0 +1,13 @@ +#define _XOPEN_SOURCE 500 +#include <stdio.h> +#include <unistd.h> + +int main(void) +{ + const char test_string[] = "Robin\nloves\nBatman\n"; + for (size_t i = 0; test_string[i]; i++) + { + usleep(100000); // wait a tenth of a second + putchar(test_string[i]); + } +} diff --git a/rushs/tinyprintf/tinyprintf/Makefile b/rushs/tinyprintf/tinyprintf/Makefile new file mode 100644 index 0000000..6a07d90 --- /dev/null +++ b/rushs/tinyprintf/tinyprintf/Makefile @@ -0,0 +1,17 @@ +CC=gcc +CFLAGS=-std=c99 -Wall -Wextra -Werror -Wvla -pedantic +LDLIBS=-lcriterion + +all: src/tinyprintf.o + +check: src/tinyprintf.o + $(CC) $(CFLAGS) -o tinytests src/tinyprintf.o tests/tests.c $(LDLIBS) + ./tinytests + +src/tinyprintf.o: src/tinyprintf.c + $(CC) $(CFLAGS) -c -o src/tinyprintf.o src/tinyprintf.c + +.PHONY: clean + +clean: + rm src/tinyprintf.o tinytests diff --git a/rushs/tinyprintf/tinyprintf/src/tinyprintf.c b/rushs/tinyprintf/tinyprintf/src/tinyprintf.c new file mode 100644 index 0000000..d005db7 --- /dev/null +++ b/rushs/tinyprintf/tinyprintf/src/tinyprintf.c @@ -0,0 +1,251 @@ +#include "tinyprintf.h" + +#include <stdio.h> +#include <stdlib.h> + +int tinyprintf(const char *format, ...) +{ + if (format == NULL || *format == '\0') + { + return 0; + } + + va_list ap; + va_start(ap, format); + int res = 0; + + size_t i = 0; + while (format[i]) + { + if (format[i] != '%') + { + putchar(format[i]); + res++; + } + else + { + dispatch(format[i + 1], ap, &res); + format++; + } + format++; + } + va_end(ap); + return res; +} + +void dispatch(char c, va_list ap, int *res) +{ + switch (c) + { + case '%': + putchar('%'); + (*res)++; + break; + case 'd': + handle_d(va_arg(ap, int), res); + break; + case 'u': + handle_u(va_arg(ap, unsigned int), res); + break; + case 'x': + handle_x(va_arg(ap, unsigned int), res); + break; + case 'o': + handle_o(va_arg(ap, unsigned int), res); + break; + case 'c': + putchar(va_arg(ap, int)); + (*res)++; + break; + case 's': + handle_s(va_arg(ap, char *), res); + break; + default: + putchar('%'); + putchar(c); + *res += 2; + break; + } +} + +void handle_d(int val, int *res) +{ + if (val < 0) + { + putchar('-'); + (*res)++; + val = -val; + } + + if (val == 0) + { + putchar('0'); + (*res)++; + } + + int t = val; + int n = 0; + while (t > 0) + { + t /= 10; + n++; + } + + char *s = malloc(n * sizeof(char)); + int m = n; + if (s == NULL) + { + return; + } + n--; + for (; n >= 0; n--) + { + s[n] = val % 10 + '0'; + val /= 10; + } + + // actual printing + for (int i = 0; i < m; i++) + { + putchar(s[i]); + (*res)++; + } + free(s); +} + +void handle_s(char *s, int *res) +{ + if (s == NULL) + { + char *text = "(null)"; + for (; *text; text++) + { + putchar(*text); + (*res)++; + } + return; + } + for (; *s; s++) + { + putchar(*s); + (*res)++; + } +} + +void handle_u(unsigned int val, int *res) +{ + if (val == 0) + { + putchar('0'); + (*res)++; + } + + unsigned int t = val; + int n = 0; + while (t > 0) + { + t /= 10; + n++; + } + + char *s = malloc(n * sizeof(char)); + int m = n; + if (s == NULL) + { + return; + } + n--; + for (; n >= 0; n--) + { + s[n] = val % 10 + '0'; + val /= 10; + } + + // actual printing + for (int i = 0; i < m; i++) + { + putchar(s[i]); + (*res)++; + } + free(s); +} + +void handle_x(unsigned int val, int *res) +{ + if (val == 0) + { + putchar('0'); + (*res)++; + } + + unsigned int t = val; + int n = 0; + while (t > 0) + { + t /= 16; + n++; + } + + char *s = malloc(n * sizeof(char)); + int m = n; + if (s == NULL) + { + return; + } + n--; + for (; n >= 0; n--) + { + s[n] = val % 16 + '0'; + if (s[n] > '9') + { + s[n] = (s[n] - '0') % 10 + 'a'; + } + val /= 16; + } + + // actual printing + for (int i = 0; i < m; i++) + { + putchar(s[i]); + (*res)++; + } + free(s); +} + +void handle_o(unsigned int val, int *res) +{ + if (val == 0) + { + putchar('0'); + (*res)++; + } + + unsigned int t = val; + int n = 0; + while (t > 0) + { + t /= 8; + n++; + } + + char *s = malloc(n * sizeof(char)); + int m = n; + if (s == NULL) + { + return; + } + n--; + for (; n >= 0; n--) + { + s[n] = val % 8 + '0'; + val /= 8; + } + + // actual printing + for (int i = 0; i < m; i++) + { + putchar(s[i]); + (*res)++; + } + free(s); +} diff --git a/rushs/tinyprintf/tinyprintf/src/tinyprintf.h b/rushs/tinyprintf/tinyprintf/src/tinyprintf.h new file mode 100644 index 0000000..fd1f0b4 --- /dev/null +++ b/rushs/tinyprintf/tinyprintf/src/tinyprintf.h @@ -0,0 +1,15 @@ +#ifndef TINYPRINTF_H +#define TINYPRINTF_H + +#include <stdarg.h> +#include <stddef.h> + +int tinyprintf(const char *format, ...); +void handle_d(int val, int *res); +void handle_u(unsigned int val, int *res); +void handle_x(unsigned int val, int *res); +void handle_o(unsigned int val, int *res); +void handle_s(char *s, int *res); +void dispatch(char c, va_list ap, int *res); + +#endif /* ! TINYPRINTF_H */ diff --git a/rushs/tinyprintf/tinyprintf/tests/tests.c b/rushs/tinyprintf/tinyprintf/tests/tests.c new file mode 100644 index 0000000..4235203 --- /dev/null +++ b/rushs/tinyprintf/tinyprintf/tests/tests.c @@ -0,0 +1,213 @@ +#include <criterion/criterion.h> +#include <criterion/assert.h> +#include <criterion/redirect.h> +#include <stdio.h> + +#include "../src/tinyprintf.h" + +TestSuite(TestHandleD); + +Test(TestHandleD, handle_d42, .init = cr_redirect_stdout) +{ + int res = 0; + handle_d(42, &res); + fflush(stdout); + cr_assert_stdout_eq_str("42"); + cr_expect(res == 2, "Expected: %d. Got: %d", 2, res); +} + +Test(TestHandleD, handle_d0, .init = cr_redirect_stdout) +{ + int res = 0; + handle_d(0, &res); + fflush(stdout); + cr_assert_stdout_eq_str("0"); + cr_expect(res == 1, "Expected: %d. Got: %d", 1, res); +} + +Test(TestHandleD, handle_dminus42, .init = cr_redirect_stdout) +{ + int res = 0; + handle_d(-42, &res); + fflush(stdout); + cr_assert_stdout_eq_str("-42"); + cr_expect(res == 3, "Expected: %d. Got: %d", 3, res); +} + +Test(TestHandleD, simple_print, .init = cr_redirect_stdout) +{ + int retval = tinyprintf("%s [%d] %s", "Hello", 42, "world!"); + fflush(stdout); + cr_assert_stdout_eq_str("Hello [42] world!"); + cr_expect(retval == 17, "Expected: %d. Got: %d", 17, retval); +} + +TestSuite(TestHandleX); + +Test(TestHandleX, handle_x42, .init = cr_redirect_stdout) +{ + int res = 0; + handle_x(42, &res); + fflush(stdout); + cr_assert_stdout_eq_str("2a"); + cr_expect(res == 2, "Expected: %d. Got: %d", 2, res); +} + +Test(TestHandleX, handle_x0, .init = cr_redirect_stdout) +{ + int res = 0; + handle_x(0, &res); + fflush(stdout); + cr_assert_stdout_eq_str("0"); + cr_expect(res == 1, "Expected: %d. Got: %d", 1, res); +} + +Test(TestHandleX, handle_x15, .init = cr_redirect_stdout) +{ + int res = 0; + handle_x(15, &res); + fflush(stdout); + cr_assert_stdout_eq_str("f"); + cr_expect(res == 1, "Expected: %d. Got: %d", 1, res); +} + +Test(TestHandleX, handle_0xdeadc0de, .init = cr_redirect_stdout) +{ + int res = 0; + handle_x(0xdeadc0de, &res); + fflush(stdout); + cr_assert_stdout_eq_str("deadc0de"); + cr_expect(res == 8, "Expected: %d. Got: %d", 8, res); +} + +Test(TestHandleX, simple_print_hexa, .init = cr_redirect_stdout) +{ + int retval = tinyprintf("%s [%x] %s", "Hello", 42, "world!"); + fflush(stdout); + cr_assert_stdout_eq_str("Hello [2a] world!"); + cr_expect(retval == 17, "Expected: %d. Got: %d", 17, retval); +} + +TestSuite(TestHandleU); + +Test(TestHandleU, handle_u42, .init = cr_redirect_stdout) +{ + int res = 0; + handle_u(42, &res); + fflush(stdout); + cr_assert_stdout_eq_str("42"); + cr_expect(res == 2, "Expected: %d. Got: %d", 2, res); +} + +Test(TestHandleU, handle_u0, .init = cr_redirect_stdout) +{ + int res = 0; + handle_u(0, &res); + fflush(stdout); + cr_assert_stdout_eq_str("0"); + cr_expect(res == 1, "Expected: %d. Got: %d", 1, res); +} + +Test(TestHandleU, handle_u15, .init = cr_redirect_stdout) +{ + int res = 0; + handle_u(15, &res); + fflush(stdout); + cr_assert_stdout_eq_str("15"); + cr_expect(res == 2, "Expected: %d. Got: %d", 2, res); +} + +Test(TestHandleU, simple_print_unsigned, .init = cr_redirect_stdout) +{ + int retval = tinyprintf("%s [%u] %s", "Hello", 42, "world!"); + fflush(stdout); + cr_assert_stdout_eq_str("Hello [42] world!"); + cr_expect(retval == 17, "Expected: %d. Got: %d", 17, retval); +} + +TestSuite(TestHandleO); + +Test(TestHandleO, handle_o42, .init = cr_redirect_stdout) +{ + int res = 0; + handle_o(42, &res); + fflush(stdout); + cr_assert_stdout_eq_str("52"); + cr_expect(res == 2, "Expected: %d. Got: %d", 2, res); +} + +Test(TestHandleO, handle_o0, .init = cr_redirect_stdout) +{ + int res = 0; + handle_o(0, &res); + fflush(stdout); + cr_assert_stdout_eq_str("0"); + cr_expect(res == 1, "Expected: %d. Got: %d", 1, res); +} + +Test(TestHandleO, handle_o7, .init = cr_redirect_stdout) +{ + int res = 0; + handle_o(7, &res); + fflush(stdout); + cr_assert_stdout_eq_str("7"); + cr_expect(res == 1, "Expected: %d. Got: %d", 1, res); +} + +Test(TestHandleO, simple_print_octal, .init = cr_redirect_stdout) +{ + int retval = tinyprintf("%s [%o] %s", "Hello", 42, "world!"); + fflush(stdout); + cr_assert_stdout_eq_str("Hello [52] world!"); + cr_expect(retval == 17, "Expected: %d. Got: %d", 17, retval); +} + +TestSuite(TestPrint); + +Test(TestPrint, print_percent, .init = cr_redirect_stdout) +{ + int retval = tinyprintf("%%s", "in your head"); + fflush(stdout); + cr_assert_stdout_eq_str("%s"); + cr_expect(retval == 2, "Expected: %d. Got: %d", 2, retval); +} + +Test(TestPrint, print_unknown_option, .init = cr_redirect_stdout) +{ + int retval = tinyprintf("Good morning ACU! %t Tinyprintf is cool", 12); + fflush(stdout); + cr_assert_stdout_eq_str("Good morning ACU! %t Tinyprintf is cool"); + cr_expect(retval == 39, "Expected: %d. Got: %d", 39, retval); +} + +Test(TestPrint, print_tricky, .init = cr_redirect_stdout) +{ + int retval = tinyprintf("%c%c is %s... %d too.", '4', '2', "the answer", '*'); + fflush(stdout); + cr_assert_stdout_eq_str("42 is the answer... 42 too."); + cr_expect(retval == 27, "Expected: %d. Got: %d", 27, retval); +} + +Test(TestPrint, print_null, .init = cr_redirect_stdout) +{ + int retval = tinyprintf("%c%c is %s... %d too.", '4', '2', NULL, '*'); + fflush(stdout); + cr_assert_stdout_eq_str("42 is (null)... 42 too."); + cr_expect(retval == 23, "Expected: %d. Got: %d", 23, retval); +} + +Test(TestPrint, print_null_fmt, .init = cr_redirect_stdout) +{ + int retval = tinyprintf(NULL, '4', '2', NULL, '*'); + fflush(stdout); + cr_assert_stdout_eq_str(""); + cr_expect(retval == 0, "Expected: %d. Got: %d", 0, retval); +} + +Test(TestPrint, print_empty_fmt, .init = cr_redirect_stdout) +{ + int retval = tinyprintf("", '4', '2', NULL, '*'); + fflush(stdout); + cr_assert_stdout_eq_str(""); + cr_expect(retval == 0, "Expected: %d. Got: %d", 0, retval); +} diff --git a/rushs/tinyprintf/traffic_lights/traffic_lights.c b/rushs/tinyprintf/traffic_lights/traffic_lights.c new file mode 100644 index 0000000..76ea94f --- /dev/null +++ b/rushs/tinyprintf/traffic_lights/traffic_lights.c @@ -0,0 +1,38 @@ +#include "traffic_lights.h" + +void init(unsigned char *lights) +{ + *lights <<= 4; +} + +void turn_on(unsigned char *lights, unsigned char light_num) +{ + *lights |= 1 << (light_num - 1); +} + +void turn_off(unsigned char *lights, unsigned char light_num) +{ + *lights &= ~(1 << (light_num - 1)); +} + +void next_step(unsigned char *lights) +{ + *lights <<= 1; + *lights += *lights >> 4; +} + +void reverse(unsigned char *lights) +{ + *lights = ~*lights; +} + +void swap(unsigned char *lights_1, unsigned char *lights_2) +{ + if (lights_1 == lights_2) + { + return; + } + *lights_1 = *lights_2 ^ *lights_1; + *lights_2 = *lights_1 ^ *lights_2; + *lights_1 = *lights_2 ^ *lights_1; +} diff --git a/rushs/tinyprintf/traffic_lights/traffic_lights.h b/rushs/tinyprintf/traffic_lights/traffic_lights.h new file mode 100644 index 0000000..7c803ea --- /dev/null +++ b/rushs/tinyprintf/traffic_lights/traffic_lights.h @@ -0,0 +1,11 @@ +#ifndef TRAFFIC_LIGHTS_H +#define TRAFFIC_LIGHTS_H + +void init(unsigned char *lights); +void turn_on(unsigned char *lights, unsigned char light_num); +void turn_off(unsigned char *lights, unsigned char light_num); +void next_step(unsigned char *lights); +void reverse(unsigned char *lights); +void swap(unsigned char *lights_1, unsigned char *lights_2); + +#endif /* !TRAFFIC_LIGHTS_H */ diff --git a/rushs/tinyprintf/user_ids/get_ids.sh b/rushs/tinyprintf/user_ids/get_ids.sh new file mode 100644 index 0000000..2a1668b --- /dev/null +++ b/rushs/tinyprintf/user_ids/get_ids.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +cut --delimiter=: --fields=3 -s /etc/passwd | sort -rgu diff --git a/rushs/tinyprintf/using_special_variables/print.sh b/rushs/tinyprintf/using_special_variables/print.sh new file mode 100755 index 0000000..c24bebd --- /dev/null +++ b/rushs/tinyprintf/using_special_variables/print.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +echo $# +echo $* +echo $1 +echo $2 +echo $3 +echo ${13} +echo $0 diff --git a/rushs/tinyprintf/variant/variant.c b/rushs/tinyprintf/variant/variant.c new file mode 100644 index 0000000..eb2f8a8 --- /dev/null +++ b/rushs/tinyprintf/variant/variant.c @@ -0,0 +1,96 @@ +#include "variant.h" + +#include <stdio.h> +#include <string.h> + +void variant_display(const struct variant *e) +{ + switch (e->type) + { + case (TYPE_INT): + printf("%d\n", e->value.int_v); + break; + case (TYPE_FLOAT): + printf("%f\n", e->value.float_v); + break; + case (TYPE_CHAR): + printf("%c\n", e->value.char_v); + break; + default: + printf("%s\n", e->value.str_v); + break; + } +} + +bool variant_equal(const struct variant *left, const struct variant *right) +{ + if (left == NULL || right == NULL) + { + return true; + } + switch (left->type) + { + case (TYPE_INT): + if (right->type != TYPE_INT) + { + return false; + } + return left->value.int_v == right->value.int_v; + case (TYPE_FLOAT): + if (right->type != TYPE_FLOAT) + { + return false; + } + return left->value.float_v == right->value.float_v; + case (TYPE_CHAR): + if (right->type != TYPE_CHAR) + { + return false; + } + return left->value.char_v == right->value.char_v; + default: + if (right->type != TYPE_STRING) + { + return false; + } + return strcmp(left->value.str_v, right->value.str_v) == 0; + } +} + +int variant_find(const struct variant *array, size_t len, enum type type, + union type_any value) +{ + size_t i; + struct variant cmp = { type, value }; + for (i = 0; i < len && variant_equal(array + i, &cmp) == false; i++) + { + continue; + } + + if (i == len) + { + return -1; + } + return i; +} + +float variant_sum(const struct variant *array, size_t len) +{ + if (array == NULL) + { + return 0; + } + float sum = 0; + for (size_t i = 0; i < len; i++) + { + if (array[i].type == TYPE_FLOAT) + { + sum += array[i].value.float_v; + } + else if (array[i].type == TYPE_INT) + { + sum += array[i].value.int_v; + } + } + return sum; +} diff --git a/rushs/tinyprintf/variant/variant.h b/rushs/tinyprintf/variant/variant.h new file mode 100644 index 0000000..9983bc1 --- /dev/null +++ b/rushs/tinyprintf/variant/variant.h @@ -0,0 +1,35 @@ +#ifndef VARIANT_H +#define VARIANT_H + +#include <stdbool.h> +#include <stddef.h> + +enum type +{ + TYPE_INT, + TYPE_FLOAT, + TYPE_CHAR, + TYPE_STRING +}; + +union type_any +{ + int int_v; + float float_v; + char char_v; + const char *str_v; +}; + +struct variant +{ + enum type type; + union type_any value; +}; + +void variant_display(const struct variant *e); +bool variant_equal(const struct variant *left, const struct variant *right); +int variant_find(const struct variant *array, size_t len, enum type type, + union type_any value); +float variant_sum(const struct variant *array, size_t len); + +#endif /* !VARIANT_H */ diff --git a/rushs/tinyprintf/vector/Makefile b/rushs/tinyprintf/vector/Makefile new file mode 100644 index 0000000..744241f --- /dev/null +++ b/rushs/tinyprintf/vector/Makefile @@ -0,0 +1,13 @@ +CC = gcc +CFLAGS = -Wall -Werror -Wvla -Wextra -std=c99 -pedantic + +.PHONY: library clean + +library: vector.o + ar csr libvector.a vector.o + +vector.o: vector.c + $(CC) $(CFLAGS) -c -o vector.o vector.c + +clean: + $(RM) libvector.a vector.o diff --git a/rushs/tinyprintf/vector/vector.c b/rushs/tinyprintf/vector/vector.c new file mode 100644 index 0000000..cbd19aa --- /dev/null +++ b/rushs/tinyprintf/vector/vector.c @@ -0,0 +1,152 @@ +#include "vector.h" + +#include <stdio.h> +#include <stdlib.h> + +struct vector *vector_init(size_t n) +{ + struct vector *res = malloc(sizeof(struct vector)); + if (res == NULL) + { + return NULL; + } + + res->size = 0; + res->capacity = n; + res->data = malloc(n * sizeof(int)); + if (res->data == NULL) + { + return NULL; + } + return res; +} + +void vector_destroy(struct vector *v) +{ + if (v) + { + free(v->data); + free(v); + } +} + +struct vector *vector_resize(struct vector *v, size_t n) +{ + if (!v) + { + return NULL; + } + if (n == v->capacity) + { + return v; + } + if (v) + { + int *tmp = realloc(v->data, n * sizeof(int)); + if (tmp == NULL) + { + return NULL; + } + v->data = tmp; + + if (n < v->size) + { + v->size = n; + } + v->capacity = n; + return v; + } + return NULL; +} + +struct vector *vector_append(struct vector *v, int elt) +{ + if (v == NULL) + { + return NULL; + } + + if (v->size == v->capacity) + { + if (vector_resize(v, v->capacity * 2) == NULL) + { + return NULL; + } + } + v->data[v->size] = elt; + v->size++; + return v; +} + +void vector_print(const struct vector *v) +{ + if (v == NULL || v->size == 0) + { + printf("\n"); + return; + } + for (size_t i = 0; i < v->size - 1; i++) + { + printf("%d,", v->data[i]); + } + printf("%d\n", v->data[v->size - 1]); +} + +struct vector *vector_reset(struct vector *v, size_t n) +{ + if (vector_resize(v, n) == NULL) + { + return NULL; + } + + v->size = 0; + return v; +} + +struct vector *vector_insert(struct vector *v, size_t i, int elt) +{ + if (v == NULL || i > v->size) + return NULL; + if (i == v->size) + { + return vector_append(v, elt); + } + if (v->size == v->capacity) + { + if (vector_resize(v, v->capacity * 2) == NULL) + { + return NULL; + } + } + for (size_t j = v->size; j > i; j--) + { + v->data[j] = v->data[j - 1]; + } + v->size++; + + v->data[i] = elt; + return v; +} + +struct vector *vector_remove(struct vector *v, size_t i) +{ + if (v == NULL || v->size == 0 || i >= v->size) + { + return NULL; + } + for (size_t j = i; j < v->size - 1; j++) + { + v->data[j] = v->data[j + 1]; + } + + v->size--; + + if (v->size < v->capacity / 2) + { + if (vector_resize(v, v->capacity / 2) == NULL) + { + return NULL; + } + } + return v; +} diff --git a/rushs/tinyprintf/vector/vector.h b/rushs/tinyprintf/vector/vector.h new file mode 100644 index 0000000..5afada7 --- /dev/null +++ b/rushs/tinyprintf/vector/vector.h @@ -0,0 +1,64 @@ +#ifndef VECTOR_H +#define VECTOR_H + +#include <stddef.h> + +struct vector +{ + // The number of elements in the vector + size_t size; + // The maximum number of elements in the vector + size_t capacity; + // The elements themselves + int *data; +}; + +/* +** Initialize the vector with `n` capacity. +** Returns `NULL` if an error occured. +*/ +struct vector *vector_init(size_t n); + +/* +** Release the memory used by the vector. +** Does nothing if `v` is `NULL`. +*/ +void vector_destroy(struct vector *v); + +/* +** Resize the vector to `n` capacity. +** Returns `NULL` if an error occured. +*/ +struct vector *vector_resize(struct vector *v, size_t n); + +/* +** Append an element to the end of the vector. Expand the vector if needed. +** Returns `NULL` if an error occured. +*/ +struct vector *vector_append(struct vector *v, int elt); + +/* +** Display the vector contents on `stdout`. +** Displays `\n` if `v` is `NULL`. +*/ +void vector_print(const struct vector *v); + +/* +** Remove all the elements of the vector, and resize it to `n` capacity. +** Returns `NULL` if an error occured. +*/ +struct vector *vector_reset(struct vector *v, size_t n); + +/* +** Insert `n` at the index `i`. Expand the vector if needed. +** Returns `NULL` if an error occured. +*/ +struct vector *vector_insert(struct vector *v, size_t i, int elt); + +/* +** Remove the element at the index `i`. +** Returns `NULL` if an error occured. +*/ +struct vector *vector_remove(struct vector *v, size_t i); + +#endif /* !VECTOR_H */ |
