opendir change: refinement
[minix.git] / commands / shar / shar.c
blob300d65a3e9930de59b5d6f6ea92880ad22a8a01d
1 /* shar - make a shell archive Author: Michiel Husijes */
3 #include <stdlib.h>
4 #include <stdio.h>
6 static void error(char *progname, char *operation, char *filename);
7 int main(int argc, char **argv);
9 int main(argc, argv)
10 int argc;
11 char *argv[];
13 int argn;
14 register int ch;
15 register FILE *fp;
16 int exitstatus;
17 char *filename;
19 exitstatus = 0;
20 for (argn = 1; argn < argc; argn++) {
21 filename = argv[argn];
22 if ((fp = fopen(filename, "r")) == NULL) {
23 error(argv[0], "opening ", filename);
24 exitstatus = 1;
25 } else {
26 fputs("echo x - ", stdout);
27 fputs(filename, stdout);
28 fputs("\nsed '/^X/s///' > ", stdout);
29 fputs(filename, stdout);
30 fputs(" << '/'\n", stdout);
31 while ((ch = getc(fp)) != EOF) {
32 putchar('X');
33 putchar(ch);
34 while (ch != '\n') {
35 ch = getc(fp);
36 if (ch == EOF) break;
37 putchar(ch);
39 if (ch == EOF) break;
41 fputs("/\n", stdout);
42 if (ferror(fp)) {
43 error(argv[0], "reading ", filename);
44 exitstatus = 1;
46 if (fclose(fp) != 0) {
47 error(argv[0], "closing ", filename);
48 exitstatus = 1;
50 if (ferror(stdout)) break; /* lost already */
53 fflush(stdout);
54 if (ferror(stdout)) {
55 error(argv[0], "writing ", "stdout");
56 exitstatus = 1;
58 return(exitstatus);
61 static void error(progname, operation, filename)
62 char *progname;
63 char *operation;
64 char *filename;
66 fputs(progname, stderr);
67 fputs(": error ", stderr);
68 fputs(operation, stderr);
69 perror(filename);