Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.4 KiB

  1. /* cp - move from one file to another
  2. *
  3. * HISTORY:
  4. *
  5. * 3-Dec-90 w-barry ported to Win32.
  6. * 07-Sep-90 w-wilson ported to cruiser
  7. * 19-Mar-87 danl exit with 1 on any errors
  8. *
  9. */
  10. #include <ctype.h>
  11. #include <fcntl.h>
  12. #include <io.h>
  13. #include <sys\types.h>
  14. #include <sys\stat.h>
  15. #include <malloc.h>
  16. #include <dos.h>
  17. #include <stdio.h>
  18. #include <conio.h>
  19. #include <process.h>
  20. #include <windows.h>
  21. #include <tools.h>
  22. #include <string.h>
  23. char src[MAX_PATH], dst[MAX_PATH], name[MAX_PATH];
  24. __cdecl main (c, v)
  25. int c;
  26. char *v[];
  27. {
  28. struct findType fbuf, dbuf;
  29. int i, t;
  30. HANDLE fh;
  31. char *s;
  32. flagType fAsk = FALSE;
  33. flagType fAppend = FALSE;
  34. int iRtn = 0;
  35. char *y;
  36. ConvertAppToOem( c, v );
  37. while (c > 1 && fSwitChr(*v[1])) {
  38. lower( v[1] );
  39. if (!strcmp( v[1]+1, "p" ))
  40. fAsk = TRUE;
  41. else
  42. if (!strcmp( v[1]+1, "a" ))
  43. fAppend = TRUE;
  44. else
  45. printf( "cp: invalid switch %s\n", v[1] );
  46. c--;
  47. v++;
  48. }
  49. if (c < 3) {
  50. printf ("Usage: cp [/p] [/a] file1 [ file2 ...] target\n");
  51. exit (1);
  52. }
  53. for (i=1; i<c; i++) {
  54. findpath (v[i], src, FALSE);
  55. pname (src);
  56. v[i] = _strdup (src);
  57. }
  58. if (rootpath (v[c-1], dst) == -1) {
  59. printf ("Cannot move to %s - %s\n", v[c-1], error ());
  60. exit (1);
  61. } else {
  62. if ( dst[0] == '\\' && dst[1] == '\\' ) {
  63. y = strbscan (&dst[3], "/\\");
  64. if ( *y != '\0' ) {
  65. y = strbscan( y+1, "/\\");
  66. if ( *y == '\0' ) {
  67. strcat(dst, "\\" );
  68. }
  69. }
  70. }
  71. }
  72. if (fPathChr (dst[strlen(dst)-1])) {
  73. SETFLAG (fbuf.fbuf.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY);
  74. }
  75. else if( ffirst( dst, FILE_ATTRIBUTE_DIRECTORY, &fbuf ) ) {
  76. findclose( &fbuf ); /* Let next ffirst work */
  77. RSETFLAG (fbuf.fbuf.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY);
  78. }
  79. else if (TESTFLAG(fbuf.fbuf.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY)) {
  80. strcat (dst, "\\");
  81. }
  82. if (c != 3 && !TESTFLAG(fbuf.fbuf.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY)) {
  83. if (!fAppend) {
  84. printf ("Use /A switch to append more than 1 file to another file\n");
  85. exit (1);
  86. }
  87. if( ( fh = CreateFile( dst, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 ) ) == (HANDLE)-1 ) {
  88. printf ("Cannot create %s - %s\n", dst, error ());
  89. exit (1);
  90. }
  91. for (i=1; i < c-1; i++) {
  92. if (rootpath (v[i], src) == -1) {
  93. printf ("Cannot move %s - %s\n", v[i], error ());
  94. iRtn = 1;
  95. continue;
  96. }
  97. printf( "%s", src );
  98. fflush( stdout );
  99. if (_access( src, 0 ) == -1) {
  100. printf( " - file not found\n" );
  101. iRtn = 1;
  102. continue;
  103. }
  104. if (s = fappend(src, fh)) {
  105. iRtn = 1;
  106. printf (" %s\n", s);
  107. }
  108. else
  109. printf (" [ok]\n");
  110. }
  111. CloseHandle( fh );
  112. }
  113. else
  114. for (i=1; i < c-1; i++) {
  115. if (rootpath (v[i], src) == -1) {
  116. printf ("Cannot move %s - %s\n", v[i], error ());
  117. iRtn = 1;
  118. continue;
  119. }
  120. if (_access( src, 0 ) == -1) {
  121. printf( "%s - file not found\n", src );
  122. iRtn = 1;
  123. continue;
  124. }
  125. strcpy (name, dst);
  126. if (TESTFLAG(fbuf.fbuf.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY)) {
  127. if (!fPathChr (name[strlen(name)-1]))
  128. strcat (name, "\\");
  129. upd (src, name, name);
  130. }
  131. if (!ffirst (name, -1, &dbuf) && fAsk) {
  132. printf( "%s (delete?)", name );
  133. fflush( stdout );
  134. t = _getch();
  135. t = tolower(t);
  136. if (t != 'y')
  137. if (t == 'p')
  138. fAsk = FALSE;
  139. else {
  140. printf( " - skipped\n" );
  141. continue;
  142. }
  143. printf( "\n" );
  144. }
  145. printf ("%s => %s", src, name);
  146. fflush (stdout);
  147. if (s = fcopy (src, name)) {
  148. iRtn = 1;
  149. printf (" %s\n", s);
  150. }
  151. else
  152. printf (" [ok]\n");
  153. }
  154. return( iRtn );
  155. }