]> mj.ucw.cz Git - pciutils.git/blob - common.c
Arguments now correspond to the format string
[pciutils.git] / common.c
1 /*
2  *      The PCI Utilities -- Common Functions
3  *
4  *      Copyright (c) 1997--2003 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <unistd.h>
14
15 #include "pciutils.h"
16
17 void NONRET
18 die(char *msg, ...)
19 {
20   va_list args;
21
22   va_start(args, msg);
23   fputs("lspci: ", stderr);
24   vfprintf(stderr, msg, args);
25   fputc('\n', stderr);
26   exit(1);
27 }
28
29 void *
30 xmalloc(unsigned int howmuch)
31 {
32   void *p = malloc(howmuch);
33   if (!p)
34     die("Unable to allocate %d bytes of memory", howmuch);
35   return p;
36 }
37
38 void *
39 xrealloc(void *ptr, unsigned int howmuch)
40 {
41   void *p = realloc(ptr, howmuch);
42   if (!p)
43     die("Unable to allocate %d bytes of memory", howmuch);
44   return p;
45 }
46
47 int
48 parse_generic_option(int i, struct pci_access *pacc, char *optarg)
49 {
50   switch (i)
51     {
52 #ifdef HAVE_PM_LINUX_PROC
53     case 'P':
54       pacc->method_params[PCI_ACCESS_PROC_BUS_PCI] = optarg;
55       pacc->method = PCI_ACCESS_PROC_BUS_PCI;
56       break;
57 #endif
58 #ifdef HAVE_PM_INTEL_CONF
59     case 'H':
60       if (!strcmp(optarg, "1"))
61         pacc->method = PCI_ACCESS_I386_TYPE1;
62       else if (!strcmp(optarg, "2"))
63         pacc->method = PCI_ACCESS_I386_TYPE2;
64       else
65         die("Unknown hardware configuration type %s", optarg);
66       break;
67 #endif
68 #ifdef HAVE_PM_DUMP
69     case 'F':
70       pacc->method_params[PCI_ACCESS_DUMP] = optarg;
71       pacc->method = PCI_ACCESS_DUMP;
72       break;
73 #endif
74     case 'G':
75       pacc->debugging++;
76       break;
77     default:
78       return 0;
79     }
80   return 1;
81 }