Upload Tizen:Main source
[external/libsatsolver.git] / tools / rpms2solv.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * rpms2solv - create a solv file from multiple rpms
10  * 
11  */
12
13 #include <sys/types.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18
19 #include "util.h"
20 #include "pool.h"
21 #include "repo.h"
22 #include "repo_rpmdb.h"
23 #include "repo_solv.h"
24 #include "common_write.h"
25
26 static char *
27 fgets0(char *s, int size, FILE *stream)
28 {
29   char *p = s;
30   int c;
31
32   while (--size > 0)
33     {
34       c = getc(stream);
35       if (c == EOF)
36         {
37           if (p == s)
38             return 0;
39           c = 0;
40         }
41       *p++ = c;
42       if (!c)
43         return s;
44     }
45   *p = 0;
46   return s;
47 }
48
49 int
50 main(int argc, char **argv)
51 {
52   const char **rpms = 0;
53   char *manifest = 0;
54   int manifest0 = 0;
55   int c, nrpms = 0;
56   Pool *pool = pool_create();
57   Repo *repo;
58   FILE *fp;
59   char buf[4096], *p;
60   const char *basefile = 0;
61
62   while ((c = getopt(argc, argv, "0b:m:")) >= 0)
63     {
64       switch(c)
65         {
66         case 'b':
67           basefile = optarg;
68           break;
69         case 'm':
70           manifest = optarg;
71           break;
72         case '0':
73           manifest0 = 1;
74           break;
75         default:
76           exit(1);
77         }
78     }
79   if (manifest)
80     {
81       if (!strcmp(manifest, "-"))
82         fp = stdin;
83       else if ((fp = fopen(manifest, "r")) == 0)
84         {
85           perror(manifest);
86           exit(1);
87         }
88       for (;;)
89         {
90           if (manifest0)
91             {
92               if (!fgets0(buf, sizeof(buf), fp))
93                 break;
94             }
95           else
96             {
97               if (!fgets(buf, sizeof(buf), fp))
98                 break;
99               if ((p = strchr(buf, '\n')) != 0)
100                 *p = 0;
101             }
102           rpms = sat_extend(rpms, nrpms, 1, sizeof(char *), 15);
103           rpms[nrpms++] = strdup(buf);
104         }
105       if (fp != stdin)
106         fclose(fp);
107     }
108   while (optind < argc)
109     {
110       rpms = sat_extend(rpms, nrpms, 1, sizeof(char *), 15);
111       rpms[nrpms++] = strdup(argv[optind++]);
112     }
113   repo = repo_create(pool, "rpms2solv");
114   repo_add_rpms(repo, rpms, nrpms, 0);
115   tool_write(repo, basefile, 0);
116   pool_free(pool);
117   for (c = 0; c < nrpms; c++)
118     free((char *)rpms[c]);
119   sat_free(rpms);
120   exit(0);
121 }
122