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

📄 out.c

📁 This directory contains source code for tcpdump, a tool for network monitoring and data acquisition
💻 C
字号:
/*
 * DOS/tcpdump.
 *
 * Output driver for tcpdump; print using faster conio or
 * slower stdio functions (default).
 *
 * By G. Vanem 1998
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <malloc.h>

#if defined(__WATCOMC__) || defined(__HIGHC__)
#include <mw/conio.h>
#else
#include <conio.h>
#endif

#if defined(__DJGPP__)
#include <pc.h>
#include <go32.h>
#include <sys/farptr.h>
#endif

#include "interfac.h"

#define STATUS_RIGHT 50   /* right column for eth-status */

int colour_main  = 0x07;  /* black on light-grey */
int colour_debug = 0x0F;  /* black on white */
int colour_stat  = 0x70;  /* light-gray on black */
int use_conio    = 0;

static int  stdio_puts   (const char *s);
static int  stdio_printf (const char *fmt, ...);
static void conio_write  (const char *p);

#if defined(__DJGPP__) && defined(USE_CONIO)
static void patch_gotoxy (void);
static void unpatch_gotoxy (void);
#endif

int (*__puts)    (const char *s)        = stdio_puts;
int (*__printf)  (const char *fmt, ...) = stdio_printf;
int (*__putchar) (int c)                = putchar;

#ifdef USE_32BIT_DRIVERS
extern
#endif
size_t (*_printk_out)();  /* in pcap/pm_drvr/printk.c */

extern int vsnprintf (char *, size_t, const char *, va_list);

static struct text_info dos_ti;
static int    conio_set = 0;

static int stdio_puts (const char *s)
{
  return fputs (s, stdout);
}

static int stdio_printf (const char *fmt, ...)
{
  int     len;
  va_list args;
  va_start (args, fmt);
  len = vfprintf (stdout, fmt, args);
  va_end (args);
  return (len);
}

static int conio_puts (const char *s)
{
  conio_write (s);
  return strlen (s);
}

static size_t conio_fwrite (const char *s, size_t size, size_t nelem)
{
  textattr (colour_debug);
  conio_write (s);
  textattr (colour_main);
  return (size*nelem);
}

static int conio_putch (int ch)
{
  if (ch == '\n')
     putch ('\r');
  return putch (ch);
}

static int conio_printf (const char *fmt, ...)
{
  va_list args;
  char    buf[256];
  int     len;

  va_start (args, fmt);

#if defined(__HIGHC__) || defined(__WATCOMC__)
  len = _vbprintf (buf, sizeof(buf)-1, fmt, args);
#else
  len = vsnprintf (buf, sizeof(buf)-1, fmt, args);
#endif
  conio_write (buf);
  return (len);
}

void set_conio (void)
{
#ifdef USE_CONIO
  struct text_info ti;

  __puts      = conio_puts;
  __printf    = conio_printf;
  __putchar   = conio_putch;
  _printk_out = (size_t(*)()) conio_fwrite;

  gettextinfo (&ti);
  memcpy (&dos_ti, &ti, sizeof(dos_ti));

  gotoxy (1, ti.screenheight);
  textattr (colour_stat);
  clreol();
  cprintf (" tcpdump %s", TCPDUMP_VERSION);

  window (1, 1, ti.screenwidth, ti.screenheight-1);
  textattr (colour_main);
  clrscr();
  conio_set = 1;
#ifdef __DJGPP__
  patch_gotoxy();
#endif
#endif
}


void unset_conio (void)
{
#ifdef USE_CONIO
  if (!conio_set)
     return;

#ifdef __DJGPP__
  unpatch_gotoxy();
#endif

  window (1, 1, dos_ti.screenwidth, dos_ti.screenheight);
  gotoxy (1, dos_ti.screenheight);
  textattr (dos_ti.attribute);
  putch ('\r');
  clreol();

  conio_set   = 0;
  __puts      = stdio_puts;
  __printf    = stdio_printf;
  __putchar   = putchar;
  _printk_out = (size_t(*)()) fwrite;
#endif
}

static void conio_write (const char *p)
{
  while (*p)
  { 
    if (*p == '\t')
    {
      cputs ("        ");
      p++;
      continue;
    }
    if (*p == '\n')
       putch ('\r');
    putch (*p++);
  }
}

void status_write (const char *fmt, ...)
{
#if defined(USE_CONIO)
  static  char *scr;
  char   *buf;
  int     len, y, i, wth;
  va_list args;

  if (!conio_set || !fmt)
     return;

  if (!scr)
     scr = malloc (3*dos_ti.screenwidth);
  if (!scr)
     return;

  buf = scr + 2 * dos_ti.screenwidth;
  wth = min (STATUS_RIGHT, dos_ti.screenwidth - STATUS_RIGHT);
  va_start (args, fmt);
  len = vsprintf (buf, fmt, args);
  va_end (args);

  for (i = 0; i < wth; i++)
  {
    if (i < len)
         scr[2*i] = buf[i];
    else scr[2*i] = ' ';
    scr[2*i+1] = colour_stat;
  }
  y = dos_ti.screenheight;
  puttext (STATUS_RIGHT, y, dos_ti.screenwidth, y, scr);
#endif
}

#if defined(__DJGPP__) && defined(USE_CONIO)

static BYTE patch_save[5];
static WORD crt_base = 0x3D4;

static void fast_gotoxy (int x, int y)
{
  WORD xy = (y + dos_ti.wintop) * dos_ti.screenwidth +
            (x + dos_ti.winleft);
  outportb (crt_base, 0x0E);
  outportb (crt_base+1, xy >> 8);
  outportb (crt_base, 0x0F);
  outportb (crt_base+1, xy & 0xFF);
  _farpokew (_dos_ds, 0x450, xy);
}

static void unpatch_gotoxy (void)
{
  memcpy ((void*)gotoxy, &patch_save, sizeof(patch_save));
}

static void patch_gotoxy (void)
{
  DWORD ofs = (DWORD)fast_gotoxy - (DWORD)gotoxy;
  BYTE  buf[5];

  crt_base = _farpeekw (_dos_ds, 0x463);
  memcpy (&patch_save, (const void*)gotoxy, sizeof(patch_save));

  buf[0] = 0xE9;               /* "jmp" (relative) */
  *(DWORD*)&buf[1] = ofs - 5;  /* 5 == sizeof('jmp xyz') */
  memcpy ((void*)gotoxy, &buf, sizeof(buf));
}
#endif /* __DJGPP__ && USE_CONIO */

⌨️ 快捷键说明

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