Mercurial > ecos-v3_0-branch
comparison packages/io/fileio/current/src/io.cxx @ 115:6ed91473a1cd ecos-sw-2000-08-21
Merge from eCos master repository on 2000-08-21-22:40:54-BST
| author | jlarmour |
|---|---|
| date | Fri, 25 Aug 2000 17:32:38 +0000 |
| parents | |
| children | eb9fd8c04db3 |
comparison
equal
deleted
inserted
replaced
| 114:5ad2b71d525e | 115:6ed91473a1cd |
|---|---|
| 1 //========================================================================== | |
| 2 // | |
| 3 // io.cxx | |
| 4 // | |
| 5 // Fileio IO operations | |
| 6 // | |
| 7 //========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
| 9 // | |
| 10 // ------------------------------------------- | |
| 11 // The contents of this file are subject to the Red Hat eCos Public License | |
| 12 // Version 1.1 (the "License"); you may not use this file except in | |
| 13 // compliance with the License. You may obtain a copy of the License at | |
| 14 // http://www.redhat.com/ | |
| 15 // | |
| 16 // Software distributed under the License is distributed on an "AS IS" | |
| 17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | |
| 18 // License for the specific language governing rights and limitations under | |
| 19 // the License. | |
| 20 // | |
| 21 // The Original Code is eCos - Embedded Configurable Operating System, | |
| 22 // released September 30, 1998. | |
| 23 // | |
| 24 // The Initial Developer of the Original Code is Red Hat. | |
| 25 // Portions created by Red Hat are | |
| 26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. | |
| 27 // All Rights Reserved. | |
| 28 // ------------------------------------------- | |
| 29 // | |
| 30 //####COPYRIGHTEND#### | |
| 31 //========================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 34 // Author(s): nickg | |
| 35 // Contributors: nickg | |
| 36 // Date: 2000-05-25 | |
| 37 // Purpose: Fileio IO operations | |
| 38 // Description: These are the functions that operate on open files, | |
| 39 // such as read(), write(), fstat() etc. | |
| 40 // | |
| 41 // | |
| 42 // | |
| 43 //####DESCRIPTIONEND#### | |
| 44 // | |
| 45 //========================================================================== | |
| 46 | |
| 47 #include <pkgconf/hal.h> | |
| 48 #include <pkgconf/kernel.h> | |
| 49 #include <pkgconf/io_fileio.h> | |
| 50 | |
| 51 #include <cyg/kernel/ktypes.h> // base kernel types | |
| 52 #include <cyg/infra/cyg_trac.h> // tracing macros | |
| 53 #include <cyg/infra/cyg_ass.h> // assertion macros | |
| 54 | |
| 55 #include <stdarg.h> // for fcntl() | |
| 56 | |
| 57 #include "fio.h" // Private header | |
| 58 | |
| 59 #include <cyg/kernel/mutex.hxx> // mutex definitions | |
| 60 | |
| 61 //========================================================================== | |
| 62 // File object locking | |
| 63 | |
| 64 #define LOCK_FILE( fp ) cyg_file_lock( fp, fp->f_syncmode ) | |
| 65 | |
| 66 #define UNLOCK_FILE( fp ) cyg_file_unlock( fp, fp->f_syncmode ) | |
| 67 | |
| 68 //========================================================================== | |
| 69 // Read from a file | |
| 70 | |
| 71 __externC ssize_t read( int fd, void *buf, size_t len ) | |
| 72 { | |
| 73 FILEIO_ENTRY(); | |
| 74 | |
| 75 CYG_CANCELLATION_POINT; | |
| 76 | |
| 77 ssize_t cnt; | |
| 78 int ret; | |
| 79 cyg_file *fp; | |
| 80 | |
| 81 if( len > SSIZE_MAX ) | |
| 82 FILEIO_RETURN(EINVAL); | |
| 83 | |
| 84 fp = cyg_fp_get( fd ); | |
| 85 | |
| 86 if( fp == NULL ) | |
| 87 FILEIO_RETURN(EBADF); | |
| 88 | |
| 89 if( (fp->f_flag & CYG_FREAD) == 0 ) | |
| 90 { | |
| 91 cyg_fp_free( fp ); | |
| 92 FILEIO_RETURN(EBADF); | |
| 93 } | |
| 94 | |
| 95 cyg_uio uio; | |
| 96 cyg_iovec iov; | |
| 97 | |
| 98 iov.iov_base = buf; | |
| 99 iov.iov_len = len; | |
| 100 uio.uio_iov = &iov; | |
| 101 uio.uio_iovcnt = 1; | |
| 102 uio.uio_resid = len; | |
| 103 uio.uio_rw = UIO_READ; | |
| 104 uio.uio_segflg = UIO_USERSPACE; | |
| 105 | |
| 106 cnt = len; | |
| 107 | |
| 108 LOCK_FILE( fp ); | |
| 109 | |
| 110 ret = fp->f_ops->fo_read( fp, &uio ); | |
| 111 | |
| 112 UNLOCK_FILE( fp ); | |
| 113 | |
| 114 cnt -= uio.uio_resid; | |
| 115 | |
| 116 cyg_fp_free( fp ); | |
| 117 | |
| 118 CYG_CANCELLATION_POINT; | |
| 119 | |
| 120 CYG_REPORT_RETVAL(cnt); | |
| 121 return cnt; | |
| 122 } | |
| 123 | |
| 124 //========================================================================== | |
| 125 // Write to a file | |
| 126 | |
| 127 __externC ssize_t write( int fd, const void *buf, size_t len ) | |
| 128 { | |
| 129 FILEIO_ENTRY(); | |
| 130 | |
| 131 CYG_CANCELLATION_POINT; | |
| 132 | |
| 133 ssize_t cnt; | |
| 134 int ret; | |
| 135 cyg_file *fp; | |
| 136 | |
| 137 if( len > SSIZE_MAX ) | |
| 138 FILEIO_RETURN(EINVAL); | |
| 139 | |
| 140 fp = cyg_fp_get( fd ); | |
| 141 | |
| 142 if( fp == NULL ) | |
| 143 FILEIO_RETURN(EBADF); | |
| 144 | |
| 145 if( (fp->f_flag & CYG_FWRITE) == 0) | |
| 146 { | |
| 147 cyg_fp_free( fp ); | |
| 148 FILEIO_RETURN(EBADF); | |
| 149 } | |
| 150 cyg_uio uio; | |
| 151 cyg_iovec iov; | |
| 152 | |
| 153 iov.iov_base = (void *)buf; | |
| 154 iov.iov_len = len; | |
| 155 uio.uio_iov = &iov; | |
| 156 uio.uio_iovcnt = 1; | |
| 157 uio.uio_resid = len; | |
| 158 uio.uio_rw = UIO_WRITE; | |
| 159 uio.uio_segflg = UIO_USERSPACE; | |
| 160 | |
| 161 cnt = len; | |
| 162 | |
| 163 LOCK_FILE( fp ); | |
| 164 | |
| 165 ret = fp->f_ops->fo_write( fp, &uio ); | |
| 166 | |
| 167 UNLOCK_FILE( fp ); | |
| 168 | |
| 169 cnt -= uio.uio_resid; | |
| 170 | |
| 171 cyg_fp_free( fp ); | |
| 172 | |
| 173 CYG_CANCELLATION_POINT; | |
| 174 | |
| 175 CYG_REPORT_RETVAL(cnt); | |
| 176 return cnt; | |
| 177 } | |
| 178 | |
| 179 //========================================================================== | |
| 180 // Close a file | |
| 181 | |
| 182 __externC int close( int fd ) | |
| 183 { | |
| 184 FILEIO_ENTRY(); | |
| 185 | |
| 186 CYG_CANCELLATION_POINT; | |
| 187 | |
| 188 int ret; | |
| 189 cyg_file *fp; | |
| 190 | |
| 191 fp = cyg_fp_get( fd ); | |
| 192 | |
| 193 if( fp == NULL ) | |
| 194 FILEIO_RETURN(EBADF); | |
| 195 | |
| 196 cyg_fp_free( fp ); | |
| 197 | |
| 198 // The file's fo_close entry may be called as a side | |
| 199 // effect of this operation... | |
| 200 ret = cyg_fd_free( fd ); | |
| 201 | |
| 202 CYG_CANCELLATION_POINT; | |
| 203 | |
| 204 FILEIO_RETURN(ret); | |
| 205 } | |
| 206 | |
| 207 //========================================================================== | |
| 208 // Seek a file | |
| 209 | |
| 210 __externC off_t lseek( int fd, off_t pos, int whence ) | |
| 211 { | |
| 212 FILEIO_ENTRY(); | |
| 213 | |
| 214 int ret; | |
| 215 cyg_file *fp; | |
| 216 | |
| 217 fp = cyg_fp_get( fd ); | |
| 218 | |
| 219 if( fp == NULL ) | |
| 220 FILEIO_RETURN(EBADF); | |
| 221 | |
| 222 LOCK_FILE( fp ); | |
| 223 | |
| 224 ret = fp->f_ops->fo_lseek( fp, &pos, whence ); | |
| 225 | |
| 226 UNLOCK_FILE( fp ); | |
| 227 | |
| 228 cyg_fp_free( fp ); | |
| 229 | |
| 230 if( ret != 0 ) | |
| 231 FILEIO_RETURN(ret); | |
| 232 | |
| 233 CYG_REPORT_RETVAL(pos); | |
| 234 return pos; | |
| 235 } | |
| 236 | |
| 237 //========================================================================== | |
| 238 // ioctl | |
| 239 | |
| 240 __externC int ioctl( int fd, CYG_ADDRWORD com, CYG_ADDRWORD data ) | |
| 241 { | |
| 242 FILEIO_ENTRY(); | |
| 243 | |
| 244 int ret; | |
| 245 cyg_file *fp; | |
| 246 | |
| 247 fp = cyg_fp_get( fd ); | |
| 248 | |
| 249 if( fp == NULL ) | |
| 250 FILEIO_RETURN(EBADF); | |
| 251 | |
| 252 LOCK_FILE( fp ); | |
| 253 | |
| 254 ret = fp->f_ops->fo_ioctl( fp, com, data ); | |
| 255 | |
| 256 UNLOCK_FILE( fp ); | |
| 257 | |
| 258 cyg_fp_free( fp ); | |
| 259 | |
| 260 FILEIO_RETURN(ret); | |
| 261 } | |
| 262 | |
| 263 //========================================================================== | |
| 264 // fsync | |
| 265 | |
| 266 __externC int fsync( int fd ) | |
| 267 { | |
| 268 FILEIO_ENTRY(); | |
| 269 | |
| 270 CYG_CANCELLATION_POINT; | |
| 271 | |
| 272 int ret; | |
| 273 cyg_file *fp; | |
| 274 | |
| 275 fp = cyg_fp_get( fd ); | |
| 276 | |
| 277 if( fp == NULL ) | |
| 278 FILEIO_RETURN(EBADF); | |
| 279 | |
| 280 LOCK_FILE( fp ); | |
| 281 | |
| 282 ret = fp->f_ops->fo_fsync( fp, CYG_FSYNC ); | |
| 283 | |
| 284 UNLOCK_FILE( fp ); | |
| 285 | |
| 286 cyg_fp_free( fp ); | |
| 287 | |
| 288 CYG_CANCELLATION_POINT; | |
| 289 | |
| 290 FILEIO_RETURN(ret); | |
| 291 } | |
| 292 | |
| 293 //========================================================================== | |
| 294 // fdatasync() | |
| 295 | |
| 296 __externC int fdatasync( int fd ) | |
| 297 { | |
| 298 FILEIO_ENTRY(); | |
| 299 | |
| 300 CYG_CANCELLATION_POINT; | |
| 301 | |
| 302 int ret; | |
| 303 cyg_file *fp; | |
| 304 | |
| 305 fp = cyg_fp_get( fd ); | |
| 306 | |
| 307 if( fp == NULL ) | |
| 308 FILEIO_RETURN(EBADF); | |
| 309 | |
| 310 LOCK_FILE( fp ); | |
| 311 | |
| 312 ret = fp->f_ops->fo_fsync( fp, CYG_FDATASYNC ); | |
| 313 | |
| 314 UNLOCK_FILE( fp ); | |
| 315 | |
| 316 cyg_fp_free( fp ); | |
| 317 | |
| 318 CYG_CANCELLATION_POINT; | |
| 319 | |
| 320 FILEIO_RETURN(ret); | |
| 321 } | |
| 322 | |
| 323 //========================================================================== | |
| 324 // fstat | |
| 325 | |
| 326 __externC int fstat( int fd, struct stat *buf ) | |
| 327 { | |
| 328 FILEIO_ENTRY(); | |
| 329 | |
| 330 int ret; | |
| 331 cyg_file *fp; | |
| 332 | |
| 333 fp = cyg_fp_get( fd ); | |
| 334 | |
| 335 if( fp == NULL ) | |
| 336 FILEIO_RETURN(EBADF); | |
| 337 | |
| 338 LOCK_FILE( fp ); | |
| 339 | |
| 340 ret = fp->f_ops->fo_fstat( fp, buf ); | |
| 341 | |
| 342 UNLOCK_FILE( fp ); | |
| 343 | |
| 344 cyg_fp_free( fp ); | |
| 345 | |
| 346 FILEIO_RETURN(ret); | |
| 347 } | |
| 348 | |
| 349 //========================================================================== | |
| 350 // fpathconf | |
| 351 | |
| 352 __externC long fpathconf( int fd, int name ) | |
| 353 { | |
| 354 FILEIO_ENTRY(); | |
| 355 | |
| 356 int ret; | |
| 357 cyg_file *fp; | |
| 358 | |
| 359 fp = cyg_fp_get( fd ); | |
| 360 | |
| 361 if( fp == NULL ) | |
| 362 FILEIO_RETURN(EBADF); | |
| 363 | |
| 364 struct cyg_pathconf_info info; | |
| 365 | |
| 366 info.name = name; | |
| 367 info.value = 0; | |
| 368 | |
| 369 LOCK_FILE( fp ); | |
| 370 | |
| 371 ret = fp->f_ops->fo_getinfo( fp, FILE_INFO_CONF, (char *)&info, sizeof(info) ); | |
| 372 | |
| 373 UNLOCK_FILE( fp ); | |
| 374 | |
| 375 cyg_fp_free( fp ); | |
| 376 | |
| 377 if( ret != 0 ) | |
| 378 FILEIO_RETURN(ret); | |
| 379 | |
| 380 CYG_REPORT_RETVAL(info.value); | |
| 381 return info.value; | |
| 382 } | |
| 383 | |
| 384 //========================================================================== | |
| 385 // fcntl | |
| 386 | |
| 387 __externC int fcntl( int fd, int cmd, ... ) | |
| 388 { | |
| 389 FILEIO_ENTRY(); | |
| 390 | |
| 391 CYG_CANCELLATION_POINT; | |
| 392 | |
| 393 int ret = 0; | |
| 394 cyg_file *fp; | |
| 395 va_list a; | |
| 396 | |
| 397 fp = cyg_fp_get( fd ); | |
| 398 | |
| 399 if( fp == NULL ) | |
| 400 FILEIO_RETURN(EBADF); | |
| 401 | |
| 402 va_start( a, cmd ); | |
| 403 | |
| 404 switch( cmd ) | |
| 405 { | |
| 406 case F_DUPFD: | |
| 407 { | |
| 408 int fda = va_arg(a, int); | |
| 409 | |
| 410 if( fda < 0 || fda >= OPEN_MAX ) | |
| 411 { | |
| 412 errno = EBADF; | |
| 413 break; | |
| 414 } | |
| 415 | |
| 416 int fd2 = cyg_fd_alloc( fda ); | |
| 417 | |
| 418 if( fd2 == -1 ) | |
| 419 { | |
| 420 ret = EMFILE; | |
| 421 break; | |
| 422 } | |
| 423 | |
| 424 cyg_fd_assign( fd2, fp ); | |
| 425 | |
| 426 break; | |
| 427 } | |
| 428 | |
| 429 default: | |
| 430 ret = ENOTSUP; | |
| 431 break; | |
| 432 } | |
| 433 | |
| 434 va_end(a); | |
| 435 | |
| 436 cyg_fp_free( fp ); | |
| 437 | |
| 438 CYG_CANCELLATION_POINT; | |
| 439 | |
| 440 FILEIO_RETURN(ret); | |
| 441 } | |
| 442 | |
| 443 //========================================================================== | |
| 444 // isatty() | |
| 445 | |
| 446 __externC int isatty( int fd ) | |
| 447 { | |
| 448 FILEIO_ENTRY(); | |
| 449 | |
| 450 int ret = 0; | |
| 451 struct stat buf; | |
| 452 int err; | |
| 453 | |
| 454 err = fstat( fd, &buf ); | |
| 455 | |
| 456 // Any error and we return zero. If the client wants to | |
| 457 // they can always pick up the error code from errno. | |
| 458 if( err < 0 ) | |
| 459 FILEIO_RETURN_VALUE(0); | |
| 460 | |
| 461 // For now we assume that all char devices are ttys. | |
| 462 // In future we may need to have a special getinfo() | |
| 463 // call to decide this more specifically. | |
| 464 | |
| 465 if( S_ISCHR( buf.st_mode ) ) | |
| 466 ret = 1; | |
| 467 | |
| 468 FILEIO_RETURN_VALUE(ret); | |
| 469 } | |
| 470 | |
| 471 // ------------------------------------------------------------------------- | |
| 472 // EOF io.cxx |
