Homework 3
From CS113
Implement a program that reverses an input text file. The input text file will be presented on standard input (stdin). Your program should be able to handle arbitrarily long files. All memory allocations (malloc's) must be limited to 1024 bytes or less.
Extra credit: Write a macro that ensures you do not accidentally call malloc with more than 1024 bytes.
Examples:
[netid@submit cs113]$ echo -n foo | ./hw3 oof[netid@submit cs113]$
[netid@submit cs113]$ cat /var/tmp/war_and_peace.txt | ./hw3 *DNE*20/11/20.reV*SKOOBE NIAMOD CILBUP ROF !TNIRP LLAMS EHT DNE* ].noissimrep sserpxe tuohtiw tcudorp detaler rehto yna ro erawtfos ro erawdrah yeht eb slairetam rehto ro skooBe grebnetuG tcejorP fo selas yna ni desu eb ton yam dna kraMedarT a si grebnetuG tcejorP .traH .S leahciM yb 2002 ,1002 )C( thgirypoC .seef lla fo eerf detubirtsid nehw ylno detnirper eb yam reliart dna redaeh s'kooBe siht fo snoitroP[ ... (67398 lines ommitted) ... .noissimrep nettirw tuohtiw redaeh eht tide ro egnahc ton oD .ti evomer ton od esaelP .elif grebnetuG tcejorP siht gniweiv nehw nees gniht tsrif eht eb dluohs redaeh sihT .kooBe grebnetuG tcejorP rehto yna ro siht gnitubirtsider ro gnidaolnwod erofeb yrtnuoc ruoy rof swal thgirypoc eht kcehc ot erus eB .dlrow eht revo lla gnignahc era swal thgirypoC )yotsloT oeL yb seires ruo ni 9#( yotsloT oeL yb ,ecaeP dna raW fo kooBE grebnetuG tcejorP ehT[netid@submit cs113]$
For testing, you can chain two copies of your program and use the diff command to ensure that the final output is identical to the input.
[netid@submit cs113]$ cat /var/tmp/pride_and_prejudice.txt | ./hw3 | ./hw3 > output [netid@submit cs113]$ diff output /var/tmp/pride_and_prejudice.txt [netid@submit cs113]$
Hints: Use fgets to read from stdin and store the input using a linked list. The linked list struct must be smaller than 1024 bytes including the data and the pointer to other instances of the struct. Be sure to free all list elements once you are done!