Rename printf -> kprintf in sys/ and add some defines where necessary
[dragonfly.git] / sys / emulation / linux / linux_getcwd.c
1 /* $FreeBSD: src/sys/compat/linux/linux_getcwd.c,v 1.2.2.3 2001/11/05 19:08:22 marcel Exp $ */
2 /* $DragonFly: src/sys/emulation/linux/linux_getcwd.c,v 1.23 2006/12/23 00:27:02 swildner Exp $ */
3 /* $OpenBSD: linux_getcwd.c,v 1.2 2001/05/16 12:50:21 ho Exp $ */
4 /* $NetBSD: vfs_getcwd.c,v 1.3.2.3 1999/07/11 10:24:09 sommerfeld Exp $ */
5
6 /*-
7  * Copyright (c) 1999 The NetBSD Foundation, Inc.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Bill Sommerfeld.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *        This product includes software developed by the NetBSD
24  *        Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41 #include "opt_compat.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysproto.h>
46 #include <sys/filedesc.h>
47 #include <sys/kernel.h>
48 #include <sys/file.h>
49 #include <sys/stat.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52 #include <sys/proc.h>
53 #include <sys/namei.h>
54 #include <sys/uio.h>
55 #include <sys/malloc.h>
56 #include <sys/dirent.h>
57 #include <sys/kern_syscall.h>
58 #include <vfs/ufs/dir.h>        /* XXX only for DIRBLKSIZ */
59
60 #include <arch_linux/linux.h>
61 #include <arch_linux/linux_proto.h>
62 #include "linux_util.h"
63
64 /*
65  * Find pathname of process's current directory.
66  *
67  * Use vfs vnode-to-name reverse cache; if that fails, fall back
68  * to reading directory contents.
69  */
70
71 int
72 sys_linux_getcwd(struct linux_getcwd_args *args)
73 {
74         int buflen;
75         int error;
76         char *buf;
77         char *bp;
78
79 #ifdef DEBUG
80         kprintf("Linux-emul(%d): getcwd(%p, %d)\n", 
81             (curthread->td_proc ? (int)curthread->td_proc->p_pid : -1),
82             args->buf, args->bufsize);
83 #endif
84         buflen = args->bufsize;
85         if (buflen < 2)
86                 return (EINVAL);
87         if (buflen > MAXPATHLEN)
88                 buflen = MAXPATHLEN;
89
90         buf = kmalloc(buflen, M_TEMP, M_WAITOK);
91         bp = kern_getcwd(buf, buflen, &error);
92         if (error == 0) {
93                 buflen = strlen(bp) + 1;
94                 error = copyout(bp, args->buf, buflen);
95                 args->sysmsg_result = buflen;
96         }
97         kfree(buf, M_TEMP);
98         return (error);
99 }
100