📄 devopen.c
字号:
/* $NetBSD$ *//* * Copyright (C) 2001 Bruno Achauer. * Copyright (C) 2001 Exet AG. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Exet AG. * 4. The name of Exet AG may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY EXET AG ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL EXET AG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */#include <sys/param.h>#include <netinet/in.h>#include <lib/libkern/libkern.h>#include <lib/libsa/stand.h>#include <lib/libsa/nfs.h>static int dkopen (struct open_file *, ...);static int dkclose (struct open_file *);static int dkstrategy (void *, int, daddr_t, size_t, void *, size_t *);static struct devsw devsw[] ={ { "dk", dkstrategy, dkopen, dkclose, noioctl }};static int mem_open (char *path, struct open_file *f);static int mem_close (struct open_file *f);static int mem_read (struct open_file *f, void *buf, size_t size, size_t *resid);static int mem_write (struct open_file *f, void *buf, size_t size, size_t *resid);static off_t mem_seek (struct open_file *f, off_t offset, int where);static int mem_stat (struct open_file *f, struct stat *sb);struct fs_ops file_system[1] ={ { mem_open, mem_close, mem_read, mem_write, mem_seek, mem_stat }};int nfsys = 1;intdevopen (struct open_file *of, const char *name, char **file){ static char opened_name[256]; if (of->f_flags != F_READ) return EPERM; if (strncmp (name, "mem@", 4) != 0) return ENOENT; strcpy(opened_name, name + 4); *file = opened_name; of->f_dev = devsw; of->f_devdata = (void *) 0xdeadbeef; /* XXX: &devdata; */ return 0;}static intdkopen (struct open_file *f, ...){ return 0;}static intdkclose (struct open_file *f){ return 0;}static intdkstrategy (void *devdata, int rw, daddr_t block, size_t size, void *buf, size_t *rsize){ return 0;}struct mem_file /* an in-memory "file": */{ char *mf_base; ulong mf_size; off_t mf_pos;};static longstrtol (const char *str){ long res = 0; const char *dp = str; int d; if (*dp++ != '0' || *dp++ != 'x') return -1; str += 2; for (;;) { d = *dp; if (d >= '0' && d <= '9') d -= '0'; else if (d >= 'a' && d <= 'f') d -= ('a' - 10); else if (d >= 'A' && d <= 'F') d -= ('A' - 10); else break; res = (res << 4) + d; dp += 1; } return (dp == &str[2]) ? -1 : res;}static intmem_open (char *path, struct open_file *f){ char *sep; long base, size; struct mem_file *fp; if (! (sep = strchr (path, ','))) return EINVAL; if ((base = strtol (path)) == -1 || (size = strtol (sep+1)) < 0) return ENOENT; f->f_fsdata = fp = alloc (sizeof(struct mem_file)); fp->mf_base = (char *) base; fp->mf_size = size; fp->mf_pos = 0; return 0;}static intmem_close (struct open_file *f){ struct mem_file *fp = (struct mem_file *)f->f_fsdata; free (fp, sizeof(struct mem_file)); return 0;}static intmem_read (struct open_file *f, void *buf, size_t want, size_t *resid){ struct mem_file *fp = (struct mem_file *)f->f_fsdata; size_t have; size_t got; have = (fp->mf_size >= fp->mf_pos) ? (fp->mf_size - fp->mf_pos) : 0; got = (want <= have) ? want : have; if (got) { bcopy (&fp->mf_base[fp->mf_pos], buf, got); fp->mf_pos += got; } if (resid) *resid = want - got; return 0;}static intmem_write (struct open_file *f, void *buf, size_t size, size_t *resid){ return (EROFS);}static off_tmem_seek (struct open_file *f, off_t offset, int where){ struct mem_file *fp = (struct mem_file *)f->f_fsdata; switch (where) { case SEEK_SET: fp->mf_pos = offset; break; case SEEK_CUR: fp->mf_pos += offset; break; case SEEK_END: fp->mf_pos = fp->mf_size - offset; break; default: return (-1); } return (fp->mf_pos);}static intmem_stat (struct open_file *f, struct stat *sb){ struct mem_file *fp = (struct mem_file *)f->f_fsdata; /* only important stuff */ sb->st_mode = 0444; sb->st_uid = 0; sb->st_gid = 0; sb->st_size = fp->mf_size; return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -