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");
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();
20 if(is_c_arg(argv
[1])) {
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;
29 static const char redir
[][3] = {">", ">>"};
31 while(fread(buf
, 1, 1, f
)) {
34 printf("\" %s f", redir
[printed
]);
36 } else printf("#!/bin/sh");
37 printf("\n\necho -ne \"");
39 printf("\\x%02X", buf
[0]);
42 if(cnt
% cpl
) printf("\" %s f\n", redir
[printed
]);