enquiry.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /*
  2. * dpkg - main program for package management
  3. * enquiry.c - status enquiry and listing options
  4. *
  5. * Copyright (C) 1995,1996 Ian Jackson <ijackson@gnu.ai.mit.edu>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. /* fixme: per-package audit */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <fnmatch.h>
  26. #include <assert.h>
  27. #include <unistd.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/termios.h>
  32. #include <fcntl.h>
  33. #include <config.h>
  34. #include <dpkg.h>
  35. #include <dpkg-db.h>
  36. #include <myopt.h>
  37. #include "filesdb.h"
  38. #include "main.h"
  39. int pkglistqsortcmp(const void *a, const void *b) {
  40. struct pkginfo *pa= *(struct pkginfo**)a;
  41. struct pkginfo *pb= *(struct pkginfo**)b;
  42. return strcmp(pa->name,pb->name);
  43. }
  44. static void limiteddescription(struct pkginfo *pkg, int maxl,
  45. const char **pdesc_r, int *l_r) {
  46. const char *pdesc, *p;
  47. int l;
  48. pdesc= pkg->installed.valid ? pkg->installed.description : 0;
  49. if (!pdesc) pdesc= _("(no description available)");
  50. p= strchr(pdesc,'\n');
  51. if (!p) p= pdesc+strlen(pdesc);
  52. l= (p - pdesc > maxl) ? maxl : (int)(p - pdesc);
  53. *pdesc_r=pdesc; *l_r=l;
  54. }
  55. static int getttywidth() {
  56. int fd;
  57. int res;
  58. struct winsize ws;
  59. if ((fd=open("/dev/tty",O_RDONLY))!=-1) {
  60. if (ioctl(fd, TIOCGWINSZ, &ws)==-1)
  61. ws.ws_col=80;
  62. close(fd);
  63. }
  64. return ws.ws_col;
  65. }
  66. static void list1package(struct pkginfo *pkg, int *head) {
  67. int l,w;
  68. int nw,vw,dw;
  69. const char *pdesc;
  70. char format[80];
  71. w=getttywidth()-80; /* get spare width */
  72. if (w<0) w=0; /* lets not try to deal with terminals that are too small */
  73. w>>=2; /* halve that so we can add that to the both the name and description */
  74. nw=(14+w); /* name width */
  75. vw=(14+w); /* version width */
  76. dw=(44+(2*w)); /* description width */
  77. sprintf(format,"%%c%%c%%c %%-%d.%ds %%-%d.%ds %%.*s\n", nw, nw, vw, vw);
  78. if (!*head) {
  79. fputs(_("\
  80. Desired=Unknown/Install/Remove/Purge\n\
  81. | Status=Not/Installed/Config-files/Unpacked/Failed-config/Half-installed\n\
  82. |/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)\n"), stdout);
  83. printf(format,'|','|','/', _("Name"), _("Version"), 40, _("Description"));
  84. printf("+++-"); /* status */
  85. for (l=0;l<nw;l++) printf("="); printf("-"); /* packagename */
  86. for (l=0;l<vw;l++) printf("="); printf("-"); /* version */
  87. for (l=0;l<dw;l++) printf("="); /* description */
  88. printf("\n");
  89. *head= 1;
  90. }
  91. if (!pkg->installed.valid) blankpackageperfile(&pkg->installed);
  92. limiteddescription(pkg,dw,&pdesc,&l);
  93. printf(format,
  94. "uihrp"[pkg->want],
  95. "nUFiHc"[pkg->status],
  96. " R?#"[pkg->eflag],
  97. pkg->name,
  98. versiondescribe(&pkg->installed.version,vdew_never),
  99. l, pdesc);
  100. }
  101. void listpackages(const char *const *argv) {
  102. struct pkgiterator *it;
  103. struct pkginfo *pkg;
  104. struct pkginfo **pkgl;
  105. const char *thisarg;
  106. int np, i, head, found;
  107. modstatdb_init(admindir,msdbrw_readonly);
  108. np= countpackages();
  109. pkgl= m_malloc(sizeof(struct pkginfo*)*np);
  110. it= iterpkgstart(); i=0;
  111. while ((pkg= iterpkgnext(it))) {
  112. assert(i<np);
  113. pkgl[i++]= pkg;
  114. }
  115. iterpkgend(it);
  116. assert(i==np);
  117. qsort(pkgl,np,sizeof(struct pkginfo*),pkglistqsortcmp);
  118. head=0;
  119. if (!*argv) {
  120. for (i=0; i<np; i++) {
  121. pkg= pkgl[i];
  122. if (pkg->status == stat_notinstalled) continue;
  123. list1package(pkg,&head);
  124. }
  125. } else {
  126. while ((thisarg= *argv++)) {
  127. found= 0;
  128. for (i=0; i<np; i++) {
  129. pkg= pkgl[i];
  130. if (fnmatch(thisarg,pkg->name,0)) continue;
  131. list1package(pkg,&head); found++;
  132. }
  133. if (!found)
  134. fprintf(stderr,_("No packages found matching %s.\n"),thisarg);
  135. }
  136. }
  137. if (ferror(stdout)) werr("stdout");
  138. if (ferror(stderr)) werr("stderr");
  139. }
  140. struct badstatinfo {
  141. int (*yesno)(struct pkginfo*, const struct badstatinfo *bsi);
  142. int val;
  143. const char *explanation;
  144. };
  145. static int bsyn_reinstreq(struct pkginfo *pkg, const struct badstatinfo *bsi) {
  146. return pkg->eflag &= eflagf_reinstreq;
  147. }
  148. static int bsyn_status(struct pkginfo *pkg, const struct badstatinfo *bsi) {
  149. if (pkg->eflag &= eflagf_reinstreq) return 0;
  150. return pkg->status == bsi->val;
  151. }
  152. static const struct badstatinfo badstatinfos[]= {
  153. {
  154. bsyn_reinstreq, 0,
  155. N_("The following packages are in a mess due to serious problems during\n"
  156. "installation. They must be reinstalled for them (and any packages\n"
  157. "that depend on them) to function properly:\n")
  158. }, {
  159. bsyn_status, stat_unpacked,
  160. N_("The following packages have been unpacked but not yet configured.\n"
  161. "They must be configured using dpkg --configure or the configure\n"
  162. "menu option in dselect for them to work:\n")
  163. }, {
  164. bsyn_status, stat_halfconfigured,
  165. N_("The following packages are only half configured, probably due to problems\n"
  166. "configuring them the first time. The configuration should be retried using\n"
  167. "dpkg --configure <package> or the configure menu option in " DSELECT ":\n")
  168. }, {
  169. bsyn_status, stat_halfinstalled,
  170. N_("The following packages are only half installed, due to problems during\n"
  171. "installation. The installation can probably be completed by retrying it;\n"
  172. "the packages can be removed using dselect or dpkg --remove:\n")
  173. }, {
  174. 0
  175. }
  176. };
  177. static void describebriefly(struct pkginfo *pkg) {
  178. int maxl, l;
  179. const char *pdesc;
  180. maxl= 57;
  181. l= strlen(pkg->name);
  182. if (l>20) maxl -= (l-20);
  183. limiteddescription(pkg,maxl,&pdesc,&l);
  184. printf(" %-20s %.*s\n",pkg->name,l,pdesc);
  185. }
  186. void audit(const char *const *argv) {
  187. struct pkgiterator *it;
  188. struct pkginfo *pkg;
  189. const struct badstatinfo *bsi;
  190. int head;
  191. if (*argv) badusage(_("--audit does not take any arguments"));
  192. modstatdb_init(admindir,msdbrw_readonly);
  193. for (bsi= badstatinfos; bsi->yesno; bsi++) {
  194. head= 0;
  195. it= iterpkgstart();
  196. while ((pkg= iterpkgnext(it))) {
  197. if (!bsi->yesno(pkg,bsi)) continue;
  198. if (!head) {
  199. fputs(gettext(bsi->explanation),stdout);
  200. head= 1;
  201. }
  202. describebriefly(pkg);
  203. }
  204. iterpkgend(it);
  205. if (head) putchar('\n');
  206. }
  207. if (ferror(stderr)) werr("stderr");
  208. }
  209. struct sectionentry {
  210. struct sectionentry *next;
  211. const char *name;
  212. int count;
  213. };
  214. static int yettobeunpacked(struct pkginfo *pkg, const char **thissect) {
  215. if (pkg->want != want_install) return 0;
  216. switch (pkg->status) {
  217. case stat_unpacked: case stat_installed: case stat_halfconfigured:
  218. return 0;
  219. case stat_notinstalled: case stat_halfinstalled: case stat_configfiles:
  220. if (thissect)
  221. *thissect= pkg->section && *pkg->section ? pkg->section : _("<unknown>");
  222. return 1;
  223. default:
  224. internerr("unknown status checking for unpackedness");
  225. }
  226. }
  227. void unpackchk(const char *const *argv) {
  228. int totalcount, sects;
  229. struct sectionentry *sectionentries, *se, **sep;
  230. struct pkgiterator *it;
  231. struct pkginfo *pkg;
  232. const char *thissect;
  233. char buf[20];
  234. int width;
  235. if (*argv) badusage(_("--yet-to-unpack does not take any arguments"));
  236. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  237. totalcount= 0;
  238. sectionentries= 0;
  239. sects= 0;
  240. it= iterpkgstart();
  241. while ((pkg= iterpkgnext(it))) {
  242. if (!yettobeunpacked(pkg, &thissect)) continue;
  243. for (se= sectionentries; se && strcasecmp(thissect,se->name); se= se->next);
  244. if (!se) {
  245. se= nfmalloc(sizeof(struct sectionentry));
  246. for (sep= &sectionentries;
  247. *sep && strcasecmp(thissect,(*sep)->name) > 0;
  248. sep= &(*sep)->next);
  249. se->name= thissect;
  250. se->count= 0;
  251. se->next= *sep;
  252. *sep= se;
  253. sects++;
  254. }
  255. se->count++; totalcount++;
  256. }
  257. iterpkgend(it);
  258. if (totalcount == 0) exit(0);
  259. if (totalcount <= 12) {
  260. it= iterpkgstart();
  261. while ((pkg= iterpkgnext(it))) {
  262. if (!yettobeunpacked(pkg,0)) continue;
  263. describebriefly(pkg);
  264. }
  265. iterpkgend(it);
  266. } else if (sects <= 12) {
  267. for (se= sectionentries; se; se= se->next) {
  268. sprintf(buf,"%d",se->count);
  269. printf(_(" %d in %s: "),se->count,se->name);
  270. width= 70-strlen(se->name)-strlen(buf);
  271. while (width > 59) { putchar(' '); width--; }
  272. it= iterpkgstart();
  273. while ((pkg= iterpkgnext(it))) {
  274. if (!yettobeunpacked(pkg,&thissect)) continue;
  275. if (strcasecmp(thissect,se->name)) continue;
  276. width -= strlen(pkg->name); width--;
  277. if (width < 4) { printf(" ..."); break; }
  278. printf(" %s",pkg->name);
  279. }
  280. iterpkgend(it);
  281. putchar('\n');
  282. }
  283. } else {
  284. printf(_(" %d packages, from the following sections:"),totalcount);
  285. width= 0;
  286. for (se= sectionentries; se; se= se->next) {
  287. sprintf(buf,"%d",se->count);
  288. width -= (6 + strlen(se->name) + strlen(buf));
  289. if (width < 0) { putchar('\n'); width= 73 - strlen(se->name) - strlen(buf); }
  290. printf(" %s (%d)",se->name,se->count);
  291. }
  292. putchar('\n');
  293. }
  294. fflush(stdout);
  295. if (ferror(stdout)) werr("stdout");
  296. }
  297. static int searchoutput(struct filenamenode *namenode) {
  298. int found, i;
  299. struct filepackages *packageslump;
  300. if (namenode->divert) {
  301. for (i=0; i<2; i++) {
  302. if (namenode->divert->pkg) printf(_("diversion by %s"),namenode->divert->pkg->name);
  303. else printf(_("local diversion"));
  304. printf(" %s: %s\n", i ? _("to") : _("from"),
  305. i ?
  306. (namenode->divert->useinstead
  307. ? namenode->divert->useinstead->name
  308. : namenode->name)
  309. :
  310. (namenode->divert->camefrom
  311. ? namenode->divert->camefrom->name
  312. : namenode->name));
  313. }
  314. }
  315. found= 0;
  316. for (packageslump= namenode->packages;
  317. packageslump;
  318. packageslump= packageslump->more) {
  319. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  320. if (found) fputs(", ",stdout);
  321. fputs(packageslump->pkgs[i]->name,stdout);
  322. found++;
  323. }
  324. }
  325. if (found) printf(": %s\n",namenode->name);
  326. return found + (namenode->divert ? 1 : 0);
  327. }
  328. void searchfiles(const char *const *argv) {
  329. struct filenamenode *namenode;
  330. struct fileiterator *it;
  331. const char *thisarg;
  332. int found;
  333. static struct varbuf vb;
  334. if (!*argv)
  335. badusage(_("--search needs at least one file name pattern argument"));
  336. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  337. ensure_allinstfiles_available_quiet();
  338. ensure_diversions();
  339. while ((thisarg= *argv++) != 0) {
  340. found= 0;
  341. if (!strchr("*[?/",*thisarg)) {
  342. varbufreset(&vb);
  343. varbufaddc(&vb,'*');
  344. varbufaddstr(&vb,thisarg);
  345. varbufaddc(&vb,'*');
  346. varbufaddc(&vb,0);
  347. thisarg= vb.buf;
  348. }
  349. if (strcspn(thisarg,"*[?\\") == strlen(thisarg)) {
  350. namenode= findnamenode(thisarg, 0);
  351. found += searchoutput(namenode);
  352. } else {
  353. it= iterfilestart();
  354. while ((namenode= iterfilenext(it)) != 0) {
  355. if (fnmatch(thisarg,namenode->name,0)) continue;
  356. found+= searchoutput(namenode);
  357. }
  358. iterfileend(it);
  359. }
  360. if (!found) {
  361. fprintf(stderr,_("dpkg: %s not found.\n"),thisarg);
  362. if (ferror(stderr)) werr("stderr");
  363. } else {
  364. if (ferror(stdout)) werr("stdout");
  365. }
  366. }
  367. }
  368. void enqperpackage(const char *const *argv) {
  369. int failures;
  370. const char *thisarg;
  371. struct fileinlist *file;
  372. struct pkginfo *pkg;
  373. struct filenamenode *namenode;
  374. if (!*argv)
  375. badusage(_("--%s needs at least one package name argument"), cipaction->olong);
  376. failures= 0;
  377. if (cipaction->arg==act_listfiles)
  378. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  379. else
  380. modstatdb_init(admindir,msdbrw_readonly);
  381. while ((thisarg= *argv++) != 0) {
  382. pkg= findpackage(thisarg);
  383. switch (cipaction->arg) {
  384. case act_status:
  385. if (pkg->status == stat_notinstalled &&
  386. pkg->priority == pri_unknown &&
  387. !(pkg->section && *pkg->section) &&
  388. !pkg->files &&
  389. pkg->want == want_unknown &&
  390. !informative(pkg,&pkg->installed)) {
  391. printf(_("Package `%s' is not installed and no info is available.\n"),pkg->name);
  392. failures++;
  393. } else {
  394. writerecord(stdout, "<stdout>", pkg, &pkg->installed);
  395. }
  396. break;
  397. case act_printavail:
  398. if (!informative(pkg,&pkg->available)) {
  399. printf(_("Package `%s' is not available.\n"),pkg->name);
  400. failures++;
  401. } else {
  402. writerecord(stdout, "<stdout>", pkg, &pkg->available);
  403. }
  404. break;
  405. case act_listfiles:
  406. switch (pkg->status) {
  407. case stat_notinstalled:
  408. printf(_("Package `%s' is not installed.\n"),pkg->name);
  409. failures++;
  410. break;
  411. default:
  412. ensure_packagefiles_available(pkg);
  413. ensure_diversions();
  414. file= pkg->clientdata->files;
  415. if (!file) {
  416. printf(_("Package `%s' does not contain any files (!)\n"),pkg->name);
  417. } else {
  418. while (file) {
  419. namenode= file->namenode;
  420. puts(namenode->name);
  421. if (namenode->divert && !namenode->divert->camefrom) {
  422. if (!namenode->divert->pkg) printf(_("locally diverted"));
  423. else if (pkg == namenode->divert->pkg) printf(_("package diverts others"));
  424. else printf(_("diverted by %s"),namenode->divert->pkg->name);
  425. printf(" to: %s\n",namenode->divert->useinstead->name);
  426. }
  427. file= file->next;
  428. }
  429. }
  430. break;
  431. }
  432. break;
  433. default:
  434. internerr("unknown action");
  435. }
  436. putchar('\n');
  437. if (ferror(stdout)) werr("stdout");
  438. }
  439. if (failures) {
  440. puts(_("Use dpkg --info (= dpkg-deb --info) to examine archive files,\n"
  441. "and dpkg --contents (= dpkg-deb --contents) to list their contents."));
  442. if (ferror(stdout)) werr("stdout");
  443. }
  444. }
  445. static void assertversion(const char *const *argv,
  446. struct versionrevision *verrev_buf,
  447. const char *reqversion) {
  448. struct pkginfo *pkg;
  449. if (*argv) badusage(_("--assert-* does not take any arguments"));
  450. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  451. if (verrev_buf->epoch == ~0UL) {
  452. verrev_buf->epoch= 0;
  453. verrev_buf->version= nfstrsave(reqversion);
  454. verrev_buf->revision= 0;
  455. }
  456. pkg= findpackage("dpkg");
  457. switch (pkg->status) {
  458. case stat_installed:
  459. break;
  460. case stat_unpacked: case stat_halfconfigured: case stat_halfinstalled:
  461. if (versionsatisfied3(&pkg->configversion,verrev_buf,dvr_laterequal))
  462. break;
  463. printf(_("Version of dpkg with working epoch support not yet configured.\n"
  464. " Please use `dpkg --configure dpkg', and then try again.\n"));
  465. exit(1);
  466. default:
  467. printf(_("dpkg not recorded as installed, cannot check for epoch support !\n"));
  468. exit(1);
  469. }
  470. }
  471. void assertpredep(const char *const *argv) {
  472. static struct versionrevision predepversion = {~0UL,0,0};
  473. assertversion(argv,&predepversion,"1.1.0");
  474. }
  475. void assertepoch(const char *const *argv) {
  476. static struct versionrevision epochversion = {~0UL,0,0};
  477. assertversion(argv,&epochversion,"1.4.0.7");
  478. }
  479. void assertlongfilenames(const char *const *argv) {
  480. static struct versionrevision epochversion = {~0UL,0,0};
  481. assertversion(argv,&epochversion,"1.4.1.17");
  482. }
  483. void assertmulticonrep(const char *const *argv) {
  484. static struct versionrevision epochversion = {~0UL,0,0};
  485. assertversion(argv,&epochversion,"1.4.1.19");
  486. }
  487. void predeppackage(const char *const *argv) {
  488. /* Print a single package which:
  489. * (a) is the target of one or more relevant predependencies.
  490. * (b) has itself no unsatisfied pre-dependencies.
  491. * If such a package is present output is the Packages file entry,
  492. * which can be massaged as appropriate.
  493. * Exit status:
  494. * 0 = a package printed, OK
  495. * 1 = no suitable package available
  496. * 2 = error
  497. */
  498. static struct varbuf vb;
  499. struct pkgiterator *it;
  500. struct pkginfo *pkg= 0, *startpkg, *trypkg;
  501. struct dependency *dep;
  502. struct deppossi *possi, *provider;
  503. if (*argv) badusage(_("--predep-package does not take any argument"));
  504. modstatdb_init(admindir,msdbrw_readonly);
  505. clear_istobes(); /* We use clientdata->istobe to detect loops */
  506. for (it=iterpkgstart(), dep=0;
  507. !dep && (pkg=iterpkgnext(it));
  508. ) {
  509. if (pkg->want != want_install) continue; /* Ignore packages user doesn't want */
  510. if (!pkg->files) continue; /* Ignore packages not available */
  511. pkg->clientdata->istobe= itb_preinstall;
  512. for (dep= pkg->available.depends; dep; dep= dep->next) {
  513. if (dep->type != dep_predepends) continue;
  514. if (depisok(dep,&vb,0,1)) continue;
  515. break; /* This will leave dep non-NULL, and so exit the loop. */
  516. }
  517. pkg->clientdata->istobe= itb_normal;
  518. /* If dep is NULL we go and get the next package. */
  519. }
  520. if (!dep) exit(1); /* Not found */
  521. assert(pkg);
  522. startpkg= pkg;
  523. pkg->clientdata->istobe= itb_preinstall;
  524. /* OK, we have found an unsatisfied predependency.
  525. * Now go and find the first thing we need to install, as a first step
  526. * towards satisfying it.
  527. */
  528. do {
  529. /* We search for a package which would satisfy dep, and put it in pkg */
  530. for (possi=dep->list, pkg=0;
  531. !pkg && possi;
  532. possi=possi->next) {
  533. trypkg= possi->ed;
  534. if (!trypkg->available.valid) continue;
  535. if (trypkg->files && versionsatisfied(&trypkg->available,possi)) {
  536. if (trypkg->clientdata->istobe == itb_normal) { pkg= trypkg; break; }
  537. }
  538. if (possi->verrel != dvr_none) continue;
  539. for (provider=possi->ed->available.depended;
  540. !pkg && provider;
  541. provider=provider->next) {
  542. if (provider->up->type != dep_provides) continue;
  543. trypkg= provider->up->up;
  544. if (!trypkg->available.valid || !trypkg->files) continue;
  545. if (trypkg->clientdata->istobe == itb_normal) { pkg= trypkg; break; }
  546. }
  547. }
  548. if (!pkg) {
  549. varbufreset(&vb);
  550. describedepcon(&vb,dep);
  551. varbufaddc(&vb,0);
  552. fprintf(stderr, _("dpkg: cannot see how to satisfy pre-dependency:\n %s\n"),vb.buf);
  553. ohshit(_("cannot satisfy pre-dependencies for %.250s (wanted due to %.250s)"),
  554. dep->up->name,startpkg->name);
  555. }
  556. pkg->clientdata->istobe= itb_preinstall;
  557. for (dep= pkg->available.depends; dep; dep= dep->next) {
  558. if (dep->type != dep_predepends) continue;
  559. if (depisok(dep,&vb,0,1)) continue;
  560. break; /* This will leave dep non-NULL, and so exit the loop. */
  561. }
  562. } while (dep);
  563. /* OK, we've found it - pkg has no unsatisfied pre-dependencies ! */
  564. writerecord(stdout,"<stdout>",pkg,&pkg->available);
  565. if (fflush(stdout)) werr("stdout");
  566. }
  567. static void badlgccfn(const char *compiler, const char *output, const char *why)
  568. NONRETURNING;
  569. static void badlgccfn(const char *compiler, const char *output, const char *why) {
  570. fprintf(stderr,
  571. _("dpkg: unexpected output from `%s --print-libgcc-file-name':\n"
  572. " `%s'\n"),
  573. compiler,output);
  574. ohshit(_("compiler libgcc filename not understood: %.250s"),why);
  575. }
  576. void printinstarch(const char *const *argv) {
  577. if (*argv) badusage(_("--print-installation-architecture does not take any argument"));
  578. if (printf("%s\n",architecture) == EOF) werr("stdout");
  579. if (fflush(stdout)) werr("stdout");
  580. }
  581. void printarch(const char *const *argv) {
  582. static const struct { const char *from, *to, *gnu; } archtable[]= {
  583. #include "archtable.h"
  584. { 0,0,0 }
  585. }, *archp;
  586. const char *ccompiler, *arch;
  587. int p1[2], c;
  588. pid_t c1;
  589. FILE *ccpipe;
  590. struct varbuf vb;
  591. ptrdiff_t ll;
  592. char *p, *q;
  593. if (*argv) badusage(_("--print-architecture does not take any argument"));
  594. ccompiler= getenv("CC");
  595. if (!ccompiler) ccompiler= "gcc";
  596. varbufinit(&vb);
  597. m_pipe(p1);
  598. ccpipe= fdopen(p1[0],"r"); if (!ccpipe) ohshite(_("failed to fdopen CC pipe"));
  599. if (!(c1= m_fork())) {
  600. m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
  601. execlp(ccompiler,ccompiler,"--print-libgcc-file-name",(char*)0);
  602. /* if we have a problem excuting the C compiler, we don't
  603. * want to fail. If there is a problem with the compiler,
  604. * like not being installed, or CC being set incorrectly,
  605. * then important problems will show up elsewhere, not in
  606. * dpkg. If a C compiler is not important to the reason we
  607. * are being called, then we should just give them the built
  608. * in arch.
  609. */
  610. if (printf("/usr/lib/gcc-lib/%s-none/0.0.0/libgcc.a\n",architecture) == EOF)
  611. werr("stdout");
  612. if (fflush(stdout)) werr("stdout");
  613. exit(0);
  614. }
  615. close(p1[1]);
  616. while ((c= getc(ccpipe)) != EOF) varbufaddc(&vb,c);
  617. if (ferror(ccpipe)) ohshite(_("error reading from CC pipe"));
  618. waitsubproc(c1,"gcc --print-libgcc-file-name",0);
  619. if (!vb.used) badlgccfn(ccompiler,"",_("empty output"));
  620. varbufaddc(&vb,0);
  621. if (vb.buf[vb.used-2] != '\n') badlgccfn(ccompiler,vb.buf,_("no newline"));
  622. vb.used-= 2; varbufaddc(&vb,0);
  623. p= strstr(vb.buf,"/gcc-lib/");
  624. if (!p) badlgccfn(ccompiler,vb.buf,_("no gcc-lib component"));
  625. p+= 9;
  626. q= strchr(p,'-'); if (!q) badlgccfn(ccompiler,vb.buf,_("no hyphen after gcc-lib"));
  627. ll= q-p;
  628. for (archp=archtable;
  629. archp->from && !(strlen(archp->from) == ll && !strncmp(archp->from,p,ll));
  630. archp++);
  631. switch (cipaction->arg) {
  632. case act_printarch: arch= archp->to; break;
  633. case act_printgnuarch: arch= archp->gnu; break;
  634. default: internerr("unknown action in printarch");
  635. }
  636. if (!arch) {
  637. *q= 0; arch= p;
  638. fprintf(stderr, _("dpkg: warning, architecture `%s' not in remapping table\n"),arch);
  639. }
  640. if (printf("%s\n",arch) == EOF) werr("stdout");
  641. if (fflush(stdout)) werr("stdout");
  642. }
  643. void cmpversions(const char *const *argv) {
  644. struct relationinfo {
  645. const char *string;
  646. /* These values are exit status codes, so 0=true, 1=false */
  647. int if_lesser, if_equal, if_greater;
  648. int if_none_a, if_none_both, if_none_b;
  649. };
  650. static const struct relationinfo relationinfos[]= {
  651. /* < = > !a!2!b */
  652. { "le", 0,0,1, 0,0,1 },
  653. { "lt", 0,1,1, 0,1,1 },
  654. { "eq", 1,0,1, 1,0,1 },
  655. { "ne", 0,1,0, 0,1,0 },
  656. { "ge", 1,0,0, 1,0,0 },
  657. { "gt", 1,1,0, 1,1,0 },
  658. { "le-nl", 0,0,1, 1,0,0 }, /* Here none */
  659. { "lt-nl", 0,1,1, 1,1,0 }, /* is counted */
  660. { "ge-nl", 1,0,0, 0,0,1 }, /* than any version.*/
  661. { "gt-nl", 1,1,0, 0,1,1 }, /* */
  662. { "<", 0,0,1, 0,0,1 }, /* For compatibility*/
  663. { "<=", 0,0,1, 0,0,1 }, /* with dpkg */
  664. { "<<", 0,1,1, 0,1,1 }, /* control file */
  665. { "=", 1,0,1, 1,0,1 }, /* syntax */
  666. { ">", 1,0,0, 1,0,0 }, /* */
  667. { ">=", 1,0,0, 1,0,0 },
  668. { ">>", 1,1,0, 1,1,0 },
  669. { 0 }
  670. };
  671. const struct relationinfo *rip;
  672. const char *emsg;
  673. struct versionrevision a, b;
  674. int r;
  675. if (!argv[0] || !argv[1] || !argv[2] || argv[3])
  676. badusage(_("--cmpversions takes three arguments:"
  677. " <version> <relation> <version>"));
  678. for (rip=relationinfos; rip->string && strcmp(rip->string,argv[1]); rip++);
  679. if (!rip->string) badusage(_("--cmpversions bad relation"));
  680. if (*argv[0] && strcmp(argv[0],"<unknown>")) {
  681. emsg= parseversion(&a,argv[0]);
  682. if (emsg) {
  683. if (printf(_("version a has bad syntax: %s\n"),emsg) == EOF) werr("stdout");
  684. if (fflush(stdout)) werr("stdout");
  685. exit(1);
  686. }
  687. } else {
  688. blankversion(&a);
  689. }
  690. if (*argv[2] && strcmp(argv[2],"<unknown>")) {
  691. emsg= parseversion(&b,argv[2]);
  692. if (emsg) {
  693. if (printf(_("version b has bad syntax: %s\n"),emsg) == EOF) werr("stdout");
  694. if (fflush(stdout)) werr("stdout");
  695. exit(1);
  696. }
  697. } else {
  698. blankversion(&b);
  699. }
  700. if (!informativeversion(&a)) {
  701. exit(informativeversion(&b) ? rip->if_none_a : rip->if_none_both);
  702. } else if (!informativeversion(&b)) {
  703. exit(rip->if_none_b);
  704. }
  705. r= versioncompare(&a,&b);
  706. debug(dbg_general,"cmpversions a=`%s' b=`%s' r=%d",
  707. versiondescribe(&a,vdew_always),
  708. versiondescribe(&b,vdew_always),
  709. r);
  710. if (r>0) exit(rip->if_greater);
  711. else if (r<0) exit(rip->if_lesser);
  712. else exit(rip->if_equal);
  713. }