File: fib.c

package info (click to toggle)
lbzip2 2.5-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,624 kB
  • sloc: ansic: 27,584; sh: 4,306; perl: 154; makefile: 70
file content (42 lines) | stat: -rw-r--r-- 672 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* Copyright (C) 2011 Mikolaj Izdebski */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char **argv)
{
  char *p, *q, *r, *s;
  size_t req_sz;

  errno = 0;
  req_sz = 900000;
  if (argc > 1)
    req_sz = strtol(argv[1], 0, 10);
  if (errno != 0) {
    perror("strtol");
    return EXIT_FAILURE;
  }

  p = q = r = s = malloc(req_sz+1);
  if (p == 0) {
    perror("malloc");
    exit(EXIT_FAILURE);
  }

  r = q + req_sz;
  *q++ = 'a';
  while (q < r) {
    if (*p++ == 'a') *q++ = 'b';
    *q++ = 'a';
  }

  if (fwrite(s, req_sz, 1, stdout) != 1) {
    perror("fwrite");
    return EXIT_FAILURE;
  }

  free(s);
  return EXIT_SUCCESS;
}