logo

cmd-urlencode

percent-encode stdin for making URLs git clone https://anongit.hacktivis.me/git/cmd-urlencode.git
commit: d10ec7c13b7f2024b69e38c08c267762bf4a701e
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue, 10 Mar 2026 06:34:00 +0100

init

Diffstat:

A.gitignore5+++++
ALICENSES/MIT.txt18++++++++++++++++++
AMakefile10++++++++++
Aurlencode.123+++++++++++++++++++++++
Aurlencode.c57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 113 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,5 @@ +# urlencode: percent-encode stdin for making URLs +# Copyright © 2026 Haelwenn (lanodan) Monnier +# SPDX-License-Identifier: MIT + +/urlencode diff --git a/LICENSES/MIT.txt b/LICENSES/MIT.txt @@ -0,0 +1,18 @@ +MIT License + +Copyright (c) <year> <copyright holders> + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +associated documentation files (the "Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the +following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial +portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT +LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO +EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile @@ -0,0 +1,10 @@ +# urlencode: percent-encode stdin for making URLs +# Copyright © 2026 Haelwenn (lanodan) Monnier +# SPDX-License-Identifier: MIT + +urlencode: urlencode.c Makefile + $(CC) -std=c99 $(CPPFLAGS) $(CFLAGS) -o urlencode urlencode.c $(LDFLAGS) $(LDSTATIC) + +PHONY: clean +clean: + rm urlencode diff --git a/urlencode.1 b/urlencode.1 @@ -0,0 +1,23 @@ +.\" urlencode: percent-encode stdin for making URLs +.\" Copyright © 2026 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +.\" SPDX-License-Identifier: MIT +.Dd March 10, 2026 +.Dt URLENCODE 1 +.Os +.Sh NAME +.Nm urlencode +.Nd percent-encode stdin for making URLs +.Sh SYNOPSIS +.Nm +.Sh DESCRIPTION +.Nm +reads bytes from standard input, prints it as-is if unreserved, +and otherwise percent encodes it. +.Sh EXIT STATUS +.Ex -std +.Sh SEE ALSO +.Xr trurl 1 +.Sh STANDARDS +STD66, RFC3986\ -\ "Uniform Resource Identifier (URI): Generic Syntax" +.Sh AUTHORS +.An Haelwenn (lanodan) Monnier Aq Mt contact+utils@hacktivis.me diff --git a/urlencode.c b/urlencode.c @@ -0,0 +1,57 @@ +// urlencode: percent-encode stdin for making URLs +// Copyright © 2026 Haelwenn (lanodan) Monnier +// SPDX-License-Identifier: MIT +#define _POSIX_C_SOURCE 200809L +#include <errno.h> +#include <stdint.h> +#include <stdio.h> +#include <unistd.h> + +int +main(void) +{ + for(;;) + { + static char buf[512]; + ssize_t got = read(STDIN_FILENO, buf, 512); + if(got == 0) break; + if(got < 0) + { + if(errno == EAGAIN || errno == EINTR) + continue; + + perror("urlencode: error: Failed reading from stdin"); + return 1; + } + + for(ssize_t i = 0; i < got; i++) + { + register uint8_t c = buf[i] & 0xFF; + + /* A-Za-z0-9[\-._~] */ + if( + (c >= 'A' && c <= 'Z') || + (c >= 'a' && c <= 'z') || + (c >= '0' && c <= '9') || + c == '-' || c == '.' || c == '_' || c == '~' || + c == '\n' + ) + { + fputc(c, stdout); + + if(c == '\n') fflush(stdout); + continue; + } + + if(c == '\r') continue; + + fprintf(stdout, "%%%02X", c); + } + + fflush(stdout); + } + + fflush(stdout); + + return 0; +}