main.c 6.3 KB

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