Imported Upstream version 0.6.13
[platform/upstream/libsolv.git] / examples / solv / repoinfo_config_debian.c
1 #ifdef DEBIAN
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <dirent.h>
7
8 #include "pool.h"
9 #include "repo.h"
10
11 #include "repoinfo.h"
12 #include "repoinfo_config_debian.h"
13
14
15
16 struct repoinfo *
17 read_repoinfos_debian(Pool *pool, int *nrepoinfosp)
18 {
19   FILE *fp;
20   char buf[4096];
21   char buf2[4096];
22   int l;
23   char *kp, *url, *distro;
24   struct repoinfo *repoinfos = 0, *cinfo;
25   int nrepoinfos = 0;
26   DIR *dir = 0;
27   struct dirent *ent;
28
29   fp = fopen("/etc/apt/sources.list", "r");
30   while (1)
31     {
32       if (!fp)
33         {
34           if (!dir)
35             {
36               dir = opendir("/etc/apt/sources.list.d");
37               if (!dir)
38                 break;
39             }
40           if ((ent = readdir(dir)) == 0)
41             {
42               closedir(dir);
43               break;
44             }
45           if (ent->d_name[0] == '.')
46             continue;
47           l = strlen(ent->d_name);
48           if (l < 5 || strcmp(ent->d_name + l - 5, ".list") != 0)
49             continue;
50           snprintf(buf, sizeof(buf), "%s/%s", "/etc/apt/sources.list.d", ent->d_name);
51           if (!(fp = fopen(buf, "r")))
52             continue;
53         }
54       while(fgets(buf2, sizeof(buf2), fp))
55         {
56           l = strlen(buf2);
57           if (l == 0)
58             continue;
59           while (l && (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t'))
60             buf2[--l] = 0;
61           kp = buf2;
62           while (*kp == ' ' || *kp == '\t')
63             kp++;
64           if (!*kp || *kp == '#')
65             continue;
66           if (strncmp(kp, "deb", 3) != 0)
67             continue;
68           kp += 3;
69           if (*kp != ' ' && *kp != '\t')
70             continue;
71           while (*kp == ' ' || *kp == '\t')
72             kp++;
73           if (!*kp)
74             continue;
75           url = kp;
76           while (*kp && *kp != ' ' && *kp != '\t')
77             kp++;
78           if (*kp)
79             *kp++ = 0;
80           while (*kp == ' ' || *kp == '\t')
81             kp++;
82           if (!*kp)
83             continue;
84           distro = kp;
85           while (*kp && *kp != ' ' && *kp != '\t')
86             kp++;
87           if (*kp)
88             *kp++ = 0;
89           while (*kp == ' ' || *kp == '\t')
90             kp++;
91           if (!*kp)
92             continue;
93           repoinfos = solv_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
94           cinfo = repoinfos + nrepoinfos++;
95           memset(cinfo, 0, sizeof(*cinfo));
96           cinfo->baseurl = strdup(url);
97           cinfo->alias = solv_dupjoin(url, "/", distro);
98           cinfo->name = strdup(distro);
99           cinfo->type = TYPE_DEBIAN;
100           cinfo->enabled = 1;
101           cinfo->autorefresh = 1;
102           cinfo->repo_gpgcheck = 1;
103           cinfo->metadata_expire = METADATA_EXPIRE;
104           while (*kp)
105             {
106               char *compo;
107               while (*kp == ' ' || *kp == '\t')
108                 kp++;
109               if (!*kp)
110                 break;
111               compo = kp;
112               while (*kp && *kp != ' ' && *kp != '\t')
113                 kp++;
114               if (*kp)
115                 *kp++ = 0;
116               cinfo->components = solv_extend(cinfo->components, cinfo->ncomponents, 1, sizeof(*cinfo->components), 15);
117               cinfo->components[cinfo->ncomponents++] = strdup(compo);
118             }
119         }
120       fclose(fp);
121       fp = 0;
122     }
123   *nrepoinfosp = nrepoinfos;
124   return repoinfos;
125 }
126
127 #endif