main.c 6.5 KB

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