166492cf0SYuri Pankov.\" 266492cf0SYuri Pankov.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for 366492cf0SYuri Pankov.\" permission to reproduce portions of its copyrighted documentation. 466492cf0SYuri Pankov.\" Original documentation from The Open Group can be obtained online at 5c10c16deSRichard Lowe.\" http://www.opengroup.org/bookstore/. 666492cf0SYuri Pankov.\" 766492cf0SYuri Pankov.\" The Institute of Electrical and Electronics Engineers and The Open 866492cf0SYuri Pankov.\" Group, have given us permission to reprint portions of their 966492cf0SYuri Pankov.\" documentation. 1066492cf0SYuri Pankov.\" 1166492cf0SYuri Pankov.\" In the following statement, the phrase ``this text'' refers to portions 1266492cf0SYuri Pankov.\" of the system documentation. 1366492cf0SYuri Pankov.\" 1466492cf0SYuri Pankov.\" Portions of this text are reprinted and reproduced in electronic form 1566492cf0SYuri Pankov.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition, 1666492cf0SYuri Pankov.\" Standard for Information Technology -- Portable Operating System 1766492cf0SYuri Pankov.\" Interface (POSIX), The Open Group Base Specifications Issue 6, 1866492cf0SYuri Pankov.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics 1966492cf0SYuri Pankov.\" Engineers, Inc and The Open Group. In the event of any discrepancy 2066492cf0SYuri Pankov.\" between these versions and the original IEEE and The Open Group 2166492cf0SYuri Pankov.\" Standard, the original IEEE and The Open Group Standard is the referee 2266492cf0SYuri Pankov.\" document. The original Standard can be obtained online at 2366492cf0SYuri Pankov.\" http://www.opengroup.org/unix/online.html. 2466492cf0SYuri Pankov.\" 25c10c16deSRichard Lowe.\" This notice shall appear on any product containing this material. 2666492cf0SYuri Pankov.\" 2766492cf0SYuri Pankov.\" The contents of this file are subject to the terms of the 2866492cf0SYuri Pankov.\" Common Development and Distribution License (the "License"). 2966492cf0SYuri Pankov.\" You may not use this file except in compliance with the License. 3066492cf0SYuri Pankov.\" 3166492cf0SYuri Pankov.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 3266492cf0SYuri Pankov.\" or http://www.opensolaris.org/os/licensing. 3366492cf0SYuri Pankov.\" See the License for the specific language governing permissions 3466492cf0SYuri Pankov.\" and limitations under the License. 3566492cf0SYuri Pankov.\" 3666492cf0SYuri Pankov.\" When distributing Covered Code, include this CDDL HEADER in each 3766492cf0SYuri Pankov.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE. 3866492cf0SYuri Pankov.\" If applicable, add the following below this CDDL HEADER, with the 3966492cf0SYuri Pankov.\" fields enclosed by brackets "[]" replaced with your own identifying 4066492cf0SYuri Pankov.\" information: Portions Copyright [yyyy] [name of copyright owner] 4166492cf0SYuri Pankov.\" 4266492cf0SYuri Pankov.\" 4366492cf0SYuri Pankov.\" Copyright 1989 AT&T 4466492cf0SYuri Pankov.\" Copyright (c) 2001, The IEEE and The Open Group. All Rights Reserved. 4566492cf0SYuri Pankov.\" Portions Copyright (c) 2004, Sun Microsystems, Inc. All Rights Reserved. 46aa153721SAndy Fiddaman.\" Copyright 2021 OmniOS Community Edition (OmniOSce) Association. 4766492cf0SYuri Pankov.\" 48aa153721SAndy Fiddaman.Dd February 27, 2021 49aa153721SAndy Fiddaman.Dt GETCWD 3C 50aa153721SAndy Fiddaman.Os 51aa153721SAndy Fiddaman.Sh NAME 52aa153721SAndy Fiddaman.Nm getcwd 53aa153721SAndy Fiddaman.Nd get pathname of current working directory 54aa153721SAndy Fiddaman.Sh SYNOPSIS 55aa153721SAndy Fiddaman.In unistd.h 56aa153721SAndy Fiddaman.Ft "char *" 57aa153721SAndy Fiddaman.Fo getcwd 58aa153721SAndy Fiddaman.Fa "char *buf" 59aa153721SAndy Fiddaman.Fa "size_t size" 60aa153721SAndy Fiddaman.Fc 61aa153721SAndy Fiddaman.Sh DESCRIPTION 62aa153721SAndy FiddamanThe 63aa153721SAndy Fiddaman.Fn getcwd 64aa153721SAndy Fiddamanfunction returns a pointer to a buffer containing the absolute pathname of the 65aa153721SAndy Fiddamancurrent working directory. 66aa153721SAndy FiddamanThe returned pathname contains no components that are symbolic links. 67aa153721SAndy Fiddaman.Pp 68aa153721SAndy FiddamanWhen 69aa153721SAndy Fiddaman.Fa buf 70aa153721SAndy Fiddamanis not 71aa153721SAndy Fiddaman.Dv NULL , 72aa153721SAndy Fiddamanthe absolute pathname will be written into 73aa153721SAndy Fiddaman.Fa buf 74aa153721SAndy Fiddamanand 75aa153721SAndy Fiddaman.Fa size 76aa153721SAndy Fiddamanrepresents the length in bytes of 77aa153721SAndy Fiddaman.Fa buf . 78aa153721SAndy FiddamanIf the length of the pathname and nul terminator exceeds 79aa153721SAndy Fiddaman.Fa size , 80aa153721SAndy Fiddamannothing will be written and 81aa153721SAndy Fiddaman.Fn getcwd 82aa153721SAndy Fiddamanwill return 83aa153721SAndy Fiddaman.Dv NULL . 84aa153721SAndy FiddamanOtherwise, 85aa153721SAndy Fiddaman.Fn getcwd 86aa153721SAndy Fiddamanreturns 87aa153721SAndy Fiddaman.Fa buf . 88aa153721SAndy Fiddaman.Pp 89aa153721SAndy FiddamanWhen 90aa153721SAndy Fiddaman.Fa buf 91aa153721SAndy Fiddamanis 92aa153721SAndy Fiddaman.Dv NULL 93aa153721SAndy Fiddamanthen 94aa153721SAndy Fiddaman.Fn getcwd 95aa153721SAndy Fiddamanwill allocate memory in which to store the pathname of the current working 96c10c16deSRichard Lowedirectory. 97aa153721SAndy FiddamanIf 98aa153721SAndy Fiddaman.Fa size 99aa153721SAndy Fiddamanis non-zero, then 100aa153721SAndy Fiddaman.Fa size 101aa153721SAndy Fiddamanbytes will be allocated. 102aa153721SAndy FiddamanIf the length of the pathname and nul terminator exceeds 103aa153721SAndy Fiddaman.Fa size , 104aa153721SAndy Fiddamanthe memory will be freed and 105aa153721SAndy Fiddaman.Fn getcwd 106aa153721SAndy Fiddamanwill return 107aa153721SAndy Fiddaman.Dv NULL . 108aa153721SAndy FiddamanIf 109aa153721SAndy Fiddaman.Fa size 110aa153721SAndy Fiddamanis zero, then 111aa153721SAndy Fiddaman.Fn getcwd 112aa153721SAndy Fiddamanwill attempt to allocate enough space to hold the pathname. 113aa153721SAndy FiddamanIn both cases, it is the caller's responsibility to free the returned buffer 114aa153721SAndy Fiddamanwith the 115aa153721SAndy Fiddaman.Xr free 3C 116aa153721SAndy Fiddamanfunction. 117aa153721SAndy Fiddaman.Sh RETURN VALUES 118aa153721SAndy FiddamanUpon successful completion, the 119aa153721SAndy Fiddaman.Fn getcwd 120aa153721SAndy Fiddamanfunction returns a pointer to a buffer containing the pathname. 121aa153721SAndy FiddamanOtherwise, 122aa153721SAndy Fiddaman.Dv NULL 123aa153721SAndy Fiddamanis returned and 124aa153721SAndy Fiddaman.Va errno 125aa153721SAndy Fiddamanis set to indicate the error. 126aa153721SAndy Fiddaman.Sh EXAMPLES 127aa153721SAndy Fiddaman.Sy Example 1 128aa153721SAndy FiddamanDetermine the absolute pathname of the current working directory. 129aa153721SAndy Fiddaman.Pp 130c10c16deSRichard LoweThe following example returns a pointer to an array that holds the absolute 131aa153721SAndy Fiddamanpathname of the current working directory. 132aa153721SAndy FiddamanThe pointer is returned in the 133aa153721SAndy Fiddaman.Va ptr 134aa153721SAndy Fiddamanvariable, which points to the 135aa153721SAndy Fiddaman.Va buf 136aa153721SAndy Fiddamanarray where the pathname is stored. 137aa153721SAndy Fiddaman.Bd -literal -offset Ds 138c10c16deSRichard Lowe#include <stdlib.h> 139c10c16deSRichard Lowe#include <unistd.h> 140c10c16deSRichard Lowe\&... 141c10c16deSRichard Lowelong size; 142c10c16deSRichard Lowechar *buf; 143c10c16deSRichard Lowechar *ptr; 144c10c16deSRichard Lowesize = pathconf(".", _PC_PATH_MAX); 145c10c16deSRichard Loweif ((buf = (char *)malloc((size_t)size)) != NULL) 146c10c16deSRichard Lowe ptr = getcwd(buf, (size_t)size); 147c10c16deSRichard Lowe\&... 148aa153721SAndy Fiddaman.Ed 149aa153721SAndy Fiddaman.Pp 150aa153721SAndy Fiddaman.Sy Example 2 151aa153721SAndy FiddamanPrint the current working directory. 152aa153721SAndy Fiddaman.Pp 153c10c16deSRichard LoweThe following example prints the current working directory. 154aa153721SAndy Fiddaman.Bd -literal -offset Ds 155c10c16deSRichard Lowe#include <stdio.h> 156aa153721SAndy Fiddaman#include <unistd.h> 157c10c16deSRichard Lowe 158aa153721SAndy Fiddamanint 159aa153721SAndy Fiddamanmain(void) 160c10c16deSRichard Lowe{ 161c10c16deSRichard Lowe char *cwd; 162aa153721SAndy Fiddaman 163aa153721SAndy Fiddaman if ((cwd = getcwd(NULL, 0)) == NULL) { 164c10c16deSRichard Lowe perror("pwd"); 165c10c16deSRichard Lowe exit(2); 166c10c16deSRichard Lowe } 167c10c16deSRichard Lowe (void)printf("%s\en", cwd); 168c10c16deSRichard Lowe free(cwd); /* free memory allocated by getcwd() */ 169c10c16deSRichard Lowe return(0); 170c10c16deSRichard Lowe} 171aa153721SAndy Fiddaman.Ed 1726edc7986SAndy Fiddaman.Sh ERRORS 1736edc7986SAndy FiddamanThe 1746edc7986SAndy Fiddaman.Fn getcwd 1756edc7986SAndy Fiddamanfunction will fail if: 1766edc7986SAndy Fiddaman.Bl -tag -width Er 1776edc7986SAndy Fiddaman.It Er EFAULT 1786edc7986SAndy FiddamanThe 1796edc7986SAndy Fiddaman.Fa buf 1806edc7986SAndy Fiddamanargument points to an invalid address. 1816edc7986SAndy Fiddaman.It Er EINVAL 1826edc7986SAndy FiddamanThe 1836edc7986SAndy Fiddaman.Fa buf 1846edc7986SAndy Fiddamanargument is not 1856edc7986SAndy Fiddaman.Dv NULL 1866edc7986SAndy Fiddamanand the 1876edc7986SAndy Fiddaman.Fa size 1886edc7986SAndy Fiddamanargument is 0. 1896edc7986SAndy Fiddaman.It Er ERANGE 1906edc7986SAndy FiddamanThe pathname 1916edc7986SAndy Fiddaman.Pq including its terminating nul character 1926edc7986SAndy Fiddamanis too long to fit into the provided 1936edc7986SAndy Fiddaman.Pq or allocated 1946edc7986SAndy Fiddamanbuffer. 1956edc7986SAndy Fiddaman.It Er EACCESS 1966edc7986SAndy FiddamanA parent directory cannot be read to get its name. 1976edc7986SAndy Fiddaman.It Er ENOMEM 1986edc7986SAndy FiddamanInsufficient storage space is available. 1996edc7986SAndy Fiddaman.El 200aa153721SAndy Fiddaman.Sh USAGE 201aa153721SAndy FiddamanApplications should exercise care when using 202aa153721SAndy Fiddaman.Xr chdir 2 203aa153721SAndy Fiddamanin conjunction with 204aa153721SAndy Fiddaman.Fn getcwd . 205aa153721SAndy FiddamanThe current working directory is global to all threads within a process. 206aa153721SAndy FiddamanIf more than one thread calls 207aa153721SAndy Fiddaman.Xr chdir 2 208aa153721SAndy Fiddamanto change the working directory, a subsequent call to 209aa153721SAndy Fiddaman.Fn getcwd 210aa153721SAndy Fiddamancould produce unexpected results. 211aa153721SAndy Fiddaman.Sh INTERFACE STABILITY 212aa153721SAndy Fiddaman.Sy Committed 213aa153721SAndy Fiddaman.Sh SEE ALSO 214aa153721SAndy Fiddaman.Xr chdir 2 , 215aa153721SAndy Fiddaman.Xr free 3C , 216*bbf21555SRichard Lowe.Xr attributes 7 , 217*bbf21555SRichard Lowe.Xr standards 7 218