Several new additions to net-tools. From Alexey's iproute2,
[platform/upstream/net-tools.git] / plipconfig.c
1 /*
2
3    plipconfig.c: plip-ifconfig program for the Linux PLIP device driver
4    Copyright (c) 1994 John Paul Morrison (VE7JPM).
5
6    version 0.2
7    
8    Changed by Alan Cox, to reflect the way SIOCDEVPRIVATE is meant to work
9    and for the extra parameter added by Niibe.
10
11    plipconfig is a quick hack to set PLIP parameters by using driver
12    ioctls.  plipconfig will no doubt be revised many times as the Linux
13    PLIP driver and Linux 1.1 mutates.
14
15 */
16
17 /*
18    This program is free software; you can redistribute it and/or modify
19    it under the terms of the GNU General Public License version 2, as
20    published by the Free Software Foundation.
21
22    This program is distributed in the hope that it will be useful, but
23    WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25    General Public License for more details.
26
27    You should have received a copy of the GNU General Public License
28    along with this program; if not, write to the Free Software Foundation,
29    Inc., 675 Mass Ave, Cambridge MA 02139, USA.
30 */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
38 #include <net/if.h>
39 #include <linux/if_plip.h>
40 #include "config.h"
41 #include "intl.h"
42
43 int opt_a = 0;
44 int opt_i = 0;
45 int opt_v = 0;
46 int skfd = -1;
47
48 struct ifreq ifr;
49 struct plipconf *plip;
50
51 void usage(void)
52 {
53     fprintf(stderr, _("Usage: plipconfig [-a] [-i] [-v] interface\n"));
54     fprintf(stderr, _("                  [nibble NN] [trigger NN]\n"));
55     exit(-1);
56 }
57
58 void print_plip(void)
59 {
60     printf(_("%s\tnibble %lu  trigger %lu\n"), ifr.ifr_name, plip->nibble, plip->trigger);
61 }
62
63 int main(int argc, char **argv)
64 {
65     int ret = 0;
66     char **spp;
67
68     if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
69         perror("socket");
70         exit(-1);
71     }
72     /* Find any options. */
73     argc--;
74     argv++;
75     while (argv[0] && *argv[0] == '-') {
76         if (!strcmp(*argv, "-a"))
77             opt_a = 1;
78         if (!strcmp(*argv, "-v"))
79             opt_v = 1;
80         argv++;
81         argc--;
82     }
83
84     if (argc == 0)
85         usage();
86
87     spp = argv;
88     strncpy(ifr.ifr_name, *spp++, IFNAMSIZ);
89     plip=(struct plipconf *)&ifr.ifr_data;
90
91     plip->pcmd = PLIP_GET_TIMEOUT;      /* get current settings for device */
92     if (ioctl(skfd, SIOCDEVPLIP, &ifr) < 0) {
93         perror("ioctl");
94         exit(-1);
95     }
96     if (*spp == (char *) NULL) {
97         print_plip();
98         (void) close(skfd);
99         exit(0);
100     }
101     while (*spp != (char *) NULL) {
102         if (!strcmp(*spp, "nibble")) {
103             if (*++spp == NULL)
104                 usage();
105             plip->nibble = atoi(*spp);
106             spp++;
107             continue;
108         }
109         if (!strcmp(*spp, "trigger")) {
110             if (*++spp == NULL)
111                 usage();
112             plip->trigger = atoi(*spp);
113             spp++;
114             continue;
115         }
116         usage();
117     }
118
119     plip->pcmd = PLIP_SET_TIMEOUT;
120     if (ioctl(skfd, SIOCDEVPLIP, &ifr) < 0)
121         perror("ioctl");
122
123     print_plip();
124
125     /* Close the socket. */
126     (void) close(skfd);
127
128     return (ret);
129 }