#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
if (argc != 3)
return 1;
int in = open(argv[1], O_RDONLY);
if (in == -1)
return 1;
if (access(argv[2], F_OK) == 0) {
char answer;
printf("Replace? (y/n): ");
fflush(stdout);
read(0, &answer, 1);
if (answer != 'y') {
close(in);
return 0;
}
}
int out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (out == -1) {
close(in);
return 1;
}
int16_t x;
int i = 1;
char oct[12], hex[12], line[64];
while (read(in, &x, sizeof(x)) == sizeof(x)) {
unsigned int a;
if (x < 0)
a = -(int)x;
else
a = x;
if (x < 0) {
snprintf(oct, sizeof(oct), "-0%o", a);
snprintf(hex, sizeof(hex), "-0x%x", a);
} else {
snprintf(oct, sizeof(oct), "0%o", a);
snprintf(hex, sizeof(hex), "0x%x", a);
}
snprintf(line, sizeof(line),
"%10d|%10d|%10s|%10s\n",
i, x, oct, hex);
write(out, line, strlen(line));
i++;
}
close(in);
close(out);
return 0;
}