main.c 6.3 KB

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