mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
cp(1) can now support the syntax cp /bin/snake /
This commit is contained in:
parent
1b56d01f17
commit
6986963b4b
1 changed files with 33 additions and 6 deletions
39
utils/cp.cpp
39
utils/cp.cpp
|
@ -18,23 +18,50 @@ bool writeall(int fd, const void* buffer, size_t len)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char* basename(const char* path)
|
||||||
|
{
|
||||||
|
size_t len = strlen(path);
|
||||||
|
while ( len-- ) { if ( path[len] == '/' ) { return path + len + 1; } }
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
if ( argc != 3 ) { printf("usage: %s <from> <to>\n", argv[0]); return 0; }
|
if ( argc != 3 ) { printf("usage: %s <from> <to>\n", argv[0]); return 0; }
|
||||||
|
|
||||||
int fromfd = open(argv[1], O_RDONLY);
|
const char* frompath = argv[1];
|
||||||
if ( fromfd < 0 ) { printf("%s: %s: %s\n", argv[0], argv[1], strerror(errno)); return 1; }
|
const char* topath = argv[2];
|
||||||
|
char tobuffer[256];
|
||||||
|
|
||||||
int tofd = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0777);
|
int fromfd = open(frompath, O_RDONLY);
|
||||||
if ( tofd < 0 ) { printf("%s: %s: %s\n", argv[0], argv[2], strerror(errno)); return 1; }
|
if ( fromfd < 0 ) { printf("%s: %s: %s\n", argv[0], frompath, strerror(errno)); return 1; }
|
||||||
|
|
||||||
|
int tofd = open(topath, O_WRONLY | O_TRUNC | O_CREAT, 0777);
|
||||||
|
if ( tofd < 0 )
|
||||||
|
{
|
||||||
|
if ( errno == EISDIR )
|
||||||
|
{
|
||||||
|
strcpy(tobuffer, topath);
|
||||||
|
if ( tobuffer[strlen(tobuffer)-1] != '/' ) { strcat(tobuffer, "/"); }
|
||||||
|
strcat(tobuffer, basename(frompath));
|
||||||
|
topath = tobuffer;
|
||||||
|
tofd = open(topath, O_WRONLY | O_TRUNC | O_CREAT, 0777);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( tofd < 0 )
|
||||||
|
{
|
||||||
|
printf("%s: %s: %s\n", argv[0], topath, strerror(errno));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
while ( true )
|
while ( true )
|
||||||
{
|
{
|
||||||
const size_t BUFFER_SIZE = 4096;
|
const size_t BUFFER_SIZE = 4096;
|
||||||
char buffer[BUFFER_SIZE];
|
char buffer[BUFFER_SIZE];
|
||||||
ssize_t bytesread = read(fromfd, buffer, BUFFER_SIZE);
|
ssize_t bytesread = read(fromfd, buffer, BUFFER_SIZE);
|
||||||
if ( bytesread < 0 ) { printf("%s: %s: %s\n", argv[0], argv[1], strerror(errno)); return 1; }
|
if ( bytesread < 0 ) { printf("%s: %s: %s\n", argv[0], frompath, strerror(errno)); return 1; }
|
||||||
if ( bytesread == 0 ) { return 0; }
|
if ( bytesread == 0 ) { return 0; }
|
||||||
if ( !writeall(tofd, buffer, bytesread) ) { printf("%s: %s: %s\n", argv[0], argv[2], strerror(errno)); return 1; }
|
if ( !writeall(tofd, buffer, bytesread) ) { printf("%s: %s: %s\n", argv[0], topath, strerror(errno)); return 1; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue