main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * dpkg-split - splitting and joining of multipart *.deb archives
  3. * main.c - main program
  4. *
  5. * Copyright (C) 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 <stdlib.h>
  23. #include <limits.h>
  24. #include <assert.h>
  25. #include "config.h"
  26. #include "dpkg.h"
  27. #include "dpkg-db.h"
  28. #include "version.h"
  29. #include "myopt.h"
  30. #include "dpkg-split.h"
  31. static void printversion(void) {
  32. if (!fputs
  33. ("Debian Linux `" SPLITTER "' package split/join tool; "
  34. "version " DPKG_VERSION_ARCH ".\n"
  35. "Copyright (C) 1994-1996 Ian Jackson. This is free software; see the\n"
  36. "GNU General Public Licence version 2 or later for copying conditions.\n"
  37. "There is NO warranty. See dpkg-split --licence for details.\n",
  38. stdout)) werr("stdout");
  39. }
  40. static void usage(void) {
  41. if (!fputs("\
  42. Usage: " SPLITTER " -s|--split <file> [<prefix>] Split an archive.\n\
  43. " SPLITTER " -j|--join <part> <part> ... Join parts together.\n\
  44. " SPLITTER " -I|--info <part> ... Display info about a part.\n\
  45. " SPLITTER " -h|--help|--version|--licence Show help/version/licence.\n\
  46. \n\
  47. " SPLITTER " -a|--auto -o <complete> <part> Auto-accumulate parts.\n\
  48. " SPLITTER " -l|--listq List unmatched pieces.\n\
  49. " SPLITTER " -d|--discard [<filename> ...] Discard unmatched pieces.\n\
  50. \n\
  51. Options: --depotdir <directory> (default is /var/lib/dpkg/parts)\n\
  52. -S|--partsize <size> (in Kb, for -s, default is 450)\n\
  53. -o|--output <file> (for -j, default is <package>-<version>.deb)\n\
  54. -Q|--npquiet (be quiet when -a is not a part)\n\
  55. --msdos (generate 8.3 filenames)\n\
  56. \n\
  57. Exit status: 0 = OK; 1 = -a is not a part; 2 = trouble!\n",
  58. stdout)) werr("stdout");
  59. }
  60. const char thisname[]= SPLITTER;
  61. const char printforhelp[]= "Type " SPLITTER " --help for help.";
  62. dofunction *action=0;
  63. const struct cmdinfo *cipaction=0;
  64. long maxpartsize= SPLITPARTDEFMAX;
  65. const char *depotdir= ADMINDIR "/" PARTSDIR, *outputfile= 0;
  66. struct partqueue *queue= 0;
  67. int npquiet= 0, msdos= 0;
  68. void rerr(const char *fn) {
  69. ohshite("error reading %s",fn);
  70. }
  71. void rerreof(FILE *f, const char *fn) {
  72. if (ferror(f)) ohshite("error reading %.250s",fn);
  73. ohshit("unexpected end of file in %.250s",fn);
  74. }
  75. static void helponly(const struct cmdinfo *cip, const char *value) {
  76. usage(); exit(0);
  77. }
  78. static void versiononly(const struct cmdinfo *cip, const char *value) {
  79. printversion(); exit(0);
  80. }
  81. static void setaction(const struct cmdinfo *cip, const char *value);
  82. static void setpartsize(const struct cmdinfo *cip, const char *value) {
  83. long newpartsize;
  84. char *endp;
  85. newpartsize= strtol(value,&endp,10);
  86. if (newpartsize <= 0 || newpartsize > (INT_MAX >> 10))
  87. badusage("part size is far too large or is not positive");
  88. maxpartsize= newpartsize << 10;
  89. if (maxpartsize <= HEADERALLOWANCE)
  90. badusage("part size must be at least %dk (to allow for header)",
  91. (HEADERALLOWANCE >> 10) + 1);
  92. }
  93. static dofunction *const dofunctions[]= {
  94. do_split,
  95. do_join,
  96. do_info,
  97. do_auto,
  98. do_queue,
  99. do_discard,
  100. };
  101. /* NB: the entries using setaction must appear first and be in the
  102. * same order as dofunctions:
  103. */
  104. static const struct cmdinfo cmdinfos[]= {
  105. { "split", 's', 0, 0, 0, setaction },
  106. { "join", 'j', 0, 0, 0, setaction },
  107. { "info", 'I', 0, 0, 0, setaction },
  108. { "auto", 'a', 0, 0, 0, setaction },
  109. { "listq", 'l', 0, 0, 0, setaction },
  110. { "discard", 'd', 0, 0, 0, setaction },
  111. { "help", 'h', 0, 0, 0, helponly },
  112. { "version", 0, 0, 0, 0, versiononly },
  113. { "licence", 0, 0, 0, 0, showcopyright }, /* UK spelling */
  114. { "license", 0, 0, 0, 0, showcopyright }, /* US spelling */
  115. { "depotdir", 0, 1, 0, &depotdir, 0 },
  116. { "partsize", 'S', 1, 0, 0, setpartsize },
  117. { "output", 'o', 1, 0, &outputfile, 0 },
  118. { "npquiet", 'Q', 0, &npquiet, 0, 0, 1 },
  119. { "msdos", 0, 0, &msdos, 0, 0, 1 },
  120. { 0, 0 }
  121. };
  122. static void setaction(const struct cmdinfo *cip, const char *value) {
  123. if (cipaction)
  124. badusage("conflicting actions --%s and --%s",cip->olong,cipaction->olong);
  125. cipaction= cip;
  126. assert(cip-cmdinfos < sizeof(dofunctions)*sizeof(dofunction*));
  127. action= dofunctions[cip-cmdinfos];
  128. }
  129. int main(int argc, const char *const *argv) {
  130. jmp_buf ejbuf;
  131. int l;
  132. char *p;
  133. if (setjmp(ejbuf)) { /* expect warning about possible clobbering of argv */
  134. error_unwind(ehflag_bombout); exit(2);
  135. }
  136. push_error_handler(&ejbuf,print_error_fatal,0);
  137. myopt(&argv,cmdinfos);
  138. if (!cipaction) badusage("need an action option");
  139. l= strlen(depotdir);
  140. if (l && depotdir[l-1] != '/') {
  141. p= nfmalloc(l+2);
  142. strcpy(p,depotdir);
  143. strcpy(p+l,"/");
  144. depotdir= p;
  145. }
  146. setvbuf(stdout,0,_IONBF,0);
  147. action(argv);
  148. if (ferror(stderr)) werr("stderr");
  149. set_error_display(0,0);
  150. error_unwind(ehflag_normaltidy);
  151. exit(0);
  152. }