cp.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * cp - Copy files
  7. *
  8. */
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <dirent.h>
  13. #include <unistd.h>
  14. #include <fcntl.h>
  15. #include <sys/stat.h>
  16. #include <sys/ioctl.h>
  17. #define CHUNK_SIZE 4096
  18. static int recursive = 0;
  19. static int symlinks = 0;
  20. static int copy_thing(char * tmp, char * tmp2);
  21. static int copy_link(char * source, char * dest, int mode, int uid, int gid) {
  22. //fprintf(stderr, "need to copy link %s to %s\n", source, dest);
  23. char tmp[1024];
  24. readlink(source, tmp, 1024);
  25. symlink(tmp, dest);
  26. chmod(dest, mode);
  27. chown(dest, uid, gid);
  28. return 0;
  29. }
  30. static int copy_file(char * source, char * dest, int mode,int uid, int gid) {
  31. //fprintf(stderr, "need to copy file %s to %s %x\n", source, dest, mode);
  32. int d_fd = open(dest, O_WRONLY | O_CREAT, mode);
  33. int s_fd = open(source, O_RDONLY);
  34. ssize_t length;
  35. length = lseek(s_fd, 0, SEEK_END);
  36. lseek(s_fd, 0, SEEK_SET);
  37. //fprintf(stderr, "%d bytes to copy\n", length);
  38. char buf[CHUNK_SIZE];
  39. while (length > 0) {
  40. size_t r = read(s_fd, buf, length < CHUNK_SIZE ? length : CHUNK_SIZE);
  41. //fprintf(stderr, "copying %d bytes from %s to %s\n", r, source, dest);
  42. write(d_fd, buf, r);
  43. length -= r;
  44. //fprintf(stderr, "%d bytes remaining\n", length);
  45. }
  46. close(s_fd);
  47. close(d_fd);
  48. chown(dest, uid, gid);
  49. return 0;
  50. }
  51. static int copy_directory(char * source, char * dest, int mode, int uid, int gid) {
  52. DIR * dirp = opendir(source);
  53. if (dirp == NULL) {
  54. fprintf(stderr, "Failed to copy directory %s\n", source);
  55. return 1;
  56. }
  57. //fprintf(stderr, "Creating %s\n", dest);
  58. if (!strcmp(dest, "/")) {
  59. dest = "";
  60. } else {
  61. mkdir(dest, mode);
  62. }
  63. struct dirent * ent = readdir(dirp);
  64. while (ent != NULL) {
  65. if (!strcmp(ent->d_name,".") || !strcmp(ent->d_name,"..")) {
  66. //fprintf(stderr, "Skipping %s\n", ent->d_name);
  67. ent = readdir(dirp);
  68. continue;
  69. }
  70. //fprintf(stderr, "not skipping %s/%s → %s/%s\n", source, ent->d_name, dest, ent->d_name);
  71. char tmp[strlen(source)+strlen(ent->d_name)+2];
  72. sprintf(tmp, "%s/%s", source, ent->d_name);
  73. char tmp2[strlen(dest)+strlen(ent->d_name)+2];
  74. sprintf(tmp2, "%s/%s", dest, ent->d_name);
  75. //fprintf(stderr,"%s → %s\n", tmp, tmp2);
  76. copy_thing(tmp,tmp2);
  77. ent = readdir(dirp);
  78. }
  79. closedir(dirp);
  80. chown(dest, uid, gid);
  81. return 0;
  82. }
  83. static int copy_thing(char * tmp, char * tmp2) {
  84. struct stat statbuf;
  85. if (symlinks) {
  86. lstat(tmp,&statbuf);
  87. } else {
  88. stat(tmp,&statbuf);
  89. }
  90. if (S_ISLNK(statbuf.st_mode)) {
  91. return copy_link(tmp, tmp2, statbuf.st_mode & 07777, statbuf.st_uid, statbuf.st_gid);
  92. } else if (S_ISDIR(statbuf.st_mode)) {
  93. if (!recursive) {
  94. fprintf(stderr, "cp: %s: omitting directory\n", tmp);
  95. return 1;
  96. }
  97. return copy_directory(tmp, tmp2, statbuf.st_mode & 07777, statbuf.st_uid, statbuf.st_gid);
  98. } else if (S_ISREG(statbuf.st_mode)) {
  99. return copy_file(tmp, tmp2, statbuf.st_mode & 07777, statbuf.st_uid, statbuf.st_gid);
  100. } else {
  101. fprintf(stderr, "cp: %s is not any of the required file types?\n", tmp);
  102. return 1;
  103. }
  104. }
  105. int main(int argc, char ** argv) {
  106. int opt;
  107. while ((opt = getopt(argc, argv, "RrP")) != -1) {
  108. switch (opt) {
  109. case 'R':
  110. case 'r':
  111. recursive = 1;
  112. symlinks = 1;
  113. break;
  114. case 'P':
  115. symlinks = 0;
  116. break;
  117. default:
  118. fprintf(stderr, "cp: unrecognized option '%c'\n", opt);
  119. break;
  120. }
  121. }
  122. if (optind < argc - 1) {
  123. char * destination = argv[argc-1];
  124. struct stat statbuf;
  125. stat((destination), &statbuf);
  126. if (S_ISDIR(statbuf.st_mode)) {
  127. while (optind < argc - 1) {
  128. char * source = strrchr(argv[optind], '/');
  129. if (!source) source = argv[optind];
  130. char output[4096];
  131. sprintf(output, "%s/%s", destination, source);
  132. copy_thing(argv[optind], output);
  133. optind++;
  134. }
  135. } else {
  136. if (optind < argc - 2) {
  137. fprintf(stderr, "cp: target '%s' is not a directory\n", destination);
  138. return 1;
  139. }
  140. copy_thing(argv[optind], destination);
  141. }
  142. } else {
  143. fprintf(stderr, "cp: not enough arguments\n");
  144. }
  145. return 0;
  146. }