9 char *checksum_field
=NULL
;
11 static char *fieldcpy(char *dst
, char *fld
) {
12 while (*fld
&& *fld
!= ':')
16 while (isspace(*fld
)) fld
++;
17 return strcpy(dst
, fld
);
20 static void dogetdeps(char *pkgsfile
, char **in_pkgs
, int pkgc
) {
22 char cur_pkg
[MAX_LINE
];
23 char cur_deps
[MAX_LINE
];
29 cur_pkg
[0] = cur_deps
[0] = '\0';
31 for (i
= 0; i
< pkgc
; i
++) pkgs
[i
] = in_pkgs
[i
];
33 f
= fopen(pkgsfile
, "r");
40 while (fgets(buf
, sizeof(buf
), f
)) {
41 if (*buf
&& buf
[strlen(buf
)-1] == '\n') buf
[strlen(buf
)-1] = '\0';
42 if (strncasecmp(buf
, "Package:", 8) == 0) {
45 fieldcpy(cur_pkg
, buf
);
46 for (i
= 0; i
< pkgc
; i
++) {
47 if (!pkgs
[i
]) continue;
49 if (strcmp(cur_pkg
, pkgs
[i
]) == 0) {
57 (strncasecmp(buf
, "Depends:", 8) == 0 ||
58 strncasecmp(buf
, "Pre-Depends:", 12) == 0))
61 fieldcpy(cur_deps
, buf
);
64 while (isspace(*pch
)) pch
++;
67 while (*pch
&& *pch
!= '(' && *pch
!= '|' && *pch
!= ','
70 fputc(*pch
++, stdout
);
73 while (*pch
&& *pch
++ != ',') (void)NULL
;
80 static void dopkgmirrorpkgs(int uniq
, char *mirror
, char *pkgsfile
,
81 char *fieldname
, char **in_pkgs
, int pkgc
)
84 char cur_field
[MAX_LINE
];
85 char cur_pkg
[MAX_LINE
];
86 char cur_ver
[MAX_LINE
];
87 char cur_arch
[MAX_LINE
];
88 char cur_size
[MAX_LINE
];
89 char cur_checksum
[MAX_LINE
];
90 char cur_filename
[MAX_LINE
];
95 cur_pkg
[0] = cur_ver
[0] = cur_arch
[0] = cur_filename
[0] = '\0';
97 for (i
= 0; i
< pkgc
; i
++) pkgs
[i
] = in_pkgs
[i
];
99 f
= fopen(pkgsfile
, "r");
104 while (fgets(buf
, sizeof(buf
), f
)) {
105 if (*buf
&& buf
[strlen(buf
)-1] == '\n') buf
[strlen(buf
)-1] = '\0';
106 if (strncasecmp(buf
, fieldname
, strlen(fieldname
)) == 0) {
107 fieldcpy(cur_field
, buf
);
109 if (strncasecmp(buf
, "Package:", 8) == 0) {
110 fieldcpy(cur_pkg
, buf
);
111 } else if (strncasecmp(buf
, "Version:", 8) == 0) {
112 fieldcpy(cur_ver
, buf
);
113 } else if (strncasecmp(buf
, "Architecture:", 13) == 0) {
114 fieldcpy(cur_arch
, buf
);
115 } else if (strncasecmp(buf
, "Size:", 5) == 0) {
116 fieldcpy(cur_size
, buf
);
117 } else if (strncasecmp(buf
, checksum_field
, strlen(checksum_field
)) == 0
118 && buf
[strlen(checksum_field
)] == ':') {
119 fieldcpy(cur_checksum
, buf
);
120 } else if (strncasecmp(buf
, "Filename:", 9) == 0) {
121 fieldcpy(cur_filename
, buf
);
124 for (i
= 0; i
< pkgc
; i
++) {
125 if (!pkgs
[i
]) continue;
127 if (strcmp(cur_field
, pkgs
[i
]) == 0) {
128 printf("%s %s %s %s %s %s %s\n", cur_pkg
, cur_ver
, cur_arch
, mirror
, cur_filename
, cur_checksum
, cur_size
);
129 if (uniq
) pkgs
[i
] = NULL
;
138 /* any that weren't found are returned as "pkg -" */
140 for (i
= 0; i
< pkgc
; i
++) {
142 printf("%s -\n", pkgs
[i
]);
148 static void oom_die(void)
150 fprintf(stderr
, "Out of memory!\n");
154 static void dopkgstanzas(char *pkgsfile
, char **pkgs
, int pkgc
)
158 size_t accum_size
= 0, accum_alloc
= MAX_LINE
* 2;
159 char cur_pkg
[MAX_LINE
];
162 accum
= malloc(accum_alloc
);
166 f
= fopen(pkgsfile
, "r");
172 while (fgets(buf
, sizeof(buf
), f
)) {
174 size_t len
= strlen(buf
);
175 if (accum_size
+ len
+ 1 > accum_alloc
) {
176 accum_alloc
= (accum_size
+ len
+ 1) * 2;
177 accum
= realloc(accum
, accum_alloc
);
181 strcpy(accum
+ accum_size
, buf
);
184 if (*buf
&& buf
[strlen(buf
)-1] == '\n') buf
[strlen(buf
)-1] = '\0';
185 if (strncasecmp(buf
, "Package:", 8) == 0) {
186 fieldcpy(cur_pkg
, buf
);
189 for (i
= 0; i
< pkgc
; i
++) {
190 if (!pkgs
[i
]) continue;
191 if (strcmp(cur_pkg
, pkgs
[i
]) == 0) {
192 fputs(accum
, stdout
);
193 if (accum
[accum_size
- 1] != '\n')
194 fputs("\n\n", stdout
);
195 else if (accum
[accum_size
- 2] != '\n')
209 static int dotranslatewgetpercent(int low
, int high
, int end
, char *str
) {
213 /* print out anything that looks like a % on its own line, appropriately
217 while ( (ch
= getchar()) != EOF
) {
219 val
*= 10; val
+= ch
- '0';
220 } else if (ch
== '%') {
221 float f
= (float) val
/ 100.0 * (high
- low
) + low
;
223 printf("P: %d %d %s\n", (int) f
, end
, str
);
225 printf("P: %d %d\n", (int) f
, end
);
232 return lastval
== 100;
235 int main(int argc
, char *argv
[]) {
236 checksum_field
=getenv("DEBOOTSTRAP_CHECKSUM_FIELD");
237 if (checksum_field
== NULL
) {
238 checksum_field
="MD5sum";
241 if ((argc
== 6 || argc
== 5) && strcmp(argv
[1], "WGET%") == 0) {
242 if (dotranslatewgetpercent(atoi(argv
[2]), atoi(argv
[3]),
243 atoi(argv
[4]), argc
== 6 ? argv
[5] : NULL
))
249 } else if (argc
>= 4 && strcmp(argv
[1], "GETDEPS") == 0) {
251 for (i
= 3; argc
- i
> MAX_PKGS
; i
+= MAX_PKGS
) {
252 dogetdeps(argv
[2], argv
+i
, MAX_PKGS
);
254 dogetdeps(argv
[2], argv
+i
, argc
-i
);
256 } else if (argc
>= 5 && strcmp(argv
[1], "PKGS") == 0) {
258 for (i
= 4; argc
- i
> MAX_PKGS
; i
+= MAX_PKGS
) {
259 dopkgmirrorpkgs(1, argv
[2], argv
[3], "Package:", argv
+i
, MAX_PKGS
);
261 dopkgmirrorpkgs(1, argv
[2], argv
[3], "Package:", argv
+i
, argc
-i
);
263 } else if (argc
>= 6 && strcmp(argv
[1], "FIELD") == 0) {
265 for (i
= 5; argc
- i
> MAX_PKGS
; i
+= MAX_PKGS
) {
266 dopkgmirrorpkgs(0, argv
[3], argv
[4], argv
[2], argv
+i
, MAX_PKGS
);
268 dopkgmirrorpkgs(0, argv
[3], argv
[4], argv
[2], argv
+i
, argc
-i
);
270 } else if (argc
>= 4 && strcmp(argv
[1], "STANZAS") == 0) {
272 for (i
= 3; argc
- i
> MAX_PKGS
; i
+= MAX_PKGS
) {
273 dopkgstanzas(argv
[2], argv
+i
, MAX_PKGS
);
275 dopkgstanzas(argv
[2], argv
+i
, argc
-i
);
278 fprintf(stderr
, "usage: %s PKGS mirror packagesfile pkgs..\n", argv
[0]);
279 fprintf(stderr
, " or: %s FIELD field mirror packagesfile pkgs..\n",
281 fprintf(stderr
, " or: %s GETDEPS packagesfile pkgs..\n", argv
[0]);
282 fprintf(stderr
, " or: %s STANZAS packagesfile pkgs..\n", argv
[0]);
283 fprintf(stderr
, " or: %s WGET%% low high end reason\n", argv
[0]);