* this function is a POSIX compliant version, which will Reposition the file offset for * an open file descriptor. * * The `lseek` function sets the file offset for the file descriptor `fd` * to a new value, determined by the `offset` and `whence` parameters. * It can be used to seek to specific positions in a file for reading or writing. * * @param fd the file descriptor. * @param offset
| 285 | * @return the resulting read/write position in the file, or -1 on failed. |
| 286 | */ |
| 287 | off_t lseek(int fd, off_t offset, int whence) |
| 288 | { |
| 289 | int result; |
| 290 | struct dfs_file *d; |
| 291 | |
| 292 | d = fd_get(fd); |
| 293 | if (d == NULL) |
| 294 | { |
| 295 | rt_set_errno(-EBADF); |
| 296 | |
| 297 | return -1; |
| 298 | } |
| 299 | |
| 300 | switch (whence) |
| 301 | { |
| 302 | case SEEK_SET: |
| 303 | break; |
| 304 | |
| 305 | case SEEK_CUR: |
| 306 | offset += d->pos; |
| 307 | break; |
| 308 | |
| 309 | case SEEK_END: |
| 310 | offset += d->vnode->size; |
| 311 | break; |
| 312 | |
| 313 | default: |
| 314 | rt_set_errno(-EINVAL); |
| 315 | |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | if (offset < 0) |
| 320 | { |
| 321 | rt_set_errno(-EINVAL); |
| 322 | |
| 323 | return -1; |
| 324 | } |
| 325 | result = dfs_file_lseek(d, offset); |
| 326 | if (result < 0) |
| 327 | { |
| 328 | rt_set_errno(result); |
| 329 | |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | return offset; |
| 334 | } |
| 335 | RTM_EXPORT(lseek); |
| 336 | |
| 337 | #ifndef _WIN32 |