In Linux, it is estimated that you often use the PWD command, which is to print the current working path, that isPRINTWOrkingDIrectroy, today we also come to the C language to implement this command.
To implement this function, you need to use the following system call:
# Include <unistd. h>
Char * getcwd (char * Buf, size_t size );
This system call returns the absolute path of the current working directory. The absolute path value is retained in the size Buf. If the buffer is too small, null is returned, and errno is set to erange, if Buf is null, the behavior is undefined. If the function is successfully called, the returned value is Buf. If the call fails, null is returned. For the cause of failure, see errno.
In addition, a function is also used:
# Include <string. h>
Char * strerror (INT errnum );
This function converts the error code errnum into an error description string and returns it.
The program is simple and the complete code is as follows:
Download: Pwd. c
- /* Pwd. C */
- # Include <unistd. h>
- # Include <stdio. h>
- # Include <string. h>
- # Include <errno. h>
- # Define buf_si Z 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 );
- }