summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/stripeof.c
diff options
context:
space:
mode:
Diffstat (limited to 'reference/C/CONTRIB/SNIP/stripeof.c')
-rwxr-xr-xreference/C/CONTRIB/SNIP/stripeof.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/reference/C/CONTRIB/SNIP/stripeof.c b/reference/C/CONTRIB/SNIP/stripeof.c
new file mode 100755
index 0000000..f2a17ac
--- /dev/null
+++ b/reference/C/CONTRIB/SNIP/stripeof.c
@@ -0,0 +1,61 @@
+/*
+** STRIPEOF.C
+**
+** public domain demo by Bob Stout
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <io.h>
+#include <fcntl.h>
+
+#define BUFSIZE 16384
+
+int main(int argc, char *argv[])
+{
+ char *buf;
+
+ if (2 > argc)
+ {
+ puts("Usage: STRIPEOF filename1 [...filenameN]");
+ return EXIT_FAILURE;
+ }
+ if (NULL == (buf = malloc(BUFSIZE)))
+ {
+ puts("STRIPEOF internal failure");
+ return EXIT_FAILURE;
+ }
+ while (--argc)
+ {
+ int fd;
+ size_t bytes;
+ int found = 0;
+ long zpos = 0L;
+
+ if (-1 == (fd = open(*(++argv), O_RDWR | O_BINARY)))
+ {
+ printf("Couldn't open %s\n", *argv);
+ return EXIT_FAILURE;
+ }
+ while (0 < (bytes = read(fd, buf, BUFSIZE)))
+ {
+ int i;
+
+ for (i = 0; i < (int)bytes; ++i)
+ {
+ if (('Z' - 64) == buf[i])
+ {
+ found = 1;
+ zpos += i;
+ break;
+ }
+ }
+ if (found)
+ break;
+ zpos += bytes;
+ }
+ if (found)
+ chsize(fd, zpos);
+ }
+ return EXIT_SUCCESS;
+}