aboutsummaryrefslogtreecommitdiffstats
path: root/copytime.c
blob: c327205633d67b7f3fefc068fef7d18865be35d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* $Id$ */
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2004 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Bostom MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * copytime.c
 *
 * Copy time(s) from a struct stat
 */

#include "mkzftree.h"		/* Must be included first! */

#include <utime.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>

int copytime(const char *filename, const struct stat *st)
{
#if defined(HAVE_UTIMES) && (defined(HAVE_STRUCT_STAT_ST_MTIM_TV_USEC) || \
			     defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC))
  
  struct timeval tv[2];
  
# ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_USEC
  tv[0] = st->st_atim;
  tv[1] = st->st_mtim;
# else
  tv[0].tv_sec  = st->st_atim.tv_sec;
  tv[0].tv_usec = st->st_atim.tv_nsec / 1000;
  tv[1].tv_sec  = st->st_mtim.tv_sec;
  tv[1].tv_usec = st->st_mtim.tv_nsec / 1000;
# endif
  return utimes(filename, tv);
  
#else
  
  struct utimbuf ut; 
  
  ut.actime  = st->st_atime;
  ut.modtime = st->st_mtime;
  return utime(filename, &ut);
  
#endif
}