GETHOSTNAME(3)
π 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]β thenameornamelenargument gave an invalid address. not the vibe π[ENAMETOOLONG]β the current host name is longer thannamelen. (Forgethostname()only.)[EPERM]β the caller tried to set the host name and was not the super-user. bestie, you are not that girl π
π SEE ALSO π
β 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
- Copyright (c) 1983, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms