main.c 6.2 KB

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