info.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. /* This created a temporary directory, so ignore the warning. */
  62. if ((dbuf= tempnam(NULL,"dpkg")) == NULL)
  63. ohshite(_("failed to make temporary filename"));
  64. *directoryp= dbuf;
  65. if (!(c1= m_fork())) {
  66. execlp(RM,"rm","-rf",dbuf,(char*)0); ohshite(_("failed to exec rm -rf"));
  67. }
  68. waitsubproc(c1,"rm -rf",0);
  69. push_cleanup(cu_info_prepare,-1, 0,0, 1, (void*)dbuf);
  70. extracthalf(*debarp, dbuf, "mx", admininfo);
  71. }
  72. static int ilist_select(const struct dirent *de) {
  73. return strcmp(de->d_name,".") && strcmp(de->d_name,"..");
  74. }
  75. static void info_spew(const char *debar, const char *directory,
  76. const char *const *argv) {
  77. const char *component;
  78. FILE *co;
  79. int re= 0;
  80. while ((component= *argv++) != 0) {
  81. co= fopen(component,"r");
  82. if (co) {
  83. stream_fd_copy(co, 1, -1, _("info_spew"));
  84. } else if (errno == ENOENT) {
  85. if (fprintf(stderr, _("dpkg-deb: `%.255s' contains no control component `%.255s'\n"),
  86. debar, component) == EOF) werr("stderr");
  87. re= 1;
  88. } else {
  89. ohshite(_("open component `%.255s' (in %.255s) failed in an unexpected way"),
  90. component, directory);
  91. }
  92. }
  93. if (re) ohshit(_("at least one requested control component missing"));
  94. }
  95. static void info_list(const char *debar, const char *directory) {
  96. char interpreter[INTERPRETER_MAX+1], *p;
  97. int il, lines;
  98. struct dirent **cdlist, *cdep;
  99. int cdn;
  100. FILE *cc;
  101. struct stat stab;
  102. int c;
  103. cdn= scandir(".", &cdlist, &ilist_select, alphasort);
  104. if (cdn == -1) ohshite(_("cannot scan directory `%.255s'"),directory);
  105. while (cdn-- >0) {
  106. cdep= *cdlist++;
  107. if (stat(cdep->d_name,&stab))
  108. ohshite(_("cannot stat `%.255s' (in `%.255s')"),cdep->d_name,directory);
  109. if (S_ISREG(stab.st_mode)) {
  110. if (!(cc= fopen(cdep->d_name,"r")))
  111. ohshite(_("cannot open `%.255s' (in `%.255s')"),cdep->d_name,directory);
  112. lines= 0; interpreter[0]= 0;
  113. if ((c= getc(cc))== '#') {
  114. if ((c= getc(cc))== '!') {
  115. while ((c= getc(cc))== ' ');
  116. p=interpreter; *p++='#'; *p++='!'; il=2;
  117. while (il<INTERPRETER_MAX && !isspace(c) && c!=EOF) {
  118. *p++= c; il++; c= getc(cc);
  119. }
  120. *p++= 0;
  121. if (c=='\n') lines++;
  122. }
  123. }
  124. while ((c= getc(cc))!= EOF) { if (c == '\n') lines++; }
  125. if (ferror(cc)) ohshite(_("failed to read `%.255s' (in `%.255s')"),
  126. cdep->d_name,directory);
  127. fclose(cc);
  128. if (printf(_(" %7ld bytes, %5d lines %c %-20.127s %.127s\n"),
  129. (long)stab.st_size, lines,
  130. S_IXUSR & stab.st_mode ? '*' : ' ',
  131. cdep->d_name, interpreter) == EOF)
  132. werr("stdout");
  133. } else {
  134. if (printf(_(" not a plain file %.255s\n"),cdep->d_name) == EOF)
  135. werr("stdout");
  136. }
  137. }
  138. if (!(cc= fopen("control","r"))) {
  139. if (errno != ENOENT) ohshite(_("failed to read `control' (in `%.255s')"),directory);
  140. if (fputs(_("(no `control' file in control archive!)\n"),stdout) < 0) werr("stdout");
  141. } else {
  142. lines= 1;
  143. while ((c= getc(cc))!= EOF) {
  144. if (lines) if (putc(' ',stdout) == EOF) werr("stdout");
  145. if (putc(c,stdout) == EOF) werr("stdout");
  146. lines= c=='\n';
  147. }
  148. if (!lines) if (putc('\n',stdout) == EOF) werr("stdout");
  149. }
  150. }
  151. static void info_field(const char *debar, const char *directory,
  152. const char *const *fields, int showfieldname) {
  153. FILE *cc;
  154. char fieldname[MAXFIELDNAME+1];
  155. char *pf;
  156. const char *const *fp;
  157. int doing, c, lno, fnl;
  158. if (!(cc= fopen("control","r"))) ohshite(_("could not open the `control' component"));
  159. doing= 1; lno= 1;
  160. for (;;) {
  161. c= getc(cc); if (c==EOF) { doing=0; break; }
  162. if (c == '\n') { lno++; doing=1; continue; }
  163. if (!isspace(c)) {
  164. doing= 0;
  165. for (pf=fieldname, fnl=0;
  166. fnl <= MAXFIELDNAME && c!=EOF && !isspace(c) && c!=':';
  167. c= getc(cc)) { *pf++= c; fnl++; }
  168. *pf++= 0;
  169. doing= fnl >= MAXFIELDNAME || c=='\n' || c==EOF;
  170. for (fp=fields; !doing && *fp; fp++)
  171. if (!strcasecmp(*fp,fieldname)) doing=1;
  172. if (showfieldname) {
  173. if (doing)
  174. fputs(fieldname,stdout);
  175. } else {
  176. if (c==':') c= getc(cc);
  177. while (c != '\n' && isspace(c)) c= getc(cc);
  178. }
  179. }
  180. for(;;) {
  181. if (c == EOF) break;
  182. if (doing) putc(c,stdout);
  183. if (c == '\n') { lno++; break; }
  184. c= getc(cc);
  185. }
  186. if (c == EOF) break;
  187. }
  188. if (ferror(cc)) ohshite(_("failed during read of `control' component"));
  189. if (doing) putc('\n',stdout);
  190. if (ferror(stdout)) werr("stdout");
  191. }
  192. void do_info(const char *const *argv) {
  193. const char *debar, *directory;
  194. if (*argv && argv[1]) {
  195. info_prepare(&argv,&debar,&directory,1);
  196. info_spew(debar,directory, argv);
  197. } else {
  198. info_prepare(&argv,&debar,&directory,2);
  199. info_list(debar,directory);
  200. }
  201. }
  202. void do_field(const char *const *argv) {
  203. const char *debar, *directory;
  204. info_prepare(&argv,&debar,&directory,1);
  205. if (*argv) {
  206. info_field(debar, directory, argv, argv[1]!=0);
  207. } else {
  208. static const char *const controlonly[]= { "control", 0 };
  209. info_spew(debar,directory, controlonly);
  210. }
  211. }
  212. void do_contents(const char *const *argv) {
  213. const char *debar;
  214. if (!(debar= *argv++) || *argv) badusage(_("--contents takes exactly one argument"));
  215. extracthalf(debar, 0, "tv", 0);
  216. }