1 /* $OpenBSD: realpath-chdir.c,v 1.1.1.1 2019/08/05 15:16:39 bluhm Exp $ */
2 /*
3 * Copyright (c) 2019 Alexander Bluhm <bluhm@openbsd.org>
4 * Copyright (c) 2019 Moritz Buhl <mbuhl@moritzbuhl.de>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <err.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24
25 int
main(int argc,char * argv[])26 main(int argc, char *argv[])
27 {
28 char *cwd, *dir, *path = NULL;
29 char res[PATH_MAX];
30
31 if (argc != 3)
32 errx(2, "usage: realpath-chdir cwd dir");
33
34 cwd = argv[1];
35 dir = argv[2];
36
37 if (chdir(cwd) == -1)
38 err(1, "chdir %s", cwd);
39
40 if (realpath(dir, res) == NULL)
41 err(1, "realpath %s", dir);
42
43 return 0;
44 }
45