-
Notifications
You must be signed in to change notification settings - Fork 0
/
normfile.c
48 lines (40 loc) · 1021 Bytes
/
normfile.c
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
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <utime.h>
#include <sys/stat.h>
#include "normfile.h"
void normfile (const char *old_filename, const char *new_filename, time_t mtime)
{
struct utimbuf times;
struct stat file_stat;
if (strcmp(old_filename, new_filename) != 0) {
if (stat(new_filename, &file_stat) && errno == ENOENT) {
if (rename (old_filename, new_filename)) {
perror ("rename");
abort();
}
} else {
fprintf(stderr, "rename %s to %s - file exists\n", old_filename, new_filename);
}
}
times.actime = time (NULL);
times.modtime = mtime;
if (utime (new_filename, ×)) {
perror ("utime");
abort();
}
}
#ifdef UNIT_TEST
#include <stdio.h>
#include <time.h>
#include "normfile.h"
int main (int argc, char *argv[])
{
normfile ("test/foo", "test/bar", time (NULL) - 3600);
exit(0);
}
#endif
/* vi:ai:ts=4:sw=4:et
*/