int __write(int fd, const void *buffer, unsigned int length) { struct UFB *ufb; int count, totcount; char *ptr; /* * Check for the break signals */ __chkabort(); /* * find the ufb * */ if ((ufb = __chkufb(fd)) == NULL) { errno = EINVAL; return -1; } /* * Check if write is allowed */ if (!(ufb->ufbflg & UFB_WA)) { _OSERR = ERROR_WRITE_PROTECTED; errno = EIO; return -1; } /* * Seek to end of the file if necessary */ if (ufb->ufbflg & UFB_APP) __lseek(fd, 0, 2); /* * Check if translation is not needed */ if (!(ufb->ufbflg & UFB_XLAT) || (ptr = memchr(buffer, 0x0A, length)) == NULL) { if (ufb->ufbflg & UFB_SOCK) { if ((count = send(fd, (char *)buffer, length, 0)) < 0) return -1; } else { if ((count = Write(ufb->ufbfh, (void *)buffer, length)) == -1) goto osfail; } return count; } totcount = length; /* * Translate, ie., append CR before each LF */ do { count = ptr - (char *)buffer; if (ufb->ufbflg & UFB_SOCK) { if (send(fd, (char *)buffer, count, 0) < 0) return -1; if (send(fd, "\015"/* CR */, 1, 0) < 0) return -1; } else { if (Write(ufb->ufbfh, (void *)buffer, count) == -1) goto osfail; if (Write(ufb->ufbfh, "\015"/* CR */, 1) == -1) goto osfail; } length -= count; buffer = ptr; } while ((ptr = memchr((char *)buffer + 1, 0x0A, length)) != NULL); if (ufb->ufbflg & UFB_SOCK) { if ((count = send(fd, (char *)buffer, length, 0)) < 0) return -1; } else { if (Write(ufb->ufbfh, (void *)buffer, length) == -1) goto osfail; } return totcount; osfail: errno = __io2errno(_OSERR = IoErr()); return -1; }
/* * I know, the goto's are ugly, but they make the code smaller and help * to prevent duplicating code. */ int access(const char *name, int mode) { BPTR lock, parentLock; LONG prot; UBYTE bytes[sizeof(struct FileInfoBlock) + sizeof(struct InfoData) + 3]; struct FileInfoBlock *fib; struct InfoData *info; /* * align the data areas */ fib = (struct FileInfoBlock *) (((ULONG) bytes+3) & (0xFFFFFFFF-3)); info = (struct InfoData *) (ULONG)(fib + 1); /* * Lock the file (or directory) */ if ((lock = Lock((STRPTR)name, SHARED_LOCK)) == NULL) goto osfail; if (!Examine(lock, fib)) goto osfail; prot = fib->fib_Protection; /* * Check each access mode */ if (mode & R_OK && prot & FIBF_READ) { errno = EACCES; goto fail; } if (mode & W_OK) { /* * Check for write protected disks */ if (!Info(lock, info)) goto osfail; if (info->id_DiskState == ID_WRITE_PROTECTED) { errno = EROFS; goto fail; } /* * not write protected: Check if the lock is to the root of the * disk, if it is, force writing to be allowed. * Check if the lock is a directory before taking ParentDir() */ if (fib->fib_DirEntryType >= 0) { /* lock is a directory */ parentLock = ParentDir(lock); if (parentLock != NULL) UnLock(parentLock); /* not the root, prot is valid */ else prot &= ~FIBF_WRITE; /* the root, force writing to be allowed */ } if (prot & FIBF_WRITE) { errno = EACCES; goto fail; } } if (mode & X_OK && prot & FIBF_EXECUTE) { errno = EACCES; goto fail; } /* F_OK */ UnLock(lock); return 0; osfail: #if __SASC errno = __io2errno(_OSERR = IoErr()); #else _ug_set_errno(IoErr()); #endif fail: if (lock != NULL) UnLock(lock); return -1; }