4
0

getoldopt.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Replacement for getopt() that can be used by tar.
  2. Copyright (C) 1988 Free Software Foundation
  3. This file is part of GNU Tar.
  4. GNU Tar is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Tar is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Tar; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /*
  16. * Plug-compatible replacement for getopt() for parsing tar-like
  17. * arguments. If the first argument begins with "-", it uses getopt;
  18. * otherwise, it uses the old rules used by tar, dump, and ps.
  19. *
  20. * Written 25 August 1985 by John Gilmore (ihnp4!hoptoad!gnu)
  21. */
  22. #include <stdio.h>
  23. #include "getopt.h"
  24. #include "tar.h" /* For msg() declaration if STDC_MSG. */
  25. #include <sys/types.h>
  26. #include "port.h"
  27. int
  28. getoldopt (argc, argv, optstring, long_options, opt_index)
  29. int argc;
  30. char **argv;
  31. char *optstring;
  32. struct option *long_options;
  33. int *opt_index;
  34. {
  35. extern char *optarg; /* Points to next arg */
  36. extern int optind; /* Global argv index */
  37. static char *key; /* Points to next keyletter */
  38. static char use_getopt; /* !=0 if argv[1][0] was '-' */
  39. char c;
  40. char *place;
  41. optarg = NULL;
  42. if (key == NULL)
  43. { /* First time */
  44. if (argc < 2)
  45. return EOF;
  46. key = argv[1];
  47. if ((*key == '-') || (*key == '+'))
  48. use_getopt++;
  49. else
  50. optind = 2;
  51. }
  52. if (use_getopt)
  53. return getopt_long (argc, argv, optstring,
  54. long_options, opt_index);
  55. c = *key++;
  56. if (c == '\0')
  57. {
  58. key--;
  59. return EOF;
  60. }
  61. place = index (optstring, c);
  62. if (place == NULL || c == ':')
  63. {
  64. msg ("unknown option %c", c);
  65. return ('?');
  66. }
  67. place++;
  68. if (*place == ':')
  69. {
  70. if (optind < argc)
  71. {
  72. optarg = argv[optind];
  73. optind++;
  74. }
  75. else
  76. {
  77. msg ("%c argument missing", c);
  78. return ('?');
  79. }
  80. }
  81. return (c);
  82. }