xref: /freebsd/sys/compat/linux/linux_getcwd.c (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
1 /* $OpenBSD: linux_getcwd.c,v 1.2 2001/05/16 12:50:21 ho Exp $ */
2 /* $NetBSD: vfs_getcwd.c,v 1.3.2.3 1999/07/11 10:24:09 sommerfeld Exp $ */
3 /*-
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * Copyright (c) 2015 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Bill Sommerfeld.
10  *
11  * Portions of this software were developed by Edward Tomasz Napierala
12  * under sponsorship from the FreeBSD Foundation.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include "opt_compat.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/proc.h>
45 #include <sys/malloc.h>
46 
47 #ifdef COMPAT_LINUX32
48 #include <machine/../linux32/linux.h>
49 #include <machine/../linux32/linux32_proto.h>
50 #else
51 #include <machine/../linux/linux.h>
52 #include <machine/../linux/linux_proto.h>
53 #endif
54 #include <compat/linux/linux_misc.h>
55 #include <compat/linux/linux_util.h>
56 
57 /*
58  * Find pathname of process's current directory.
59  */
60 int
61 linux_getcwd(struct thread *td, struct linux_getcwd_args *args)
62 {
63 	char *path;
64 	int error, lenused;
65 
66 #ifdef DEBUG
67 	if (ldebug(getcwd))
68 		printf(ARGS(getcwd, "%p, %ld"), args->buf, (long)args->bufsize);
69 #endif
70 
71 	/*
72 	 * Linux returns ERANGE instead of EINVAL.
73 	 */
74 	if (args->bufsize < 2)
75 		return (ERANGE);
76 
77 	path = malloc(LINUX_PATH_MAX, M_LINUX, M_WAITOK);
78 
79 	error = kern___getcwd(td, path, UIO_SYSSPACE, args->bufsize,
80 	    LINUX_PATH_MAX);
81 	if (error == 0) {
82 		lenused = strlen(path) + 1;
83 		error = copyout(path, args->buf, lenused);
84 		if (error == 0)
85 			td->td_retval[0] = lenused;
86 	}
87 
88 	free(path, M_LINUX);
89 	return (error);
90 }
91