Rev 5220 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5215 | serge | 1 | /* _rename.c -- Implementation of the low-level rename() routine |
2 | * |
||
3 | * Copyright (c) 2004 National Semiconductor Corporation |
||
4 | * |
||
5 | * The authors hereby grant permission to use, copy, modify, distribute, |
||
6 | * and license this software and its documentation for any purpose, provided |
||
7 | * that existing copyright notices are retained in all copies and that this |
||
8 | * notice is included verbatim in any distributions. No written agreement, |
||
9 | * license, or royalty fee is required for any of the authorized uses. |
||
10 | * Modifications to this software may be copyrighted by their authors |
||
11 | * and need not follow the licensing terms described here, provided that |
||
12 | * the new terms are clearly indicated on the first page of each file where |
||
13 | * they apply. |
||
14 | */ |
||
15 | |||
16 | #include |
||
17 | #include |
||
18 | #include |
||
19 | #include |
||
20 | |||
21 | int _rename (char *from, char *to) |
||
22 | { |
||
23 | void* buf; |
||
24 | int f_from; |
||
25 | int f_to; |
||
26 | int size; |
||
27 | |||
28 | printf("%s from %s to %s\n", __FUNCTION__, |
||
29 | from, to); |
||
30 | |||
31 | f_from = open(from,O_RDONLY); |
||
32 | |||
33 | if (f_from < 0) |
||
34 | { |
||
35 | errno = ENOENT; |
||
36 | return -1; |
||
37 | }; |
||
38 | |||
39 | f_to = open(to,O_CREAT|O_WRONLY|O_EXCL); |
||
40 | |||
41 | if (f_to < 0) |
||
42 | { |
||
43 | close(f_from); |
||
44 | errno = EACCES; |
||
45 | return -1; |
||
46 | }; |
||
47 | |||
48 | buf = alloca(32768); |
||
49 | |||
50 | do |
||
51 | { |
||
52 | size = read(f_from, buf, 32768); |
||
53 | |||
54 | if (size >= 0) |
||
55 | size = write(f_to, buf, size); |
||
56 | |||
57 | }while (size == 32768); |
||
58 | |||
59 | close(f_to); |
||
60 | close(f_from); |
||
61 | |||
62 | if (size == -1) |
||
63 | { |
||
64 | errno = EACCES; |
||
65 | return -1; |
||
66 | }; |
||
67 | |||
68 | remove(from); |
||
69 | |||
70 | return (0); |
||
71 | };>> |