info.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.h>
  38. #include <dpkg-db.h>
  39. #include <myopt.h>
  40. #include "dpkg-deb.h"
  41. static void cu_info_prepare(int argc, void **argv) {
  42. pid_t c1;
  43. int status;
  44. char *directory;
  45. struct stat stab;
  46. directory= (char*)(argv[0]);
  47. if (chdir("/")) { perror(_("failed to chdir to `/' for cleanup")); return; }
  48. if (lstat(directory,&stab) && errno==ENOENT) return;
  49. if ((c1= fork()) == -1) { perror(_("failed to fork for cleanup")); return; }
  50. if (!c1) {
  51. execlp(RM, "rm", "-rf", directory, NULL);
  52. perror(_("failed to exec rm for cleanup")); _exit(1);
  53. }
  54. if (waitpid(c1,&status,0) != c1) { perror(_("failed to wait for rm cleanup")); return; }
  55. if (status) { fprintf(stderr,_("rm cleanup failed, code %d\n"),status); }
  56. }
  57. static void info_prepare(const char *const **argvp,
  58. const char **debarp,
  59. const char **directoryp,
  60. int admininfo) {
  61. static char *dbuf;
  62. pid_t c1;
  63. *debarp= *(*argvp)++;
  64. if (!*debarp) badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  65. /* This creates a temporary directory, so ignore the warning. */
  66. if ((dbuf= tempnam(NULL,"dpkg")) == NULL)
  67. ohshite(_("failed to make temporary directoryname"));
  68. *directoryp= dbuf;
  69. if (!(c1= m_fork())) {
  70. execlp(RM, "rm", "-rf", dbuf, NULL);
  71. ohshite(_("failed to exec rm -rf"));
  72. }
  73. waitsubproc(c1,"rm -rf",0);
  74. push_cleanup(cu_info_prepare, -1, NULL, 0, 1, (void *)dbuf);
  75. extracthalf(*debarp, dbuf, "mx", admininfo);
  76. }
  77. static int ilist_select(const struct dirent *de) {
  78. return strcmp(de->d_name,".") && strcmp(de->d_name,"..");
  79. }
  80. static void info_spew(const char *debar, const char *directory,
  81. const char *const *argv) {
  82. const char *component;
  83. size_t pathlen;
  84. char *controlfile = NULL;
  85. FILE *co;
  86. int re= 0;
  87. while ((component = *argv++) != NULL) {
  88. pathlen = strlen(directory) + strlen(component) + 2;
  89. controlfile = m_realloc(controlfile, pathlen);
  90. memset(controlfile, 0, sizeof(controlfile));
  91. strcat(controlfile, directory);
  92. strcat(controlfile, "/");
  93. strcat(controlfile, component);
  94. co= fopen(controlfile,"r");
  95. if (co) {
  96. stream_fd_copy(co, 1, -1, _("info_spew"));
  97. } else if (errno == ENOENT) {
  98. if (fprintf(stderr, _("dpkg-deb: `%.255s' contains no control component `%.255s'\n"),
  99. debar, component) == EOF) werr("stderr");
  100. re++;
  101. } else {
  102. ohshite(_("open component `%.255s' (in %.255s) failed in an unexpected way"),
  103. component, directory);
  104. }
  105. }
  106. free(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; interpreter[0]= 0;
  130. if ((c= getc(cc))== '#') {
  131. if ((c= getc(cc))== '!') {
  132. while ((c= getc(cc))== ' ');
  133. p=interpreter; *p++='#'; *p++='!'; il=2;
  134. while (il<INTERPRETER_MAX && !isspace(c) && c!=EOF) {
  135. *p++= c; il++; c= getc(cc);
  136. }
  137. *p++= 0;
  138. if (c=='\n') lines++;
  139. }
  140. }
  141. while ((c= getc(cc))!= EOF) { if (c == '\n') lines++; }
  142. if (ferror(cc)) ohshite(_("failed to read `%.255s' (in `%.255s')"),
  143. cdep->d_name,directory);
  144. fclose(cc);
  145. if (printf(_(" %7ld bytes, %5d lines %c %-20.127s %.127s\n"),
  146. (long)stab.st_size, lines,
  147. S_IXUSR & stab.st_mode ? '*' : ' ',
  148. cdep->d_name, interpreter) == EOF)
  149. werr("stdout");
  150. } else {
  151. if (printf(_(" not a plain file %.255s\n"),cdep->d_name) == EOF)
  152. werr("stdout");
  153. }
  154. }
  155. if (!(cc= fopen("control","r"))) {
  156. if (errno != ENOENT) ohshite(_("failed to read `control' (in `%.255s')"),directory);
  157. if (fputs(_("(no `control' file in control archive!)\n"),stdout) < 0) werr("stdout");
  158. } else {
  159. lines= 1;
  160. while ((c= getc(cc))!= EOF) {
  161. if (lines) if (putc(' ',stdout) == EOF) werr("stdout");
  162. if (putc(c,stdout) == EOF) werr("stdout");
  163. lines= c=='\n';
  164. }
  165. if (!lines) if (putc('\n',stdout) == EOF) werr("stdout");
  166. }
  167. }
  168. static void info_field(const char *debar, const char *directory,
  169. const char *const *fields, int showfieldname) {
  170. FILE *cc;
  171. char fieldname[MAXFIELDNAME+1];
  172. char *pf;
  173. const char *const *fp;
  174. int doing, c, lno, fnl;
  175. if (!(cc= fopen("control","r"))) ohshite(_("could not open the `control' component"));
  176. doing= 1; lno= 1;
  177. for (;;) {
  178. c= getc(cc); if (c==EOF) { doing=0; break; }
  179. if (c == '\n') { lno++; doing=1; continue; }
  180. if (!isspace(c)) {
  181. doing= 0;
  182. for (pf=fieldname, fnl=0;
  183. fnl <= MAXFIELDNAME && c!=EOF && !isspace(c) && c!=':';
  184. c= getc(cc)) { *pf++= c; fnl++; }
  185. *pf++= 0;
  186. doing= fnl >= MAXFIELDNAME || c=='\n' || c==EOF;
  187. for (fp=fields; !doing && *fp; fp++)
  188. if (!strcasecmp(*fp,fieldname)) doing=1;
  189. if (showfieldname) {
  190. if (doing)
  191. fputs(fieldname,stdout);
  192. } else {
  193. if (c==':') c= getc(cc);
  194. while (c != '\n' && isspace(c)) c= getc(cc);
  195. }
  196. }
  197. for(;;) {
  198. if (c == EOF) break;
  199. if (doing) putc(c,stdout);
  200. if (c == '\n') { lno++; break; }
  201. c= getc(cc);
  202. }
  203. if (c == EOF) break;
  204. }
  205. if (ferror(cc)) ohshite(_("failed during read of `control' component"));
  206. if (doing) putc('\n',stdout);
  207. if (ferror(stdout)) werr("stdout");
  208. }
  209. void do_showinfo(const char* const* argv) {
  210. const char *debar, *directory;
  211. struct pkginfo *pkg;
  212. struct lstitem* fmt = parseformat(showformat);
  213. if (!fmt)
  214. ohshit(_("Error in format"));
  215. info_prepare(&argv,&debar,&directory,1);
  216. parsedb(CONTROLFILE, pdb_ignorefiles, &pkg, NULL, NULL);
  217. show1package(fmt,pkg);
  218. }
  219. void do_info(const char *const *argv) {
  220. const char *debar, *directory;
  221. if (*argv && argv[1]) {
  222. info_prepare(&argv,&debar,&directory,1);
  223. info_spew(debar,directory, argv);
  224. } else {
  225. info_prepare(&argv,&debar,&directory,2);
  226. info_list(debar,directory);
  227. }
  228. }
  229. void do_field(const char *const *argv) {
  230. const char *debar, *directory;
  231. info_prepare(&argv,&debar,&directory,1);
  232. if (*argv) {
  233. info_field(debar, directory, argv, argv[1] != NULL);
  234. } else {
  235. static const char *const controlonly[] = { "control", NULL };
  236. info_spew(debar,directory, controlonly);
  237. }
  238. }
  239. void do_contents(const char *const *argv) {
  240. const char *debar;
  241. if (!(debar= *argv++) || *argv) badusage(_("--contents takes exactly one argument"));
  242. extracthalf(debar, NULL, "tv", 0);
  243. }
  244. /* vi: sw=2
  245. */