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

📄 util.c

📁 This directory contains source code for tcpdump, a tool for network monitoring and data acquisition
💻 C
字号:
/* 
 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
 *      The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: (1) source code distributions
 * retain the above copyright notice and this paragraph in its entirety, (2)
 * distributions including binary code include the above copyright notice and
 * this paragraph in its entirety in the documentation or other materials
 * provided with the distribution, and (3) all advertising materials mentioning
 * features or use of this software display the following acknowledgement:
 * ``This product includes software developed by the University of California,
 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
 * the University nor the names of its contributors may be used to endorse
 * or promote products derived from this software without specific prior
 * written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <io.h>
#include <fcntl.h>
#include <time.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>

#include "interfac.h"

/* 
 * Print out a filename (or other ascii string).
 * If ep is NULL, assume no truncation check is needed.
 * Return true if truncated.
 */
int fn_print (char *s, char *ep)
{
  int    ret = 1;
  u_char c;

  while (!ep || s < ep)
  {
    c = *s++;
    if (c == '\0')
    {
      ret = 0;
      break;
    }
    if (!isascii (c))
    {
      c = toascii (c);
      PUTCHAR ('M');
      PUTCHAR ('-');
    }
    if (!isprint (c))
    {
      c ^= 0x40;                /* DEL to ?, others to alpha */
      PUTCHAR ('^');
    }
    PUTCHAR (c);
  }
  return (ret);
}

/* 
 * Print out a counted filename (or other ascii string).
 * If ep is NULL, assume no truncation check is needed.
 * Return true if truncated.
 */
int fn_printn (char *s, u_int n, char *ep)
{
  int    ret = 1;
  u_char c;

  while (!ep || s < ep)
  {
    if (n-- <= 0)
    {
      ret = 0;
      break;
    }
    c = *s++;
    if (!isascii (c))
    {
      c = toascii (c);
      PUTCHAR ('M');
      PUTCHAR ('-');
    }
    if (!isprint (c))
    {
      c ^= 0x40;                /* DEL to ?, others to alpha */
      PUTCHAR ('^');
    }
    PUTCHAR (c);
  }
  return (ret);
}

/* 
 * Print the timestamp
 */
void ts_print (const struct timeval *tvp)
{
  static unsigned b_sec;
  static unsigned b_usec;
  struct tm *tm;
  time_t Time;
  int    s;

  switch (tflag)
  {
    case 1:                 /* Default */
         s = (tvp->tv_sec + thiszone) % 86400;
         PRINTF ("%02d:%02d:%02d.%06lu ",
                 s/3600, (s%3600)/60, s%60, tvp->tv_usec);
         break;

    case -1:                /* Unix timeval style */
         PRINTF ("%lu.%06lu ", tvp->tv_sec, tvp->tv_usec);
         break;

    case -2:
         if (b_sec == 0)
            PUTS ("000000 ");
         else
         {
           int d_usec = tvp->tv_usec - b_usec;
           int d_sec  = tvp->tv_sec - b_sec;

           while (d_usec < 0)
           {
             d_usec += 1000000;
             d_sec--;
           }
           if (d_sec)
              PRINTF ("%d. ", d_sec);
           PRINTF ("%06d ", d_usec);
         }
         b_sec = tvp->tv_sec;
         b_usec = tvp->tv_usec;
         break;

    case -3:                /* Default + Date */
         s = (tvp->tv_sec + thiszone) % 86400;
         Time = (tvp->tv_sec + thiszone) - s;
         tm   = gmtime (&Time);
         PRINTF ("%02d/%02d/%04d %02d:%02d:%02d.%06u ",
                 tm->tm_mon + 1, tm->tm_mday,
                 tm->tm_year + 1900, s / 3600,
                 (s % 3600) / 60, s % 60, (unsigned)tvp->tv_usec);
         break;
  }
}

/*
 * Print a relative number of seconds (e.g. hold time, prune timer)
 * in the form 5m1s.  This does no truncation, so 32230861 seconds
 * is represented as 1y1w1d1h1m1s.
 */
void relts_print (int secs)
{
  static char *lengths[] = { "y", "w", "d", "h", "m", "s" };
  static int   seconds[] = { 31536000, 604800, 86400, 3600, 60, 1 };
  char **l = lengths;
  int   *s = seconds;

  if (secs == 0)
  {
    PUTS ("0s");
    return;
  }
  while (secs)
  {
    if (secs >= *s)
    {
      PRINTF ("%d%s", secs / *s, *l);
      secs -= (secs / *s) * *s;
    }
    s++;
    l++;
  }
}

/* 
 * Convert a token value to a string; use "fmt" if not found.
 */
char *tok2str (const struct tok *lp, const char *fmt, int v)
{
  static char buf[128];

  while (lp->s)
  {
    if (lp->v == v)
       return (lp->s);
    ++lp;
  }

  if (fmt == NULL)
      fmt = "#%d";

  sprintf (buf, fmt, v);
  return (buf);
}

char *err_file;
int   err_line;

void error (const char *fmt, ...)
{
  fprintf (stderr, "%s (%s:%d): ", program_name, err_file, err_line);
  if (fmt)
  {
    va_list args;

    va_start (args, fmt);
    vfprintf (stderr, fmt, args);
    if (*fmt)
    {
      fmt += strlen (fmt);
      if (fmt[-1] != '\n')
         fputc ('\n', stderr);
    }
    va_end (args);
  }
  exit (1);
}

void warning (const char *fmt,...)
{
  va_list args;

  va_start (args, fmt);
  fprintf (stderr, "%s (%s:%d): WARNING: ", program_name, err_file, err_line);
  vfprintf (stderr, fmt, args);
  if (*fmt)
  {
    fmt += strlen (fmt);
    if (fmt[-1] != '\n')
       fputc ('\n',stderr);
  }
  va_end (args);
}

/* 
 * Copy arg vector into a new buffer, concatenating arguments with spaces.
 */
char *copy_argv (char **argv)
{
  u_int len = 0;
  char *buf, **p;
  char *src, *dst;

  p = argv;
  if (*p == 0)
     return (NULL);

  while (*p)
     len += strlen (*p++) + 1;

  buf = malloc (len);
  if (!buf)
     PERROR (("copy_argv: malloc"));

  p   = argv;
  dst = buf;
  while ((src = *p++) != NULL)
  {
    while ((*dst++ = *src++) != '\0')
      ;
    dst[-1] = ' ';
  }
  dst[-1] = '\0';
  return (buf);
}


/*
 * Expand a relative filename to a path where program resides
 */
char *path_program (char *fname)
{
  static char buf[_MAX_PATH];
  char  *slash = strchr (fname, '\\');
  int    len;

  if (!slash)
     slash = strchr (fname, '/');

  if (slash)
  {
    if (*(slash-1) == '~')   /* "~/" -> "program_path/" */
         fname += 2;
    else return (fname);
  }

  len = strlen (program_path);
  if (len && program_path[len-1] != SLASH)
  {
    program_path[len]   = SLASH;
    program_path[len+1] = 0;
  }
  sprintf (buf, "%s%s", program_path, fname);

  if (access(buf,0))
     WARNING (("Cannot find file `%s'\n", buf));

  return (buf);
}

char *read_infile (char *fname)
{
  int    i, fd, cc;
  char  *cp;
  struct stat buf;
  int    mode = _fmode;

  _fmode = O_BINARY;    /* DOS/Win32 defaults to O_TEXT */
  fd = open(fname, O_RDONLY);
  _fmode = mode;

  if (fd < 0)
     PERROR (("can't open %s: %s", fname, strerror(errno)));

  if (fstat(fd, &buf) < 0)
     PERROR (("can't stat %s: %s", fname, strerror(errno)));

  cp = malloc ((u_int) buf.st_size + 1);
  if (!cp)
     PERROR (("read_infile: malloc"));

  cc = read (fd, cp, (int)buf.st_size);
  if (cc < 0)
     PERROR (("read %s: %s", fname, strerror(errno)));

  if (cc != buf.st_size)
     PERROR (("short read %s (%d != %d)", fname, cc, (int)buf.st_size));

  for (i = 0; i < cc; i++)
  {
    if (cp[i] == '#')
       while (i < cc && cp[i] != '\n')
             cp[i++] = ' ';
  }
  cp [cc] = '\0';
  return (cp);
}

/*
 * Return seconds offset from GMT to local time
 */
long gmt2local (time_t t)
{
  int    dt, dir;
  struct tm  sgmt;
  struct tm *gmt = &sgmt;
  struct tm *loc;

  tzset();

  if (t == 0)
     t = time (NULL);
  *gmt = *gmtime (&t);
  loc  = localtime (&t);
  dt   = 3600*(loc->tm_hour - gmt->tm_hour) +
           60*(loc->tm_min - gmt->tm_min);
  /*
   * If the year or julian day is different, we span 00:00 GMT
   * and must add or subtract a day. Check the year first to
   * avoid problems when the julian day wraps.
   */
  dir = loc->tm_year - gmt->tm_year;
  if (dir == 0)
      dir = loc->tm_yday - gmt->tm_yday;
  dt += dir * 24 * 3600;
  return (dt);
}

⌨️ 快捷键说明

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