vshost-util-vserver

Build script and sources for util-vserver.
git clone https://ccx.te2000.cz/git/vshost-util-vserver
Log | Files | Refs

mask2prefix.c (1590B)


      1 // $Id$    --*- c++ -*--
      2 
      3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
      4 //  
      5 // This program is free software; you can redistribute it and/or modify
      6 // it under the terms of the GNU General Public License as published by
      7 // the Free Software Foundation; version 2 of the License.
      8 //  
      9 // This program is distributed in the hope that it will be useful,
     10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 // GNU General Public License for more details.
     13 //  
     14 // You should have received a copy of the GNU General Public License
     15 // along with this program; if not, write to the Free Software
     16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
     17 
     18 
     19 #ifdef HAVE_CONFIG_H
     20 #  include <config.h>
     21 #endif
     22 
     23 #include "util.h"
     24 #include <stdlib.h>
     25 
     26 static inline void
     27 showHelp(int fd, int exit_code)
     28 {
     29   WRITE_MSG(fd,
     30 	    "Usage:  mask2prefix <mask>\n");
     31   exit(exit_code);
     32 }
     33 
     34 int main(int argc, char *argv[])
     35 {
     36   char		*err_ptr, *ptr;
     37   int		len = 0;
     38   size_t	i;
     39   
     40   if (argc!=2) showHelp(2,255);
     41 
     42   ptr = argv[1];
     43   for (i=0; i<4; ++i) {
     44     unsigned int	val = strtol(ptr, &err_ptr, 10);
     45 
     46     switch (*err_ptr) {
     47       case '.'	:
     48       case '\0'	:  break;
     49       default	:
     50 	WRITE_MSG(2, "Invalid mask specified\n");
     51 	return 255;
     52     }
     53     
     54     if (val>=0x100) {
     55       WRITE_MSG(2, "Invalid mask specified\n");
     56       return 255;
     57     }
     58 
     59     while (val&0x80) {
     60       ++len;
     61       val <<= 1;
     62     }
     63 
     64     if (val!=0xff00) break;
     65 
     66     ptr = err_ptr+1;
     67   }
     68 
     69   return len;
     70 }