* typo whisper-cpp
[t2sde.git] / package / network / iproute2 / ipnm2nwbc.c
blobd232b115dfa9e4bd004f2abece13335a9ad81cda
1 // --- T2-COPYRIGHT-NOTE-BEGIN ---
2 // T2 SDE: package/*/iproute2/ipnm2nwbc.c
3 // Copyright (C) 2004 - 2023 The T2 SDE Project
4 // Copyright (C) 1998 - 2003 ROCK Linux Project
5 //
6 // This Copyright note is generated by scripts/Create-CopyPatch,
7 // more information can be found in the files COPYING and README.
8 //
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License version 2.
11 // --- T2-COPYRIGHT-NOTE-END ---
13 #include <stdio.h>
14 #include <string.h>
16 int getbits(int n) {
17 int rc=0;
18 while (n) {
19 if (n&1) rc++;
20 n = n >> 1;
22 return rc;
25 int setbits(int n) {
26 int rc=0;
27 while (n--) {
28 rc = (rc>>1)|(1<<7);
30 return rc;
33 int main(int argc, char ** argv) {
34 int ip[4],nm[4],nw[4],bc[4],c;
35 int verbose=0,nmbits=0,n;
37 if ( argc > 1 && !strcmp(argv[1],"-v") ) verbose=1;
39 if ( argc == 3+verbose &&
40 sscanf(argv[1+verbose],"%d.%d.%d.%d",ip,ip+1,ip+2,ip+3) == 4 &&
41 sscanf(argv[2+verbose],"%d.%d.%d.%d",nm,nm+1,nm+2,nm+3) == 4 ) {
42 nmbits=getbits(nm[0])+getbits(nm[1])+
43 getbits(nm[2])+getbits(nm[3]);
44 } else if ( argc == 2+verbose && sscanf(argv[1+verbose],
45 "%d.%d.%d.%d/%d",ip,ip+1,ip+2,ip+3,&nmbits) == 5 ) {
46 n=nmbits;
47 if (n>0) { nm[0]=setbits(n>8?8:n); n-=8; } else nm[0]=0;
48 if (n>0) { nm[1]=setbits(n>8?8:n); n-=8; } else nm[1]=0;
49 if (n>0) { nm[2]=setbits(n>8?8:n); n-=8; } else nm[2]=0;
50 if (n>0) { nm[3]=setbits(n>8?8:n); n-=8; } else nm[3]=0;
51 } else {
52 fprintf(stderr,"\n"
53 "IP and Netmask to Network and Broadcast converter.\n"
54 "(C) under GPL, 1999 Clifford Wolf\n\n"
55 "Usage: %s [-v] <IP> <Netmask>\n"
56 " %s [-v] <IP>/<Mask>\n\n"
57 "Examples: %s -v 195.170.70.72/25\n"
58 " %s 195.170.70.72 255.255.255.128\n\n",
59 argv[0],argv[0],argv[0],argv[0]);
60 return 1;
63 for (c=0; c<4; c++) {
64 nw[c]=ip[c]&nm[c];
65 bc[c]=nw[c]|(255&~nm[c]);
68 if ( verbose ) {
69 printf("Network: %d.%d.%d.%d / %d\n",
70 nw[0],nw[1],nw[2],nw[3],nmbits);
71 printf("Netmask: %d.%d.%d.%d\n",
72 nm[0],nm[1],nm[2],nm[3]);
73 printf("Broadcast: %d.%d.%d.%d\n",
74 bc[0],bc[1],bc[2],bc[3]);
75 } else {
76 printf("%d.%d.%d.%d %d.%d.%d.%d\n",nw[0],nw[1],
77 nw[2],nw[3],bc[0],bc[1],bc[2],bc[3]);
79 return 0;