fmt_ip4(3)

πŸ“¦
version 0.34 Β· 2 versions
  • 0.34 Β· alpine 3.24, debian bookworm, debian trixie, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking (latest)
  • 0.30 Β· fedora 43, fedora 44, ubuntu jammy
, in alpine 3.24, debian bookworm, debian trixie, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

fmt_ip4 πŸ’…βœ¨

okay so bestie, we're in the Library Functions Manual today and the vibe is IPv4 addresses, so let's get into it πŸŽ€


✨ NAME ✨

fmt_ip4 β€” write a formatted ASCII representation of an IPv4 number πŸ¦‹


✨ SYNTAX ✨

#include <libowfat/ip4.h>

unsigned int fmt_ip4(char *dest,const char ip[4]);

like. that's it. that's the whole thing. iconic honestly πŸ‘‘


✨ DESCRIPTION ✨

so fmt_ip4 takes your IPv4 number in ip and formats it into the dotted-decimal ASCII representation β€” yk, the 1.2.3.4 look β€” and writes the result into dest. it then hands you back the number of bytes written. periodt πŸ’œ

now listen, there's a little trick here that's lowkey so useful: if dest equals FMT_LEN (i.e. it's zero), then fmt_ip4 doesn't write anything β€” it just returns the number of bytes it would have written. it's giving "lemme measure first, then commit" energy, and we love a girl who plans ahead πŸ™ˆ

important bestie: fmt_ip4 does not append \0. it does NOT null-terminate for you. so if you want a cute lil C string you gotta add that terminator yourself, no cap 😭

and for convenience (slay), ip4.h defines the integer IP4_FMT to be big enough to contain every possible fmt_ip4 output plus \0. so like, that's your buffer size, use it, don't be out here guessing 🌸


✨ EXAMPLE ✨

#include <libowfat/ip4.h>

  char buf[IP4_FMT];
  char ip[4];
  buf[fmt_ip4(buf,ip)]=0;

see how she puts the =0 right there at buf[fmt_ip4(buf,ip)]? that's the null terminator handoff, main character behavior fr fr πŸ’–


✨ SEE ALSO ✨


and that's the whole page bestie 🫢 this was the fmt_ip4(3) entry from the Library Functions Manual, signed off with love, sparkles, and a lil fmt_ip4(3) at the bottom for the girlies πŸ’…β­πŸ§š