summaryrefslogtreecommitdiff
path: root/editors/mg2a/patches/patch-am
blob: 0464ff7dfd3368e5b8c5426d9d9e9b83bf1f9a11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
$NetBSD: patch-am,v 1.1.1.1 2000/04/01 00:21:27 dmcmahill Exp $

--- ./file.c.orig	Sun Jul  3 10:48:57 1988
+++ ./file.c	Thu Mar 30 08:37:30 2000
@@ -447,14 +447,151 @@
 writeout(bp, fn) register BUFFER *bp; char *fn; {
 	register int	s;
 
+// ------------------> refling, for mirroring the original once
+// ------------------> if .original subdir exists in the same dir as
+// ------------------> the file to save, and there is an EMPTY file
+// ------------------> name with the same name as we wish to save
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+// main(int argc, char **argv) {
+   char *last_slash, *file_name_no_dir, dir_name[1000], cmd[1000], *end_of_dirname;
+// in[1000];
+   struct stat stat_struct;
+   const char nil[] = "nil";
+   int originaled, journaled, diffoed;
+
+   // fn is the incoming string to use, never altered.  This is for the
+   // command line transfer.  Not needed in final program.
+// if (1 == argc) strcpy(fn, "");
+// else strcpy(fn, argv[1]);
+
+   // extract the dirname part of the argument:
+
+   // handle case where string is null, just in case
+   if (0 == strlen(fn)) {
+	strcpy(dir_name, ".");
+	file_name_no_dir = (char *)nil;   // default: filename in cwd
+   }
+   
+   // this case is when there is no slash, so is filename
+   else if (NULL == (last_slash = (char *)strrchr(fn, '/'))) {
+      	strcpy(dir_name, ".");
+      	file_name_no_dir = fn;
+   }
+
+   // this case is /filename
+   else if (last_slash == fn) {
+	strcpy(dir_name, "/");
+	file_name_no_dir = last_slash + 1;
+   }
+
+   // this case is normal case
+   else {
+	*last_slash = 0;
+	strcpy(dir_name, fn);
+	*last_slash = '/';
+	file_name_no_dir = last_slash + 1;
+   }
+
+   // at this point, we have dirname in dir_name.  Store its end, so we
+   // can recover just the dirname later, after concatinating other stuff,
+   // and a pointer to the stuff following the dirname
+   end_of_dirname = dir_name + strlen(dir_name);
+
+   // get rid of unwanted filenames for this particular application
+   if (0 == strlen(file_name_no_dir)) file_name_no_dir = (char *)nil;
+   if (!strcmp(file_name_no_dir, ".")) file_name_no_dir = (char *)nil;
+   // printf("dir=%s file=%s\n", dir_name, file_name_no_dir);
+
+
+//   ///////////////////////////////////////////////////////////////////////////
+//   // start .original: concat the .original directory and filename to dir_name
+//   if (dir_name[strlen(dir_name) - 1] != '/') strcat(dir_name, "/");
+//   strcat(dir_name, ".original/");
+//
+   originaled = 0;
+//   // test if .original flag dir exists and the .original/filename does not exist
+//   if (0 == stat(dir_name, &stat_struct) && S_ISDIR(stat_struct.st_mode) &&
+//   0 != stat(dir_name, &stat_struct)) {   // .original/filename does not exist
+//      // test if the initially edited file exists
+//      if (0 == stat(fn, &stat_struct) && S_ISREG(stat_struct.st_mode)) {
+//         strcat(dir_name, file_name_no_dir);
+//	 sprintf(cmd, "/bin/cp %s %s", fn, dir_name);
+//  	 if (0 != system(cmd)) printf(".original/fn backup failed: '%s'\n", cmd);
+//	 else originaled = 1;
+//         }
+//      else if ( 0 != stat(fn, &stat_struct)) {
+//         sprintf(cmd, "/usr/bin/touch %s", fn);
+//	 if (0 != system(cmd)) printf(".original/fn touch failed: '%s'\n", cmd);
+//	 else originaled = 1;
+//	 printf(".original/fn touch: '%s'\n", cmd);
+//      }
+//   } 
+//
+//   // restore dir_name to be used in next phase
+//   *end_of_dirname = 0;
+
+   ///////////////////////////////////////////////////////////////////////////
+   // start MG_DOT_ORIG test
+   diffoed = 0;
+   if (dir_name[strlen(dir_name) - 1] != '/') strcat(dir_name, "/");
+   strcat(dir_name, file_name_no_dir);
+   strcat(dir_name, ".orig");
+   // if MG_DOT_ORIG set and there is no .orig for the edited file
+   if (NULL != getenv("MG_DOT_ORIG") && 0 != stat(dir_name, &stat_struct)) {
+      // if edited file already exists and is a file, copy it to .orig
+      if (0 == stat(fn, &stat_struct) && S_ISREG(stat_struct.st_mode)) {
+         sprintf(cmd, "/bin/cp %s %s", fn, dir_name);
+         if (0 != system(cmd)) printf(".orig backup failed: '%s'\n", cmd);
+         else diffoed = 1;
+      }
+      // if edited file does not exist yet, touch the .orig since it was empty
+      else if (0 != stat(fn, &stat_struct)) {
+         sprintf(cmd, "/usr/bin/touch %s", dir_name);
+	 if (0 != system(cmd)) printf(".orig touch failed: '%s'\n", cmd);
+	 else diffoed = 1;
+      }
+   }
+
+   // restore dir_name to be used in next phase
+   *end_of_dirname = 0;
+
+   ///////////////////////////////////////////////////////////////////////////
+   // start journal: concat the .journal directory and filename to dir_name
+   if (dir_name[strlen(dir_name) - 1] != '/') strcat(dir_name, "/");
+   strcat(dir_name, ".journal/");
+
+   journaled = 0;
+   // test if .journal flag directory exists 
+   if (0 == stat(dir_name, &stat_struct) && S_ISDIR(stat_struct.st_mode)) {
+      // test if the initially edited file exists
+      if (0 == stat(fn, &stat_struct) && S_ISREG(stat_struct.st_mode)) {
+	 sprintf(cmd, "/bin/cp %s %s%s-@%ld", fn, dir_name, file_name_no_dir, time(NULL));
+	 if (0 != system(cmd)) printf("journal failed: '%s'\n", cmd);
+	 else journaled = 1;
+      }
+      else if (0 != stat(fn, &stat_struct)) {
+	 sprintf(cmd, "/usr/bin/touch %s%s-@%ld", dir_name, file_name_no_dir, time(NULL));
+	 if (0 != system(cmd)) printf(".journal/fn touch failed: '%s'\n", cmd);
+	 else journaled = 1;
+      }
+   }
 	if ((s=ffwopen(fn)) != FIOSUC)		/* Open writes message. */
 		return (FALSE);
 	s = ffputbuf(bp);
 	if (s == FIOSUC) {			/* No write error.	*/
 		s = ffclose();
-		if (s==FIOSUC)
-			ewprintf("Wrote %s", fn);
+		if (s==FIOSUC && !diffoed && !journaled) ewprintf("Wrote %s", fn);
+		if (s==FIOSUC && !diffoed &&  journaled) ewprintf("Wrote(j) %s", fn);
+		if (s==FIOSUC &&  diffoed && !journaled) ewprintf("Wrote(o) %s", fn);
+		if (s==FIOSUC &&  diffoed &&  journaled) ewprintf("Wrote(o+j) %s", fn);
 	} else					/* Ignore close error	*/
+
+// ------------------> refling, for originaling and journaling, end
+
 		(VOID) ffclose();		/* if a write error.	*/
 	return s == FIOSUC;
 }