Advertisement
alexarcan

os_lab4_copyfile(curs)

Oct 26th, 2015
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. /*
  2. * (c) 2013 Dan C. Cosma
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11.  
  12. #define BUFSIZE 4096
  13.  
  14. void usage(char *name)
  15. {
  16. printf("Usage: %s <source> <destination>\n", name);
  17. }
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21. int fd1, fd2;
  22. int n;
  23. char buf[BUFSIZE];
  24.  
  25. /*** Verificarea argumentelor din linia de comanda */
  26. if(argc!=3)
  27. {
  28. usage(argv[0]);
  29. exit(1);
  30. }
  31.  
  32. /*** Deschiderea fisierelor */
  33. if((fd1=open(argv[1], O_RDONLY))<0)
  34. {
  35. printf("Error opening input file\n");
  36. exit(2);
  37. }
  38. if((fd2=open(argv[2], O_WRONLY | O_CREAT | O_EXCL, S_IRWXU)) < 0)
  39. {
  40. printf("Error creating destination file\n");
  41. exit(3);
  42. }
  43.  
  44. /*** Copierea propriu-zisa */
  45. while((n = read(fd1, buf, BUFSIZE)) > 0)
  46. {
  47. if(write(fd2, buf, n) < 0)
  48. {
  49. printf("Error writing to file\n");
  50. exit(4);
  51. }
  52. }
  53.  
  54. if(n < 0)
  55. {
  56. printf("Error reading from file\n");
  57. exit(5);
  58. }
  59.  
  60. /*** Inchiderea fisierelor */
  61. close(fd1);
  62. close(fd2);
  63.  
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement