plan9fox/sys/src/cmd/swap.c
Ori Bernstein 5bc9b0c3ca improve usage messages (thanks henesy)
Fix inconsistencies between programs and their usage
messages,  correct instances where information seems
to be missing or lost. This  includes missing arguments,
making usage consistent with manuals, and so on.
2020-03-10 10:09:34 -07:00

54 lines
960 B
C

#include <u.h>
#include <libc.h>
void
main(int argc, char **argv)
{
int swapfd, cswfd;
char buf[1024], *p;
Dir *d;
ARGBEGIN {
} ARGEND;
if(argc != 1){
fprint(2, "usage: swap file\n");
exits("usage");
}
swapfd = -1;
if(d = dirstat(p = *argv)){
if(d->mode & DMDIR){
p = getenv("sysname");
if(p == 0)
p = "swap";
snprint(buf, sizeof buf, "%s/%sXXXXXXX", *argv, p);
p = mktemp(buf);
} else
swapfd = open(p, ORDWR);
}
if(d == nil || (d->mode & DMDIR)){
if((swapfd = create(p, ORDWR|ORCLOSE, 0600)) >= 0){
Dir nd;
nulldir(&nd);
nd.mode = DMTMP|0600;
dirfwstat(swapfd, &nd);
}
}
if(swapfd < 0)
sysfatal("%r");
if(fd2path(swapfd, p = buf, sizeof buf))
sysfatal("fd2path: %r");
if(putenv("swap", p) < 0)
sysfatal("putenv: %r");
print("swap: %s\n", p);
if((cswfd = open("/dev/swap", OWRITE)) < 0)
sysfatal("open: %r");
if(fprint(cswfd, "%d", swapfd) < 0)
sysfatal("write: %r");
exits(0);
}