00001 /* 00002 00003 Copyright (C) 2000, 2001 Silicon Graphics, Inc. All Rights Reserved. 00004 00005 This program is free software; you can redistribute it and/or modify it 00006 under the terms of version 2.1 of the GNU Lesser General Public License 00007 as published by the Free Software Foundation. 00008 00009 This program is distributed in the hope that it would be useful, but 00010 WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00012 00013 Further, this software is distributed without any warranty that it is 00014 free of the rightful claim of any third person regarding infringement 00015 or the like. Any license provided herein, whether implied or 00016 otherwise, applies only to this software file. Patent licenses, if 00017 any, provided herein do not apply to combinations of this program with 00018 other software, or any other product whatsoever. 00019 00020 You should have received a copy of the GNU Lesser General Public 00021 License along with this program; if not, write the Free Software 00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, 00023 USA. 00024 00025 Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pky, 00026 Mountain View, CA 94043, or: 00027 00028 http://www.sgi.com 00029 00030 For further information regarding this notice, see: 00031 00032 http://oss.sgi.com/projects/GenInfo/NoticeExplan 00033 00034 */ 00035 00036 00037 #pragma ident "@(#) libf/pxf/pxfwrite.c 92.3 10/29/99 21:39:27" 00038 /* 00039 * PXFWRITE -- Write a File 00040 * (section 6.5.1 of Posix 1003.9-1992) 00041 * 00042 * Synopsis: 00043 * 00044 * SUBROUTINE PXFWRITE(ifildes, buf, nbyte, nwritten, ierror) 00045 * INTEGER ifildes, nbyte, nwritten, ierror 00046 * CHARACTER BUF(*) 00047 * 00048 * Description: 00049 * 00050 * PXFWRITE uses the write() function to write nbytes to a file 00051 * associated with ifildes from a buffer, return the actual 00052 * number of bytes written, and return the error status. 00053 * 00054 * The arguments are: 00055 * 00056 * ifildes - default integer input variable containing a file 00057 * descriptor. 00058 * buf - output character variable containing the data to be 00059 * written. 00060 * nbyte - default input integer variable specifying the 00061 * number of bytes to be written. 00062 * nwritten - default output integer variable containing the 00063 * number of bytes actually written. 00064 * ierror - default integer output variable that contains zero 00065 * if the operation was successful or nonzero if the 00066 * operation was not successful. 00067 * 00068 * PXFWRITE may return one of the following error values: 00069 * 00070 * EAGAIN Mandatory file and record locking was set, O_NDELAY was 00071 * set, and there was a blocking record lock. 00072 * 00073 * EBADF If ifildes is not a valid file descriptor open for 00074 * writing. 00075 * 00076 * EDEADLK If the write was going to go to sleep and would cause 00077 * a deadlock situation to occur. 00078 * 00079 * EFAULT If buf points outside the allocated process address 00080 * space. 00081 * 00082 * EFBIG If an attempt was made to write a file that exceeds the 00083 * file size limit or the maximum file size of the process. 00084 * 00085 * EINTR If write was interrupted by a signal. 00086 * 00087 * EINVAL If the call contains an argument that is not valid. 00088 * 00089 * ENOLCK If the sytem record lock table was full, the write 00090 * could not go to sleep until the block record lock was 00091 * removed. 00092 * 00093 * ENOMEM If PXFWRITE is unable to obtain memory to create an 00094 * internal buffer. 00095 * 00096 * ENOSPC If there is no free space on the device containing the 00097 * file. 00098 * 00099 * EPIPE If attempting to write to a pipe that is not open for 00100 * reading by any process. 00101 * 00102 * On PVP systems, PXFWRITE may also return: 00103 * 00104 * EQACT If a file or inode quota limit was reached for the 00105 * current account ID. 00106 * 00107 * EQGRP If a file or inode quota limit was reached for the 00108 * current group ID. 00109 * 00110 * EQUSR If a file or inode quota limit was reached for the 00111 * current user ID. 00112 * 00113 * On IRIX systems, PXFWRITE may also return: 00114 * 00115 * EIO If a physical I/O error has occurred, or the read is 00116 * cannot access the device. or If ifildes has O_DIRECT 00117 * or FDIRECT set and nbytes is greater than the number 00118 * of bytes between the current file pointer position 00119 * and the end of file. 00120 * 00121 * ENOSR If there is no free space for the STREAMS memory 00122 * resources. 00123 * 00124 * ENOXIO If the file pointer is out of range for a special 00125 * file. 00126 * 00127 * ERANGE If an attempt is made to write toa stream with nbyte 00128 * outside the mininum and maximun write range when the 00129 * mininum value is nonzero. 00130 * 00131 * ETIMEDOUT If the object of the write is located on a remote 00132 * system which is not available. 00133 * 00134 */ 00135 00136 #include <fortran.h> 00137 #include <errno.h> 00138 #include <liberrno.h> 00139 #include <string.h> 00140 #include <sys/types.h> 00141 #include <unistd.h> 00142 /* for malloc */ 00143 #ifdef _LITTLE_ENDIAN 00144 #include <stdlib.h> 00145 #endif 00146 00147 #ifdef _UNICOS 00148 void 00149 PXFWRITE( 00150 #else /* _UNICOS */ 00151 void 00152 _PXFWRITE( 00153 #endif /* _UNICOS */ 00154 _f_int *ifildes, 00155 _fcd buf, 00156 _f_int *nbyte, 00157 _f_int *nwritten, 00158 _f_int *ierror) 00159 { 00160 char *buffr; 00161 size_t errsts = 0; 00162 size_t tobewritten; 00163 size_t waswritten; 00164 tobewritten = (size_t)*nbyte; 00165 *ierror = 0; 00166 *nwritten = 0; 00167 buffr = NULL; 00168 if ((int)tobewritten <= 0) 00169 return; 00170 buffr = (char *) malloc(tobewritten); 00171 00172 /* return an error if no memory allocated. */ 00173 if (buffr == NULL) 00174 errsts = ENOMEM; 00175 else { 00176 /* In Fortran 77, a variable is not be an array. 00177 * Therefore, buf cannot be more than a character 00178 * scalar in the 1003.9 standard. Fortran 90 00179 * extended the definition of variable to include 00180 * an array. 00181 * 00182 * Extend PXFWRITE to allow nbyte to be greater 00183 * than the size of buf to allow buf to be an 00184 * array. A subroutine interface does not pass 00185 * the number of elements in the array and 00186 * overindexing is legal in Fortran. Sections 00187 * will be contiguous when passed. 00188 */ 00189 (void) memcpy(buffr, _fcdtocp(buf), tobewritten); 00190 waswritten = write(*ifildes, buffr, tobewritten); 00191 if ((int)waswritten < 0) 00192 errsts = errno; 00193 *nwritten = waswritten; 00194 free(buffr); 00195 } 00196 *ierror = (_f_int)errsts; 00197 return; 00198 } 00199 00200 #ifndef _UNICOS 00201 void 00202 pxfwrite_( 00203 _f_int *ifildes, 00204 char *buf, 00205 _f_int *nbyte, 00206 _f_int *nwritten, 00207 _f_int *ierror, 00208 int lenbuf) 00209 { 00210 _PXFWRITE(ifildes, _cptofcd(buf, lenbuf), nbyte, nwritten, ierror); 00211 } 00212 00213 void 00214 pxfwrite64_( 00215 _f_int8 *ifildes, 00216 char *buf, 00217 _f_int8 *nbyte, 00218 _f_int8 *nwritten, 00219 _f_int8 *ierror, 00220 int lenbuf) 00221 { 00222 _f_int4 ifildes4; 00223 _f_int4 nbyte4; 00224 _f_int4 nwritten4; 00225 _f_int4 ierror4; 00226 ifildes4 = *ifildes; 00227 nbyte4 = *nbyte; 00228 _PXFWRITE(&ifildes4, _cptofcd(buf, lenbuf), &nbyte4, 00229 &nwritten4, &ierror4); 00230 } 00231 #elif defined(_UNICOS) && defined(_CRAYMPP) 00232 /* assume integer(kind=4) arguments and default 64-bit integer */ 00233 void 00234 PXFWRITE32( 00235 _f_int4 *ifildes, 00236 _fcd buf, 00237 _f_int4 *nbyte, 00238 _f_int4 *nwritten, 00239 _f_int4 *ierror) 00240 { 00241 _f_int ifildes8; 00242 _f_int nbyte8; 00243 _f_int nwritten8; 00244 _f_int ierror8; 00245 ifildes8 = *ifildes; 00246 nbyte8 = *nbyte; 00247 PXFWRITE(&ifildes8, buf, &nbyte8, &nwritten8, &ierror8); 00248 *nwritten = nwritten8; 00249 *ierror = ierror8; 00250 } 00251 #endif /* end _UNICOS and _CRAYMPP */
1.5.6