implement Touch; include "sys.m"; include "draw.m"; include "daytime.m"; sys : Sys; ORDWR, FD : import sys; open, create, read, write, seek, fildes, fprint, stat, wstat : import sys; stderr : ref FD; Touch : module { init : fn(ctxt : ref Draw->Context, argl : list of string); }; usage() { fprint(stderr, "usage: touch [-c] files\n"); exit; } init(nil : ref Draw->Context, argl : list of string) { sys = load Sys Sys->PATH; stderr = fildes(2); force := 1; status := 0; for (argl = tl argl; argl != nil; argl = tl argl) { s := hd argl; if (len s >= 1 && s[0] == '-') { for (i := 1; i < len s; i++) { case s[i] { 'c' => force = 0; * => usage(); } } } else break; } if(argl == nil) usage(); for ( ; argl != nil; argl = tl argl) status += touch(force, hd argl); exit; } touch(force : int, name : string) : int { fd : ref FD; junk := array[1] of byte; (ok, dir) := stat(name); if (ok < 0 && force == 0) { fprint(stderr, "touch: %s: cannot stat: %r\n", name); return 1; } if (ok < 0) { if ((fd = create(name, 0, 8r666)) == nil) { fprint(stderr, "touch: %s: cannot create: %r\n", name); return 1; } fd = nil; return 0; } daytime := load Daytime Daytime->PATH; dir.mtime = dir.atime = daytime->now(); if (wstat(name, dir) < 0) { fprint(stderr, "touch: %s cannot wstat: %r\n", name); return 1; } return 0; # following fails on empty file # if ((fd = open(name, ORDWR)) == nil) { # fprint(stderr, "touch: %s: cannot open: %r\n", name); # return 1; # } # if(read(fd, junk, 1) < 1) { # fprint(stderr, "touch: %s: read error: %r\n", name); # fd = nil; # return 1; # } # seek(fd, 0, 0); # if(write(fd, junk, 1) < 1 ) { # fprint(stderr, "touch: %s: write error: %r\n", name); # fd = nil; # return 1; # } # fd = nil; # return 0; }