~ubuntu-branches/ubuntu/utopic/coreutils/utopic-proposed

« back to all changes in this revision

Viewing changes to src/seq.c

  • Committer: Colin Watson
  • Date: 2013-10-30 15:48:33 UTC
  • mfrom: (8.3.5 sid)
  • Revision ID: cjwatson@canonical.com-20131030154833-xdt6e1yfffqom1c4
merge from Debian 8.21-1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* seq - print sequence of numbers to standard output.
2
 
   Copyright (C) 1994-2012 Free Software Foundation, Inc.
 
2
   Copyright (C) 1994-2013 Free Software Foundation, Inc.
3
3
 
4
4
   This program is free software: you can redistribute it and/or modify
5
5
   it under the terms of the GNU General Public License as published by
72
72
"), program_name, program_name, program_name);
73
73
      fputs (_("\
74
74
Print numbers from FIRST to LAST, in steps of INCREMENT.\n\
75
 
\n\
 
75
"), stdout);
 
76
 
 
77
      emit_mandatory_arg_note ();
 
78
 
 
79
      fputs (_("\
76
80
  -f, --format=FORMAT      use printf style floating-point FORMAT\n\
77
81
  -s, --separator=STRING   use STRING to separate numbers (default: \\n)\n\
78
82
  -w, --equal-width        equalize width by padding with leading zeroes\n\
166
170
        {
167
171
          long exponent = strtol (e + 1, NULL, 10);
168
172
          ret.precision += exponent < 0 ? -exponent : 0;
 
173
          /* Don't account for e.... in the width since this is not output.  */
 
174
          ret.width -= strlen (arg) - (e - arg);
 
175
          /* Adjust the width as per the exponent.  */
 
176
          if (exponent < 0)
 
177
            {
 
178
              if (decimal_point)
 
179
                {
 
180
                  if (e == decimal_point + 1) /* undo #. -> # above  */
 
181
                    ret.width++;
 
182
                }
 
183
              else
 
184
                ret.width++;
 
185
              exponent = -exponent;
 
186
            }
 
187
          ret.width += exponent;
169
188
        }
170
189
    }
171
190
 
317
336
            last_width--;  /* don't include space for '.' */
318
337
          if (last.precision == 0 && prec)
319
338
            last_width++;  /* include space for '.' */
 
339
          if (first.precision == 0 && prec)
 
340
            first_width++;  /* include space for '.' */
320
341
          size_t width = MAX (first_width, last_width);
321
342
          if (width <= INT_MAX)
322
343
            {
404
425
  bool ok = cmp (p, p_len, q, q_len) <= 0;
405
426
  if (ok)
406
427
    {
407
 
      /* Buffer at least this many output lines per fwrite call.
 
428
      /* Buffer at least this many numbers per fwrite call.
408
429
         This gives a speed-up of more than 2x over the unbuffered code
409
430
         when printing the first 10^9 integers.  */
410
431
      enum {N = 40};
411
432
      char *buf = xmalloc (N * (n + 1));
412
433
      char const *buf_end = buf + N * (n + 1);
413
434
 
414
 
      puts (p);
415
435
      char *z = buf;
 
436
 
 
437
      /* Write first number to buffer.  */
 
438
      z = mempcpy (z, p, p_len);
 
439
 
 
440
      /* Append separator then number.  */
416
441
      while (cmp (p, p_len, q, q_len) < 0)
417
442
        {
 
443
          *z++ = *separator;
418
444
          incr (&p, &p_len);
419
445
          z = mempcpy (z, p, p_len);
420
 
          *z++ = *separator;
421
 
          if (buf_end - n - 1 < z)
 
446
          /* If no place for another separator + number then
 
447
             output buffer so far, and reset to start of buffer.  */
 
448
          if (buf_end - (n + 1) < z)
422
449
            {
423
450
              fwrite (buf, z - buf, 1, stdout);
424
451
              z = buf;
425
452
            }
426
453
        }
427
454
 
428
 
      /* Write any remaining, buffered output.  */
429
 
      if (buf < z)
430
 
        fwrite (buf, z - buf, 1, stdout);
 
455
      /* Write any remaining buffered output, and the terminator.  */
 
456
      *z++ = *terminator;
 
457
      fwrite (buf, z - buf, 1, stdout);
431
458
 
432
459
      IF_LINT (free (buf));
433
460
    }
538
565
     then use the much more efficient integer-only code.  */
539
566
  if (all_digits_p (argv[optind])
540
567
      && (n_args == 1 || all_digits_p (argv[optind + 1]))
541
 
      && (n_args < 3 || STREQ ("1", argv[optind + 2]))
 
568
      && (n_args < 3 || (STREQ ("1", argv[optind + 1])
 
569
                         && all_digits_p (argv[optind + 2])))
542
570
      && !equal_width && !format_str && strlen (separator) == 1)
543
571
    {
544
572
      char const *s1 = n_args == 1 ? "1" : argv[optind];
545
 
      char const *s2 = n_args == 1 ? argv[optind] : argv[optind + 1];
 
573
      char const *s2 = argv[optind + (n_args - 1)];
546
574
      if (seq_fast (s1, s2))
547
575
        exit (EXIT_SUCCESS);
548
576