main.c 6.5 KB

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