info.c 8.3 KB

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