Linux PWD command

Source: Internet
Author: User

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
  1. /* Pwd. C */
  2. # Include <unistd. h>
  3. # Include <stdio. h>
  4. # Include <string. h>
  5. # Include <errno. h>
  6. # Define buf_si Z 2048
  7. Int main ()
  8. {
  9. Char Buf [buf_siz];
  10. If (getcwd (BUF, buf_siz ))
  11. Printf ("% s/n", Buf );
  12. Else
  13. Fprintf (stderr, "error occured: % s", strerror (errno ));
  14. Exit (0 );
  15. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.