BVB Source Codes

cmus Show prog.c Source code

Return Download cmus: download prog.c Source code - Download cmus Source code - Type:.c
  1. /*
  2.  * Copyright 2008-2013 Various Authors
  3.  * Copyright 2005 Timo Hirvonen
  4.  *
  5.  * This program is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU General Public License as
  7.  * published by the Free Software Foundation; either version 2 of the
  8.  * License, or (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17.  */
  18.  
  19. #include "prog.h"
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdarg.h>
  25. #include <errno.h>
  26.  
  27. char *program_name = NULL;
  28.  
  29. void warn(const char *format, ...)
  30. {
  31.         va_list ap;
  32.  
  33.         fprintf(stderr, "%s: ", program_name);
  34.         va_start(ap, format);
  35.         vfprintf(stderr, format, ap);
  36.         va_end(ap);
  37. }
  38.  
  39. void warn_errno(const char *format, ...)
  40. {
  41.         int e = errno;
  42.         va_list ap;
  43.  
  44.         fprintf(stderr, "%s: ", program_name);
  45.         va_start(ap, format);
  46.         vfprintf(stderr, format, ap);
  47.         va_end(ap);
  48.         fprintf(stderr, ": %s\n", strerror(e));
  49. }
  50.  
  51. void CMUS_NORETURN die(const char *format, ...)
  52. {
  53.         va_list ap;
  54.  
  55.         fprintf(stderr, "%s: ", program_name);
  56.         va_start(ap, format);
  57.         vfprintf(stderr, format, ap);
  58.         va_end(ap);
  59.         exit(1);
  60. }
  61.  
  62. void CMUS_NORETURN die_errno(const char *format, ...)
  63. {
  64.         int e = errno;
  65.         va_list ap;
  66.  
  67.         fprintf(stderr, "%s: ", program_name);
  68.         va_start(ap, format);
  69.         vfprintf(stderr, format, ap);
  70.         va_end(ap);
  71.         fprintf(stderr, ": %s\n", strerror(e));
  72.         exit(1);
  73. }
  74.  
  75. static int short_option(int ch, const struct option *options)
  76. {
  77.         int i;
  78.  
  79.         for (i = 0; ; i++) {
  80.                 if (!options[i].short_opt) {
  81.                         if (!options[i].long_opt)
  82.                                 die("unrecognized option `-%c'\n", ch);
  83.                         continue;
  84.                 }
  85.                 if (options[i].short_opt != ch)
  86.                         continue;
  87.                 return i;
  88.         }
  89. }
  90.  
  91. static int long_option(const char *opt, const struct option *options)
  92. {
  93.         int len, i, idx, num;
  94.  
  95.         len = strlen(opt);
  96.         idx = -1;
  97.         num = 0;
  98.         for (i = 0; options[i].short_opt || options[i].long_opt; i++) {
  99.                 if (options[i].long_opt && strncmp(opt, options[i].long_opt, len) == 0) {
  100.                         idx = i;
  101.                         num++;
  102.                         if (options[i].long_opt[len] == 0) {
  103.                                 /* exact */
  104.                                 num = 1;
  105.                                 break;
  106.                         }
  107.                 }
  108.         }
  109.         if (num > 1)
  110.                 die("option `--%s' is ambiguous\n", opt);
  111.         if (num == 0)
  112.                 die("unrecognized option `--%s'\n", opt);
  113.         return idx;
  114. }
  115.  
  116. int get_option(char **argvp[], const struct option *options, char **arg)
  117. {
  118.         char **argv = *argvp;
  119.         const char *opt = *argv;
  120.         int i;
  121.  
  122.         *arg = NULL;
  123.         if (opt == NULL || opt[0] != '-' || opt[1] == 0)
  124.                 return -1;
  125.  
  126.         if (opt[1] == '-') {
  127.                 if (opt[2] == 0) {
  128.                         /* '--' => no more options */
  129.                         *argvp = argv + 1;
  130.                         return -1;
  131.                 }
  132.                 i = long_option(opt + 2, options);
  133.         } else if (opt[2]) {
  134.                 return -1;
  135.         } else {
  136.                 i = short_option(opt[1], options);
  137.         }
  138.         argv++;
  139.         if (options[i].has_arg) {
  140.                 if (*argv == NULL)
  141.                         die("option `%s' requires an argument\n", opt);
  142.                 *arg = *argv++;
  143.         }
  144.         *argvp = argv;
  145.         return i;
  146. }
  147.  
downloadprog.c Source code - Download cmus Source code
Related Source Codes/Software:
Ink - An HTML5/CSS3 framework used at SAPO for fast and ... 2017-04-22
memory-stats.js - minimal monitor for JS Heap Size via performance.m... 2017-04-22
LayoutKit - LayoutKit is a fast view layout library for iOS, m... 2017-04-22
Cpp-Primer - C++ Primer 5 answer 2017-04-22
RBBAnimation - Block-based animations made easy, comes with easin... 2017-04-22
phpDocumentor2 - Documentation Generator for PHP ... 2017-04-22
flexboxfroggy - A game for learning CSS flexbox ... 2017-04-22
wicked - Use wicked to turn your controller into a wizard ... 2017-04-22
Begin-Latex-in-minutes - Brief Intro to LaTeX for beginners that helps you ... 2017-04-22
guard-livereload - Guard::LiveReload automatically reload your browse... 2017-04-22
swifter - Tiny http server engine written in Swift programmi... 2017-04-29
Chartbuilder - A front-end charting application that facilitates ... 2017-04-29
offline-first - 2017-04-29
plotly.py - An interactive, browser-based graphing library for... 2017-04-29
statsmodels - Statsmodels: statistical modeling and econometrics... 2017-04-29
android-maps-utils - Handy extensions to the Google Maps Android API. 2017-04-29
enyo - A JavaScript application framework emphasizing mod... 2017-04-29
darkforestGo - DarkForest, the Facebook Go engine. 2017-04-29
erpnext - ERP made Simple http://erpn... 2017-04-29
SwiftInFlux - An attempt to gather all that is in flux in Swift. 2017-04-29

 Back to top