annotate ffmpeg/compat/getopt.c @ 13:844d341cf643 tip

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