summaryrefslogtreecommitdiff
path: root/pattest.c
diff options
context:
space:
mode:
authorKinsey Moore <kmoore@digium.com>2011-06-07 19:44:34 +0000
committerKinsey Moore <kmoore@digium.com>2011-06-07 19:44:34 +0000
commitae034293d9af863e3bb629ded097124684253aef (patch)
treeab084bedd8db55fa0e047aac52921b6516b5a118 /pattest.c
parentb845b4e8d6aefefd363b962afeaf1e20d4fab1ef (diff)
tools: Allow pattern tools to access channels above the device file limit
pattest and patgen already had this capability, but there were cases in which they would act unexpectedly. Now, if the name specified is not a character device file, it will be treated as a channel number if possible. Acked-by: Shaun Ruffell <sruffell@digium.com> git-svn-id: http://svn.asterisk.org/svn/dahdi/tools/trunk@9975 a0bf4364-ded3-4de4-8d8a-66a801d63aff
Diffstat (limited to 'pattest.c')
-rw-r--r--pattest.c51
1 files changed, 31 insertions, 20 deletions
diff --git a/pattest.c b/pattest.c
index 7e33a5c..09b0c8e 100644
--- a/pattest.c
+++ b/pattest.c
@@ -33,6 +33,7 @@
#include <linux/types.h>
#include <linux/ppp_defs.h>
#include <sys/ioctl.h>
+#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include "bittest.h"
@@ -64,30 +65,40 @@ void print_packet(unsigned char *buf, int len)
printf("}\n");
}
-int channel_open(char *name, int *bs)
+int channel_open(const char *name, int *bs)
{
- int channo;
- int fd;
- struct dahdi_params tp;
- char *dev;
-
- channo = atoi(name);
- /* channo==0: The user passed a file name to be opened. */
- dev = channo ? DEVICE : name;
-
- fd = open(dev, O_RDWR, 0600);
-
- if (fd < 0) {
- perror(DEVICE);
+ int channo, fd;
+ struct dahdi_params tp;
+ struct stat filestat;
+
+ /* stat file, if character device, open it */
+ channo = strtoul(name, NULL, 10);
+ fd = stat(name, &filestat);
+ if (!fd && S_ISCHR(filestat.st_mode)) {
+ fd = open(name, O_RDWR, 0600);
+ if (fd < 0) {
+ perror(name);
+ return -1;
+ }
+ /* try out the dahdi_specify interface */
+ } else if (channo > 0) {
+ fd = open(DEVICE, O_RDWR, 0600);
+ if (fd < 0) {
+ perror(DEVICE);
+ return -1;
+ }
+ if (ioctl(fd, DAHDI_SPECIFY, &channo) < 0) {
+ perror("DAHDI_SPECIFY ioctl failed");
+ return -1;
+ }
+ /* die */
+ } else {
+ fprintf(stderr, "Specified channel is not a valid character "
+ "device or channel number");
return -1;
}
- /* If we got a channel number, get it from /dev/dahdi/channel: */
- if(channo && ioctl(fd, DAHDI_SPECIFY, &channo) < 0) {
- perror("SPECIFY");
- return -1;
- }
- if(ioctl(fd, DAHDI_SET_BLOCKSIZE, bs) < 0) {
+ if (ioctl(fd, DAHDI_SET_BLOCKSIZE, bs) < 0) {
perror("SET_BLOCKSIZE");
return -1;
}