A very simple example, demonstrating some of Linux for system calls for file operation, and demonstrates a classic algorithm for a COPY file.
The program is extracted from http://www.fanqiang.com/ website, there are many good articles.
#include
#define buffer_size 1024 INT mail (int Argc, char ** argv) {int.com_fd, to_fd; int tents_read, bytes_write; char buffer [buffer_size]; char * ptr; if (argc! = 3) {fprintf (stderr, "usage :% s fromfile tofile / n / a ", argv [0]); exit (1);} IF ((from_fd = open (argv [1], o_rdonly) == - 1) {fprintf (stderr," Open % S Error:% S / N ", Argv [1], STRERROR (Errno)); EXIT (1);} IF ((TO_FD = Open (Argv [2], O_Wronly | O_CREAT, S_IRUSR | S_IWUSR)) == -1) {FPRINTF (stderr, "open% s error:% s / n", argv [2], strerror (errno)); exit (1);} while (bytes_read = read (from_fd, buffer, buffer_size))) {IF ((bytes_read == - 1) && (errno! = Eintr)) Break; else if (bytes_read> 0) {ptr = buffer; while (bytes_Write = Write (to_fd, ptr, bytes_read) {ix ((bytes_write) == - 1) && (errno! = Eintr)) Break; Else IF (Bytes_Write == Bytes_read) Break; Else IF (Bytes_Write> 0) {PTR = BYTES_WRITE; BYTES_READ- = BYTES_WRITE;}}}} (Bytes_Write == - 1) Break;}} close (from_fd); Close (TO_FD); exit (0);
Write here, I am very hi, I can do my own shell to achieve basic functions. This is also a job in the "Linux kernel design internship", I will try to do it.