| 1151 | } |
| 1152 | |
| 1153 | static void copy_file(const char *srcpath, |
| 1154 | const char *destpath, mode_t mode) |
| 1155 | { |
| 1156 | int nbytesread; |
| 1157 | int nbyteswritten; |
| 1158 | int rdfd; |
| 1159 | int wrfd; |
| 1160 | |
| 1161 | /* Open the source file for reading */ |
| 1162 | |
| 1163 | rdfd = open(srcpath, O_RDONLY); |
| 1164 | if (rdfd < 0) |
| 1165 | { |
| 1166 | fprintf(stderr, "ERROR: Failed to open %s for reading: %s\n", |
| 1167 | srcpath, strerror(errno)); |
| 1168 | exit(EXIT_FAILURE); |
| 1169 | } |
| 1170 | |
| 1171 | /* Now open the destination for writing */ |
| 1172 | |
| 1173 | wrfd = open(destpath, O_WRONLY | O_CREAT | O_TRUNC, mode); |
| 1174 | if (wrfd < 0) |
| 1175 | { |
| 1176 | fprintf(stderr, "ERROR: Failed to open %s for writing: %s\n", |
| 1177 | destpath, strerror(errno)); |
| 1178 | exit(EXIT_FAILURE); |
| 1179 | } |
| 1180 | |
| 1181 | /* Now copy the file */ |
| 1182 | |
| 1183 | for (; ; ) |
| 1184 | { |
| 1185 | do |
| 1186 | { |
| 1187 | nbytesread = read(rdfd, g_buffer, BUFFER_SIZE); |
| 1188 | if (nbytesread == 0) |
| 1189 | { |
| 1190 | /* End of file */ |
| 1191 | |
| 1192 | close(rdfd); |
| 1193 | close(wrfd); |
| 1194 | return; |
| 1195 | } |
| 1196 | else if (nbytesread < 0) |
| 1197 | { |
| 1198 | /* EINTR is not an error (but will still stop the copy) */ |
| 1199 | |
| 1200 | fprintf(stderr, "ERROR: Read failure: %s\n", strerror(errno)); |
| 1201 | exit(EXIT_FAILURE); |
| 1202 | } |
| 1203 | } |
| 1204 | while (nbytesread <= 0); |
| 1205 | |
| 1206 | do |
| 1207 | { |
| 1208 | nbyteswritten = write(wrfd, g_buffer, nbytesread); |
| 1209 | if (nbyteswritten >= 0) |
| 1210 | { |