info.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * dpkg-deb - construction and deconstruction of *.deb archives
  3. * info.c - providing information
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <iwj10@cus.cam.ac.uk>
  6. * Copyright (C) 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 <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <signal.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include <sys/wait.h>
  29. #include <errno.h>
  30. #include <unistd.h>
  31. #include <dirent.h>
  32. #include <limits.h>
  33. #include <ctype.h>
  34. #include <config.h>
  35. #include <dpkg.h>
  36. #include <myopt.h>
  37. #include "dpkg-deb.h"
  38. static void cu_info_prepare(int argc, void **argv) {
  39. pid_t c1;
  40. int status;
  41. char *directory;
  42. struct stat stab;
  43. directory= (char*)(argv[0]);
  44. if (chdir("/")) { perror(_("failed to chdir to `/' for cleanup")); return; }
  45. if (lstat(directory,&stab) && errno==ENOENT) return;
  46. if ((c1= fork()) == -1) { perror(_("failed to fork for cleanup")); return; }
  47. if (!c1) {
  48. execlp(RM,"rm","-rf",directory,(char*)0);
  49. perror(_("failed to exec rm for cleanup")); _exit(1);
  50. }
  51. if (waitpid(c1,&status,0) != c1) { perror(_("failed to wait for rm cleanup")); return; }
  52. if (status) { fprintf(stderr,_("rm cleanup failed, code %d\n"),status); }
  53. }
  54. static void info_prepare(const char *const **argvp,
  55. const char **debarp,
  56. const char **directoryp,
  57. int admininfo) {
  58. static char *dbuf;
  59. pid_t c1;
  60. *debarp= *(*argvp)++;
  61. if (!*debarp) badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  62. /* This creates a temporary directory, so ignore the warning. */
  63. if ((dbuf= tempnam(NULL,"dpkg")) == NULL)
  64. ohshite(_("failed to make temporary directoryname"));
  65. *directoryp= dbuf;
  66. if (!(c1= m_fork())) {
  67. execlp(RM,"rm","-rf",dbuf,(char*)0); ohshite(_("failed to exec rm -rf"));
  68. }
  69. waitsubproc(c1,"rm -rf",0);
  70. push_cleanup(cu_info_prepare,-1, 0,0, 1, (void*)dbuf);
  71. extracthalf(*debarp, dbuf, "mx", admininfo);
  72. }
  73. static int ilist_select(const struct dirent *de) {
  74. return strcmp(de->d_name,".") && strcmp(de->d_name,"..");
  75. }
  76. static void info_spew(const char *debar, const char *directory,
  77. const char *const *argv) {
  78. const char *component;
  79. FILE *co;
  80. int re= 0;
  81. while ((component= *argv++) != 0) {
  82. co= fopen(component,"r");
  83. if (co) {
  84. stream_fd_copy(co, 1, -1, _("info_spew"));
  85. } else if (errno == ENOENT) {
  86. if (fprintf(stderr, _("dpkg-deb: `%.255s' contains no control component `%.255s'\n"),
  87. debar, component) == EOF) werr("stderr");
  88. re++;
  89. } else {
  90. ohshite(_("open component `%.255s' (in %.255s) failed in an unexpected way"),
  91. component, directory);
  92. }
  93. }
  94. if (re==1)
  95. ohshit(_("One requested control component is missing"));
  96. else if (re>1)
  97. ohshit(_("%d requested control components are missing"), re);
  98. }
  99. static void info_list(const char *debar, const char *directory) {
  100. char interpreter[INTERPRETER_MAX+1], *p;
  101. int il, lines;
  102. struct dirent **cdlist, *cdep;
  103. int cdn;
  104. FILE *cc;
  105. struct stat stab;
  106. int c;
  107. cdn= scandir(".", &cdlist, &ilist_select, alphasort);
  108. if (cdn == -1) ohshite(_("cannot scan directory `%.255s'"),directory);
  109. while (cdn-- >0) {
  110. cdep= *cdlist++;
  111. if (stat(cdep->d_name,&stab))
  112. ohshite(_("cannot stat `%.255s' (in `%.255s')"),cdep->d_name,directory);
  113. if (S_ISREG(stab.st_mode)) {
  114. if (!(cc= fopen(cdep->d_name,"r")))
  115. ohshite(_("cannot open `%.255s' (in `%.255s')"),cdep->d_name,directory);
  116. lines= 0; interpreter[0]= 0;
  117. if ((c= getc(cc))== '#') {
  118. if ((c= getc(cc))== '!') {
  119. while ((c= getc(cc))== ' ');
  120. p=interpreter; *p++='#'; *p++='!'; il=2;
  121. while (il<INTERPRETER_MAX && !isspace(c) && c!=EOF) {
  122. *p++= c; il++; c= getc(cc);
  123. }
  124. *p++= 0;
  125. if (c=='\n') lines++;
  126. }
  127. }
  128. while ((c= getc(cc))!= EOF) { if (c == '\n') lines++; }
  129. if (ferror(cc)) ohshite(_("failed to read `%.255s' (in `%.255s')"),
  130. cdep->d_name,directory);
  131. fclose(cc);
  132. if (printf(_(" %7ld bytes, %5d lines %c %-20.127s %.127s\n"),
  133. (long)stab.st_size, lines,
  134. S_IXUSR & stab.st_mode ? '*' : ' ',
  135. cdep->d_name, interpreter) == EOF)
  136. werr("stdout");
  137. } else {
  138. if (printf(_(" not a plain file %.255s\n"),cdep->d_name) == EOF)
  139. werr("stdout");
  140. }
  141. }
  142. if (!(cc= fopen("control","r"))) {
  143. if (errno != ENOENT) ohshite(_("failed to read `control' (in `%.255s')"),directory);
  144. if (fputs(_("(no `control' file in control archive!)\n"),stdout) < 0) werr("stdout");
  145. } else {
  146. lines= 1;
  147. while ((c= getc(cc))!= EOF) {
  148. if (lines) if (putc(' ',stdout) == EOF) werr("stdout");
  149. if (putc(c,stdout) == EOF) werr("stdout");
  150. lines= c=='\n';
  151. }
  152. if (!lines) if (putc('\n',stdout) == EOF) werr("stdout");
  153. }
  154. }
  155. static void info_field(const char *debar, const char *directory,
  156. const char *const *fields, int showfieldname) {
  157. FILE *cc;
  158. char fieldname[MAXFIELDNAME+1];
  159. char *pf;
  160. const char *const *fp;
  161. int doing, c, lno, fnl;
  162. if (!(cc= fopen("control","r"))) ohshite(_("could not open the `control' component"));
  163. doing= 1; lno= 1;
  164. for (;;) {
  165. c= getc(cc); if (c==EOF) { doing=0; break; }
  166. if (c == '\n') { lno++; doing=1; continue; }
  167. if (!isspace(c)) {
  168. doing= 0;
  169. for (pf=fieldname, fnl=0;
  170. fnl <= MAXFIELDNAME && c!=EOF && !isspace(c) && c!=':';
  171. c= getc(cc)) { *pf++= c; fnl++; }
  172. *pf++= 0;
  173. doing= fnl >= MAXFIELDNAME || c=='\n' || c==EOF;
  174. for (fp=fields; !doing && *fp; fp++)
  175. if (!strcasecmp(*fp,fieldname)) doing=1;
  176. if (showfieldname) {
  177. if (doing)
  178. fputs(fieldname,stdout);
  179. } else {
  180. if (c==':') c= getc(cc);
  181. while (c != '\n' && isspace(c)) c= getc(cc);
  182. }
  183. }
  184. for(;;) {
  185. if (c == EOF) break;
  186. if (doing) putc(c,stdout);
  187. if (c == '\n') { lno++; break; }
  188. c= getc(cc);
  189. }
  190. if (c == EOF) break;
  191. }
  192. if (ferror(cc)) ohshite(_("failed during read of `control' component"));
  193. if (doing) putc('\n',stdout);
  194. if (ferror(stdout)) werr("stdout");
  195. }
  196. void do_info(const char *const *argv) {
  197. const char *debar, *directory;
  198. if (*argv && argv[1]) {
  199. info_prepare(&argv,&debar,&directory,1);
  200. info_spew(debar,directory, argv);
  201. } else {
  202. info_prepare(&argv,&debar,&directory,2);
  203. info_list(debar,directory);
  204. }
  205. }
  206. void do_field(const char *const *argv) {
  207. const char *debar, *directory;
  208. info_prepare(&argv,&debar,&directory,1);
  209. if (*argv) {
  210. info_field(debar, directory, argv, argv[1]!=0);
  211. } else {
  212. static const char *const controlonly[]= { "control", 0 };
  213. info_spew(debar,directory, controlonly);
  214. }
  215. }
  216. void do_contents(const char *const *argv) {
  217. const char *debar;
  218. if (!(debar= *argv++) || *argv) badusage(_("--contents takes exactly one argument"));
  219. extracthalf(debar, 0, "tv", 0);
  220. }