CHDIR(3POSIX)

πŸ’– CHDIR(3POSIX) πŸ’–

okay bestie, we're doing the POSIX Programmer's Manual today. This page lives in the POSIX Programmer's Manual, which is a whole vibe of its own β€” and heads up, the Linux implementation of this interface may differ (so go peek at the corresponding Linux manual page for the tea on Linux behavior), or honestly the interface might not even be implemented on Linux at all. πŸ™ˆ


✨ PROLOG ✨

This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β€” consult the corresponding Linux manual page for details on Linux behavior β€” or the interface may not be implemented on Linux. No cap.

🌸 NAME 🌸

chdir β€” change working directory. That's it. That's the whole job. πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

#include <unistd.h>

int chdir(const char *path);

πŸ¦‹ DESCRIPTION πŸ¦‹

So basically, the chdir() function shall cause the directory named by the pathname pointed to by the path argument to become the current working directory. Which is a fancy way of saying: it becomes the starting point for path searches for pathnames that don't begin with '/'. Like, if you're lost and you need a home base? This is how you get one. 🏠✨


⭐ RETURN VALUE ⭐

Upon successful completion, 0 shall be returned. Slay.

Otherwise, -1 shall be returned, the current working directory shall remain unchanged (it's not moving, bestie), and errno shall be set to indicate the error.


πŸ’€ ERRORS πŸ’€

The chdir() function shall fail if:

  • EACCES β€” search permission is denied for any component of the pathname. Rude, honestly.
  • ELOOP β€” a loop exists in symbolic links encountered during resolution of the path argument. πŸŒ€
  • ENAMETOOLONG β€” the length of a component of a pathname is longer than {NAME_MAX}.
  • ENOENT β€” a component of path does not name an existing directory, or path is an empty string. Nothing there, babes. πŸ«₯
  • ENOTDIR β€” a component of the pathname names an existing file that is neither a directory nor a symbolic link to a directory.

The chdir() function may fail if:

  • ELOOP β€” more than {SYMLOOP_MAX} symbolic links were encountered during resolution of the path argument.
  • ENAMETOOLONG β€” the length of a pathname exceeds {PATH_MAX}, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds {PATH_MAX}.
The following sections are informative. (i.e. the lore, not the law πŸ“œ)

πŸ“ EXAMPLES πŸ“

Changing the Current Working Directory

The following example makes the value pointed to by directory, /tmp, the current working directory. Iconic behavior, truly.

#include <unistd.h>
...
char *directory = "/tmp";
int ret;

ret = chdir (directory);

🌈 APPLICATION USAGE 🌈

None. Literally nothing. We're chill like that. ✌️


🧚 RATIONALE 🧚

The chdir() function only affects the working directory of the current process. So don't be out here thinking you're changing everyone's directory β€” you're not. It's giving "personal bubble". 🫧


πŸ”₯ FUTURE DIRECTIONS πŸ”₯

None. The future is a blank canvas and chdir is not on it. 🎨


πŸ‘‘ SEE ALSO πŸ‘‘

  • getcwd()
  • The Base Definitions volume of POSIX.1‐2017, <unistd.h>

Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


okay that's the whole page, bestie β€” massive thanks to IEEE/The Open Group, dated 2017, who brought us CHDIR(3POSIX) in the first place. mwah. πŸ’‹