All pastes #930769 Raw Edit

Miscellany

public text v1 · immutable
·published 2008-03-06 20:39 UTC
rendered paste body
#include <fcntl.h>

#include <unistd.h>

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <stdlib.h>



#define BUFSIZE 4096



int main(int argc, char **argv) {

	int fs, fd, rd;

	char buff[BUFSIZE];

	ssize_t bytes_read, bytes_write;

	if (argc != 3) {

		fprintf(stderr, "Usage: ./mycp <sursa> <dest>\n");

		return 1;

	}



	//TODO

	

	fd = open(argv[1], O_RDONLY);

	

	if (fd < 0){

		perror("open");

		exit(EXIT_FAILURE);

	}

	fs = open(argv[2],O_WRONLY | O_CREAT, 0644);

	

	if (fs < 0){

		perror("create");

		exit(EXIT_FAILURE);

	}

	while(1){

		bytes_read = read(fd,buff,100);

		if (bytes_read < 0){

			perror("read");

			exit(EXIT_FAILURE);

		}

		

		if (bytes_read == 0){			

			break;

		}

		

		bytes_write = write(fs,buff,bytes_read);

		

		if (bytes_write < 0){

			perror("write");

			exit(EXIT_FAILURE);

		}

	} 

	

	close(fd);

	close(fs);



	return 0;

}