Rev 4921 | Rev 5215 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4921 | Rev 5198 | ||
---|---|---|---|
1 | /* open.c -- open a file. |
1 | /* open.c -- open a file. |
2 | * |
2 | * |
3 | * Copyright (c) 1995 Cygnus Support |
3 | * Copyright (c) 1995 Cygnus Support |
4 | * |
4 | * |
5 | * The authors hereby grant permission to use, copy, modify, distribute, |
5 | * The authors hereby grant permission to use, copy, modify, distribute, |
6 | * and license this software and its documentation for any purpose, provided |
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 |
7 | * that existing copyright notices are retained in all copies and that this |
8 | * notice is included verbatim in any distributions. No written agreement, |
8 | * notice is included verbatim in any distributions. No written agreement, |
9 | * license, or royalty fee is required for any of the authorized uses. |
9 | * license, or royalty fee is required for any of the authorized uses. |
10 | * Modifications to this software may be copyrighted by their authors |
10 | * Modifications to this software may be copyrighted by their authors |
11 | * and need not follow the licensing terms described here, provided that |
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 |
12 | * the new terms are clearly indicated on the first page of each file where |
13 | * they apply. |
13 | * they apply. |
14 | */ |
14 | */ |
15 | #include |
15 | #include |
16 | #include |
16 | #include |
17 | #include |
17 | #include |
18 | #include |
18 | #include |
19 | #include "glue.h" |
19 | #include "glue.h" |
20 | #include "io.h" |
20 | #include "io.h" |
21 | 21 | ||
22 | #undef erro |
22 | #undef erro |
23 | extern int errno; |
23 | extern int errno; |
- | 24 | ||
- | 25 | static inline int is_slash(char c) |
|
- | 26 | { |
|
- | 27 | return c=='/' || c=='\\'; |
|
- | 28 | } |
|
- | 29 | ||
- | 30 | void fix_slashes(char * in,char * out) |
|
- | 31 | { |
|
- | 32 | int slash_count; |
|
- | 33 | ||
- | 34 | for(slash_count=1;in && out && *in;in++) |
|
- | 35 | { |
|
- | 36 | if(is_slash(*in)) |
|
- | 37 | { |
|
- | 38 | slash_count++; |
|
- | 39 | continue; |
|
- | 40 | } |
|
- | 41 | else |
|
- | 42 | { |
|
- | 43 | if(slash_count) |
|
- | 44 | { |
|
- | 45 | slash_count=0; |
|
- | 46 | *out++='/'; |
|
- | 47 | } |
|
- | 48 | *out++=*in; |
|
- | 49 | } |
|
- | 50 | } |
|
- | 51 | *out='\0'; |
|
- | 52 | }; |
|
- | 53 | ||
- | 54 | ||
- | 55 | void buildpath(char *buf, const char* file) |
|
- | 56 | { |
|
- | 57 | char *ptr; |
|
- | 58 | ||
- | 59 | ptr = buf + strlen(buf); |
|
- | 60 | ||
- | 61 | while (*file) |
|
- | 62 | { |
|
- | 63 | if (file[0] == '.' && file[1] == 0) |
|
- | 64 | break; |
|
- | 65 | ||
- | 66 | if (file[0] == '.' && file[1] == '/') |
|
- | 67 | { |
|
- | 68 | file+=2; |
|
- | 69 | continue; |
|
- | 70 | }; |
|
- | 71 | ||
- | 72 | if (file[0] == '.' && file[1] == '.' && |
|
- | 73 | (file[2] == 0 || file[2] == '/')) |
|
- | 74 | { |
|
- | 75 | while (ptr > buf && ptr[-1] != '/') |
|
- | 76 | --ptr; |
|
- | 77 | file+=2; |
|
- | 78 | if (*file == 0) |
|
- | 79 | break; |
|
- | 80 | ++file; |
|
- | 81 | continue; |
|
- | 82 | } |
|
- | 83 | *ptr++ = '/'; |
|
- | 84 | if (*file == '/') |
|
- | 85 | ++file; |
|
- | 86 | while (*file && *file!='/') |
|
- | 87 | *ptr++ = *file++; |
|
- | 88 | } |
|
- | 89 | *ptr = 0; |
|
- | 90 | }; |
|
- | 91 | ||
- | 92 | static char *getccwd(char *buf, size_t size) |
|
- | 93 | { |
|
- | 94 | int bsize; |
|
- | 95 | __asm__ __volatile__( |
|
- | 96 | "int $0x40" |
|
- | 97 | :"=a"(bsize) |
|
- | 98 | :"a"(30),"b"(2),"c"(buf), "d"(size) |
|
- | 99 | :"memory"); |
|
- | 100 | ||
- | 101 | return buf; |
|
- | 102 | }; |
|
24 | 103 | ||
25 | int open (const char * filename, int flags, ...) |
104 | int open (const char * filename, int flags, ...) |
- | 105 | { |
|
- | 106 | char buf[1024]; |
|
26 | { |
107 | |
27 | __io_handle *ioh; |
108 | __io_handle *ioh; |
28 | fileinfo_t info; |
109 | fileinfo_t info; |
29 | int iomode, rwmode, offset; |
110 | int iomode, rwmode, offset; |
30 | int hid; |
111 | int hid; |
31 | int err; |
112 | int err; |
32 | 113 | ||
33 | hid = __io_alloc(); |
114 | hid = __io_alloc(); |
34 | if(hid < 0) |
115 | if(hid < 0) |
35 | { |
116 | { |
36 | errno = EMFILE; |
117 | errno = EMFILE; |
37 | return (-1); |
118 | return (-1); |
38 | }; |
119 | }; |
- | 120 | ||
- | 121 | if (filename[0]=='/') |
|
- | 122 | { |
|
- | 123 | strcpy(buf,filename); |
|
- | 124 | } |
|
- | 125 | else |
|
- | 126 | { |
|
39 | 127 | getccwd(buf, 1024); |
|
- | 128 | buildpath(buf, filename); |
|
- | 129 | } |
|
- | 130 | ||
40 | // path = getfullpath(name); |
131 | // printf("%s %s\n", __FUNCTION__, buf); |
41 | 132 | ||
42 | err = get_fileinfo(filename, &info); |
133 | err = get_fileinfo(buf, &info); |
43 | 134 | ||
44 | if( flags & O_EXCL && |
135 | if( flags & O_EXCL && |
45 | flags & O_CREAT ) |
136 | flags & O_CREAT ) |
46 | { |
137 | { |
47 | if( !err ) |
138 | if( !err ) |
48 | { |
139 | { |
49 | errno = EEXIST; |
140 | errno = EEXIST; |
50 | return (-1); |
141 | return (-1); |
51 | }; |
142 | }; |
52 | } |
143 | } |
53 | 144 | ||
54 | if( err ) |
145 | if( err ) |
55 | { |
146 | { |
56 | if(flags & O_CREAT) |
147 | if(flags & O_CREAT) |
57 | err=create_file(filename); |
148 | err=create_file(buf); |
58 | if( err ) |
149 | if( err ) |
59 | { |
150 | { |
60 | errno = EACCES; |
151 | errno = EACCES; |
61 | return -1; |
152 | return -1; |
62 | }; |
153 | }; |
63 | }; |
154 | }; |
64 | 155 | ||
65 | if( flags & O_TRUNC ) |
156 | if( flags & O_TRUNC ) |
66 | set_file_size(filename, 0); |
157 | set_file_size(buf, 0); |
67 | 158 | ||
68 | ioh = &__io_tab[hid]; |
159 | ioh = &__io_tab[hid]; |
69 | 160 | ||
70 | rwmode = flags & ( O_RDONLY | O_WRONLY | O_RDWR ); |
161 | rwmode = flags & ( O_RDONLY | O_WRONLY | O_RDWR ); |
71 | 162 | ||
72 | iomode = 0; |
163 | iomode = 0; |
73 | offset = 0; |
164 | offset = 0; |
74 | 165 | ||
75 | if( rwmode == O_RDWR ) |
166 | if( rwmode == O_RDWR ) |
76 | iomode |= _READ | _WRITE; |
167 | iomode |= _READ | _WRITE; |
77 | else if( rwmode == O_RDONLY) |
168 | else if( rwmode == O_RDONLY) |
78 | iomode |= _READ; |
169 | iomode |= _READ; |
79 | else if( rwmode == O_WRONLY) |
170 | else if( rwmode == O_WRONLY) |
80 | iomode |= _WRITE; |
171 | iomode |= _WRITE; |
81 | 172 | ||
82 | if( flags & O_APPEND ) |
173 | if( flags & O_APPEND ) |
83 | { |
174 | { |
84 | iomode |= _APPEND; |
175 | iomode |= _APPEND; |
85 | offset = info.size; |
176 | offset = info.size; |
86 | }; |
177 | }; |
87 | 178 | ||
88 | if( flags & (O_BINARY|O_TEXT) ) |
179 | if( flags & (O_BINARY|O_TEXT) ) |
89 | { |
180 | { |
90 | if( flags & O_BINARY ) |
181 | if( flags & O_BINARY ) |
91 | iomode |= _BINARY; |
182 | iomode |= _BINARY; |
92 | } else |
183 | } else |
93 | iomode |= _BINARY; |
184 | iomode |= _BINARY; |
94 | 185 | ||
95 | ioh->name = strdup(filename); |
186 | ioh->name = strdup(buf); |
96 | ioh->offset = offset; |
187 | ioh->offset = offset; |
97 | ioh->mode = iomode; |
188 | ioh->mode = iomode; |
98 | ioh->read = read_file; |
189 | ioh->read = read_file; |
99 | ioh->write = write_file; |
190 | ioh->write = write_file; |
- | 191 | ||
- | 192 | // printf("%s %s\n", __FUNCTION__, ioh->name); |
|
100 | 193 | ||
101 | return hid; |
194 | return hid; |
102 | };> |
195 | };> |