/* * Writes to a file. */ PUBLIC ssize_t sys_write(int fd, const void *buf, size_t n) { dev_t dev; /* Device number. */ struct file *f; /* File. */ struct inode *i; /* Inode. */ ssize_t count = 0; /* Bytes actually written. */ /* Invalid file descriptor. */ if ((fd < 0) || (fd >= OPEN_MAX) || ((f = curr_proc->ofiles[fd]) == NULL)) return (-EBADF); /* File not opened for writing. */ if (ACCMODE(f->oflag) == O_RDONLY) return (-EBADF); /* Invalid buffer. */ if (!chkmem(buf, n, MAY_READ)) return (-EINVAL); i = f->inode; /* Append mode. */ if (f->oflag & O_APPEND) f->pos = i->size; /* Character special file. */ if (S_ISCHR(i->mode)) { dev = i->blocks[0]; count = cdev_write(dev, buf, n); return (count); } /* Block special file. */ else if (S_ISBLK(i->mode)) { dev = i->blocks[0]; count = bdev_write(dev, buf, n, f->pos); } /* Pipe file. */ else if (S_ISFIFO(i->mode)) { kprintf("write to pipe"); count = pipe_write(i, buf, n); } /* Regular file. */ else if (S_ISREG(i->mode)) count = file_write(i, buf, n, f->pos); /* Failed to write. */ if (count < 0) return (curr_proc->errno); f->pos += count; return (count); }
int ptyfs_write(void *v) { struct vop_write_args /* { struct vnode *a_vp; struct uio *a_uio; int a_ioflag; kauth_cred_t a_cred; } */ *ap = v; struct timespec ts; struct vnode *vp = ap->a_vp; struct ptyfsnode *ptyfs = VTOPTYFS(vp); int error; ptyfs->ptyfs_flag |= PTYFS_MODIFY; getnanotime(&ts); (void)ptyfs_update(vp, &ts, &ts, 0); switch (ptyfs->ptyfs_type) { case PTYFSpts: case PTYFSptc: VOP_UNLOCK(vp, 0); error = cdev_write(vp->v_rdev, ap->a_uio, ap->a_ioflag); vn_lock(vp, LK_RETRY|LK_EXCLUSIVE); return error; default: return EOPNOTSUPP; } }
/* ARGSUSED */ static int cnwrite(dev_t dev, struct uio *uio, struct cred *cred) { if (rconsvp == NULL) { uio->uio_resid = 0; return (0); } /* * Output to virtual console for logging if enabled. */ if (vsconsvp != NULL && vsconsvp->v_stream != NULL) { struiod_t uiod; /* * strwrite modifies uio so need to make copy. */ (void) uiodup(uio, &uiod.d_uio, uiod.d_iov, sizeof (uiod.d_iov) / sizeof (*uiod.d_iov)); (void) strwrite(vsconsvp, &uiod.d_uio, cred); } if (rconsvp->v_stream != NULL) return (strwrite(rconsvp, uio, cred)); else return (cdev_write(rconsdev, uio, cred)); }
/** * @brief Writes a message to the kernel's output device and panics the kernel. * * @param fmt Formatted message to be written onto kernel's output device. */ PUBLIC void kpanic(const char *fmt, ...) { int i; /* Loop index. */ va_list args; /* Variable arguments list. */ char buffer[KBUFFER_SIZE + 1]; /* Temporary buffer. */ kstrncpy(buffer, "PANIC: ", 7); /* Convert to raw string. */ va_start(args, fmt); i = kvsprintf(buffer + 7, fmt, args) + 7; buffer[i++] = '\n'; va_end(args); /* Save on kernel log and write on kout. */ cdev_write(kout, buffer, i); klog_write(buffer, i); /* * Disable interrupts, so we cannot * be bothered. */ disable_interrupts(); while(1); halt(); }
int cnwrite(dev_t dev, struct uio *uio, int flag) { int error; /* Redirect output, if that's appropriate. */ if (!cn_redirect(&dev, 0, &error)) return error; return cdev_write(dev, uio, flag); }
/** * @brief Changes kernel's output device. * * @param dev New output device. */ PUBLIC void chkout(dev_t dev) { ssize_t n; /* Number of bytes to be flushed. */ char buffer[KBUFFER_SIZE]; /* Temporary buffer. */ kout = dev; /* Flush the content of kernel log. */ n = klog_read(0, buffer, KLOG_SIZE); cdev_write(kout, buffer, n); }
DRESULT disk_write(FATFS *fat, const BYTE *buf, DWORD sector, BYTE count) { struct fat_priv *priv = fat->userdata; int ret; debug("%s: buf: %p sector: %ld count: %d\n", __func__, buf, sector, count); ret = cdev_write(priv->cdev, buf, count << 9, (loff_t)sector * 512, 0); if (ret != count << 9) return ret; return 0; }
/** * @brief Writes on the screen a formated string. * * @param fmt Formated string. */ PUBLIC void kprintf(const char *fmt, ...) { int i; /* Loop index. */ va_list args; /* Variable arguments list. */ char buffer[KBUFFER_SIZE + 1]; /* Temporary buffer. */ const char *buffer_no_code; /* Temporary buffer for log level printing. */ /* Convert to raw string. */ va_start(args, fmt); i = kvsprintf(buffer, fmt, args); buffer[i++] = '\n'; va_end(args); /* Save on kernel log, skip code in case it's not correctly done and write on kout. */ klog_write(0, buffer, i); buffer_no_code = skip_code(buffer, &i); cdev_write(kout, buffer_no_code, i); }