info.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * 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
  19. * License along with dpkg; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <dpkg/i18n.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <signal.h>
  29. #include <sys/stat.h>
  30. #include <sys/types.h>
  31. #include <sys/wait.h>
  32. #include <errno.h>
  33. #include <unistd.h>
  34. #include <dirent.h>
  35. #include <limits.h>
  36. #include <ctype.h>
  37. #include <dpkg/dpkg.h>
  38. #include <dpkg/dpkg-db.h>
  39. #include <dpkg/buffer.h>
  40. #include <dpkg/subproc.h>
  41. #include <dpkg/myopt.h>
  42. #include "dpkg-deb.h"
  43. static void cu_info_prepare(int argc, void **argv) {
  44. pid_t c1;
  45. int status;
  46. char *directory;
  47. struct stat stab;
  48. directory= (char*)(argv[0]);
  49. if (chdir("/")) { perror(_("failed to chdir to `/' for cleanup")); return; }
  50. if (lstat(directory,&stab) && errno==ENOENT) return;
  51. if ((c1= fork()) == -1) { perror(_("failed to fork for cleanup")); return; }
  52. if (!c1) {
  53. execlp(RM, "rm", "-rf", directory, NULL);
  54. perror(_("failed to exec rm for cleanup")); _exit(1);
  55. }
  56. if (waitpid(c1,&status,0) != c1) { perror(_("failed to wait for rm cleanup")); return; }
  57. if (status) { fprintf(stderr,_("rm cleanup failed, code %d\n"),status); }
  58. }
  59. static void info_prepare(const char *const **argvp,
  60. const char **debarp,
  61. const char **directoryp,
  62. int admininfo) {
  63. static char *dbuf;
  64. pid_t c1;
  65. *debarp= *(*argvp)++;
  66. if (!*debarp) badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  67. /* This creates a temporary directory, so ignore the warning. */
  68. if ((dbuf= tempnam(NULL,"dpkg")) == NULL)
  69. ohshite(_("failed to make temporary directoryname"));
  70. *directoryp= dbuf;
  71. if (!(c1= m_fork())) {
  72. execlp(RM, "rm", "-rf", dbuf, NULL);
  73. ohshite(_("failed to exec rm -rf"));
  74. }
  75. waitsubproc(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. FILE *co;
  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. co = fopen(controlfile.buf, "r");
  95. if (co) {
  96. stream_fd_copy(co, 1, -1, _("info_spew"));
  97. } else if (errno == ENOENT) {
  98. fprintf(stderr,
  99. _("dpkg-deb: `%.255s' contains no control component `%.255s'\n"),
  100. debar, component);
  101. re++;
  102. } else {
  103. ohshite(_("open component `%.255s' (in %.255s) failed in an unexpected way"),
  104. component, directory);
  105. }
  106. }
  107. varbuffree(&controlfile);
  108. if (re==1)
  109. ohshit(_("One requested control component is missing"));
  110. else if (re>1)
  111. ohshit(_("%d requested control components are missing"), re);
  112. }
  113. static void info_list(const char *debar, const char *directory) {
  114. char interpreter[INTERPRETER_MAX+1], *p;
  115. int il, lines;
  116. struct dirent **cdlist, *cdep;
  117. int cdn;
  118. FILE *cc;
  119. struct stat stab;
  120. int c;
  121. cdn= scandir(".", &cdlist, &ilist_select, alphasort);
  122. if (cdn == -1) ohshite(_("cannot scan directory `%.255s'"),directory);
  123. while (cdn-- >0) {
  124. cdep= *cdlist++;
  125. if (stat(cdep->d_name,&stab))
  126. ohshite(_("cannot stat `%.255s' (in `%.255s')"),cdep->d_name,directory);
  127. if (S_ISREG(stab.st_mode)) {
  128. if (!(cc= fopen(cdep->d_name,"r")))
  129. ohshite(_("cannot open `%.255s' (in `%.255s')"),cdep->d_name,directory);
  130. lines = 0;
  131. interpreter[0] = '\0';
  132. if ((c= getc(cc))== '#') {
  133. if ((c= getc(cc))== '!') {
  134. while ((c= getc(cc))== ' ');
  135. p=interpreter; *p++='#'; *p++='!'; il=2;
  136. while (il<INTERPRETER_MAX && !isspace(c) && c!=EOF) {
  137. *p++= c; il++; c= getc(cc);
  138. }
  139. *p++ = '\0';
  140. if (c=='\n') lines++;
  141. }
  142. }
  143. while ((c= getc(cc))!= EOF) { if (c == '\n') lines++; }
  144. if (ferror(cc)) ohshite(_("failed to read `%.255s' (in `%.255s')"),
  145. cdep->d_name,directory);
  146. fclose(cc);
  147. printf(_(" %7ld bytes, %5d lines %c %-20.127s %.127s\n"),
  148. (long)stab.st_size, lines, S_IXUSR & stab.st_mode ? '*' : ' ',
  149. cdep->d_name, interpreter);
  150. } else {
  151. printf(_(" not a plain file %.255s\n"), cdep->d_name);
  152. }
  153. }
  154. if (!(cc= fopen("control","r"))) {
  155. if (errno != ENOENT) ohshite(_("failed to read `control' (in `%.255s')"),directory);
  156. fputs(_("(no `control' file in control archive!)\n"), stdout);
  157. } else {
  158. lines= 1;
  159. while ((c= getc(cc))!= EOF) {
  160. if (lines)
  161. putc(' ', stdout);
  162. putc(c, stdout);
  163. lines= c=='\n';
  164. }
  165. if (!lines)
  166. putc('\n', stdout);
  167. }
  168. m_output(stdout, _("<standard output>"));
  169. }
  170. static void info_field(const char *debar, const char *directory,
  171. const char *const *fields, int showfieldname) {
  172. FILE *cc;
  173. char fieldname[MAXFIELDNAME+1];
  174. char *pf;
  175. const char *const *fp;
  176. int doing, c, lno, fnl;
  177. if (!(cc= fopen("control","r"))) ohshite(_("could not open the `control' component"));
  178. doing= 1; lno= 1;
  179. for (;;) {
  180. c= getc(cc); if (c==EOF) { doing=0; break; }
  181. if (c == '\n') { lno++; doing=1; continue; }
  182. if (!isspace(c)) {
  183. doing= 0;
  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 (doing) putc('\n',stdout);
  209. m_output(stdout, _("<standard output>"));
  210. }
  211. void do_showinfo(const char* const* argv) {
  212. const char *debar, *directory;
  213. struct pkginfo *pkg;
  214. struct lstitem* fmt = parseformat(showformat);
  215. if (!fmt)
  216. ohshit(_("Error in format"));
  217. info_prepare(&argv,&debar,&directory,1);
  218. parsedb(CONTROLFILE, pdb_ignorefiles, &pkg, NULL, NULL);
  219. show1package(fmt,pkg);
  220. }
  221. void do_info(const char *const *argv) {
  222. const char *debar, *directory;
  223. if (*argv && argv[1]) {
  224. info_prepare(&argv,&debar,&directory,1);
  225. info_spew(debar,directory, argv);
  226. } else {
  227. info_prepare(&argv,&debar,&directory,2);
  228. info_list(debar,directory);
  229. }
  230. }
  231. void do_field(const char *const *argv) {
  232. const char *debar, *directory;
  233. info_prepare(&argv,&debar,&directory,1);
  234. if (*argv) {
  235. info_field(debar, directory, argv, argv[1] != NULL);
  236. } else {
  237. static const char *const controlonly[] = { "control", NULL };
  238. info_spew(debar,directory, controlonly);
  239. }
  240. }
  241. void do_contents(const char *const *argv) {
  242. const char *debar;
  243. if (!(debar= *argv++) || *argv) badusage(_("--contents takes exactly one argument"));
  244. extracthalf(debar, NULL, "tv", 0);
  245. }
  246. /* vi: sw=2
  247. */