summaryrefslogtreecommitdiff
path: root/insertbom.c
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2019-09-17 08:40:47 +0000
committerIan C <ianc@noddybox.co.uk>2019-09-17 08:40:47 +0000
commit0cd93d3952118602e0dfc9ce96c3f26022b26655 (patch)
tree8498370b84e740438fd4d613fe8660d80510b5c2 /insertbom.c
parent6724198091eac18a38688cc8df08f625fde7892b (diff)
Added insertbom and called from ansi2unicode.sh
Diffstat (limited to 'insertbom.c')
-rw-r--r--insertbom.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/insertbom.c b/insertbom.c
new file mode 100644
index 0000000..ff5833e
--- /dev/null
+++ b/insertbom.c
@@ -0,0 +1,81 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <string.h>
+#include <sys/stat.h>
+
+static off_t Size(const char *name)
+{
+ struct stat st = {0};
+
+ stat(name, &st);
+
+ return st.st_size;
+}
+
+static void Handle(const char *name)
+{
+ FILE *fp = NULL;
+ off_t len = Size(name);
+ unsigned char *buff = NULL;
+ int write = 0;
+
+ if (len < 3)
+ {
+ printf("%s: too short\n", name);
+ return;
+ }
+
+ buff = malloc(len + 2);
+ fp = fopen(name, "rb");
+ fread(buff, 1, len, fp);
+ fclose(fp);
+
+ if ((buff[0] == 0xffu && buff[1] == 0xfeu) ||
+ (buff[0] == 0xfeu && buff[1] == 0xffu))
+ {
+ printf("%s: already has BOM\n", name);
+ free(buff);
+ return;
+ }
+
+ if (buff[0] == 0 && isprint(buff[1]))
+ {
+ printf("%s: Guessing BE UTF-16\n", name);
+ write = 1;
+ memmove(buff, buff + 2, len);
+ buff[0] = 0xfe;
+ buff[1] = 0xff;
+ }
+ else if (buff[1] == 0 && isprint(buff[0]))
+ {
+ printf("%s: Guessing LE UTF-16\n", name);
+ write = 1;
+ memmove(buff, buff + 2, len);
+ buff[0] = 0xff;
+ buff[1] = 0xfe;
+ }
+ else
+ {
+ printf("%s: Leaving alone\n", name);
+ }
+
+ if (write)
+ {
+ fp = fopen(name, "wb");
+ fwrite(buff, 1, len + 2, fp);
+ fclose(fp);
+ }
+
+ free(buff);
+}
+
+int main(int argc, char *argv[])
+{
+ int f;
+
+ for(f = 1; f < argc; f++)
+ {
+ Handle(argv[f]);
+ }
+}