Linux under pwd command __linux

Source: Internet
Author: User

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); }

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.