dc22391a7c7cc52a605bf958714fc6c3582335dc
[platform/upstream/rpm.git] / tools / rpmdeps.c
1 #include "system.h"
2 const char *__progname;
3
4 #include <rpmbuild.h>
5 #include <argv.h>
6 #include <rpmds.h>
7 #include <rpmfc.h>
8
9 #include "debug.h"
10
11 char *progname;
12
13 static int print_provides;
14
15 static int print_requires;
16
17 static void rpmdsPrint(const char * msg, rpmds ds, FILE * fp)
18 {
19     if (fp == NULL) fp = stderr;
20
21     if (msg)
22         fprintf(fp, "===================================== %s\n", msg);
23
24     ds = rpmdsInit(ds);
25     while (rpmdsNext(ds) >= 0)
26         fprintf(fp, "%s\n", rpmdsDNEVR(ds)+2);
27 }
28
29 static struct poptOption optionsTable[] = {
30
31  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
32         N_("Common options for all rpm modes and executables:"),
33         NULL }, 
34
35  { "provides", 'P', POPT_ARG_VAL, &print_provides, -1,
36         NULL, NULL },
37  { "requires", 'R', POPT_ARG_VAL, &print_requires, -1,
38         NULL, NULL },
39
40    POPT_AUTOALIAS
41    POPT_AUTOHELP
42    POPT_TABLEEND
43 };
44
45 int
46 main(int argc, char *const argv[])
47 {
48     poptContext optCon;
49     ARGV_t av = NULL;
50     rpmfc fc;
51     int ac = 0;
52     int ec = 1;
53     int xx;
54 char buf[BUFSIZ];
55
56     if ((progname = strrchr(argv[0], '/')) != NULL)
57         progname++;
58     else
59         progname = argv[0];
60
61     optCon = rpmcliInit(argc, argv, optionsTable);
62     if (optCon == NULL)
63         goto exit;
64
65     av = poptGetArgs(optCon);
66     ac = argvCount(av);
67
68     if (ac == 0) {
69         char * b, * be;
70         av = NULL;
71         while ((b = fgets(buf, sizeof(buf), stdin)) != NULL) {
72             buf[sizeof(buf)-1] = '\0';
73             be = b + strlen(buf) - 1;
74             while (strchr("\r\n", *be) != NULL)
75                 *be-- = '\0';
76             xx = argvAdd(&av, b);
77         }
78         ac = argvCount(av);
79     }
80
81     /* Make sure file names are sorted. */
82     xx = argvSort(av, NULL);
83
84     /* Build file class dictionary. */
85     fc = rpmfcNew();
86     xx = rpmfcClassify(fc, av, NULL);
87
88     /* Build file/package dependency dictionary. */
89     xx = rpmfcApply(fc);
90
91 if (_rpmfc_debug) {
92 sprintf(buf, "final: files %d cdict[%d] %d%% ddictx[%d]", fc->nfiles, argvCount(fc->cdict), ((100 * fc->fknown)/fc->nfiles), argiCount(fc->ddictx));
93 rpmfcPrint(buf, fc, NULL);
94 }
95
96     if (print_provides)
97         rpmdsPrint(NULL, fc->provides, stdout);
98     if (print_requires)
99         rpmdsPrint(NULL, fc->requires, stdout);
100
101     fc = rpmfcFree(fc);
102
103     ec = 0;
104
105 exit:
106     optCon = rpmcliFini(optCon);
107     return ec;
108 }