MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
getopt.c
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libobjs/getopt.c,v 1.20 2017/01/12 14:44:46 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 /*
33  * The body of the `getopt' function comes from the OpenLDAP
34  *
35  * http://www.openldap.org
36  *
37  * implementation for those systems that do not have a native one.
38  * The intellectual property of the OpenLDAP implementation remains
39  * with the original Authors, whose contribution is kindly appreciated.
40  */
41 
42 /*
43  * Copyright 1998-2017 The OpenLDAP Foundation, All Rights Reserved.
44  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
45  */
46 /*
47  getopt.c
48 
49  modified public-domain AT&T getopt(3)
50  modified by Kurt Zeilenga for inclusion into OpenLDAP
51  modified by Pierangelo Masarati for inclusion with MBDyn
52 */
53 
54 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
55 
56 #ifndef HAVE_GETOPT
57 
58 #include <stdio.h>
59 
60 #include <string.h>
61 #include <unistd.h>
62 
63 #ifdef HAVE_IO_H
64 #include <io.h>
65 #endif
66 
67 #ifndef STDERR_FILENO
68 #define STDERR_FILENO 2
69 #endif
70 
71 int opterr = 1;
72 int optind = 1;
73 int optopt;
74 char * optarg;
75 
76 static void ERR (char * const argv[], const char * s, char c)
77 {
78  char errbuf[2];
79 
80 #ifdef DF_TRACE_DEBUG
81 printf("DF_TRACE_DEBUG: static void ERR () in getopt.c\n");
82 #endif
83  if (opterr)
84  {
85  errbuf[0] = c;
86  errbuf[1] = '\n';
87  (void) write(STDERR_FILENO,argv[0],strlen(argv[0]));
88  (void) write(STDERR_FILENO,s,strlen(s));
89  (void) write(STDERR_FILENO,errbuf,sizeof errbuf);
90  }
91 }
92 
93 int getopt (int argc, char * const argv [], const char * opts)
94 {
95  static int sp = 1, error = (int) '?';
96  static char sw = '-', eos = '\0', arg = ':';
97  register char c, * cp;
98 
99 #ifdef DF_TRACE_DEBUG
100 printf("DF_TRACE_DEBUG: int getopt () in getopt.c\n");
101 #endif
102  if (sp == 1)
103  {
104  if (optind >= argc || argv[optind][0] != sw
105  || argv[optind][1] == eos)
106  return EOF;
107  else if (strcmp(argv[optind],"--") == 0)
108  {
109  optind++;
110  return EOF;
111  }
112  }
113  c = argv[optind][sp];
114  optopt = (int) c;
115  if (c == arg || (cp = strchr(opts,c)) == NULL)
116  {
117  ERR(argv,": illegal option--",c);
118  if (argv[optind][++sp] == eos)
119  {
120  optind++;
121  sp = 1;
122  }
123  return error;
124  }
125  else if (*++cp == arg)
126  {
127  if (argv[optind][sp + 1] != eos)
128  optarg = &argv[optind++][sp + 1];
129  else if (++optind >= argc)
130  {
131  ERR(argv,": option requires an argument--",c);
132  sp = 1;
133  return error;
134  }
135  else
136  optarg = argv[optind++];
137  sp = 1;
138  }
139  else
140  {
141  if (argv[optind][++sp] == eos)
142  {
143  sp = 1;
144  optind++;
145  }
146  optarg = NULL;
147  }
148  return (int) c;
149 }
150 #endif /* HAVE_GETOPT */
151 
int optopt
Definition: getopt.c:73
int optind
Definition: getopt.c:72
int opterr
Definition: getopt.c:71
int error(const char *test, int value)
static void ERR(char *const argv[], const char *s, char c)
Definition: getopt.c:76
static std::stack< cleanup * > c
Definition: cleanup.cc:59
int getopt(int argc, char *const argv[], const char *opts)
Definition: getopt.c:93
char * optarg
Definition: getopt.c:74