info.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * dpkg-deb - construction and deconstruction of *.deb archives
  3. * info.c - providing information
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2001 Wichert Akkerman
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <sys/wait.h>
  26. #include <errno.h>
  27. #include <limits.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30. #include <fcntl.h>
  31. #include <dirent.h>
  32. #include <unistd.h>
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <dpkg/i18n.h>
  36. #include <dpkg/dpkg.h>
  37. #include <dpkg/dpkg-db.h>
  38. #include <dpkg/buffer.h>
  39. #include <dpkg/subproc.h>
  40. #include <dpkg/myopt.h>
  41. #include "dpkg-deb.h"
  42. static void cu_info_prepare(int argc, void **argv) {
  43. pid_t c1;
  44. int status;
  45. char *directory;
  46. struct stat stab;
  47. directory= (char*)(argv[0]);
  48. if (chdir("/")) { perror(_("failed to chdir to `/' for cleanup")); return; }
  49. if (lstat(directory,&stab) && errno==ENOENT) return;
  50. if ((c1= fork()) == -1) { perror(_("failed to fork for cleanup")); return; }
  51. if (!c1) {
  52. execlp(RM, "rm", "-rf", directory, NULL);
  53. perror(_("failed to exec rm for cleanup")); _exit(1);
  54. }
  55. if (waitpid(c1,&status,0) != c1) { perror(_("failed to wait for rm cleanup")); return; }
  56. if (status) { fprintf(stderr,_("rm cleanup failed, code %d\n"),status); }
  57. }
  58. static void info_prepare(const char *const **argvp,
  59. const char **debarp,
  60. const char **directoryp,
  61. int admininfo) {
  62. char *dbuf;
  63. pid_t c1;
  64. *debarp= *(*argvp)++;
  65. if (!*debarp) badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  66. /* This creates a temporary directory, so ignore the warning. */
  67. if ((dbuf= tempnam(NULL,"dpkg")) == NULL)
  68. ohshite(_("failed to make temporary directoryname"));
  69. *directoryp= dbuf;
  70. c1 = subproc_fork();
  71. if (!c1) {
  72. execlp(RM, "rm", "-rf", dbuf, NULL);
  73. ohshite(_("failed to exec rm -rf"));
  74. }
  75. subproc_wait_check(c1, "rm -rf", 0);
  76. push_cleanup(cu_info_prepare, -1, NULL, 0, 1, (void *)dbuf);
  77. extracthalf(*debarp, dbuf, "mx", admininfo);
  78. }
  79. static int ilist_select(const struct dirent *de) {
  80. return strcmp(de->d_name,".") && strcmp(de->d_name,"..");
  81. }
  82. static void info_spew(const char *debar, const char *directory,
  83. const char *const *argv) {
  84. const char *component;
  85. struct varbuf controlfile = VARBUF_INIT;
  86. int fd;
  87. int re= 0;
  88. while ((component = *argv++) != NULL) {
  89. varbufreset(&controlfile);
  90. varbufaddstr(&controlfile, directory);
  91. varbufaddc(&controlfile, '/');
  92. varbufaddstr(&controlfile, component);
  93. varbufaddc(&controlfile, '\0');
  94. fd = open(controlfile.buf, O_RDONLY);
  95. if (fd >= 0) {
  96. fd_fd_copy(fd, 1, -1, _("info_spew"));
  97. close(fd);
  98. } else if (errno == ENOENT) {
  99. fprintf(stderr,
  100. _("dpkg-deb: `%.255s' contains no control component `%.255s'\n"),
  101. debar, component);
  102. re++;
  103. } else {
  104. ohshite(_("open component `%.255s' (in %.255s) failed in an unexpected way"),
  105. component, directory);
  106. }
  107. }
  108. varbuf_destroy(&controlfile);
  109. if (re==1)
  110. ohshit(_("One requested control component is missing"));
  111. else if (re>1)
  112. ohshit(_("%d requested control components are missing"), re);
  113. }
  114. static void info_list(const char *debar, const char *directory) {
  115. char interpreter[INTERPRETER_MAX+1], *p;
  116. int il, lines;
  117. struct dirent **cdlist, *cdep;
  118. int cdn;
  119. FILE *cc;
  120. struct stat stab;
  121. int c;
  122. cdn= scandir(".", &cdlist, &ilist_select, alphasort);
  123. if (cdn == -1) ohshite(_("cannot scan directory `%.255s'"),directory);
  124. while (cdn-- >0) {
  125. cdep= *cdlist++;
  126. if (stat(cdep->d_name,&stab))
  127. ohshite(_("cannot stat `%.255s' (in `%.255s')"),cdep->d_name,directory);
  128. if (S_ISREG(stab.st_mode)) {
  129. if (!(cc= fopen(cdep->d_name,"r")))
  130. ohshite(_("cannot open `%.255s' (in `%.255s')"),cdep->d_name,directory);
  131. lines = 0;
  132. interpreter[0] = '\0';
  133. if (getc(cc) == '#') {
  134. if (getc(cc) == '!') {
  135. while ((c= getc(cc))== ' ');
  136. p=interpreter; *p++='#'; *p++='!'; il=2;
  137. while (il<INTERPRETER_MAX && !isspace(c) && c!=EOF) {
  138. *p++= c; il++; c= getc(cc);
  139. }
  140. *p = '\0';
  141. if (c=='\n') lines++;
  142. }
  143. }
  144. while ((c= getc(cc))!= EOF) { if (c == '\n') lines++; }
  145. if (ferror(cc)) ohshite(_("failed to read `%.255s' (in `%.255s')"),
  146. cdep->d_name,directory);
  147. fclose(cc);
  148. printf(_(" %7ld bytes, %5d lines %c %-20.127s %.127s\n"),
  149. (long)stab.st_size, lines, S_IXUSR & stab.st_mode ? '*' : ' ',
  150. cdep->d_name, interpreter);
  151. } else {
  152. printf(_(" not a plain file %.255s\n"), cdep->d_name);
  153. }
  154. }
  155. if (!(cc= fopen("control","r"))) {
  156. if (errno != ENOENT) ohshite(_("failed to read `control' (in `%.255s')"),directory);
  157. fputs(_("(no `control' file in control archive!)\n"), stdout);
  158. } else {
  159. lines= 1;
  160. while ((c= getc(cc))!= EOF) {
  161. if (lines)
  162. putc(' ', stdout);
  163. putc(c, stdout);
  164. lines= c=='\n';
  165. }
  166. if (!lines)
  167. putc('\n', stdout);
  168. }
  169. m_output(stdout, _("<standard output>"));
  170. }
  171. static void info_field(const char *debar, const char *directory,
  172. const char *const *fields, int showfieldname) {
  173. FILE *cc;
  174. char fieldname[MAXFIELDNAME+1];
  175. char *pf;
  176. const char *const *fp;
  177. int doing, c, lno, fnl;
  178. if (!(cc= fopen("control","r"))) ohshite(_("could not open the `control' component"));
  179. doing= 1; lno= 1;
  180. for (;;) {
  181. c= getc(cc); if (c==EOF) { doing=0; break; }
  182. if (c == '\n') { lno++; doing=1; continue; }
  183. if (!isspace(c)) {
  184. for (pf=fieldname, fnl=0;
  185. fnl <= MAXFIELDNAME && c!=EOF && !isspace(c) && c!=':';
  186. c= getc(cc)) { *pf++= c; fnl++; }
  187. *pf = '\0';
  188. doing= fnl >= MAXFIELDNAME || c=='\n' || c==EOF;
  189. for (fp=fields; !doing && *fp; fp++)
  190. if (!strcasecmp(*fp,fieldname)) doing=1;
  191. if (showfieldname) {
  192. if (doing)
  193. fputs(fieldname,stdout);
  194. } else {
  195. if (c==':') c= getc(cc);
  196. while (c != '\n' && isspace(c)) c= getc(cc);
  197. }
  198. }
  199. for(;;) {
  200. if (c == EOF) break;
  201. if (doing) putc(c,stdout);
  202. if (c == '\n') { lno++; break; }
  203. c= getc(cc);
  204. }
  205. if (c == EOF) break;
  206. }
  207. if (ferror(cc)) ohshite(_("failed during read of `control' component"));
  208. if (fclose(cc))
  209. ohshite(_("error closing the '%s' component"), "control");
  210. if (doing) putc('\n',stdout);
  211. m_output(stdout, _("<standard output>"));
  212. }
  213. void do_showinfo(const char* const* argv) {
  214. const char *debar, *directory;
  215. struct pkginfo *pkg;
  216. struct lstitem* fmt = parseformat(showformat);
  217. if (!fmt)
  218. ohshit(_("Error in format"));
  219. info_prepare(&argv,&debar,&directory,1);
  220. parsedb(CONTROLFILE, pdb_ignorefiles, &pkg, NULL, NULL);
  221. show1package(fmt, pkg, &pkg->installed);
  222. }
  223. void do_info(const char *const *argv) {
  224. const char *debar, *directory;
  225. if (*argv && argv[1]) {
  226. info_prepare(&argv,&debar,&directory,1);
  227. info_spew(debar,directory, argv);
  228. } else {
  229. info_prepare(&argv,&debar,&directory,2);
  230. info_list(debar,directory);
  231. }
  232. }
  233. void do_field(const char *const *argv) {
  234. const char *debar, *directory;
  235. info_prepare(&argv,&debar,&directory,1);
  236. if (*argv) {
  237. info_field(debar, directory, argv, argv[1] != NULL);
  238. } else {
  239. static const char *const controlonly[] = { "control", NULL };
  240. info_spew(debar,directory, controlonly);
  241. }
  242. }
  243. void do_contents(const char *const *argv) {
  244. const char *debar;
  245. if (!(debar= *argv++) || *argv) badusage(_("--contents takes exactly one argument"));
  246. extracthalf(debar, NULL, "tv", 0);
  247. }
  248. /* vi: sw=2
  249. */