HomeLinuxGetcwd() Perform in C Language

Getcwd() Perform in C Language


Establishing a working listing ranging from the Linux root listing opens up the potential of accessing and dealing with your entire file construction within the varied system and consumer folders.

The C language gives the chdir() operate to arrange a working listing and likewise has a operate to question the listing that you’re working in.

On this Linux Trace article, you’ll discover ways to use the getcwd() operate to question the listing during which you might be at present working. We’ll clarify the theoretical workings of this operate, its syntax, the varieties of its enter and output arguments, and every kind of information.

To make use of this operate in a sensible method, we’ll apply every thing we discovered via a sensible instance the place you be taught step-by-step on easy methods to use this operate to retrieve the present working listing.

Syntax of the Getcwd() Perform in C Language

char *getcwd ( char *buf, size_t dimension );

Description of the Getcwd() Perform in C Language

The getcwd() operate queries the present working listing of the calling program or course of. This operate returns the pointer to a “buf of dimension” character array which incorporates a string with absolutely the path of the present working listing.

If the decision to the getcwd() operate is profitable, it returns the pointer to the string with the trail of the working listing. If an error happens, the result’s a null pointer. On this case, the error identification code is mechanically saved within the errno international variable.

Each the declaration of the errno variable and the definitions of the error codes are within the “errno.h” header. Within the following instance, we’ll see a particular part which explains easy methods to detect and establish the errors.

The getcwd() operate is outlined within the “unistd.h” header. To make use of it, we have now to incorporate it in our “.c” file as follows:

How you can Question the Present Working Listing with the Getcwd() Perform within the C Language

On this instance, we’ll clarify easy methods to retrieve the present working listing and show it within the command console.

Step one to do that is to incorporate the mandatory headers. On this case, they’re the “unistd.h” and “stdio.h” recordsdata. Then, we open a fundamental() operate which returns an empty return worth and during which we declare an array path with 128 components of kind char. On this array, the getcwd() operate shops the string with the listing.

As soon as the mandatory variables are declared, we name the getcwd() operate. We move the “buf” enter argument because the pointer to the array path and the scale of the buffer as argument dimension, as 128 on this case.

Then, utilizing the printf() operate, we print the “The precise listing is:” message adopted by the listing path that’s returned by the getcwd() operate. Right here is the entire code for this instance:

#embody <stdio.h>

#embody <unistd.h>

void fundamental ()
  {
char path [128];
getcwd (path, 128);
printf (nnThe precise listing is: %sn,  path);

  }

Subsequent, we see a picture with the compilation and execution of this code. On this case, getcwd() returns absolutely the path of the consumer’s residence listing in session:

The identical pointer that getcwd() returns within the “buf” argument returns the next outcome:

pointer to buff = getcwd ( pointer to buff, dimension );

This permits the getcwd() operate to be referred to as from the enter arguments of one other operate. The next code reveals the decision from the enter arguments of printf():

#embody <stdio.h>

#embody <unistd.h>

void fundamental ()
  {
char path [128];
printf (nnThe precise listing is: %sn,  getcwd (path, 128));

  }

On this case, printf() takes the pointer to the string that’s returned by getcwd() and prints it to the command console with just one line of code.

The next picture reveals that this technique has the identical impact as this system within the earlier instance:

Errors When Utilizing the Getcwd() Perform: How you can Detect and Establish Them

If an error happens when calling the getcwd() operate, it returns a null pointer because of this. The best technique to detect an error is to make use of an “if” situation the place the enter situation is set by a outcome that is the same as NULL. The next is a code snippet that makes use of this technique for error detection:

if (getcwd( path, 128) == NULL)

 {

printf( “An error occurred within the getcwd() operate name.n );

 }

When an error happens, a numeric code that identifies it’s mechanically saved within the errno international variable which is said within the “errno.h” header.

This header additionally defines the error codes and their numeric illustration.

You’ll be able to see the errors that the getcwd() operate can generate, their numeric illustration which is returned within the errno variable, and a quick description of every one:

Definition Worth in errno Description
ENOENT 2 No such file or listing.
EACCES 13 Permission denied
EFAULT 14 Unhealthy handle.
EINVAL 22 Invalid argument.
ENAMETOOLONG 36 File title or path too lengthy.

The best technique to establish an error is to open a conditional change the place the bounce situation is the errno international variable and every case is an error definition. We see the code for this detection technique within the following:

if (getcwd( path, 128) == NULL)
   {
change ( errno ){

          case  EINVAL:{
printf ( “Invalid argument.n );
break;
          case  EFAULT:{
printf ( “Unhealthy handle.n );
break;
          case  ENOENT:{
printf ( “No such file or listing.n );
break;
         }

   }

The variables and error codes are outlined within the “errno.h” header. To make use of them, we should embody them in our “.c” file as follows:

 

 

Conclusion

On this Linux Trace article, we defined easy methods to use the getcwd() operate to find out the present working listing of the calling program.

We checked out a theoretical part about this operate, what its enter and output arguments are, and what sort of knowledge it makes use of. Then, we utilized what we discovered via the sensible examples with photographs and code snippets that present easy methods to declare the mandatory variables and name the getcwd() operate.

We additionally included a particular part the place we record the completely different errors that this operate can generate and defined easy methods to detect them.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments