GETHOSTNAME(3)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1

πŸ’… GETHOSTNAME(3) β€” the get/set name of current host era

okay bestie so this is gethostname(3), straight out of the Library Functions Manual, and it's literally the two little functions that tell you your own computer's name and (if you're built different) change it. Main character energy for your machine, fr fr πŸŽ€


✨ NAME ✨

gethostname, sethostname β€” get/set name of current host


πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc)


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <unistd.h>

int
gethostname(char *name, size_t namelen);

int
sethostname(const char *name, int namelen);

πŸ’– DESCRIPTION πŸ’–

So gethostname() hands you back the standard host name of the current processor, like whatever sethostname() set it to before. The namelen arg is the size of the name array, bestie. And listen β€” the name that comes back is null-terminated, unless there wasn't enough space. πŸ’€

Then there's sethostname(), which sets your host machine's name to name, which has length namelen. But heads up: this call is restricted to the super-user only πŸ‘‘, and it's normally only used when the system is bootstrapped. Like, this is not a casual little vibe-check-and-rename situation, iykyk.

Applications should use sysconf(_SC_HOST_NAME_MAX) to find the maximum length of a host name (not including the terminating null). That's the move. ✨


🌈 RETURN VALUES 🌈

Slay, we love a clean success: on successful completion, the value 0 is returned. Otherwise the value -1 comes back and the global variable errno gets set to indicate the error. 😭


πŸ”₯ ERRORS πŸ”₯

Here are the errors these calls may throw at you:

  • [EFAULT] β€” the name or namelen argument gave an invalid address. not the vibe πŸ™ˆ
  • [ENAMETOOLONG] β€” the current host name is longer than namelen. (For gethostname() only.)
  • [EPERM] β€” the caller tried to set the host name and was not the super-user. bestie, you are not that girl πŸ’…

πŸ‘€ SEE ALSO πŸ‘€

sysconf(3), sysctl(3)


⭐ STANDARDS ⭐

The gethostname() function conforms to IEEE Std 1003.1-2001 (β€œPOSIX.1”). Callers should be aware that {HOST_NAME_MAX} may be variable or infinite, but is guaranteed to be no less than {_POSIX_HOST_NAME_MAX}. πŸ‘‘

On older systems, this limit was defined in the non-standard header <sys/param.h> as MAXHOSTNAMELEN, and it counted the terminating null. The sethostname() function and the error returns for gethostname() are not standardized. so like, don't count on those two being super consistent, ngl 🫢


🧚 HISTORY 🧚

The gethostname() function appeared in 4.2BSD. The namelen argument to gethostname() was changed to size_t in FreeBSD 5.2 for alignment with IEEE Std 1003.1-2001 (β€œPOSIX.1”). glow-up fr fr ✨


and that's the whole page, bestie πŸ“ this one's from the Debian crew, dated July 13, 2020 β€” mwah, tysm for reading, ily, go check sysconf(_SC_HOST_NAME_MAX) πŸ’œ </parameter>

πŸ“¦ where this page comes from
  • freebsd 15.1: base 15.1
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms