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

📄 printif.c

📁 不用说了吧
💻 C
📖 第 1 页 / 共 2 页
字号:
/* printif.c -- print an interface configuration   Copyright (C) 2001, 2002 Free Software Foundation, Inc.   Written by Marcus Brinkmann.   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., 51 Franklin Street, Fifth Floor, Boston,   MA 02110-1301 USA. */#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <sys/stat.h>#include <stdio.h>#include <errno.h>#if HAVE_UNISTD_H#include <unistd.h>#endif#if HAVE_STRING_H# include <string.h>#else# include <strings.h>#endif#if STDC_HEADERS# include <stdlib.h>#else# ifndef HAVE_STRCHR#  define strchr index#  define strrchr rindex# endif#endif#include <ctype.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <net/if.h>#include <netinet/in.h>#include <arpa/inet.h>#include "ifconfig.h"FILE *ostream;	/* Either stdout or stderror.  */int column_stdout;	/* The column position of the cursor on stdout.  */int column_stderr;	/* The column position of the cursor on stderr.  */int *column = &column_stdout;	/* The column marker of ostream.  */int had_output;		/* True if we had any output.  */struct format_handle format_handles[] ={#ifdef SYSTEM_FORMAT_HANDLER  SYSTEM_FORMAT_HANDLER#endif  {"", fh_nothing},  {"newline", fh_newline},  {"\\n", fh_newline},  {"\\t", fh_tabulator},  {"first?", fh_first},  {"tab", fh_tab},  {"join", fh_join},  {"exists?", fh_exists_query},  {"format", fh_format},  {"error", fh_error},  {"progname", fh_progname},  {"exit", fh_exit},  {"name", fh_name},  {"index?", fh_index_query},  {"index", fh_index},  {"addr?", fh_addr_query},  {"addr", fh_addr},  {"netmask?", fh_netmask_query},  {"netmask", fh_netmask},  {"brdaddr?", fh_brdaddr_query},  {"brdaddr", fh_brdaddr},  {"dstaddr?", fh_dstaddr_query},  {"dstaddr", fh_dstaddr},  {"flags?", fh_flags_query},  {"flags", fh_flags},  {"mtu?", fh_mtu_query},  {"mtu", fh_mtu},  {"metric?", fh_metric_query},  {"metric", fh_metric},  {NULL, NULL}};/* Various helper functions to get the job done.  */voidput_char (format_data_t form, char c){  switch (c)    {    case '\n':      *column = 0;      break;    case '\t':      *column = ((*column / TAB_STOP) + 1) * TAB_STOP;      break;    default:      (*column)++;    }  putc (c, ostream);  had_output = 1;}/* This is a simple print function, which tries to keep track of the   column.  Of course, terminal behaviour can defeat this.  We should   provide a handler to switch on/off column counting.  */voidput_string (format_data_t form, const char *s){  while (*s != '\0')    put_char (form, *(s++));}voidput_int (format_data_t form, int argc, char *argv[], int nr){  char *fmt;  if (argc > 0)    {      char *p = argv[0];      if (*p != '%')	fmt = "%i";      else	{	  p++;	  while (isdigit (*p))	    p++;	  if (*p == '#')	    p++;	  switch (*p)	    {	    default:	    case 'i':	    case 'd':	    case 'D':	      *p = 'i';	      break;	    case 'x':	    case 'h':	      *p = 'x';	      break;	    case 'X':	    case 'H':	      *p = 'X';	      break;	    case 'o':	    case 'O':	      *p = 'o';	      break;	    }	  p++;	  *p = '\0';	  fmt = argv[0];	}    }  else    fmt = "%i";  *column += printf (fmt, nr);  had_output = 1;}voidselect_arg (format_data_t form, int argc, char *argv[], int nr){  if (nr < argc)    {      form->format = argv[nr];      print_interfaceX (form, 0);    }}voidput_addr (format_data_t form, int argc, char *argv[], struct sockaddr *sa){  struct sockaddr_in *sin = (struct sockaddr_in *) sa;  char *addr = inet_ntoa (sin->sin_addr);  long byte[4];  char *p = strchr (addr, '.');  *p = '\0';  byte[0] = strtol (addr, NULL, 0);  addr = p + 1;  p = strchr (addr, '.');  *p = '\0';  byte[1] = strtol (addr, NULL, 0);  addr = p + 1;  p = strchr (addr, '.');  *p = '\0';  byte[2] = strtol (addr, NULL, 0);  byte[3] = strtol (p + 1, NULL, 0);  addr = inet_ntoa (sin->sin_addr);  if (argc > 0)    {      long i = strtol (argv[0], NULL, 0);      if (i >= 0 && i <= 3)	put_int (form, argc - 1, &argv[1], byte[i]);    }  else    put_string (form, addr);}voidput_flags (format_data_t form, int argc, char *argv[], short flags){  /* XXX */  short int f = 1;  const char *name;  int first = 1;  while (flags && f)    {      if (f & flags)        {          name = if_flagtoname (f, "" /* XXX: avoid */);          if (name)            {              if (!first)		{		  if (argc > 0)		    put_string (form, argv[0]);		  else		    put_char (form, ' ');		}              put_string (form, name);              flags &= ~f;              first = 0;            }        }      f = f << 1;    }  if (flags)    {      if (!first)	{	  if (argc > 0)	    put_string (form, argv[0]);	  else	    put_char (form, ' ');	}      put_int (form, argc - 1, &argv[1], flags);    }}/* Format handler can mangle form->format, so update it after calling   here.  */voidformat_handler (const char *name, format_data_t form, int argc, char *argv[]){  struct format_handle *fh;  fh = format_handles;  while (fh->name != NULL)    {      if (! strcmp (fh->name, name))	break;      fh++;    }  if (fh->handler)    (fh->handler) (form, argc, argv);  else    {      *column += printf ("(");      put_string (form, name);      *column += printf (" unknown)");      had_output = 1;    }}voidfh_nothing (format_data_t form, int argc, char *argv[]){}voidfh_newline (format_data_t form, int argc, char *argv[]){  put_char (form, '\n');}voidfh_tabulator (format_data_t form, int argc, char *argv[]){  put_char (form, '\t');}voidfh_first (format_data_t form, int argc, char *argv[]){  select_arg (form, argc, argv, form->first ? 0 : 1);}/* A tab implementation, which fills with spaces up to requested column or next   tabstop.  */voidfh_tab (format_data_t form, int argc, char *argv[]){  long goal = 0;  errno = 0;  if (argc >= 1)    goal = strtol (argv[0], NULL, 0);  if (goal <= 0)    goal = ((*column / TAB_STOP) + 1) * TAB_STOP;  while (*column < goal)    put_char (form, ' ');}voidfh_join (format_data_t form, int argc, char *argv[]){  int had_output_saved = had_output;  int count = 0;  if (argc < 2)    return;  /* Suppress delimiter before first argument.  */  had_output = 0;  while (++count < argc)    {      if (had_output)	{	  put_string (form, argv[0]);	  had_output = 0;	  had_output_saved = 1;	}      form->format = argv[count];      print_interfaceX (form, 0);    }  had_output = had_output_saved;}voidfh_exists_query (format_data_t form, int argc, char *argv[]){  struct format_handle *fh;  fh = format_handles;  if (argc > 0)    {      while (fh->name != NULL)	{	  if (! strcmp (fh->name, argv[0]))	    break;	  fh++;	}      select_arg (form, argc, argv, (fh->name != NULL) ? 1 : 2);    }}voidfh_format (format_data_t form, int argc, char *argv[]){  int i = 0;  while (i < argc)    {      struct format *frm = formats;      while (frm->name && strcmp (argv[i], frm->name))	frm++;      if (frm->name)	{	  /* XXX: Avoid infinite recursion by appending name to a list	   during the next call (but removing it afterwards, and	   checking in this function if the name is in the list	   already.  */	  form->format = frm->templ;	  print_interfaceX (form, 0);	}      i++;    }}voidfh_error (format_data_t form, int argc, char *argv[]){  int i = 0;  FILE *s = ostream;  int *c = column;  ostream = stderr;  column = &column_stderr;  while (i < argc)    select_arg (form, argc, argv, i++);  ostream = s;  column = c;}

⌨️ 快捷键说明

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