rtmp: Add support for receiving incoming streams
[libav.git] / compat / getopt.c
CommitLineData
667fb97a
RB
1/*
2 * This file is part of Libav.
3 *
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19/*
20 * This file was copied from the following newsgroup posting:
21 *
22 * Newsgroups: mod.std.unix
23 * Subject: public domain AT&T getopt source
24 * Date: 3 Nov 85 19:34:15 GMT
25 *
26 * Here's something you've all been waiting for: the AT&T public domain
27 * source for getopt(3). It is the code which was given out at the 1985
28 * UNIFORUM conference in Dallas. I obtained it by electronic mail
29 * directly from AT&T. The people there assure me that it is indeed
30 * in the public domain.
31 */
32
33#define EOF (-1)
34
35static int opterr = 1;
36static int optind = 1;
37static int optopt;
38static char *optarg;
39
40#undef fprintf
41
42static int getopt(int argc, char *argv[], char *opts)
43{
44 static int sp = 1;
45 int c;
46 char *cp;
47
48 if (sp == 1)
49 if (optind >= argc ||
50 argv[optind][0] != '-' || argv[optind][1] == '\0')
51 return EOF;
52 else if (!strcmp(argv[optind], "--")) {
53 optind++;
54 return EOF;
55 }
56 optopt = c = argv[optind][sp];
57 if (c == ':' || (cp = strchr(opts, c)) == NULL) {
58 fprintf(stderr, ": illegal option -- %c\n", c);
59 if (argv[optind][++sp] == '\0') {
60 optind++;
61 sp = 1;
62 }
63 return '?';
64 }
65 if (*++cp == ':') {
66 if (argv[optind][sp+1] != '\0')
67 optarg = &argv[optind++][sp+1];
68 else if(++optind >= argc) {
69 fprintf(stderr, ": option requires an argument -- %c\n", c);
70 sp = 1;
71 return '?';
72 } else
73 optarg = argv[optind++];
74 sp = 1;
75 } else {
76 if (argv[optind][++sp] == '\0') {
77 sp = 1;
78 optind++;
79 }
80 optarg = NULL;
81 }
82
83 return c;
84}