fix missing output in unbuffered mode when redirected to a file
[rofl0r-jobflow.git] / tests / memwaste.c
blob2d62b299d6bdcb20c5fe3ea5ef864a73b471028b
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 int syntax() {
6 puts("prog size_of_mem_to_alloc");
7 return 1;
10 long parse_human_number(char* num) {
11 long ret = 0;
12 char buf[64];
13 if(!num) return 0;
14 size_t l = strlen(num);
16 if(l && l < sizeof(buf)) {
17 if(num[l -1] == 'G')
18 ret = 1024 * 1024 * 1024;
19 else if(num[l -1] == 'M')
20 ret = 1024 * 1024;
21 else if(num[l -1] == 'K')
22 ret = 1024;
23 if(ret) {
24 memcpy(buf, num, l);
25 buf[l] = 0;
26 return atol(buf) * ret;
28 return atol(num);
30 return ret;
33 int main(int argc, char** argv) {
34 void* buf;
35 long mem;
36 if (argc != 2) return syntax();
37 mem = parse_human_number(argv[1]);
38 if((buf = malloc(mem))) {
39 fprintf(stdout, "malloc %lu bytes succeeded.\n", mem);
40 return 0;
41 } else {
42 fprintf(stdout, "malloc %lu bytes failed.\n", mem);
43 return 1;