"getting" the way in Linux

I am writing a c program in linux. Commands like execv () require a path as a string c. Is there a command that will return the current path in the form of a c-style string?

+3
source share
5 answers

getcwd () :

SYNTAX

#include <unistd.h>

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

DESCRIPTION

The getcwd () function should indicate the absolute path to the current working directory in the array pointed to by buf and return buf. The path copied to the array must not contain components that are symbolic links. The size argument is the size, in bytes, of the character array pointed to by the buf argument. If buf is a null pointer, the behavior of getcwd () is not specified.

RETURN VALUE

getcwd() buf. getcwd() errno . , buf, undefined....

+11

execv() - , , ( getcwd()) ( getenv ( "PATH" )).

, , system() C, exec() .

+6

ANSI C:

#include <unistd.h>

char path[MAXPATHLEN];
getcwd(path, MAXPATHLEN);
printf("pwd -> %s\n", path);
+2

, '.' './' . , , .

0

I am not a professional programmer, so this is not an exact answer.

What you need to do is grab the PWD environment variable (current working directory)

I'm not sure which library it is in, but this is the standard linux header.

I will look around and see if I can find him.

edit:

I thought getenv () would help if you also need to run system commands and need various bin paths located in PATH

-2
source

Source: https://habr.com/ru/post/1696705/


All Articles