📄 trv_proc.c
字号:
/*********************************************************************** * * (C) Copyright 2000 * Jean-Jacques Germond, Fr閐閞ic Soulier, Christian Batt; Alcatel * C/O jjg@sxb.bsf.alcatel.fr * * All rights reserved. * * This code is free software; you can redistribute it and/or * modify it under the terms of the GNU *Library* General Public License * as published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This code is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * *Library* General Public License for more details. * * You should have received a copy of the GNU *Library* General Public * License along with this program (see file COPYING.LIB); if not, * write to the Free Software Foundation, Inc., 675 Mass Ave, * Cambridge, MA 02139, USA. * ***********************************************************************//* The "trivial module" /proc routines (see AR pg: 74) *//* This should be a module of its own */#define __NO_VERSION__ #include "trv_def.h"#ifndef CONFIG_PROC_FS# define CONFIG_PROC_FS /* This is for proc_fs.h */#endif#include "linux/proc_fs.h"/* A strucure to control writing in the PAGE_SIZE restricted buffer */typedef struct { char* ptst; int nbchars;} TrvProc; static voidtrv_proc_put(char carac, TrvProc* tp) /* Put one char in the buffer if there is still room into */ /* Notice the the final end-of-string will be deposited */{ tp->ptst[tp->nbchars] = carac; if (tp->nbchars < (PAGE_SIZE - 2)) { tp->nbchars++; }} static voidtrv_proc_puthex8(unsigned long val, TrvProc* tp){ int i; for (i = 0; i < 8; i++) { unsigned int aux = (val >> 28) & 0x0f; val <<= 4; trv_proc_put((aux < 10) ? (aux + '0') : (aux + 'A' - 10), tp); }} static voidtrv_proc_str(char* str, TrvProc* tp){ while (*str != '\0') trv_proc_put(*str++, tp);} static voidtrv_proc_soent(int val, TrvProc* tp){ if ( val >= 10) { trv_proc_soent(val/10, tp); } trv_proc_put( (val % 10) + '0', tp);} inttrv_edit_mem(char* buf, char **start, off_t offset, int len, int unused) /* Edit the Trivial device info */{ int dev_nbr; /* Device number */ int i; /* Work */ TrvProc trvProcObj; /* Want this code to be reentrant */ TrvProc* tp = &trvProcObj; /* .... */ char* ptld; /* Work load pointer */ /* Init the write control structure */ tp->ptst = buf; tp->nbchars = 0; /* Loop to edit all the devices */ for ( dev_nbr = 0; dev_nbr < trv_nr_devs; dev_nbr++) { Trv_Dev *dev = trv_devices + dev_nbr; /* The device number */ trv_proc_str("trv", tp); trv_proc_soent(dev_nbr, tp); /* The descriptor addr */ trv_proc_str(" Device_addr = 0x", tp); trv_proc_puthex8((unsigned long)dev, tp); /* The buffer address and nb_chars available */ ptld = dev->buffer; trv_proc_str(" Data_addr = 0x", tp); trv_proc_puthex8((unsigned long)dev->buffer, tp); trv_proc_str(" nb_chars= ", tp); trv_proc_soent(dev->nbchars, tp); /* Edit the device's content */ if (ptld) { char aux; if (dev->nbchars != 0) { for (i = 0; ((i < dev->nbchars) && (i < 200)); i++) { if ( (i % 72) == 0 ) trv_proc_str("\n", tp); /* Give a newline */ aux = *ptld++; /* make it readable */ if ((aux < ' ') || (aux > '~')) aux = '.'; trv_proc_put(aux, tp); } } else { trv_proc_str("\n--- EMPTY DEVICE ---", tp); } } trv_proc_str("\n", tp); /* Give a newline */ } /* terminate the string and return */ trv_proc_put('\0', tp); return tp->nbchars - 1; /* Do not count the trailing zero */}static struct proc_dir_entry trv_proc_entry = { 0, /* low_ino: the inode -- dynamic */ 6, "trvmem", /* len of name and name */ S_IFREG | S_IRUGO, /* mode */ 1, 0, 0, /* nlinks, owner, group */ 0, NULL, /* size - unused; operations -- use default */ &trv_edit_mem, /* function used to read data */ /* nothing more */}; voidtrv_proc_register(void) /* Register the trivial device in /proc */{ proc_register(&proc_root, &trv_proc_entry);} voidtrv_proc_unregister(void){ proc_unregister(&proc_root, trv_proc_entry.low_ino);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -