main.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <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=NULL;
  64. const struct cmdinfo *cipaction=NULL;
  65. long maxpartsize= SPLITPARTDEFMAX;
  66. const char *depotdir= ADMINDIR "/" PARTSDIR, *outputfile= NULL;
  67. struct partqueue *queue= NULL;
  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) NONRETURNING;
  77. static void helponly(const struct cmdinfo *cip, const char *value) {
  78. usage(); exit(0);
  79. }
  80. static void versiononly(const struct cmdinfo *cip, const char *value) NONRETURNING;
  81. static void versiononly(const struct cmdinfo *cip, const char *value) {
  82. printversion(); exit(0);
  83. }
  84. static void setaction(const struct cmdinfo *cip, const char *value);
  85. static void setpartsize(const struct cmdinfo *cip, const char *value) {
  86. long newpartsize;
  87. char *endp;
  88. newpartsize= strtol(value,&endp,10);
  89. if (newpartsize <= 0 || newpartsize > (INT_MAX >> 10))
  90. badusage(_("part size is far too large or is not positive"));
  91. maxpartsize= newpartsize << 10;
  92. if (maxpartsize <= HEADERALLOWANCE)
  93. badusage(_("part size must be at least %dk (to allow for header)"),
  94. (HEADERALLOWANCE >> 10) + 1);
  95. }
  96. static dofunction *const dofunctions[]= {
  97. do_split,
  98. do_join,
  99. do_info,
  100. do_auto,
  101. do_queue,
  102. do_discard,
  103. };
  104. /* NB: the entries using setaction must appear first and be in the
  105. * same order as dofunctions:
  106. */
  107. static const struct cmdinfo cmdinfos[]= {
  108. { "split", 's', 0, NULL, NULL, setaction },
  109. { "join", 'j', 0, NULL, NULL, setaction },
  110. { "info", 'I', 0, NULL, NULL, setaction },
  111. { "auto", 'a', 0, NULL, NULL, setaction },
  112. { "listq", 'l', 0, NULL, NULL, setaction },
  113. { "discard", 'd', 0, NULL, NULL, setaction },
  114. { "help", 'h', 0, NULL, NULL, helponly },
  115. { "version", 0, 0, NULL, NULL, versiononly },
  116. { "licence", 0, 0, NULL, NULL, showcopyright }, /* UK spelling */
  117. { "license", 0, 0, NULL, NULL, showcopyright }, /* US spelling */
  118. { "depotdir", 0, 1, NULL, &depotdir, NULL },
  119. { "partsize", 'S', 1, NULL, NULL, setpartsize },
  120. { "output", 'o', 1, NULL, &outputfile, NULL },
  121. { "npquiet", 'Q', 0, &npquiet, NULL, NULL, 1 },
  122. { "msdos", 0, 0, &msdos, NULL, NULL, 1 },
  123. { NULL, 0 }
  124. };
  125. static void setaction(const struct cmdinfo *cip, const char *value) {
  126. if (cipaction)
  127. badusage(_("conflicting actions --%s and --%s"),cip->olong,cipaction->olong);
  128. cipaction= cip;
  129. assert((int)(cip-cmdinfos) < (int)(sizeof(dofunctions)*sizeof(dofunction*)));
  130. action= dofunctions[cip-cmdinfos];
  131. }
  132. int main(int argc, const char *const *argv) NONRETURNING;
  133. int main(int argc, const char *const *argv) {
  134. jmp_buf ejbuf;
  135. int l;
  136. char *p;
  137. standard_startup(&ejbuf, argc, &argv, NULL, 0, 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,NULL,_IONBF,0);
  147. action(argv);
  148. if (ferror(stderr)) werr("stderr");
  149. standard_shutdown(0);
  150. exit(0);
  151. }