summaryrefslogtreecommitdiff
path: root/lolcat.c
diff options
context:
space:
mode:
authorjaseg <git@jaseg.net>2018-07-22 18:38:00 +0200
committerjaseg <git@jaseg.net>2018-07-22 18:39:01 +0200
commit32c34ab174f1dd93985831926241ed3f20f4d0c7 (patch)
treeb495f1684d3f69db15de3822d4f1ec0ceb924614 /lolcat.c
parente298c102bd51ae67fbae09df5dfc00cba16a2231 (diff)
downloadlolcat-32c34ab174f1dd93985831926241ed3f20f4d0c7.tar.gz
lolcat-32c34ab174f1dd93985831926241ed3f20f4d0c7.tar.bz2
lolcat-32c34ab174f1dd93985831926241ed3f20f4d0c7.zip
Remove fmemopen altogether since it just doesn't work.
Closes #9.
Diffstat (limited to 'lolcat.c')
-rw-r--r--lolcat.c60
1 files changed, 38 insertions, 22 deletions
diff --git a/lolcat.c b/lolcat.c
index 687f259..ad1893b 100644
--- a/lolcat.c
+++ b/lolcat.c
@@ -13,6 +13,8 @@
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*/
+#define _XOPEN_SOURCE
+
#include <stdint.h>
#include <stdio.h>
#include <wchar.h>
@@ -25,13 +27,6 @@
#include <unistd.h>
#include <sys/time.h>
-#ifdef __APPLE__
-#include "fmemopen.h"
-#else // __APPLE__
-#define _GNU_SOURCE //for fmemopen
-#endif // __APPLE__
-
-
static char helpstr[] = "\n"
"Usage: lolcat [-h horizontal_speed] [-v vertical_speed] [--] [FILES...]\n"
"\n"
@@ -79,7 +74,8 @@ void version(){
}
int main(int argc, char **argv){
- int c, cc=-1, i, l=0;
+ int cc=-1, i, l=0;
+ wint_t c;
int colors=1;
double freq_h = 0.23, freq_v = 0.1;
@@ -128,20 +124,38 @@ int main(int argc, char **argv){
i=0;
for(char **filename=inputs; filename<inputs_end; filename++){
- FILE *f = stdin;
+ wint_t (*this_file_read_wchar)(FILE *); /* Used for --help because fmemopen is universally broken when used with fgetwc */
+ FILE *f;
int escape_state = 0;
- if(!strcmp(*filename, "--help"))
- f = fmemopen(helpstr, strlen(helpstr), "r");
- else if(strcmp(*filename, "-"))
+ wint_t helpstr_hack(FILE * _ignored) {
+ (void) _ignored;
+ static size_t idx = 0;
+ char c = helpstr[idx++];
+ if (c)
+ return c;
+ idx = 0;
+ return WEOF;
+ }
+
+ if(!strcmp(*filename, "--help")) {
+ this_file_read_wchar = &helpstr_hack;
+ f = 0;
+
+ } else if(!strcmp(*filename, "-")) {
+ this_file_read_wchar = &fgetwc;
+ f = stdin;
+
+ } else {
+ this_file_read_wchar = &fgetwc;
f = fopen(*filename, "r");
+ if(!f){
+ fprintf(stderr, "Cannot open input file \"%s\": %s\n", *filename, strerror(errno));
+ return 2;
+ }
+ }
- if(!f){
- fprintf(stderr, "Cannot open input file \"%s\": %s\n", *filename, strerror(errno));
- return 2;
- }
-
- while((c = fgetwc(f)) > 0){
+ while((c = this_file_read_wchar(f)) != WEOF) {
if(colors){
find_escape_sequences(c, &escape_state);
@@ -165,11 +179,13 @@ int main(int argc, char **argv){
printf("\n\033[0m");
cc = -1;
- fclose(f);
+ if (f) {
+ fclose(f);
- if(c != WEOF && c != 0){
- fprintf(stderr, "Error reading input file \"%s\": %s\n", *filename, strerror(errno));
- return 2;
+ if(ferror(f)){
+ fprintf(stderr, "Error reading input file \"%s\": %s\n", *filename, strerror(errno));
+ return 2;
+ }
}
}
}