summaryrefslogtreecommitdiff
path: root/pico/osdep/chkpoint.c
blob: f9b7aa19e75fb59b2544a8df07028c9395ef4ed8 (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
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: chkpoint.c 769 2007-10-24 00:15:40Z hubert@u.washington.edu $";
#endif

/*
 * ========================================================================
 * Copyright 2006-2007 University of Washington
 * Copyright 2013-2020 Eduardo Chappa
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * ========================================================================
 */


#include <system.h>
#include <general.h>

#include "../estruct.h"
#include "../mode.h"
#include "../pico.h"
#include "../edef.h"
#include "../efunc.h"
#include "../keydefs.h"
#include "filesys.h"

#include "../../pith/charconv/filesys.h"

#include "chkpoint.h"


/*
 * chkptinit -- initialize anything we need to support composer
 *		checkpointing
 */
void
chkptinit(char *file, size_t filelen)
{
#ifndef _WINDOWS
    unsigned pid;
    char    *chp;
#endif

    if(!file[0]){
	long gmode_save = gmode;

	if(gmode&MDCURDIR)
	  gmode &= ~MDCURDIR;  /* so fixpath will use home dir */

#ifndef _WINDOWS
	strncpy(file, "#picoXXXXX#", filelen);
#else
	strncpy(file, "#picoTM0.txt", filelen);
#endif
	file[filelen-1] = '\0';
	fixpath(file, filelen);
	gmode = gmode_save;
    }
    else{
	size_t l = strlen(file);

	if(l+2 <= filelen && file[l-1] != C_FILESEP){
	    file[l++] = C_FILESEP;
	    file[l]   = '\0';
	}

#ifndef _WINDOWS
	strncpy(file + l, "#picoXXXXX#", filelen-l);
#else
	strncpy(file + l, "#picoTM0.txt", filelen-l);
#endif
	file[filelen-1] = '\0';
    }

#ifndef _WINDOWS
    pid = (unsigned)getpid();
    for(chp = file+strlen(file) - 2; *chp == 'X'; chp--){
	*chp = (pid % 10) + '0';
	pid /= 10;
    }
#else
    if(fexist(file, "r", (off_t *)NULL) == FIOSUC){ /* does file exist? */
	char copy[NLINE];

	strncpy(copy, "#picoTM1.txt", sizeof(copy));
	copy[sizeof(copy)-1] = '\0';
	fixpath(copy, sizeof(copy));
	our_rename(file, copy);  /* save so we don't overwrite it */
    }
#endif /* _WINDOWS */

    our_unlink(file);
}