Under Linux, it is estimated that you often use pwd this command, which is to print the current work path, that is, print working DIrectroy, today we also come to the C language to implement this command.
To implement this functionality, you need to use one of the following system calls: #include < unistd.h >
char * GETCWD (char * buf, size_t size);
The system call returns the absolute path of the current working directory, the value of the absolute path remains in the size of buf, if the buffer is too small, NULL is returned, and errno is set to Erange, and if buf equals null, the behavior is undefined. If the function call succeeds, the return value is buf, and if the call fails, NULL is returned and the reason for the failure can be viewed errno.
In addition, you need to use a function: #include < string.h >
char * strerror (int errnum);
The function bar error code errnum into the error description string and returns.
The program is simple, complete code as follows: Download: PWD.C/* PWD.C/#include <unistd.h> #include <stdio.h> #include <string.h> #inclu De <errno.h> #define BUF_SIZ 2048 int main () { char buf[buf_siz]; if (GETCWD (buf, Buf_siz)) printf ("%s/n", buf); Else fprintf (stderr, "Error occured:%s", Strerror (errno)); exit (0); }