2
0

isopen.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
  24. /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
  25. /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
  26. /*%% (c) Copyright 1993, 1994 Novell, Inc. */
  27. /*%% $XConsortium: isopen.c /main/3 1995/10/23 11:42:40 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isopen.c
  33. *
  34. * Description:
  35. * Open an ISAM file.
  36. */
  37. #include "isam_impl.h"
  38. #include <netdb.h>
  39. #include <sys/file.h>
  40. #include <sys/time.h>
  41. static int _am_open();
  42. /*
  43. * isfd = isopen(isfname, mode)
  44. *
  45. * Isopen() determines on which machine the ISAM file resides,
  46. * checks if the file exists, creates a File access block,
  47. * and initilizes it. It also checks permissions. Returns an ISAM file
  48. * descriptor (isfd), or a value of -1 if the open failed.
  49. *
  50. * Errors:
  51. * EBADARG Improper mode parameter
  52. * EBADFILE ISAM file is corrupted or it is not an NetISAM file
  53. * EFLOCKED The file is exclusively locked by other process.
  54. * EFNAME Invalid ISAM file name
  55. * EFNAME ISAM file does not exist
  56. * ETOOMANY Too many ISAM file descriptors are in use (128 is the limit)
  57. *
  58. * The following error code is "borrowed" from UNIX:
  59. * EACCES UNIX file system protection denies access to the file:
  60. * - mode is INOUT or OUTPUT and ISAM file is on
  61. * a Read-Only mounted file system
  62. * - UNIX file permissions don't allow access to the file
  63. */
  64. int
  65. isopen(char *isfname, int mode)
  66. {
  67. Fab *fab;
  68. Isfd isfd;
  69. enum openmode openmode;
  70. /*
  71. * Check if the user is allowed to access the ISAM file.
  72. * Use UNIX and NFS permissions.
  73. */
  74. /* Get file open mode part of the mode parameter. */
  75. if ((openmode = _getopenmode(mode)) == OM_BADMODE) {
  76. _setiserrno2(EBADARG, '9', '0');
  77. return (NOISFD);
  78. }
  79. /* Create a Fab object. */
  80. fab = _fab_new(isfname,
  81. openmode,
  82. (Bool)((mode & ISLENMODE) == ISVARLEN),
  83. 0,
  84. 0);
  85. if (fab == NULL) {
  86. return (NOISFD); /* iserrno is set by fab_new */
  87. }
  88. /* Get an ISAM file descriptor for this fab */
  89. if ((isfd = _isfd_insert(fab)) == NOISFD) {
  90. /* Table of ISAM file descriptors would overflow. */
  91. _fab_destroy(fab);
  92. _setiserrno2(ETOOMANY, '9', '0');
  93. return (NOISFD);
  94. }
  95. FAB_ISFDSET(fab, isfd);
  96. if (_am_open(fab)) {
  97. _seterr_errcode(&fab->errcode);
  98. _fab_destroy(fab);
  99. return (NOISFD);
  100. }
  101. isreclen = fab->maxreclen;
  102. return ((int)isfd); /* Successful isopen() */
  103. }
  104. Static int _am_open(Fab *fab)
  105. {
  106. return (_amopen(fab->isfname, fab->openmode, &fab->varlength,
  107. &fab->minreclen, &fab->maxreclen, &fab->isfhandle,
  108. &fab->curpos, &fab->errcode));
  109. }