dwrite.c

来自「请在解压时看说明文档 内有详细说明」· C语言 代码 · 共 70 行

C
70
字号
/************************************************************//***** dwrite.c, this is a test program for upperdrv.c *****//************************************************************/#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <stdlib.h>#include <string.h>#include <fcntl.h>#include <unistd.h>#include <errno.h>#include <asm/io.h>/************************************************************//***** define some constants used in upperdrv.c  *****//************************************************************/#ifndef _UPPER_DEVICE_H#define _UPPER_DEVICE_H#include <linux/ioctl.h>#define UPPER_MAJOR 117#define UPPER_LOW2UPPER 1 // change the input string from low to upper characters#define UPPER_UNCHAN 0     //copy the input string to the output#define UPPER_MAGIC UPPER_MAJOR#define UPPER_RESET _IO(UPPER_MAGIC,0)   // reset the data#define UPPER_QUERY_NEW_MSG _IO(UPPER_MAGIC,1) // check for new message#define UPPER_QUERY_MSG_LENGTH _IO(UPPER_MAGIC,2) // get message length#define IOC_NEW_MSG 1#endifchar buf[1512];int main(int argc,char **argv) {	int fd,len=0,quit=0;	int dbg;	if(argc<3){		printf("usage: dwrite+device name+[string]\n");		exit(0);}	fd=open("/dev/upper",O_WRONLY);	if(fd<=0) {		printf("Error opening device for writing!\n");		return 0;	}	while(!quit){		sscanf(argv[2],"%s",buf);		printf("buf=\"%s\" ",buf);		if(!strcmp(buf,"exit"))		quit=1;		while(ioctl(fd,UPPER_QUERY_NEW_MSG)){			usleep(1000);//			printf("usleep\n");			}		printf("strlen(buf) = %d\n",strlen(buf));		len=write(fd,buf,strlen(buf));		printf("  After write,return len=%d\n",len);		if(len<0) {			printf("Error writing to device!\n");			printf("len=%d",len);			close(fd);			return 0;		}		else			quit=1;		printf("%d bytes written to device!\n",len);	}	close(fd);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?