Code:
Code: Select all
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
if(argc < 2) {
printf("Usage: %s <from> <to>\n", argv[0]);
return 1;
}
int sourcefd = open(argv[1], O_RDONLY);
int destinationfd = open(argv[2], O_WRONLY | O_CREAT);
if(sourcefd == -1 || destinationfd == -1) {
printf("Cannot open a file, dumping descriptors:\n");
printf("sourcefd: %i\n", sourcefd);
printf("destinationfd: %i\n", destinationfd);
return 1;
}
int chownVal = fchown(destinationfd, (uid_t)getuid(), (gid_t)getgid());
if(chownVal == -1) {
printf("Unable to chown to current user :(\n");
}
char* buffer = (char*) malloc(sizeof(char*) * 1024); //make a 1KB buffer
int numRead;
while( (numRead = read(sourcefd, buffer, 1024)) > 0) {
write(destinationfd, buffer, numRead);
}
//clean up
close(sourcefd);
close(destinationfd);
free(buffer);
return 0;
}
Another thing that confuses me is that the copied file has the executable flag set, even though its just a .c file and I don't--wx--x--T 1 ajtgarber ajtgarber 911 Dec 8 14:05 copied.c
think I told it to give the new file that permission