summaryrefslogtreecommitdiff
path: root/common.c
blob: 94bd4e05a79e6f3727d2431724236da65b296c64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 *	$Id: common.c,v 1.2 2002/03/30 15:39:24 mj Exp $
 *
 *	Linux PCI Utilities -- Common Functions
 *
 *	Copyright (c) 1997--1999 Martin Mares <mj@ucw.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

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

#include "pciutils.h"

void __attribute__((noreturn))
die(char *msg, ...)
{
  va_list args;

  va_start(args, msg);
  fputs("lspci: ", stderr);
  vfprintf(stderr, msg, args);
  fputc('\n', stderr);
  exit(1);
}

void *
xmalloc(unsigned int howmuch)
{
  void *p = malloc(howmuch);
  if (!p)
    die("Unable to allocate %d bytes of memory", howmuch);
  return p;
}

int
parse_generic_option(int i, struct pci_access *pacc, char *optarg)
{
  switch (i)
    {
#ifdef HAVE_PM_LINUX_PROC
    case 'P':
      pacc->method_params[PCI_ACCESS_PROC_BUS_PCI] = optarg;
      pacc->method = PCI_ACCESS_PROC_BUS_PCI;
      break;
#endif
#ifdef HAVE_PM_INTEL_CONF
    case 'H':
      if (!strcmp(optarg, "1"))
	pacc->method = PCI_ACCESS_I386_TYPE1;
      else if (!strcmp(optarg, "2"))
	pacc->method = PCI_ACCESS_I386_TYPE2;
      else
	die("Unknown hardware configuration type %s", optarg);
      break;
#endif
#ifdef HAVE_PM_SYSCALLS
    case 'S':
      pacc->method = PCI_ACCESS_SYSCALLS;
      break;
#endif
#ifdef HAVE_PM_DUMP
    case 'F':
      pacc->method_params[PCI_ACCESS_DUMP] = optarg;
      pacc->method = PCI_ACCESS_DUMP;
      break;
#endif
    case 'G':
      pacc->debugging++;
      break;
    default:
      return 0;
    }
  return 1;
}