mirror of https://git.qorg11.net/k9core.git
Don't take file as stdin if you passed a flag after optind
In good c libraries, the argv[] array is not resorted with the flags at the beginning, then normal argvs, for example in glibc if you run ./wc file -l -c, argv will be ./wc -l -c file
This commit is contained in:
parent
84715bc3d4
commit
6f6e702fbe
20
src/wc.c
20
src/wc.c
|
@ -5,14 +5,6 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
/* TODO: fucking make this thing read binary data
|
|
||||||
* Possible solutions:
|
|
||||||
* fread()
|
|
||||||
* fgets()
|
|
||||||
* rewrite this entire thing using read() (not a good idea
|
|
||||||
* because it's slow as shit)
|
|
||||||
*/
|
|
||||||
|
|
||||||
int show_lines, show_words, show_bytes;
|
int show_lines, show_words, show_bytes;
|
||||||
struct wc_values data;
|
struct wc_values data;
|
||||||
|
|
||||||
|
@ -33,14 +25,13 @@ wc(const char *filename, struct wc_values *data)
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
char c;
|
size_t c;
|
||||||
char a;
|
|
||||||
char buf;
|
char buf;
|
||||||
int newlines, spaces, bytes = 0;
|
int newlines, spaces, bytes = 0;
|
||||||
newlines = spaces = bytes = 0;
|
newlines = spaces = bytes = 0;
|
||||||
while((c = fread(&buf,1,1, file)) > 0) {
|
while((c = fread(&buf,1,1, file)) > 0) {
|
||||||
if(!isascii(buf))
|
if(!isascii(buf))
|
||||||
a = toascii(buf);
|
buf = toascii(buf);
|
||||||
bytes++;
|
bytes++;
|
||||||
if(buf == '\n')
|
if(buf == '\n')
|
||||||
newlines++;
|
newlines++;
|
||||||
|
@ -54,6 +45,7 @@ wc(const char *filename, struct wc_values *data)
|
||||||
fclose(file);
|
fclose(file);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
print_values(const char *filename, struct wc_values data)
|
print_values(const char *filename, struct wc_values data)
|
||||||
{
|
{
|
||||||
|
@ -72,6 +64,7 @@ print_values(const char *filename, struct wc_values data)
|
||||||
}
|
}
|
||||||
printf(" %s\n",filename);
|
printf(" %s\n",filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
@ -100,8 +93,9 @@ main(int argc, char *argv[])
|
||||||
wc("/dev/stdin",&data); /* lol */
|
wc("/dev/stdin",&data); /* lol */
|
||||||
print_values("stdin",data);
|
print_values("stdin",data);
|
||||||
}
|
}
|
||||||
else for(int i = optind; i<argc; i++) {
|
else
|
||||||
if(argv[i][0] == '-')
|
for(int i = optind; i<argc; i++) {
|
||||||
|
if(argv[i][0] == '-' && argv[i][1] == '\0')
|
||||||
return_value = wc("/dev/stdin",&data);
|
return_value = wc("/dev/stdin",&data);
|
||||||
else
|
else
|
||||||
return_value = wc(argv[i],&data);
|
return_value = wc(argv[i],&data);
|
||||||
|
|
Loading…
Reference in New Issue