NoPaste

seq + ruler patch for coreutils seq.c v1.2

von linuxCowboy
SNIPPET_DESC:
diff -u seq.c.org seq.c
SNIPPET_CREATION_TIME:
03.09.2015 21:10:31
SNIPPET_PRUNE_TIME:
Unendlich

SNIPPET_TEXT:
  1. --- seq.c.org   2015-08-22 11:03:42.000000000 +0200
  2. +++ seq.c       2015-09-03 14:42:44.000000000 +0200
  3. @@ -20,6 +20,7 @@
  4.  #include <getopt.h>
  5.  #include <stdio.h>
  6.  #include <sys/types.h>
  7. +#include <sys/ioctl.h>
  8.  
  9.  #include "system.h"
  10.  #include "c-strtod.h"
  11. @@ -48,11 +49,19 @@
  12.     Usually "\n" or "\0".  */
  13.  static char const terminator[] = "\n";
  14.  
  15. +/* print a ruler */
  16. +static bool ruler;
  17. +
  18. +/* true for alphabetic: A-Z a-z */
  19. +static bool alpha;
  20. +
  21.  static struct option const long_options[] =
  22.  {
  23.    { "equal-width", no_argument, NULL, 'w'},
  24.    { "format", required_argument, NULL, 'f'},
  25.    { "separator", required_argument, NULL, 's'},
  26. +  { "ruler", no_argument, NULL, 'r'},
  27. +  { "alpha", no_argument, NULL, 'a'},
  28.    {GETOPT_HELP_OPTION_DECL},
  29.    {GETOPT_VERSION_OPTION_DECL},
  30.    { NULL, 0, NULL, 0}
  31. @@ -80,6 +89,8 @@
  32.    -f, --format=FORMAT      use printf style floating-point FORMAT\n\
  33.    -s, --separator=STRING   use STRING to separate numbers (default: \\n)\n\
  34.    -w, --equal-width        equalize width by padding with leading zeroes\n\
  35. +  -a, --alpha              alphabetic: A-Z a-z\n\
  36. +  -r, --ruler              print a ruler\n\
  37.  "), stdout);
  38.        fputs (HELP_OPTION_DESCRIPTION, stdout);
  39.        fputs (VERSION_OPTION_DESCRIPTION, stdout);
  40. @@ -555,7 +566,7 @@
  41.            break;
  42.          }
  43.  
  44. -      optc = getopt_long (argc, argv, "+f:s:w", long_options, NULL);
  45. +      optc = getopt_long (argc, argv, "+af:rs:w", long_options, NULL);
  46.        if (optc == -1)
  47.          break;
  48.  
  49. @@ -573,6 +584,14 @@
  50.            equal_width = true;
  51.            break;
  52.  
  53. +        case 'a':
  54. +          alpha = true;
  55. +          break;
  56. +
  57. +        case 'r':
  58. +          ruler = true;
  59. +          break;
  60. +
  61.          case_GETOPT_HELP_CHAR;
  62.  
  63.          case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
  64. @@ -582,6 +601,19 @@
  65.          }
  66.      }
  67.  
  68. +  if (ruler) {
  69. +    char *p = "012345678_";
  70. +    struct winsize w;
  71. +
  72. +    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
  73. +
  74. +    for (int i = 1; i <= (w.ws_col > 0 && w.ws_col < 256 ? w.ws_col : 80); ++i)
  75. +      printf("%c", i % 10 ? p[i % 10] : i / 10 % 10 + 48);
  76. +
  77. +      fputs (terminator, stdout);
  78. +      return EXIT_SUCCESS;
  79. +  }
  80. +
  81.    unsigned int n_args = argc - optind;
  82.    if (n_args < 1)
  83.      {
  84. @@ -595,6 +627,34 @@
  85.        usage (EXIT_FAILURE);
  86.      }
  87.  
  88. +  if (alpha) {
  89. +    unsigned char last = *argv[argc - 1];
  90. +
  91. +    if (! ((last >= 'A' && last <= 'Z') || (last >= 'a' && last <= 'z')))
  92. +      error(EXIT_FAILURE, 0, _("ascii letter only!"));
  93. +
  94. +    int incr = n_args == 3 ? atoi(argv[argc - 2]) : 1;
  95. +
  96. +    if (! (incr && incr >= -25 && incr <= +25))
  97. +      error(EXIT_FAILURE, 0, _("bad increment!"));
  98. +
  99. +    unsigned char first = n_args == 3 ? *argv[argc - 3] : n_args == 2 ? *argv[argc - 2] : last <= 'Z' ? 'A' : 'a';
  100. +
  101. +    if (! ((first >= 'A' && first <= 'Z') || (first >= 'a' && first <= 'z')))
  102. +      error(EXIT_FAILURE, 0, _("ascii letter only!"));
  103. +
  104. +    if (! ((first <= 'Z' && last <= 'Z') || (first >= 'a' && last >= 'a')))
  105. +      error(EXIT_FAILURE, 0, _("bad range!"));
  106. +
  107. +    for (int i = first; incr > 0 && i <= last || incr < 0 && i >= last; i += incr) {
  108. +      i == first || fputs (separator, stdout);
  109. +      fputc(i, stdout);
  110. +    }
  111. +    fputs (terminator, stdout);
  112. +
  113. +    return EXIT_SUCCESS;
  114. +  }
  115. +
  116.    if (format_str)
  117.      format_str = long_double_format (format_str, &layout);

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN