xref: /titanic_44/usr/src/lib/libast/common/port/touch.c (revision 2b4a78020b9c38d1b95e2f3fefa6d6e4be382d1f)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * Glenn Fowler
25  * AT&T Research
26  *
27  * touch file access and modify times of file
28  * if flags&PATH_TOUCH_CREATE then file will be created if it doesn't exist
29  * if flags&PATH_TOUCH_VERBATIM then times are taken verbatim
30  * times have one second granularity
31  *
32  *	(time_t)(-1)	retain old time
33  *	0		use current time
34  *
35  * the old interface flag values were:
36  *	 1	PATH_TOUCH_CREATE
37  *	-1	PATH_TOUCH_CREATE|PATH_TOUCH_VERBATIM
38  *		PATH_TOUCH_VERBATIM -- not supported
39  */
40 
41 #include <ast.h>
42 #include <times.h>
43 #include <tv.h>
44 
45 int
46 touch(const char* path, time_t at, time_t mt, int flags)
47 {
48 	Tv_t	av;
49 	Tv_t	mv;
50 	Tv_t*	ap;
51 	Tv_t*	mp;
52 
53 	if (at == (time_t)(-1) && !(flags & PATH_TOUCH_VERBATIM))
54 		ap = TV_TOUCH_RETAIN;
55 	else if (!at && !(flags & PATH_TOUCH_VERBATIM))
56 		ap = 0;
57 	else
58 	{
59 		av.tv_sec = at;
60 		av.tv_nsec = 0;
61 		ap = &av;
62 	}
63 	if (mt == (time_t)(-1) && !(flags & PATH_TOUCH_VERBATIM))
64 		mp = TV_TOUCH_RETAIN;
65 	else if (!mt && !(flags & PATH_TOUCH_VERBATIM))
66 		mp = 0;
67 	else
68 	{
69 		mv.tv_sec = mt;
70 		mv.tv_nsec = 0;
71 		mp = &mv;
72 	}
73 	return tvtouch(path, ap, mp, NiL, flags & 1);
74 }
75