CHDIR(2)

πŸ’– CHDIR(2) β€” System Calls Manual πŸ’–

okay bestie, welcome to CHDIR(2), the chdir and fchdir system calls, straight outta the System Calls Manual. this is the page that tells you how to move your whole vibe to a different directory. sit down, hydrate, let's go 🫢


✨ NAME ✨

chdir, fchdir β€” change current working directory

πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc)

🌸 SYNOPSIS 🌸

#include <unistd.h>

int
chdir(const char *path);

int
fchdir(int fd);

πŸ’… DESCRIPTION πŸ’…

so the path argument points to the pathname of a directory. the chdir() system call causes the named directory to become the current working directory β€” that is, the starting point for path searches of pathnames not beginning with a slash, /. like, it's your new home base for everything relative. no cap. 🏠✨

the fchdir() system call causes the directory referenced by fd to become the current working directory β€” the starting point for path searches of pathnames not beginning with a slash, /. same energy, but you're coming in through a file descriptor instead of a string. iconic.

and in order for a directory to become the current directory, a process must have execute (search) access to the directory. no permission, no entry. that's the vibe check, and you did not pass it. πŸ’€

πŸŽ€ RETURN VALUES πŸŽ€

upon successful completion, the value 0 is returned ✨ otherwise the value -1 is returned and the global variable errno is set to indicate the error. so, like, 0 = slay, -1 = check errno, bestie.

πŸ‘‘ ERRORS πŸ‘‘

the chdir() system call will fail and the current working directory will be unchanged if one or more of the following are true:

  • [ENOTDIR] β€” a component of the path prefix is not a directory. rude.
  • [ENAMETOOLONG] β€” a component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters. it's giving excessive, honestly.
  • [ENOENT] β€” the named directory does not exist. she's not there. never was. 🫠
  • [ELOOP] β€” too many symbolic links were encountered in translating the pathname. the symlinks are linking the symlinks, it's a whole situation.
  • [EACCES] β€” search permission is denied for any component of the path name.
  • [EFAULT] β€” the path argument points outside the process's allocated address space. out of bounds, literally.
  • [EIO] β€” an I/O error occurred while reading from or writing to the file system.
  • [EINTEGRITY] β€” corrupted data was detected while reading from the file system. the data is not okay. 😭

the fchdir() system call will fail and the current working directory will be unchanged if one or more of the following are true:

  • [EACCES] β€” search permission is denied for the directory referenced by the file descriptor.
  • [ENOTDIR] β€” the file descriptor does not reference a directory.
  • [EBADF] β€” the argument fd is not a valid file descriptor.
note the pattern: if it fails, your cwd stays exactly where it was. no half-moves, no chaos. very demure, very mindful.

🌈 SEE ALSO 🌈

chroot(2)

⭐ STANDARDS ⭐

the chdir() system call is expected to conform to ISO/IEC 9945-1:1990 (β€œPOSIX.1”). periodt.

πŸ¦‹ HISTORY πŸ¦‹

the chdir() system call appeared in Version 1 AT&T UNIX. the fchdir() system call appeared in 4.2BSD. ancient icons, both of them. πŸ’œ


and that's the whole page, bestie! this beauty came to you from Debian, dated March 30, 2020 β€” go change directories responsibly, mwah πŸ’…βœ¨

πŸ“¦ where this page comes from