1 /*
2  * cpnt.c - Copy over file without truncating.
3  *          For use on my current floppy, since it's 'cp' insist
4  *          on truncating first, and NTFS doesn't like that yet.
5  *
6  * 2003-apr: First version
7  *
8  *****
9  *
10  * Copyright (c) 1997-2007 Petter Nordahl-Hagen.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; version 2 of the License.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * See file GPL.txt for the full license.
22  *
23  */
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <inttypes.h>
34 
35 
36 #define BUFSIZE 16384
37 
main(int argc,char ** argv)38 int main(int argc, char **argv)
39 {
40 
41   void *buf;
42   int sf,df,rb,wb;
43   int going = 1;
44   int e = 0;
45 
46   if (argc != 3) {
47     printf("usage: cpnt <sourcefile> <destfile>\n");
48     printf("       sorry, only one file at a time yet.\n");
49     return(1);
50   }
51 
52 #if 0
53   printf("input : %s\n",argv[1]);
54   printf("output: %s\n",argv[2]);
55 #endif
56 
57   buf = malloc(BUFSIZE);
58   if (!buf) {
59     printf("cpnt: could not allocate buffer\n");
60     return(1);
61   }
62 
63   sf = open(argv[1],O_RDONLY);
64   if (sf < 0) {
65     e = errno;
66     printf("cpnt: %s: %s\n",argv[1],strerror(e));
67     return(1);
68   }
69 
70   df = open(argv[2],O_WRONLY|O_CREAT,00666);
71   if (df < 0) {
72     e = errno;
73     printf("cpnt: %s: %s\n",argv[2],strerror(e));
74     return(1);
75   }
76 
77   while (going) {
78     rb = read(sf,buf,BUFSIZE);
79     if (rb < 0) {
80       e = errno;
81       printf("cpnt: error while reading: %s\n",strerror(e));
82       going = 0;
83       break;
84     }
85     if (rb == 0) going = 0;
86     wb = write(df,buf,rb);
87     if (wb < 0) {
88       e = errno;
89       printf("cpnt: error while writing: %s\n",strerror(e));
90       going = 0;
91     }
92 
93   }
94 
95   close(sf);
96   close(df);
97   free(buf);
98 
99   return(e ? 1 : 0);
100 }
101