xref: /openbsd/lib/libc/stdlib/realpath.c (revision 170ca6cd)
1*170ca6cdSguenther /*	$OpenBSD: realpath.c,v 1.28 2023/05/18 16:11:10 guenther Exp $ */
21f4d754aSderaadt /*
31f4d754aSderaadt  * Copyright (c) 2019 Bob Beck <beck@openbsd.org>
4390a7db7Sderaadt  * Copyright (c) 2019 Theo de Raadt <deraadt@openbsd.org>
51f4d754aSderaadt  *
61f4d754aSderaadt  * Permission to use, copy, modify, and/or distribute this software for any
71f4d754aSderaadt  * purpose with or without fee is hereby granted, provided that the above
81f4d754aSderaadt  * copyright notice and this permission notice appear in all copies.
91f4d754aSderaadt  *
101f4d754aSderaadt  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
111f4d754aSderaadt  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
121f4d754aSderaadt  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
131f4d754aSderaadt  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
141f4d754aSderaadt  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
151f4d754aSderaadt  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
161f4d754aSderaadt  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
171f4d754aSderaadt  */
181f4d754aSderaadt 
19390a7db7Sderaadt #include <errno.h>
20390a7db7Sderaadt #include <stdlib.h>
21390a7db7Sderaadt #include <string.h>
22390a7db7Sderaadt #include <unistd.h>
23390a7db7Sderaadt #include <limits.h>
24390a7db7Sderaadt #include <syslog.h>
25390a7db7Sderaadt #include <stdarg.h>
26390a7db7Sderaadt 
271f4d754aSderaadt /*
281f4d754aSderaadt  * wrapper for kernel __realpath
291f4d754aSderaadt  */
301f4d754aSderaadt 
311f4d754aSderaadt char *
realpath(const char * path,char * resolved)321f4d754aSderaadt realpath(const char *path, char *resolved)
331f4d754aSderaadt {
34390a7db7Sderaadt 	char rbuf[PATH_MAX];
351f4d754aSderaadt 
361409e966Sderaadt 	if (__realpath(path, rbuf) == -1)
37390a7db7Sderaadt 		return NULL;
381f4d754aSderaadt 	if (resolved == NULL)
39390a7db7Sderaadt 		return (strdup(rbuf));
40390a7db7Sderaadt 	strlcpy(resolved, rbuf, PATH_MAX);
411f4d754aSderaadt 	return (resolved);
421f4d754aSderaadt }
43