Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – planix.SVN – Blame – /os/branches/feature_fixcpp/sys/src/cmd/swap.c – Rev 2

Subversion Repositories planix.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#include <u.h>
2
#include <libc.h>
3
 
4
void error(char *);
5
 
6
void
7
main(int argc, char **argv)
8
{
9
	Dir *d;
10
	int swapfd, cswfd;
11
	char buf[128], *p;
12
	int i, j;
13
 
14
	if(argc != 2) {
15
		print("Usage: swap path\n");
16
		exits("swap: failed");
17
	}
18
 
19
	d = dirstat(argv[1]);
20
	if(d == nil){
21
		print("swap: can't stat %s: %r\n", argv[1]);
22
		exits("swap: failed");
23
	}
24
	if(d->type != 'M'){		/* kernel device */
25
		swapfd = open(argv[1], ORDWR);
26
		p = argv[1];
27
	}
28
	else {
29
		p = getenv("sysname");
30
		if(p == 0)
31
			p = "swap";
32
		sprint(buf, "%s/%sXXXXXXX", argv[1], p);
33
		p = mktemp(buf);
34
		swapfd = create(p, ORDWR|ORCLOSE, 0600);
35
	}
36
 
37
	if(swapfd < 0 || (p[0] == '/' && p[1] == '\0')) {
38
		print("swap: failed to make a file: %r\n");
39
		exits("swap: failed");
40
	}
41
 
42
	i = create("/env/swap", OWRITE, 0666);
43
	if(i < 0) 
44
		error("open /env/swap");
45
 
46
	if(write(i, p, strlen(p)) != strlen(p))
47
		error("sysname");
48
	close(i);
49
	print("swap: %s\n", p);
50
 
51
	cswfd = open("/dev/swap", OWRITE);
52
	if(cswfd < 0)
53
		error("open: /dev/swap");
54
 
55
	j = sprint(buf, "%d", swapfd);
56
	if(write(cswfd, buf, j) != j)
57
		error("write: /dev/swap");
58
	exits(0);
59
}
60
 
61
void
62
error(char *s)
63
{
64
	perror(s);
65
	exits(s);
66
}