add netbsd nl(1)
[rofl0r-hardcore-utils.git] / bin2sh.c
blob4a9aa367b51e481d41022c83bb619b96e6019b92
1 #include <stdio.h>
2 #include <stdlib.h>
4 static int syntax() {
5 printf("bin2sh - converts a file into a shellscript which recreates "
6 "the file when run\nthe output is written to stdout\n"
7 "bin2sh [-c N] filename\n"
8 "where N is the number of chars per line\n");
9 return 1;
12 static int is_c_arg(char* s) {
13 return s[0]=='-' && s[1]=='c' &&!s[2];
15 int main(int argc, char** argv) {
16 if(argc == 1 || argc > 4 || argc == 3 ||
17 (is_c_arg(argv[1]) && argc != 4)) return syntax();
18 int f_arg = 1;
19 unsigned cpl = 20;
20 if(is_c_arg(argv[1])) {
21 f_arg = 3;
22 cpl = atoi(argv[2]);
23 if(!cpl) return syntax();
25 FILE *f = fopen(argv[f_arg], "r");
26 if(!f) { perror("fopen"); return 1; }
27 unsigned long long cnt = 0;
28 unsigned char buf[1];
29 static const char redir[][3] = {">", ">>"};
30 int printed = 0;
31 while(fread(buf, 1, 1, f)) {
32 if(!(cnt % cpl)) {
33 if(cnt) {
34 printf("\" %s f", redir[printed]);
35 printed = 1;
36 } else printf("#!/bin/sh");
37 printf("\n\necho -ne \"");
39 printf("\\x%02X", buf[0]);
40 cnt++;
42 if(cnt % cpl) printf("\" %s f\n", redir[printed]);
43 fclose(f);
44 return 0;