xref: /onnv-gate/usr/src/lib/libast/common/port/touch.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin  * Glenn Fowler
254887Schin  * AT&T Research
264887Schin  *
274887Schin  * touch file access and modify times of file
284887Schin  * if flags&PATH_TOUCH_CREATE then file will be created if it doesn't exist
294887Schin  * if flags&PATH_TOUCH_VERBATIM then times are taken verbatim
304887Schin  * times have one second granularity
314887Schin  *
324887Schin  *	(time_t)(-1)	retain old time
334887Schin  *	0		use current time
344887Schin  *
354887Schin  * the old interface flag values were:
364887Schin  *	 1	PATH_TOUCH_CREATE
374887Schin  *	-1	PATH_TOUCH_CREATE|PATH_TOUCH_VERBATIM
384887Schin  *		PATH_TOUCH_VERBATIM -- not supported
394887Schin  */
404887Schin 
414887Schin #include <ast.h>
424887Schin #include <times.h>
434887Schin #include <tv.h>
444887Schin 
454887Schin int
touch(const char * path,time_t at,time_t mt,int flags)464887Schin touch(const char* path, time_t at, time_t mt, int flags)
474887Schin {
484887Schin 	Tv_t	av;
494887Schin 	Tv_t	mv;
504887Schin 	Tv_t*	ap;
514887Schin 	Tv_t*	mp;
524887Schin 
534887Schin 	if (at == (time_t)(-1) && !(flags & PATH_TOUCH_VERBATIM))
544887Schin 		ap = TV_TOUCH_RETAIN;
554887Schin 	else if (!at && !(flags & PATH_TOUCH_VERBATIM))
564887Schin 		ap = 0;
574887Schin 	else
584887Schin 	{
594887Schin 		av.tv_sec = at;
604887Schin 		av.tv_nsec = 0;
614887Schin 		ap = &av;
624887Schin 	}
634887Schin 	if (mt == (time_t)(-1) && !(flags & PATH_TOUCH_VERBATIM))
644887Schin 		mp = TV_TOUCH_RETAIN;
654887Schin 	else if (!mt && !(flags & PATH_TOUCH_VERBATIM))
664887Schin 		mp = 0;
674887Schin 	else
684887Schin 	{
694887Schin 		mv.tv_sec = mt;
704887Schin 		mv.tv_nsec = 0;
714887Schin 		mp = &mv;
724887Schin 	}
734887Schin 	return tvtouch(path, ap, mp, NiL, flags & 1);
744887Schin }
75