PostgreSQL Source Code: src/port/getopt.c Source File (original) (raw)

Go to the documentation of this file.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32#include "c.h"

33

35

36#if defined(LIBC_SCCS) && !defined(lint)

37static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";

38#endif

39

40

41

42

43

44

45

46

47

48#ifndef HAVE_INT_OPTERR

49

50int opterr = 1,

51 optind = 1,

52 optopt;

53char *optarg;

54

55#endif

56

57#define BADCH (int)'?'

58#define BADARG (int)':'

59#define EMSG ""

60

61

62

63

64

65

66

67

68

69

70

71int

72getopt(int nargc, char *const *nargv, const char *ostr)

73{

74 static char *place = EMSG;

75 char *oli;

76

77 if (!*place)

78 {

79 if (optind >= nargc || *(place = nargv[optind]) != '-')

80 {

82 return -1;

83 }

84 if (place[1] && *++place == '-' && place[1] == '\0')

85 {

88 return -1;

89 }

90 }

91 if ((optopt = (int) *place++) == (int) ':' ||

92 !(oli = strchr(ostr, optopt)))

93 {

94

95

96

97 if (optopt == (int) '-')

98 {

100 return -1;

101 }

102 if (!*place)

104 if (opterr && *ostr != ':')

106 "illegal option -- %c\n", optopt);

108 }

109 if (*++oli != ':')

110 {

112 if (!*place)

114 }

115 else

116 {

117 if (*place)

119 else if (nargc <= ++optind)

120 {

121 place = EMSG;

122 if (*ostr == ':')

126 "option requires an argument -- %c\n",

129 }

130 else

131

133 place = EMSG;

135 }

136 return optopt;

137}

#define fprintf(file, fmt, msg)

int getopt(int nargc, char *const *nargv, const char *ostr)