Last active
October 24, 2017 07:16
-
-
Save jamesmcdonald/819d8b0e90fa1dc66d51476bbe76946c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
/* 101 bytes - the 102nd is the \n */ | |
#define BYTES "This line of text has been engineered by " \ | |
"only the very finest, wisest squirrels to contain 102 bytes." | |
void usage(char *progname) | |
{ | |
fprintf(stderr, "usage: %s <number>\n", progname); | |
fprintf(stderr, "Generate a number of kB of text and newlines.\n"); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
if (argc != 2) { | |
usage(argv[0]); | |
exit(EXIT_FAILURE); | |
} | |
errno = 0; | |
long kibs = strtol(argv[1], NULL, 10); | |
if (errno != 0) { | |
fprintf(stderr, "%s: %s is either out of range or exploded strtol\n", argv[0], argv[1]); | |
usage(argv[0]); | |
exit(EXIT_FAILURE); | |
} | |
for(long int i=0; i<kibs; i++) { | |
for (int j=0; j<10; j++) { | |
printf("%s\n", BYTES); | |
} | |
printf("KiB\n"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"log" | |
"os" | |
"strconv" | |
) | |
var bytes = []byte("This line of text has been engineered by " + | |
"only the very finest, wisest squirrels to contain 102 bytes.\n") | |
var kibstr = []byte("KiB\n") | |
func kib(w io.Writer) { | |
for i := 0; i < 10; i++ { | |
w.Write(bytes) | |
} | |
w.Write(kibstr) | |
} | |
func main() { | |
if len(os.Args) != 2 { | |
fmt.Fprintf(os.Stderr, "usage: %s <number>\n", os.Args[0]) | |
os.Exit(1) | |
} | |
kibs, err := strconv.Atoi(os.Args[1]) | |
if err != nil { | |
log.Fatalf("%s: %s isn't a number of KiB I can cope with", os.Args[0], os.Args[1]) | |
} | |
writer := bufio.NewWriter(os.Stdout) | |
defer writer.Flush() | |
for i := 0; i < kibs; i++ { | |
kib(writer) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment