1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: chkpoint.c 769 2007-10-24 00:15:40Z hubert@u.washington.edu $";
3 #endif
4 
5 /*
6  * ========================================================================
7  * Copyright 2006-2007 University of Washington
8  * Copyright 2013-2021 Eduardo Chappa
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *     http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * ========================================================================
17  */
18 
19 
20 #include <system.h>
21 #include <general.h>
22 
23 #include "../estruct.h"
24 #include "../mode.h"
25 #include "../pico.h"
26 #include "../edef.h"
27 #include "../efunc.h"
28 #include "../keydefs.h"
29 #include "filesys.h"
30 
31 #include "../../pith/charconv/filesys.h"
32 
33 #include "chkpoint.h"
34 
35 
36 /*
37  * chkptinit -- initialize anything we need to support composer
38  *		checkpointing
39  */
40 void
chkptinit(char * file,size_t filelen)41 chkptinit(char *file, size_t filelen)
42 {
43 #ifndef _WINDOWS
44     unsigned pid;
45     char    *chp;
46 #endif
47 
48     if(!file[0]){
49 	long gmode_save = gmode;
50 
51 	if(gmode&MDCURDIR)
52 	  gmode &= ~MDCURDIR;  /* so fixpath will use home dir */
53 
54 #ifndef _WINDOWS
55 	strncpy(file, "#picoXXXXX#", filelen);
56 #else
57 	strncpy(file, "#picoTM0.txt", filelen);
58 #endif
59 	file[filelen-1] = '\0';
60 	fixpath(file, filelen);
61 	gmode = gmode_save;
62     }
63     else{
64 	size_t l = strlen(file);
65 
66 	if(l+2 <= filelen && file[l-1] != C_FILESEP){
67 	    file[l++] = C_FILESEP;
68 	    file[l]   = '\0';
69 	}
70 
71 #ifndef _WINDOWS
72 	strncpy(file + l, "#picoXXXXX#", filelen-l);
73 #else
74 	strncpy(file + l, "#picoTM0.txt", filelen-l);
75 #endif
76 	file[filelen-1] = '\0';
77     }
78 
79 #ifndef _WINDOWS
80     pid = (unsigned)getpid();
81     for(chp = file+strlen(file) - 2; *chp == 'X'; chp--){
82 	*chp = (pid % 10) + '0';
83 	pid /= 10;
84     }
85 #else
86     if(fexist(file, "r", (off_t *)NULL) == FIOSUC){ /* does file exist? */
87 	char copy[NLINE];
88 
89 	strncpy(copy, "#picoTM1.txt", sizeof(copy));
90 	copy[sizeof(copy)-1] = '\0';
91 	fixpath(copy, sizeof(copy));
92 	our_rename(file, copy);  /* save so we don't overwrite it */
93     }
94 #endif /* _WINDOWS */
95 
96     our_unlink(file);
97 }
98 
99