info.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <signal.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <sys/wait.h>
  28. #include <errno.h>
  29. #include <unistd.h>
  30. #include <dirent.h>
  31. #include <limits.h>
  32. #include <ctype.h>
  33. #include <config.h>
  34. #include <dpkg.h>
  35. #include <myopt.h>
  36. #include "dpkg-deb.h"
  37. static void cu_info_prepare(int argc, void **argv) {
  38. pid_t c1;
  39. int status;
  40. char *directory;
  41. struct stat stab;
  42. directory= (char*)(argv[0]);
  43. if (chdir("/")) { perror(_("failed to chdir to `/' for cleanup")); return; }
  44. if (lstat(directory,&stab) && errno==ENOENT) return;
  45. if ((c1= fork()) == -1) { perror(_("failed to fork for cleanup")); return; }
  46. if (!c1) {
  47. execlp(RM,"rm","-rf",directory,(char*)0);
  48. perror(_("failed to exec rm for cleanup")); _exit(1);
  49. }
  50. if (waitpid(c1,&status,0) != c1) { perror(_("failed to wait for rm cleanup")); return; }
  51. if (status) { fprintf(stderr,_("rm cleanup failed, code %d\n"),status); }
  52. }
  53. static void info_prepare(const char *const **argvp,
  54. const char **debarp,
  55. const char **directoryp,
  56. int admininfo) {
  57. static char *dbuf;
  58. pid_t c1;
  59. *debarp= *(*argvp)++;
  60. if (!*debarp) badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  61. if ((dbuf= tempnam(NULL,"dpkg")) == NULL)
  62. ohshite(_("failed to make temporary filename"));
  63. *directoryp= dbuf;
  64. if (!(c1= m_fork())) {
  65. execlp(RM,"rm","-rf",dbuf,(char*)0); ohshite(_("failed to exec rm -rf"));
  66. }
  67. waitsubproc(c1,"rm -rf",0,0);
  68. push_cleanup(cu_info_prepare,-1, 0,0, 1, (void*)dbuf);
  69. extracthalf(*debarp, dbuf, "mx", admininfo);
  70. }
  71. static int ilist_select(const struct dirent *de) {
  72. return strcmp(de->d_name,".") && strcmp(de->d_name,"..");
  73. }
  74. static void info_spew(const char *debar, const char *directory,
  75. const char *const *argv) {
  76. const char *component;
  77. FILE *co;
  78. int re= 0;
  79. while ((component= *argv++) != 0) {
  80. co= fopen(component,"r");
  81. if (co) {
  82. stream_fd_copy(co, 1, -1, _("info_spew"));
  83. } else if (errno == ENOENT) {
  84. if (fprintf(stderr, _("dpkg-deb: `%.255s' contains no control component `%.255s'\n"),
  85. debar, component) == EOF) werr("stderr");
  86. re= 1;
  87. } else {
  88. ohshite(_("open component `%.255s' (in %.255s) failed in an unexpected way"),
  89. component, directory);
  90. }
  91. }
  92. if (re) ohshit(_("at least one requested control component missing"));
  93. }
  94. static void info_list(const char *debar, const char *directory) {
  95. char interpreter[INTERPRETER_MAX+1], *p;
  96. int il, lines;
  97. struct dirent **cdlist, *cdep;
  98. int cdn;
  99. FILE *cc;
  100. struct stat stab;
  101. int c;
  102. cdn= scandir(".", &cdlist, &ilist_select, alphasort);
  103. if (cdn == -1) ohshite(_("cannot scan directory `%.255s'"),directory);
  104. while (cdn-- >0) {
  105. cdep= *cdlist++;
  106. if (stat(cdep->d_name,&stab))
  107. ohshite(_("cannot stat `%.255s' (in `%.255s')"),cdep->d_name,directory);
  108. if (S_ISREG(stab.st_mode)) {
  109. if (!(cc= fopen(cdep->d_name,"r")))
  110. ohshite(_("cannot open `%.255s' (in `%.255s')"),cdep->d_name,directory);
  111. lines= 0; interpreter[0]= 0;
  112. if ((c= getc(cc))== '#') {
  113. if ((c= getc(cc))== '!') {
  114. while ((c= getc(cc))== ' ');
  115. p=interpreter; *p++='#'; *p++='!'; il=2;
  116. while (il<INTERPRETER_MAX && !isspace(c) && c!=EOF) {
  117. *p++= c; il++; c= getc(cc);
  118. }
  119. *p++= 0;
  120. if (c=='\n') lines++;
  121. }
  122. }
  123. while ((c= getc(cc))!= EOF) { if (c == '\n') lines++; }
  124. if (ferror(cc)) ohshite(_("failed to read `%.255s' (in `%.255s')"),
  125. cdep->d_name,directory);
  126. fclose(cc);
  127. if (printf(_(" %7ld bytes, %5d lines %c %-20.127s %.127s\n"),
  128. (long)stab.st_size, lines,
  129. S_IXUSR & stab.st_mode ? '*' : ' ',
  130. cdep->d_name, interpreter) == EOF)
  131. werr("stdout");
  132. } else {
  133. if (printf(_(" not a plain file %.255s\n"),cdep->d_name) == EOF)
  134. werr("stdout");
  135. }
  136. }
  137. if (!(cc= fopen("control","r"))) {
  138. if (errno != ENOENT) ohshite(_("failed to read `control' (in `%.255s')"),directory);
  139. if (fputs(_("(no `control' file in control archive!)\n"),stdout) < 0) werr("stdout");
  140. } else {
  141. lines= 1;
  142. while ((c= getc(cc))!= EOF) {
  143. if (lines) if (putc(' ',stdout) == EOF) werr("stdout");
  144. if (putc(c,stdout) == EOF) werr("stdout");
  145. lines= c=='\n';
  146. }
  147. if (!lines) if (putc('\n',stdout) == EOF) werr("stdout");
  148. }
  149. }
  150. static void info_field(const char *debar, const char *directory,
  151. const char *const *fields, int showfieldname) {
  152. FILE *cc;
  153. char fieldname[MAXFIELDNAME+1];
  154. char *pf;
  155. const char *const *fp;
  156. int doing, c, lno, fnl;
  157. if (!(cc= fopen("control","r"))) ohshite(_("could not open the `control' component"));
  158. doing= 1; lno= 1;
  159. for (;;) {
  160. c= getc(cc); if (c==EOF) { doing=0; break; }
  161. if (c == '\n') { lno++; doing=1; continue; }
  162. if (!isspace(c)) {
  163. doing= 0;
  164. for (pf=fieldname, fnl=0;
  165. fnl <= MAXFIELDNAME && c!=EOF && !isspace(c) && c!=':';
  166. c= getc(cc)) { *pf++= c; fnl++; }
  167. *pf++= 0;
  168. doing= fnl >= MAXFIELDNAME || c=='\n' || c==EOF;
  169. for (fp=fields; !doing && *fp; fp++)
  170. if (!strcasecmp(*fp,fieldname)) doing=1;
  171. if (showfieldname) {
  172. if (doing)
  173. fputs(fieldname,stdout);
  174. } else {
  175. if (c==':') c= getc(cc);
  176. while (c != '\n' && isspace(c)) c= getc(cc);
  177. }
  178. }
  179. for(;;) {
  180. if (c == EOF) break;
  181. if (doing) putc(c,stdout);
  182. if (c == '\n') { lno++; break; }
  183. c= getc(cc);
  184. }
  185. if (c == EOF) break;
  186. }
  187. if (ferror(cc)) ohshite(_("failed during read of `control' component"));
  188. if (doing) putc('\n',stdout);
  189. if (ferror(stdout)) werr("stdout");
  190. }
  191. void do_info(const char *const *argv) {
  192. const char *debar, *directory;
  193. if (*argv && argv[1]) {
  194. info_prepare(&argv,&debar,&directory,1);
  195. info_spew(debar,directory, argv);
  196. } else {
  197. info_prepare(&argv,&debar,&directory,2);
  198. info_list(debar,directory);
  199. }
  200. }
  201. void do_field(const char *const *argv) {
  202. const char *debar, *directory;
  203. info_prepare(&argv,&debar,&directory,1);
  204. if (*argv) {
  205. info_field(debar, directory, argv, argv[1]!=0);
  206. } else {
  207. static const char *const controlonly[]= { "control", 0 };
  208. info_spew(debar,directory, controlonly);
  209. }
  210. }
  211. void do_contents(const char *const *argv) {
  212. const char *debar;
  213. if (!(debar= *argv++) || *argv) badusage(_("--contents takes exactly one argument"));
  214. extracthalf(debar, 0, "tv", 0);
  215. }