info.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. size_t pathlen;
  86. char *controlfile = NULL;
  87. FILE *co;
  88. int re= 0;
  89. while ((component = *argv++) != NULL) {
  90. pathlen = strlen(directory) + strlen(component) + 2;
  91. controlfile = m_realloc(controlfile, pathlen);
  92. memset(controlfile, 0, sizeof(controlfile));
  93. strcat(controlfile, directory);
  94. strcat(controlfile, "/");
  95. strcat(controlfile, component);
  96. co= fopen(controlfile,"r");
  97. if (co) {
  98. stream_fd_copy(co, 1, -1, _("info_spew"));
  99. } else if (errno == ENOENT) {
  100. fprintf(stderr,
  101. _("dpkg-deb: `%.255s' contains no control component `%.255s'\n"),
  102. debar, component);
  103. re++;
  104. } else {
  105. ohshite(_("open component `%.255s' (in %.255s) failed in an unexpected way"),
  106. component, directory);
  107. }
  108. }
  109. free(controlfile);
  110. if (re==1)
  111. ohshit(_("One requested control component is missing"));
  112. else if (re>1)
  113. ohshit(_("%d requested control components are missing"), re);
  114. }
  115. static void info_list(const char *debar, const char *directory) {
  116. char interpreter[INTERPRETER_MAX+1], *p;
  117. int il, lines;
  118. struct dirent **cdlist, *cdep;
  119. int cdn;
  120. FILE *cc;
  121. struct stat stab;
  122. int c;
  123. cdn= scandir(".", &cdlist, &ilist_select, alphasort);
  124. if (cdn == -1) ohshite(_("cannot scan directory `%.255s'"),directory);
  125. while (cdn-- >0) {
  126. cdep= *cdlist++;
  127. if (stat(cdep->d_name,&stab))
  128. ohshite(_("cannot stat `%.255s' (in `%.255s')"),cdep->d_name,directory);
  129. if (S_ISREG(stab.st_mode)) {
  130. if (!(cc= fopen(cdep->d_name,"r")))
  131. ohshite(_("cannot open `%.255s' (in `%.255s')"),cdep->d_name,directory);
  132. lines = 0;
  133. interpreter[0] = '\0';
  134. if ((c= getc(cc))== '#') {
  135. if ((c= getc(cc))== '!') {
  136. while ((c= getc(cc))== ' ');
  137. p=interpreter; *p++='#'; *p++='!'; il=2;
  138. while (il<INTERPRETER_MAX && !isspace(c) && c!=EOF) {
  139. *p++= c; il++; c= getc(cc);
  140. }
  141. *p++ = '\0';
  142. if (c=='\n') lines++;
  143. }
  144. }
  145. while ((c= getc(cc))!= EOF) { if (c == '\n') lines++; }
  146. if (ferror(cc)) ohshite(_("failed to read `%.255s' (in `%.255s')"),
  147. cdep->d_name,directory);
  148. fclose(cc);
  149. printf(_(" %7ld bytes, %5d lines %c %-20.127s %.127s\n"),
  150. (long)stab.st_size, lines, S_IXUSR & stab.st_mode ? '*' : ' ',
  151. cdep->d_name, interpreter);
  152. } else {
  153. printf(_(" not a plain file %.255s\n"), cdep->d_name);
  154. }
  155. }
  156. if (!(cc= fopen("control","r"))) {
  157. if (errno != ENOENT) ohshite(_("failed to read `control' (in `%.255s')"),directory);
  158. fputs(_("(no `control' file in control archive!)\n"), stdout);
  159. } else {
  160. lines= 1;
  161. while ((c= getc(cc))!= EOF) {
  162. if (lines)
  163. putc(' ', stdout);
  164. putc(c, stdout);
  165. lines= c=='\n';
  166. }
  167. if (!lines)
  168. putc('\n', stdout);
  169. }
  170. m_output(stdout, _("<standard output>"));
  171. }
  172. static void info_field(const char *debar, const char *directory,
  173. const char *const *fields, int showfieldname) {
  174. FILE *cc;
  175. char fieldname[MAXFIELDNAME+1];
  176. char *pf;
  177. const char *const *fp;
  178. int doing, c, lno, fnl;
  179. if (!(cc= fopen("control","r"))) ohshite(_("could not open the `control' component"));
  180. doing= 1; lno= 1;
  181. for (;;) {
  182. c= getc(cc); if (c==EOF) { doing=0; break; }
  183. if (c == '\n') { lno++; doing=1; continue; }
  184. if (!isspace(c)) {
  185. doing= 0;
  186. for (pf=fieldname, fnl=0;
  187. fnl <= MAXFIELDNAME && c!=EOF && !isspace(c) && c!=':';
  188. c= getc(cc)) { *pf++= c; fnl++; }
  189. *pf++ = '\0';
  190. doing= fnl >= MAXFIELDNAME || c=='\n' || c==EOF;
  191. for (fp=fields; !doing && *fp; fp++)
  192. if (!strcasecmp(*fp,fieldname)) doing=1;
  193. if (showfieldname) {
  194. if (doing)
  195. fputs(fieldname,stdout);
  196. } else {
  197. if (c==':') c= getc(cc);
  198. while (c != '\n' && isspace(c)) c= getc(cc);
  199. }
  200. }
  201. for(;;) {
  202. if (c == EOF) break;
  203. if (doing) putc(c,stdout);
  204. if (c == '\n') { lno++; break; }
  205. c= getc(cc);
  206. }
  207. if (c == EOF) break;
  208. }
  209. if (ferror(cc)) ohshite(_("failed during read of `control' component"));
  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);
  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. */