brewco/util.c

changeset 464
4a624c071ca9
parent 446
78e9d5234d15
child 471
1564b60558b1
equal deleted inserted replaced
463:a1da58215b65 464:4a624c071ca9
74 return 0; 74 return 0;
75 } 75 }
76 76
77 77
78 78
79 /*
80 * Buffered filecopy, filetime is preserved.
81 */
82 int file_cp(char *from, char *to)
83 {
84 char *line;
85 FILE *stfrom, *stto;
86 int dummy, bread;
87 static int error;
88 struct stat sb;
89 struct utimbuf ut;
90
91 stfrom = fopen(from, "r");
92 if (stfrom == NULL)
93 return errno;
94
95 stto = fopen(to, "w");
96 if (stto == NULL) {
97 error = errno;
98 fclose(stfrom);
99 return error;
100 }
101
102 line = malloc(16384);
103 do {
104 bread = fread(line, 1, 16384, stfrom);
105 dummy = fwrite(line, 1, bread, stto);
106 if (bread != dummy) {
107 error = errno;
108 fclose(stfrom);
109 fclose(stto);
110 unlink(to);
111 free(line);
112 return error;
113 }
114 } while (bread != 0);
115 free(line);
116 fclose(stfrom);
117 if (fclose(stto) != 0) {
118 error = errno;
119 unlink(to);
120 return error;
121 }
122
123 /*
124 * copy successfull, now copy file- and modification-time
125 */
126 if (stat(from, &sb) == 0) {
127 ut.actime = mktime(localtime(&sb.st_atime));
128 ut.modtime = mktime(localtime(&sb.st_mtime));
129 if (utime(to, &ut) != 0) {
130 error = errno;
131 unlink(to);
132 return error;
133 }
134 chmod(to, sb.st_mode);
135 }
136
137 return 0;
138 }
139
140
141
79 long millis(void) 142 long millis(void)
80 { 143 {
81 struct timespec now; 144 struct timespec now;
82 145
83 clock_gettime(CLOCK_REALTIME , &now); 146 clock_gettime(CLOCK_REALTIME , &now);

mercurial