1/*
2 * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3 *  AUTHOR		: William Roske
4 *  CO-PILOT		: Dave Fenner
5 * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it would be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like.  Any license provided herein, whether implied or
18 * otherwise, applies only to this software file.  Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA  94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 */
35
36/*
37 * Tests that link(2) succeds.
38 */
39
40#include <sys/types.h>
41#include <sys/fcntl.h>
42#include <sys/stat.h>
43#include <errno.h>
44#include <string.h>
45#include <signal.h>
46#include "test.h"
47#include "safe_macros.h"
48
49static void setup(void);
50static void cleanup(void);
51
52char *TCID = "link02";
53int TST_TOTAL = 1;
54
55#define OLDPATH "oldpath"
56#define NEWPATH "newpath"
57
58static void verify_link(void)
59{
60	struct stat fbuf, lbuf;
61
62	TEST(link(OLDPATH, NEWPATH));
63
64	if (TEST_RETURN == 0) {
65		SAFE_STAT(cleanup, OLDPATH, &fbuf);
66		SAFE_STAT(cleanup, NEWPATH, &lbuf);
67		if (fbuf.st_nlink > 1 && lbuf.st_nlink > 1 &&
68		    fbuf.st_nlink == lbuf.st_nlink) {
69			tst_resm(TPASS, "link("OLDPATH","NEWPATH") "
70			         "returned 0 and link counts match");
71		} else {
72			tst_resm(TFAIL, "link("OLDPATH","NEWPATH") returned 0"
73				 " but stat lin count do not match %d %d",
74				 (int)fbuf.st_nlink, (int)lbuf.st_nlink);
75		}
76		SAFE_UNLINK(cleanup, NEWPATH);
77	} else {
78		tst_resm(TFAIL | TTERRNO,
79		         "link("OLDPATH","NEWPATH") returned %ld",
80		         TEST_RETURN);
81	}
82}
83
84int main(int ac, char **av)
85{
86	int lc;
87
88	tst_parse_opts(ac, av, NULL, NULL);
89
90	setup();
91
92	for (lc = 0; TEST_LOOPING(lc); lc++) {
93		tst_count = 0;
94		verify_link();
95	}
96
97	cleanup();
98	tst_exit();
99}
100
101static void setup(void)
102{
103	tst_sig(NOFORK, DEF_HANDLER, cleanup);
104
105	TEST_PAUSE;
106
107	tst_tmpdir();
108
109	SAFE_TOUCH(cleanup, OLDPATH, 0700, NULL);
110}
111
112static void cleanup(void)
113{
114	tst_rmdir();
115}
116