/* $OpenBSD: linux_getcwd.c,v 1.2 2001/05/16 12:50:21 ho Exp $ */1/* $NetBSD: vfs_getcwd.c,v 1.3.2.3 1999/07/11 10:24:09 sommerfeld Exp $ */2/*-3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (c) 1999 The NetBSD Foundation, Inc.6* Copyright (c) 2015 The FreeBSD Foundation7* All rights reserved.8*9* This code is derived from software contributed to The NetBSD Foundation10* by Bill Sommerfeld.11*12* Portions of this software were developed by Edward Tomasz Napierala13* under sponsorship from the FreeBSD Foundation.14*15* Redistribution and use in source and binary forms, with or without16* modification, are permitted provided that the following conditions17* are met:18* 1. Redistributions of source code must retain the above copyright19* notice, this list of conditions and the following disclaimer.20* 2. Redistributions in binary form must reproduce the above copyright21* notice, this list of conditions and the following disclaimer in the22* documentation and/or other materials provided with the distribution.23*24* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS25* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED26* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR27* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS28* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR29* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF30* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN32* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)33* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE34* POSSIBILITY OF SUCH DAMAGE.35*/3637#include <sys/param.h>38#include <sys/malloc.h>39#include <sys/proc.h>40#include <sys/vnode.h>4142#ifdef COMPAT_LINUX3243#include <machine/../linux32/linux.h>44#include <machine/../linux32/linux32_proto.h>45#else46#include <machine/../linux/linux.h>47#include <machine/../linux/linux_proto.h>48#endif49#include <compat/linux/linux_misc.h>5051/*52* Find pathname of process's current directory.53*/54int55linux_getcwd(struct thread *td, struct linux_getcwd_args *uap)56{57char *buf, *retbuf;58size_t buflen;59int error;6061buflen = uap->bufsize;62if (__predict_false(buflen < 2))63return (ERANGE);64if (buflen > LINUX_PATH_MAX)65buflen = LINUX_PATH_MAX;6667buf = malloc(buflen, M_TEMP, M_WAITOK);68error = vn_getcwd(buf, &retbuf, &buflen);69if (error == ENOMEM)70error = ERANGE;71if (error == 0) {72error = copyout(retbuf, uap->buf, buflen);73if (error == 0)74td->td_retval[0] = buflen;75}76free(buf, M_TEMP);77return (error);78}798081