iswrite.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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: iswrite.c /main/3 1995/10/23 11:46:15 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * iswrite.c
  33. *
  34. * Description:
  35. * Write a record to ISAM file.
  36. */
  37. #include "isam_impl.h"
  38. #include <sys/file.h>
  39. #include <sys/time.h>
  40. static int _am_write();
  41. /*
  42. * err = iswrite(isfd, record)
  43. *
  44. * Iswrite() adds a new record to an ISAM file. All indexes of the ISAM
  45. * file are updated.
  46. *
  47. * Current record position is not changed.
  48. * isrecnum is set to indicate the new record.
  49. *
  50. * If the ISAM file is for variable length records, the isreclen variable
  51. * must be set to indicate the actual length of the record, which must
  52. * be between the minimum and maximum length, as specified in isbuild().
  53. *
  54. * Returns 0 if successful, or -1 of any error.
  55. *
  56. * Errors:
  57. * EDUPL The write woul result in a duplicate on a key that
  58. * does not allow duplicates.
  59. * ELOCKED The file has been locked by another process.
  60. * ENOTOPEN isfd does not correspond to an open ISAM file, or the
  61. * ISAM file was opened with ISINPUT mode.
  62. * EBADARG Unacceptable value of isreclen for variable length record.
  63. */
  64. int
  65. iswrite(int isfd, char *record)
  66. {
  67. int _am_write();
  68. Fab *fab;
  69. int reclen;
  70. Recno recnum;
  71. Bytearray curpos;
  72. int ret;
  73. /*
  74. * Get File Access Block.
  75. */
  76. if ((fab = _isfd_find(isfd)) == NULL) {
  77. _setiserrno2(ENOTOPEN, '9', '0');
  78. return (ISERROR);
  79. }
  80. /*
  81. * Check that the open mode was ISOUTPUT, or ISINOUT.
  82. */
  83. if (fab->openmode != OM_OUTPUT && fab->openmode != OM_INOUT) {
  84. _setiserrno2(ENOTOPEN, '9', '0');
  85. return (ISERROR);
  86. }
  87. /*
  88. * Determine record length. Check it against min and max record length.
  89. */
  90. reclen = (fab->varlength == TRUE) ? isreclen : fab->minreclen;
  91. if (reclen < fab->minreclen || reclen > fab->maxreclen) {
  92. _setiserrno2(EBADARG, '9', '0');
  93. return (ISERROR);
  94. }
  95. /*
  96. * Call the Access Method
  97. */
  98. curpos = _bytearr_dup(&fab->curpos);
  99. if ((ret = _am_write(fab, record, reclen, &curpos, &recnum)) == ISOK) {
  100. isrecnum = recnum; /* Set isrecnum */
  101. }
  102. _bytearr_free(&curpos);
  103. _seterr_errcode(&fab->errcode);
  104. return (ret); /* Successful write */
  105. }
  106. Static int _am_write(Fab *fab, char *record, int reclen,
  107. Bytearray *curpos, Recno *recnum)
  108. {
  109. return (_amwrite(&fab->isfhandle, record, reclen,
  110. curpos, recnum, &fab->errcode));
  111. }