UnixOperatingSystem.st
changeset 7198 62450598d208
parent 7185 0a3ba41f0d31
child 7203 60fa52f0c2a8
equal deleted inserted replaced
7197:a68adb223592 7198:62450598d208
  9871     INT fd;
  9871     INT fd;
  9872     INT cnt, offs;
  9872     INT cnt, offs;
  9873     int nInstBytes, objSize;
  9873     int nInstBytes, objSize;
  9874 
  9874 
  9875     if (! __isSmallInteger(__INST(fd))) {
  9875     if (! __isSmallInteger(__INST(fd))) {
  9876 	error = @symbol(errorNotOpen);
  9876         error = @symbol(errorNotOpen);
  9877 	goto bad;
  9877         goto bad;
  9878     }
  9878     }
  9879     if (! __bothSmallInteger(count, firstIndex)) {
  9879     if (! __bothSmallInteger(count, firstIndex)) {
  9880 	error = @symbol(badArgument);
  9880         error = @symbol(badArgument);
  9881 	goto bad;
  9881         goto bad;
  9882     }
  9882     }
  9883     fd = __smallIntegerVal(__INST(fd));
  9883     fd = __smallIntegerVal(__INST(fd));
  9884     cnt = __smallIntegerVal(count);
  9884     cnt = __smallIntegerVal(count);
  9885     offs = __smallIntegerVal(firstIndex) - 1;
  9885     offs = __smallIntegerVal(firstIndex) - 1;
  9886 
  9886 
  9887     if (fd < 0) {
  9887     if (fd < 0) {
  9888 	error = @symbol(internalError);
  9888         error = @symbol(internalError);
  9889 	goto bad;
  9889         goto bad;
  9890     }
  9890     }
  9891     if (__isExternalBytes(aByteBuffer)) {
  9891     if (__isExternalBytesLike(aByteBuffer)) {
  9892 	OBJ sz;
  9892         OBJ sz;
  9893 
  9893 
  9894 	nInstBytes = 0;
  9894         nInstBytes = 0;
  9895 	extPtr = (char *)(__externalBytesAddress(aByteBuffer));
  9895         extPtr = (char *)(__externalBytesAddress(aByteBuffer));
  9896 	sz = __externalBytesSize(aByteBuffer);
  9896         sz = __externalBytesSize(aByteBuffer);
  9897 	if (__isSmallInteger(sz)) {
  9897         if (__isSmallInteger(sz)) {
  9898 	    objSize = __smallIntegerVal(sz);
  9898             objSize = __smallIntegerVal(sz);
  9899 	} else {
  9899         } else {
  9900 	    objSize = -1; /* unknown */
  9900             objSize = -1; /* unknown */
  9901 	}
  9901         }
  9902     } else {
  9902     } else {
  9903 	OBJ oClass;
  9903         OBJ oClass;
  9904 	int nInstVars;
  9904         int nInstVars;
  9905 
  9905 
  9906 	oClass = __Class(aByteBuffer);
  9906         oClass = __Class(aByteBuffer);
  9907 	switch (__smallIntegerVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
  9907         switch (__smallIntegerVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
  9908 	    case BYTEARRAY:
  9908             case BYTEARRAY:
  9909 	    case WORDARRAY:
  9909             case WORDARRAY:
  9910 	    case LONGARRAY:
  9910             case LONGARRAY:
  9911 	    case SWORDARRAY:
  9911             case SWORDARRAY:
  9912 	    case SLONGARRAY:
  9912             case SLONGARRAY:
  9913 	    case FLOATARRAY:
  9913             case FLOATARRAY:
  9914 	    case DOUBLEARRAY:
  9914             case DOUBLEARRAY:
  9915 		break;
  9915                 break;
  9916 	    default:
  9916             default:
  9917 		error = @symbol(badArgument1);
  9917                 error = @symbol(badArgument1);
  9918 		goto bad;
  9918                 goto bad;
  9919 	}
  9919         }
  9920 	extPtr = (char *)0;
  9920         extPtr = (char *)0;
  9921 	nInstVars = __smallIntegerVal(__ClassInstPtr(oClass)->c_ninstvars);
  9921         nInstVars = __smallIntegerVal(__ClassInstPtr(oClass)->c_ninstvars);
  9922 	nInstBytes = __OBJS2BYTES__(nInstVars);
  9922         nInstBytes = __OBJS2BYTES__(nInstVars);
  9923 	objSize = __Size(aByteBuffer) - OHDR_SIZE - nInstBytes;
  9923         objSize = __Size(aByteBuffer) - OHDR_SIZE - nInstBytes;
  9924     }
  9924     }
  9925     if ((offs >= 0)
  9925     if ((offs >= 0)
  9926 	&& (cnt >= 0)
  9926         && (cnt >= 0)
  9927 	&& ((objSize == -1) || (objSize >= (cnt + offs)))) {
  9927         && ((objSize == -1) || (objSize >= (cnt + offs)))) {
  9928 	nRead = 0;
  9928         nRead = 0;
  9929 
  9929 
  9930 	do {
  9930         do {
  9931 	    int n;
  9931             int n;
  9932 
  9932 
  9933 	    if (extPtr) {
  9933             if (extPtr) {
  9934 		n = read(fd, extPtr+offs, cnt);
  9934                 n = read(fd, extPtr+offs, cnt);
  9935 	    } else {
  9935             } else {
  9936 		char *bp;
  9936                 char *bp;
  9937 
  9937 
  9938 		/*
  9938                 /*
  9939 		 * on interrupt, anObject may be moved to another location.
  9939                  * on interrupt, anObject may be moved to another location.
  9940 		 * So we recompute the byte-address here.
  9940                  * So we recompute the byte-address here.
  9941 		 */
  9941                  */
  9942 		bp = __byteArrayVal(aByteBuffer) + nInstBytes;
  9942                 bp = __byteArrayVal(aByteBuffer) + nInstBytes;
  9943 
  9943 
  9944 		n = read(fd, bp + offs, cnt);
  9944                 n = read(fd, bp + offs, cnt);
  9945 	    }
  9945             }
  9946 	    if (n > 0) {
  9946             if (n > 0) {
  9947 		cnt -= n;
  9947                 cnt -= n;
  9948 		offs += n;
  9948                 offs += n;
  9949 		nRead += n;
  9949                 nRead += n;
  9950 	    } else if (n == 0) {
  9950             } else if (n == 0) {
  9951 		break;
  9951                 break;
  9952 	    } else if (n < 0) {
  9952             } else if (n < 0) {
  9953 		if (0
  9953                 if (0
  9954 #ifdef EWOULDBLOCK
  9954 #ifdef EWOULDBLOCK
  9955 		    || errno == EWOULDBLOCK
  9955                     || errno == EWOULDBLOCK
  9956 #endif
  9956 #endif
  9957 #ifdef EAGAIN
  9957 #ifdef EAGAIN
  9958 		    || errno == EAGAIN
  9958                     || errno == EAGAIN
  9959 #endif
  9959 #endif
  9960 		) {
  9960                 ) {
  9961 		     RETURN(nil);
  9961                      RETURN(nil);
  9962 		}
  9962                 }
  9963 		if (errno != EINTR) {
  9963                 if (errno != EINTR) {
  9964 		     error = __mkSmallInteger(errno);
  9964                      error = __mkSmallInteger(errno);
  9965 		     goto bad;
  9965                      goto bad;
  9966 		}
  9966                 }
  9967 		__HANDLE_INTERRUPTS__;
  9967                 __HANDLE_INTERRUPTS__;
  9968 	    }
  9968             }
  9969 	} while (cnt > 0);
  9969         } while (cnt > 0);
  9970 
  9970 
  9971 	RETURN (__mkSmallInteger(nRead));
  9971         RETURN (__mkSmallInteger(nRead));
  9972     }
  9972     }
  9973 bad: ;   
  9973 bad: ;   
  9974 %}.
  9974 %}.
  9975     ^ self error:error.
  9975     ^ self error:error.
  9976 
  9976 
 10005     INT fd;
 10005     INT fd;
 10006     INT cnt, offs;
 10006     INT cnt, offs;
 10007     int nInstBytes, objSize;
 10007     int nInstBytes, objSize;
 10008 
 10008 
 10009     if (! __isSmallInteger(__INST(fd))) {
 10009     if (! __isSmallInteger(__INST(fd))) {
 10010 	error = @symbol(errorNotOpen);
 10010         error = @symbol(errorNotOpen);
 10011 	goto bad;
 10011         goto bad;
 10012     }
 10012     }
 10013     if (! __bothSmallInteger(count, firstIndex)) {
 10013     if (! __bothSmallInteger(count, firstIndex)) {
 10014 	error = @symbol(badArgument);
 10014         error = @symbol(badArgument);
 10015 	goto bad;
 10015         goto bad;
 10016     }
 10016     }
 10017     fd = __smallIntegerVal(__INST(fd));
 10017     fd = __smallIntegerVal(__INST(fd));
 10018     cnt = __smallIntegerVal(count);
 10018     cnt = __smallIntegerVal(count);
 10019     offs = __smallIntegerVal(firstIndex) - 1;
 10019     offs = __smallIntegerVal(firstIndex) - 1;
 10020 
 10020 
 10021     if (fd < 0) {
 10021     if (fd < 0) {
 10022 	error = @symbol(internalError);
 10022         error = @symbol(internalError);
 10023 	goto bad;
 10023         goto bad;
 10024     }
 10024     }
 10025     if (__isExternalBytes(aByteBuffer)) {
 10025     if (__isExternalBytesLike(aByteBuffer)) {
 10026 	OBJ sz;
 10026         OBJ sz;
 10027 
 10027 
 10028 	nInstBytes = 0;
 10028         nInstBytes = 0;
 10029 	extPtr = (char *)(__externalBytesAddress(aByteBuffer));
 10029         extPtr = (char *)(__externalBytesAddress(aByteBuffer));
 10030 	sz = __externalBytesSize(aByteBuffer);
 10030         sz = __externalBytesSize(aByteBuffer);
 10031 	if (__isSmallInteger(sz)) {
 10031         if (__isSmallInteger(sz)) {
 10032 	    objSize = __smallIntegerVal(sz);
 10032             objSize = __smallIntegerVal(sz);
 10033 	} else {
 10033         } else {
 10034 	    objSize = -1; /* unknown */
 10034             objSize = -1; /* unknown */
 10035 	}
 10035         }
 10036     } else {
 10036     } else {
 10037 	OBJ oClass;
 10037         OBJ oClass;
 10038 	int nInstVars;
 10038         int nInstVars;
 10039 
 10039 
 10040 	oClass = __Class(aByteBuffer);
 10040         oClass = __Class(aByteBuffer);
 10041 	switch (__smallIntegerVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
 10041         switch (__smallIntegerVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
 10042 	    case BYTEARRAY:
 10042             case BYTEARRAY:
 10043 	    case WORDARRAY:
 10043             case WORDARRAY:
 10044 	    case LONGARRAY:
 10044             case LONGARRAY:
 10045 	    case SWORDARRAY:
 10045             case SWORDARRAY:
 10046 	    case SLONGARRAY:
 10046             case SLONGARRAY:
 10047 	    case FLOATARRAY:
 10047             case FLOATARRAY:
 10048 	    case DOUBLEARRAY:
 10048             case DOUBLEARRAY:
 10049 		break;
 10049                 break;
 10050 	    default:
 10050             default:
 10051 		error = @symbol(badArgument1);
 10051                 error = @symbol(badArgument1);
 10052 		goto bad;
 10052                 goto bad;
 10053 	}
 10053         }
 10054 	extPtr = (char *)0;
 10054         extPtr = (char *)0;
 10055 	nInstVars = __smallIntegerVal(__ClassInstPtr(oClass)->c_ninstvars);
 10055         nInstVars = __smallIntegerVal(__ClassInstPtr(oClass)->c_ninstvars);
 10056 	nInstBytes = __OBJS2BYTES__(nInstVars);
 10056         nInstBytes = __OBJS2BYTES__(nInstVars);
 10057 	objSize = __Size(aByteBuffer) - OHDR_SIZE - nInstBytes;
 10057         objSize = __Size(aByteBuffer) - OHDR_SIZE - nInstBytes;
 10058     }
 10058     }
 10059     if ((offs >= 0)
 10059     if ((offs >= 0)
 10060 	&& (cnt >= 0)
 10060         && (cnt >= 0)
 10061 	&& ((objSize == -1) || (objSize >= (cnt + offs)))) {
 10061         && ((objSize == -1) || (objSize >= (cnt + offs)))) {
 10062 	nWritten = 0;
 10062         nWritten = 0;
 10063 
 10063 
 10064 	do {
 10064         do {
 10065 	    int n;
 10065             int n;
 10066 
 10066 
 10067 	    if (extPtr) {
 10067             if (extPtr) {
 10068 		n = write(fd, extPtr+offs, cnt);
 10068                 n = write(fd, extPtr+offs, cnt);
 10069 	    } else {
 10069             } else {
 10070 		char *bp;
 10070                 char *bp;
 10071 
 10071 
 10072 		/*
 10072                 /*
 10073 		 * on interrupt, anObject may be moved to another location.
 10073                  * on interrupt, anObject may be moved to another location.
 10074 		 * So we recompute the byte-address here.
 10074                  * So we recompute the byte-address here.
 10075 		 */
 10075                  */
 10076 		bp = __byteArrayVal(aByteBuffer) + nInstBytes;
 10076                 bp = __byteArrayVal(aByteBuffer) + nInstBytes;
 10077 
 10077 
 10078 		n = write(fd, bp + offs, cnt);
 10078                 n = write(fd, bp + offs, cnt);
 10079 	    }
 10079             }
 10080 	    if (n > 0) {
 10080             if (n > 0) {
 10081 		cnt -= n;
 10081                 cnt -= n;
 10082 		offs += n;
 10082                 offs += n;
 10083 		nWritten += n;
 10083                 nWritten += n;
 10084 	    } else if (n == 0) {
 10084             } else if (n == 0) {
 10085 		break;
 10085                 break;
 10086 	    } else if (n < 0) {
 10086             } else if (n < 0) {
 10087 		if (0
 10087                 if (0
 10088 #ifdef EWOULDBLOCK
 10088 #ifdef EWOULDBLOCK
 10089 		    || errno == EWOULDBLOCK
 10089                     || errno == EWOULDBLOCK
 10090 #endif
 10090 #endif
 10091 #ifdef EAGAIN
 10091 #ifdef EAGAIN
 10092 		    || errno == EAGAIN
 10092                     || errno == EAGAIN
 10093 #endif
 10093 #endif
 10094 		) {
 10094                 ) {
 10095 		     RETURN(nil);
 10095                      RETURN(nil);
 10096 		}
 10096                 }
 10097 		if (errno != EINTR) {
 10097                 if (errno != EINTR) {
 10098 		     error = __mkSmallInteger(errno);
 10098                      error = __mkSmallInteger(errno);
 10099 		     goto bad;
 10099                      goto bad;
 10100 		}
 10100                 }
 10101 		__HANDLE_INTERRUPTS__;
 10101                 __HANDLE_INTERRUPTS__;
 10102 	    }
 10102             }
 10103 	} while (cnt > 0);
 10103         } while (cnt > 0);
 10104 
 10104 
 10105 	RETURN (__mkSmallInteger(nWritten));
 10105         RETURN (__mkSmallInteger(nWritten));
 10106     }
 10106     }
 10107 bad: ;   
 10107 bad: ;   
 10108 %}.
 10108 %}.
 10109     ^ self error:error
 10109     ^ self error:error
 10110 
 10110 
 12786 ! !
 12786 ! !
 12787 
 12787 
 12788 !UnixOperatingSystem class methodsFor:'documentation'!
 12788 !UnixOperatingSystem class methodsFor:'documentation'!
 12789 
 12789 
 12790 version
 12790 version
 12791     ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.156 2003-04-10 11:03:26 penk Exp $'
 12791     ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.157 2003-04-10 23:37:49 cg Exp $'
 12792 ! !
 12792 ! !
 12793 
 12793 
 12794 UnixOperatingSystem initialize!
 12794 UnixOperatingSystem initialize!
 12795 UnixOperatingSystem::FileDescriptorHandle initialize!
 12795 UnixOperatingSystem::FileDescriptorHandle initialize!