package main

import (
	"fmt"
	"os"
)

func init() {
	builtins["pwd"] = BuiltinPwd
}

func BuiltinPwd(command *CommandStatement, state *ExecutionState) ExitState {
	// ignore any arguments in command

	//fmt.Println("in pwd")
	//fmt.Printf("%v %v\n", os.Stdout, state.Descriptors[DESCRIPTOR_STDOUT])

	dir, err := os.Getwd()
	if err != nil {
		fmt.Fprintf(state.Descriptors[DESCRIPTOR_STDERR], "cwd: %s\n", err.Error())
		return ExitState{ExitCode: 255}
	}

	fmt.Fprintf(state.Descriptors[DESCRIPTOR_STDOUT], "%s\n", dir)
	return ExitState{ExitCode: 0}
}
