https://github.com/pol-cova/copy-command
Simple implementations of the copy command in C and Rust, focusing on file I/O operations, error handling, and performance in both.
https://github.com/pol-cova/copy-command
c low-level low-level-programming rust
Last synced: about 1 month ago
JSON representation
Simple implementations of the copy command in C and Rust, focusing on file I/O operations, error handling, and performance in both.
- Host: GitHub
- URL: https://github.com/pol-cova/copy-command
- Owner: pol-cova
- License: mit
- Created: 2025-02-14T06:30:17.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-14T07:13:31.000Z (over 1 year ago)
- Last Synced: 2025-05-16T09:07:40.327Z (about 1 year ago)
- Topics: c, low-level, low-level-programming, rust
- Language: C
- Homepage:
- Size: 881 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Copy Command
## How it works?
This command copies a file by **reading it in small chunks** (4 KB at a time) and writing each chunk to a new file. If you imagine a **16 KB file**, it is split into **four 4 KB blocks**, which are read and written one by one until the entire file is copied.
## ๐ Workflow Diagram
```mermaid
graph TD;
A(Start) --> B(Open source file);
B -->|Success| C(Open destination file);
B -->|Failure| X(Print error and exit);
C -->|Success| D{Read up to 4096 bytes};
C -->|Failure| Y(Print error, close source file, exit);
D -->|Read > 0| E{Bytes left to write?};
D -->|Read failed| Z(Print error, close files, exit);
E -->|Yes| F(Write bytes to destination);
E -->|No| D;
F -->|Success| E;
F -->|Failure| W(Print error, close files, exit);
D -->|Read == 0| G(Close source file);
G --> H(Close destination file);
H --> I(End);
X -->|Exit| X1[Stop];
Y -->|Exit| Y1[Stop];
Z -->|Exit| Z1[Stop];
W -->|Exit| W1[Stop];
```
## ๐ How to Run
### 1๏ธโฃ Compile the program
**For C:**
```sh
gcc copy.c -o copy
```
**For Rust:**
```sh
cd copy
cargo build --release
mv target/release/copy .
```
### 2๏ธโฃ Run the command
```sh
./copy source.txt destination.txt
```
Replace `source.txt` with the file you want to copy and `destination.txt` with the output file.
### 3๏ธโฃ Option: Move the binary to a system path
Once the binary is built, move it to a directory that is included in your system's `$PATH`, such as `/usr/local/bin`:
```sh
sudo mv copy /usr/local/bin/
sudo chmod +x /usr/local/bin/copy
```
๐ **Explanation:**
- `mv copy /usr/local/bin/` โ Moves the binary to a globally accessible location.
- `chmod +x /usr/local/bin/copy` โ Ensures it's executable.
Now you can run the command from any directory.
```sh
copy file1.txt file2.txt
```
## ๐ Error Handling
- If the source file **doesnโt exist**, the program exits with an error.
- If thereโs an issue **writing to the destination file**, it also exits with an error.
- All files are **closed properly** after copying to avoid resource leaks.
## ๐ Notes
- Uses a **4 KB buffer size** for efficiency.
- Works on **Unix-based systems** (Linux/macOS).