touch.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
ad2e68e571352b6759441733df697e075ceed341Robert Savu/***********************************************************************
ad2e68e571352b6759441733df697e075ceed341Robert Savu* This software is part of the ast package *
ad2e68e571352b6759441733df697e075ceed341Robert Savu* Copyright (c) 1989-2012 AT&T Intellectual Property *
a9c461443a740732a62d58c1c465b88cba3c606bRobert Savu* and is licensed under the *
ad2e68e571352b6759441733df697e075ceed341Robert Savu* Eclipse Public License, Version 1.0 *
d0adfa619575f357a79e0075cab46f68201379d2Robert Savu* by AT&T Intellectual Property *
37dd4c99dbe470cce3fe0d89a011186f080e8910Robert Savu* A copy of the License is available at *
37dd4c99dbe470cce3fe0d89a011186f080e8910Robert Savu* http://www.eclipse.org/org/documents/epl-v10.html *
37dd4c99dbe470cce3fe0d89a011186f080e8910Robert Savu* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
37dd4c99dbe470cce3fe0d89a011186f080e8910Robert Savu* Information and Software Systems Research *
37dd4c99dbe470cce3fe0d89a011186f080e8910Robert Savu* AT&T Research *
d0adfa619575f357a79e0075cab46f68201379d2Robert Savu* Florham Park NJ *
d0adfa619575f357a79e0075cab46f68201379d2Robert Savu* Glenn Fowler <gsf@research.att.com> *
d0adfa619575f357a79e0075cab46f68201379d2Robert Savu* David Korn <dgk@research.att.com> *
d0adfa619575f357a79e0075cab46f68201379d2Robert Savu* Eduardo Krell <ekrell@adexus.cl> *
ad2e68e571352b6759441733df697e075ceed341Robert Savu***********************************************************************/
d0adfa619575f357a79e0075cab46f68201379d2Robert Savu * NOTE: obsolete touch() for 3d private use
ad2e68e571352b6759441733df697e075ceed341Robert Savu * -last touch() handles subsecond times
ad2e68e571352b6759441733df697e075ceed341Robert Savu * via tvtouch()
ad2e68e571352b6759441733df697e075ceed341Robert Savu * Glenn Fowler
ad2e68e571352b6759441733df697e075ceed341Robert Savu * AT&T Research
ad2e68e571352b6759441733df697e075ceed341Robert Savu * touch file access and modify times of file
ad2e68e571352b6759441733df697e075ceed341Robert Savu * if force>0 then file will be created if it doesn't exist
ad2e68e571352b6759441733df697e075ceed341Robert Savu * if force<0 then times are taken verbatim
ad2e68e571352b6759441733df697e075ceed341Robert Savu * times have one second granularity
ad2e68e571352b6759441733df697e075ceed341Robert Savu * (time_t)(-1) retain old time
ad2e68e571352b6759441733df697e075ceed341Robert Savu * 0 use current time
ad2e68e571352b6759441733df697e075ceed341Robert Savu#if defined(__STDPP__directive) && defined(__STDPP__hide)
ad2e68e571352b6759441733df697e075ceed341Robert Savu#if defined(__STDPP__directive) && defined(__STDPP__hide)
ad2e68e571352b6759441733df697e075ceed341Robert Savuextern int utime(const char*, const struct utimbuf*);
ad2e68e571352b6759441733df697e075ceed341Robert Savutouch(const char* file, time_t atime, time_t mtime, int force)
37dd4c99dbe470cce3fe0d89a011186f080e8910Robert Savu if (atime == (time_t)(-1) || mtime == (time_t)(-1))
37dd4c99dbe470cce3fe0d89a011186f080e8910Robert Savu if (stat(file, &st)) st.st_atime = st.st_mtime = 0;
a9c461443a740732a62d58c1c465b88cba3c606bRobert Savu n = utime(file, (force < 0 || atime || mtime) ? &ut : (struct utimbuf*)0);
a9c461443a740732a62d58c1c465b88cba3c606bRobert Savu * NOTE: the emulation allows atime to change
37dd4c99dbe470cce3fe0d89a011186f080e8910Robert Savu * for mtime only requests
a9c461443a740732a62d58c1c465b88cba3c606bRobert Savu mode = (~mode) & (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
37dd4c99dbe470cce3fe0d89a011186f080e8910Robert Savu if ((fd = open(file, O_WRONLY|O_CREAT|O_TRUNC|O_cloexec, mode)) < 0) return(-1);
37dd4c99dbe470cce3fe0d89a011186f080e8910Robert Savu return((force < 0 || atime || mtime) ? utime(file, &ut) : 0);
37dd4c99dbe470cce3fe0d89a011186f080e8910Robert Savu return((atime != now || mtime != now) ? utime(file, ut) : 0);