Skip to content

Instantly share code, notes, and snippets.

@x-yuri
x-yuri / c, c++: jumping over declarations.md
Last active October 28, 2024 13:03
c/c++: jumping over declarations

c/c++: jumping over declarations

In C one can't have a labeled variable declaration. This quirk probably appeared in C99. Before that variable declarations had to be at the beginnings of blocks, so there could never be a variable declaration after a label. C99 allowed intermixing variable declarations and statements, but the grammar [remained the same][a], allowing only statements after labels.

cases
$ gcc --version
gcc (Alpine 13.2.1_git20240309) 13.2.1 20240309
@x-yuri
x-yuri / A poor man's http server.md
Last active January 3, 2025 05:48
A poor man's http server

A poor man's http server

The server process handles a request and exits. But the server is running in a docker container with --restart always, so after each request docker restarts the server.

Dockerfile:

FROM alpine:3.20
COPY a.c a.c
RUN apk add gcc musl-dev \
@x-yuri
x-yuri / Reading an HTTP request without blocking.md
Created October 26, 2024 18:57
Reading an HTTP request without blocking

Reading an HTTP request without blocking

It's assumed that only valid requests are received. And that read() doesn't block until \r\n\r\n is received.

a.c:

#include <unistd.h>
#include <err.h>
#include <string.h>
@x-yuri
x-yuri / a. mecab.md
Last active October 31, 2024 12:25
mecab

mecab

Dockerfile (git):

FROM alpine:3.20
RUN apk add git build-base curl \
    && git clone https://github.com/taku910/mecab \
    && cd mecab/mecab \
    && ./configure \
@x-yuri
x-yuri / BRE|ERE: escape special chars.md
Created October 20, 2024 08:32
BRE/ERE: escape special chars

BRE/ERE: escape special chars

a.bats:

setup() {
    load bats-support/load
    load bats-assert/load
}
@x-yuri
x-yuri / iptables-save != iptables-save + iptables-xml + iptables.xslt.md
Created October 18, 2024 07:54
iptables-save != iptables-save + iptables-xml + iptables.xslt

iptables-save != iptables-save + iptables-xml + iptables.xslt

$ sudo iptables-save | iptables-xml > /tmp/iptables.xml
$ diff -u <(sudo iptables-save) <(xsltproc /usr/share/xtables/iptables.xslt /tmp/iptables.xml)

Output:

@x-yuri
x-yuri / XSLT: swap 2 elements.md
Last active October 18, 2024 07:36
XSLT: swap 2 elements

XSLT: swap 2 elements

a.xml:

<a>
    <b/>
    <c/>
    <d/>
@x-yuri
x-yuri / xargs.md
Created October 18, 2024 03:29
xargs

tl;dr By default quotes and backslash quote blanks (spaces), and only backslash can quote newlines. With -d and -0, all of those are not special (unless one of those is passed with -d). With -I the separator is a newline and it uses one argument per invocation. Quotes and backslash are stripped (1). Quotes must be balanced (2). Use along with -d\\n to avoid (1) and (2).

By default single, double quotes and backslash quote blanks (e.g. spaces):

$ echo "'a b' c" | xargs -t
echo 'a b' c
a b c
@x-yuri
x-yuri / docker run: determining if --init is needed.md
Last active October 14, 2024 07:00
docker run: determining if --init is needed

docker run: determining if --init is needed

Normally, if a program doesn't set a SIGTERM handler, it needs --init:

Dockerfile:

FROM alpine:3.20
COPY a.c .
RUN apk add build-base \
@x-yuri
x-yuri / go runtime sets its own signal handlers.md
Last active October 14, 2024 02:28
go runtime sets its own signal handlers

go runtime sets its own signal handlers

By default go sets its own [signal handlers][ae]:

  • runtime.mstart0() -> runtime.mstart1()
  • runtime.mstart1() -> [runtime.mstartm0()][ab]
  • runtime.mstartm0() -> [runtime.initsig()][ac]
  • runtime.initsig() -> [runtime.setsig(i, abi.FuncPCABIInternal(sighandler))][ad]