How to get relative path of dir?

I`m implementing pwd builtin in mini shell.

How could I get relative path without using environment variable "PWD"

(getenv("PWD"))

Example:

CWD = /tmp

getcwd("/tmp", buff, size)

returns absolute location => /private/tmp.

How can I get "/tmp" in order to print in?

Upvotes: 0

Views: 364

Answers (1)

You can not do so.

When shell initialize, it stores current working directory to shell`s local variable. Shell gets cwd from getcwd() (if enviroment variable "PWD" is not set) and getenv("PWD") (if "PWD" is set) during the initialization.

Example: You are in /tmp (PWD=/tmp) (tmp = symbolic link)

CASE A

zsh

pwd

Result: /tmp

CASE B

env -i zsh

pwd

Result: /private/tmp

Upvotes: 0

Related Questions