info.c 8.1 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 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 <dirent.h>
  31. #include <signal.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. static 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. if (!(c1= m_fork())) {
  71. execlp(RM, "rm", "-rf", dbuf, NULL);
  72. ohshite(_("failed to exec rm -rf"));
  73. }
  74. subproc_wait_check(c1, "rm -rf", 0);
  75. push_cleanup(cu_info_prepare, -1, NULL, 0, 1, (void *)dbuf);
  76. extracthalf(*debarp, dbuf, "mx", admininfo);
  77. }
  78. static int ilist_select(const struct dirent *de) {
  79. return strcmp(de->d_name,".") && strcmp(de->d_name,"..");
  80. }
  81. static void info_spew(const char *debar, const char *directory,
  82. const char *const *argv) {
  83. const char *component;
  84. struct varbuf controlfile = VARBUF_INIT;
  85. FILE *co;
  86. int re= 0;
  87. while ((component = *argv++) != NULL) {
  88. varbufreset(&controlfile);
  89. varbufaddstr(&controlfile, directory);
  90. varbufaddc(&controlfile, '/');
  91. varbufaddstr(&controlfile, component);
  92. varbufaddc(&controlfile, '\0');
  93. co = fopen(controlfile.buf, "r");
  94. if (co) {
  95. stream_fd_copy(co, 1, -1, _("info_spew"));
  96. } else if (errno == ENOENT) {
  97. fprintf(stderr,
  98. _("dpkg-deb: `%.255s' contains no control component `%.255s'\n"),
  99. debar, component);
  100. re++;
  101. } else {
  102. ohshite(_("open component `%.255s' (in %.255s) failed in an unexpected way"),
  103. component, directory);
  104. }
  105. }
  106. varbuffree(&controlfile);
  107. if (re==1)
  108. ohshit(_("One requested control component is missing"));
  109. else if (re>1)
  110. ohshit(_("%d requested control components are missing"), re);
  111. }
  112. static void info_list(const char *debar, const char *directory) {
  113. char interpreter[INTERPRETER_MAX+1], *p;
  114. int il, lines;
  115. struct dirent **cdlist, *cdep;
  116. int cdn;
  117. FILE *cc;
  118. struct stat stab;
  119. int c;
  120. cdn= scandir(".", &cdlist, &ilist_select, alphasort);
  121. if (cdn == -1) ohshite(_("cannot scan directory `%.255s'"),directory);
  122. while (cdn-- >0) {
  123. cdep= *cdlist++;
  124. if (stat(cdep->d_name,&stab))
  125. ohshite(_("cannot stat `%.255s' (in `%.255s')"),cdep->d_name,directory);
  126. if (S_ISREG(stab.st_mode)) {
  127. if (!(cc= fopen(cdep->d_name,"r")))
  128. ohshite(_("cannot open `%.255s' (in `%.255s')"),cdep->d_name,directory);
  129. lines = 0;
  130. interpreter[0] = '\0';
  131. if ((c= getc(cc))== '#') {
  132. if ((c= getc(cc))== '!') {
  133. while ((c= getc(cc))== ' ');
  134. p=interpreter; *p++='#'; *p++='!'; il=2;
  135. while (il<INTERPRETER_MAX && !isspace(c) && c!=EOF) {
  136. *p++= c; il++; c= getc(cc);
  137. }
  138. *p++ = '\0';
  139. if (c=='\n') lines++;
  140. }
  141. }
  142. while ((c= getc(cc))!= EOF) { if (c == '\n') lines++; }
  143. if (ferror(cc)) ohshite(_("failed to read `%.255s' (in `%.255s')"),
  144. cdep->d_name,directory);
  145. fclose(cc);
  146. printf(_(" %7ld bytes, %5d lines %c %-20.127s %.127s\n"),
  147. (long)stab.st_size, lines, S_IXUSR & stab.st_mode ? '*' : ' ',
  148. cdep->d_name, interpreter);
  149. } else {
  150. printf(_(" not a plain file %.255s\n"), cdep->d_name);
  151. }
  152. }
  153. if (!(cc= fopen("control","r"))) {
  154. if (errno != ENOENT) ohshite(_("failed to read `control' (in `%.255s')"),directory);
  155. fputs(_("(no `control' file in control archive!)\n"), stdout);
  156. } else {
  157. lines= 1;
  158. while ((c= getc(cc))!= EOF) {
  159. if (lines)
  160. putc(' ', stdout);
  161. putc(c, stdout);
  162. lines= c=='\n';
  163. }
  164. if (!lines)
  165. putc('\n', stdout);
  166. }
  167. m_output(stdout, _("<standard output>"));
  168. }
  169. static void info_field(const char *debar, const char *directory,
  170. const char *const *fields, int showfieldname) {
  171. FILE *cc;
  172. char fieldname[MAXFIELDNAME+1];
  173. char *pf;
  174. const char *const *fp;
  175. int doing, c, lno, fnl;
  176. if (!(cc= fopen("control","r"))) ohshite(_("could not open the `control' component"));
  177. doing= 1; lno= 1;
  178. for (;;) {
  179. c= getc(cc); if (c==EOF) { doing=0; break; }
  180. if (c == '\n') { lno++; doing=1; continue; }
  181. if (!isspace(c)) {
  182. doing= 0;
  183. for (pf=fieldname, fnl=0;
  184. fnl <= MAXFIELDNAME && c!=EOF && !isspace(c) && c!=':';
  185. c= getc(cc)) { *pf++= c; fnl++; }
  186. *pf++ = '\0';
  187. doing= fnl >= MAXFIELDNAME || c=='\n' || c==EOF;
  188. for (fp=fields; !doing && *fp; fp++)
  189. if (!strcasecmp(*fp,fieldname)) doing=1;
  190. if (showfieldname) {
  191. if (doing)
  192. fputs(fieldname,stdout);
  193. } else {
  194. if (c==':') c= getc(cc);
  195. while (c != '\n' && isspace(c)) c= getc(cc);
  196. }
  197. }
  198. for(;;) {
  199. if (c == EOF) break;
  200. if (doing) putc(c,stdout);
  201. if (c == '\n') { lno++; break; }
  202. c= getc(cc);
  203. }
  204. if (c == EOF) break;
  205. }
  206. if (ferror(cc)) ohshite(_("failed during read of `control' component"));
  207. if (doing) putc('\n',stdout);
  208. m_output(stdout, _("<standard output>"));
  209. }
  210. void do_showinfo(const char* const* argv) {
  211. const char *debar, *directory;
  212. struct pkginfo *pkg;
  213. struct lstitem* fmt = parseformat(showformat);
  214. if (!fmt)
  215. ohshit(_("Error in format"));
  216. info_prepare(&argv,&debar,&directory,1);
  217. parsedb(CONTROLFILE, pdb_ignorefiles, &pkg, NULL, NULL);
  218. show1package(fmt,pkg);
  219. }
  220. void do_info(const char *const *argv) {
  221. const char *debar, *directory;
  222. if (*argv && argv[1]) {
  223. info_prepare(&argv,&debar,&directory,1);
  224. info_spew(debar,directory, argv);
  225. } else {
  226. info_prepare(&argv,&debar,&directory,2);
  227. info_list(debar,directory);
  228. }
  229. }
  230. void do_field(const char *const *argv) {
  231. const char *debar, *directory;
  232. info_prepare(&argv,&debar,&directory,1);
  233. if (*argv) {
  234. info_field(debar, directory, argv, argv[1] != NULL);
  235. } else {
  236. static const char *const controlonly[] = { "control", NULL };
  237. info_spew(debar,directory, controlonly);
  238. }
  239. }
  240. void do_contents(const char *const *argv) {
  241. const char *debar;
  242. if (!(debar= *argv++) || *argv) badusage(_("--contents takes exactly one argument"));
  243. extracthalf(debar, NULL, "tv", 0);
  244. }
  245. /* vi: sw=2
  246. */