comparison cpx.c @ 7:e5a905df7e0b draft

Uploaded
author david-hoover
date Thu, 04 Oct 2012 14:48:02 -0400
parents c772c8912663
children
comparison
equal deleted inserted replaced
6:cbe8bdc3bdac 7:e5a905df7e0b
141 141
142 int copyFile(char *src, char *dst) 142 int copyFile(char *src, char *dst)
143 { 143 {
144 // printf("Copying %s to %s\n",src,dst); 144 // printf("Copying %s to %s\n",src,dst);
145 145
146 int c; 146 char *c;
147 FILE *fs,*ft; 147 FILE *fs,*ft;
148 struct stat fileStat; 148 struct stat fileStat;
149 149 size_t st=10000000;
150 int numr,numw;
151
152 // buffer = 10000;
150 /* must be able to stat file */ 153 /* must be able to stat file */
151 if (stat(src, &fileStat) < 0) 154 if (stat(src, &fileStat) < 0)
152 { 155 {
153 fprintf(stderr,"%s: No such file\n",src); 156 fprintf(stderr,"%s: No such file\n",src);
154 exit(1); 157 exit(1);
170 if (ft==NULL) { 173 if (ft==NULL) {
171 fprintf(stderr,"Can't write to file %s!\n",dst); 174 fprintf(stderr,"Can't write to file %s!\n",dst);
172 exit(1); 175 exit(1);
173 } 176 }
174 177
175 c = getc(fs); 178 c=malloc(st);
176 while (c != EOF) { 179 while(feof(fs)==0)
177 putc(c,ft); 180 {
178 c = getc(fs); 181 if((numr=fread(c,1,st,fs))!=st)
179 } 182 {
180 183 if(ferror(fs)!=0)
184 {
185 fprintf(stderr,"read file error.\n");
186 exit(1);
187 }
188 else if(feof(fs)!=0);
189 }
190 if((numw=fwrite(c,1,numr,ft))!=numr)
191 {
192 fprintf(stderr,"write file error.\n");
193 exit(1);
194 }
195 }
196
181 fclose(fs); 197 fclose(fs);
182 fclose(ft); 198 fclose(ft);
199 free(c);
183 200
184 return 0; 201 return 0;
185 202
186 } 203 }
187 204
238 { 255 {
239 fprintf(stderr,"usage: cpx source dest user\n"); 256 fprintf(stderr,"usage: cpx source dest user\n");
240 exit(1); 257 exit(1);
241 } 258 }
242 259
260 if (!strncmp(argv[1],"/data",5) || !strncmp(argv[1],"/home",5))
261 {
262
243 /* is file readable by given user? */ 263 /* is file readable by given user? */
244 explodePath(argv[1],argv[3]); 264 explodePath(argv[1],argv[3]);
245 265
246 /* copy the file and change the owner */ 266 /* copy the file and change the owner */
247 copyFile(argv[1],argv[2]); 267 copyFile(argv[1],argv[2]);
248 chownToUser(argv[2]); 268 chownToUser(argv[2]);
269 }
270 else
271 {
272 fprintf(stderr,"%s: Permission denied.\n",argv[1]);
273 exit(1);
274 }
249 275
250 exit(0); 276 exit(0);
251 } 277 }