⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rwf.c

📁 7号信令功能代码,为开源代码
💻 C
字号:
/***************************************************************************** @(#) rwf.c,v LiS-2_16_18-8(1.1.1.1.8.1) 2004/01/12 23:33:17 ----------------------------------------------------------------------------- Copyright (c) 2003-2004  OpenSS7 Corporation <http://www.openss7.com> All Rights Reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ----------------------------------------------------------------------------- U.S. GOVERNMENT RESTRICTED RIGHTS.  If you are licensing this Software on behalf of the U.S. Government ("Government"), the following provisions apply to you.  If the Software is supplied by the Department of Defense ("DoD"), it is classified as "Commercial Computer Software" under paragraph 252.227-7014 of the DoD Supplement to the Federal Acquisition Regulations ("DFARS") (or any successor regulations) and the Government is acquiring only the license rights granted herein (the license rights customarily provided to non-Government users).  If the Software is supplied to any unit or agency of the Government other than DoD, it is classified as "Restricted Computer Software" and the Government's rights in the Software are defined in paragraph 52.227-19 of the Federal Acquisition Regulations ("FAR") (or any success regulations) or, in the cases of NASA, in paragraph 18.52.227-86 of the NASA Supplement to the FAR (or any successor regulations). ----------------------------------------------------------------------------- Commercial licensing and support of this software is available from OpenSS7 Corporation at a fee.  See http://www.openss7.com/ ----------------------------------------------------------------------------- Last Modified 2004/01/12 23:33:17 by brian ----------------------------------------------------------------------------- rwf.c,v Revision 1.1.1.1.8.1  2004/01/12 23:33:17  brian - Updated LiS-2.16.18 gcom release to autoconf. *****************************************************************************/#ident "@(#) rwf.c,v LiS-2_16_18-8(1.1.1.1.8.1) 2004/01/12 23:33:17"static char const ident[] = "rwf.c,v LiS-2_16_18-8(1.1.1.1.8.1) 2004/01/12 23:33:17";/*  *  rwf.c - simple reading/writing test using poll() to get FIFO input. * *  This program could be changed to use getmsg()/putmsg() instead of *  read()/write(), hopefully in several obvious ways.  It has been *  written for read()/write() in order to be useful with non-STREAMS *  fifos. * *  Copyright (C) 2000  John A. Boyd Jr.  protologos, LLC * *  This library 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 library 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 library; if not, write to the *  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, *  MA 02139, USA. */#ident "@(#) LiS rwf.c 1.1 12/15/00"#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <memory.h>#include <fcntl.h>#ifdef _GNU_SOURCE#include <getopt.h>#endif#include <stropts.h>#include <poll.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/sysmacros.h>#include <sys/ioctl.h>int verbose = 1;static inline int _getmsg(int fd, struct strbuf *sb){	int len;	if ((len = read(fd, sb->buf, sb->maxlen)) >= 0)		sb->len = len;	return (len < 0 ? len : 0);}static inline int _putmsg(int fd, struct strbuf *sb){	int len;	if ((len = write(fd, sb->buf, sb->len)) >= 0)		sb->len = len;	return (len < 0 ? len : 0);}void version(int argc, char *argv[]){	if (!verbose)		return;	fprintf(stdout, "\%1$s %2$s:\n\    Copyright (c) 2003-2004  OpenSS7 Corporation.  All Rights Reserved.\n\    Copyright (c) 2000       John A. Boyd Jr.  protologos, LLC\n\\n\    Distributed by OpenSS7 Corporation under GPL Version 2,\n\    included here by reference.\n\", argv[0], ident);}void usage(int argc, char *argv[]){	if (!verbose)		return;	fprintf(stderr, "\Usage:\n\    %1$s [options] PATH\n\    %1$s {-h,--help}\n\    %1$s {-V,--version}\n\", argv[0]);}void help(int argc, char *argv[]){	if (!verbose)		return;	fprintf(stdout, "\Usage:\n\    %1$s [options] PATH\n\    %1$s {-h,--help}\n\    %1$s {-V,--version}\n\Arguments:\n\    PATH\n\        The path to file.\n\Options:\n\    -m, --number NUMBER\n\        Number of iterations\n\    -l, --length LENGTH\n\        Length of the message to send.\n\    -w, --wait WAIT\n\        Set the wait in milliseconds [default: 0]\n\    -r, --relay\n\        Push the relay module\n\    -v, --verbose [LEVEL]\n\        Increase verbosity or set to LEVEL [default: 1]\n\        This option may be repeated.\n\    -q, --quiet\n\        Suppress normal output (equivalent to --verbose=0)\n\    -h, --help, -?, --?\n\        Print this usage message and exits\n\    -V, --version\n\        Print the version and exits\n\", argv[0]);}int main(int argc, char *argv[]){	char path[40];	int fd, j, m = 0, l = 1;	struct stat st;	int wt = 0;	int is_stream = 0, push_relay = 0;	for (;;) {		int c;#ifdef _GNU_SOURCE		int option_index = 0;		static struct option long_options[] = {			{"number", 1, 0, 'm'},			{"length", 1, 0, 'l'},			{"wait", 1, 0, 'w'},			{"relay", 0, 0, 'r'},			{"quiet", 0, 0, 'q'},			{"verbose", 2, 0, 'v'},			{"help", 0, 0, 'h'},			{"version", 0, 0, 'V'},			{"?", 0, 0, 'h'},		};		c = getopt_long_only(argc, argv, "m:l:w:rqv::hV?", long_options, &option_index);#else				/* _GNU_SOURCE */		c = getopt(argc, argv, "m:l:w:rqv::hV?");#endif				/* _GNU_SOURCE */		if (c == -1)			break;		switch (c) {		case 'm':	/* -m, --number */			m = strtol(optarg, NULL, 0);			break;		case 'l':			l = strtol(optarg, NULL, 0);			break;		case 'w':	/* -w, --wait WAIT(ms) */			wt = strtol(optarg, NULL, 0);			break;		case 'r':	/* -r, --relay */			push_relay = 1;			break;		case 'q':	/* -q, --quiet */			verbose = 0;			break;		case 'v':	/* -v, --verbose [LEVEL] */			if (!optarg)				verbose++;			else				verbose = strtol(optarg, NULL, 0);			break;		case 'h':	/* -h, --help, -?, --? */			help(argc, argv);			exit(0);		case 'V':	/* -V, --version */			version(argc, argv);			exit(0);		case '?':		default:			optind--;			if (optind < argc && verbose) {				fprintf(stderr, "%s: illegal syntax -- ", argv[0]);				for (; optind < argc; optind++)					fprintf(stderr, "%s ", argv[optind]);				fprintf(stderr, "\n");			}		      bad_usage:			usage(argc, argv);			exit(2);		}	}	if (argc == optind)		goto bad_usage;	strcpy(path, argv[optind]);	if ((fd = open(path, O_RDWR | O_NONBLOCK)) < 0) {		if (verbose)			fprintf(stderr, "open(\"%s\", O_RDWR|O_NONBLOCK) failed: %s\n",				path, strerror(errno));		exit(1);	}	if (verbose > 1) {		fstat(fd, &st);		printf("%d: open \"%s\" (mode 0%o dev 0x%04x rdev 0x%04x)",		       fd, path, (int) (st.st_mode), (int) (st.st_dev), (int) (st.st_rdev));		is_stream = isastream(fd);		printf("%s\n", (is_stream ? " [STREAM]" : ""));	}	if (is_stream) {		if (push_relay) {			if (ioctl(fd, I_PUSH, "relay") < 0) {				if (verbose)					fprintf(stderr,						"ioctl(%d, I_PUSH, relay) failed: %s\n",						fd, strerror(errno));				exit(1);			}			if (verbose > 1)				printf("module \"relay\" pushed onto %d\n", fd);		}	}	if (m > 0) {		struct pollfd pfd;		struct strbuf sbo, sbi;		pfd.fd = fd;		pfd.events = POLLIN | POLLHUP;		sbo.len = l;		sbo.buf = malloc(l);		memset(sbo.buf, 'x', l);		sbi.maxlen = l;		sbi.buf = malloc(l);		for (j = 0; j < m; j++) {			_putmsg(fd, &sbo);			poll(&pfd, 1, wt);			if (verbose > 1)				printf("poll(%d,...) -> 0x%x\n", fd, pfd.revents);			if (pfd.revents & POLLHUP) {				if (verbose)					fprintf(stderr, "POLLHUP received - exiting\n");				break;			} else if (!pfd.revents) {				if (verbose)					fprintf(stderr, "poll(%d,...) timed out - exiting\n",						pfd.fd);				break;			}			sbi.len = 0;			_getmsg(fd, &sbi);			if (verbose > 1)				printf("read(%d,...) -> len %d\n", fd, sbi.len);		}	}	exit(0);}

⌨️ 快捷键说明

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