Get the current working directory

From CodeCodex

Revision as of 14:14, 7 December 2010 by 211.2.129.92 (Talk)

Implementations

C: POSIX

#include <unistd.h>
#include <stdio.h>
#define BUF_SIZE 40

int main() {
    char buf[BUF_SIZE];
    char *curDir = getcwd(buf, BUF_SIZE);
    if (curDir)
        puts(curDir);
    return 0;
}

Erlang

Cwd = file:get_cwd().

Haskell

import System.Directory(getCurrentDirectory)
main = putStrLn =<< getCurrentDirectory

Java

The working directory is the location in the file system from where the java command was invoked.

String curDir = System.getProperty("user.dir");

Objective-C

NSString *curDir = [[NSFileManager defaultManager] currentDirectoryPath];

OCaml

let curDir = Sys.getcwd ()

Perl

use Cwd;
my $curDir = getcwd;

PHP

$curDir = getcwd();

Python

import os
curDir = os.getcwd()

Ruby

curDir = Dir.pwd

Zsh

print $PWD