func touch(filename string, nacc, nmod int64) (errcnt int) { var st syscall.Stat_t var ut syscall.Utimbuf if e := syscall.Stat(filename, &st); e != 0 { if e == syscall.ENOENT { if *cflag { errcnt++ return } var fd int defer syscall.Close(fd) if fd, e = syscall.Creat(filename, 0666); e != 0 { fmt.Fprintf(os.Stderr, "touch: can not create %s\n", filename) errcnt += 1 return } if e = syscall.Fstat(fd, &st); e != 0 { fmt.Fprintf(os.Stderr, "touch: can't stat %s\n", filename) errcnt += 1 return } } else { fmt.Fprintf(os.Stderr, "touch: can't stat %s\n", filename) errcnt += 1 return } } if *aflag { ut.Actime = nacc } else { ut.Actime = st.Atim.Sec } if *mflag { ut.Modtime = nmod } else { ut.Modtime = st.Mtim.Sec } if nulltime { if e := syscall.Utime(filename, nil); e != 0 { fmt.Fprintf(os.Stderr, "touch: unable to touch %s", filename) errcnt += 1 } } else { if e := syscall.Utime(filename, &ut); e != 0 { fmt.Fprintf(os.Stderr, "touch: unable to touch %s", filename) errcnt += 1 } } return }
func main() { flag.Parse() if flag.NArg() < 1 { usage() } for i := 0; i < flag.NArg(); i++ { var name = flag.Arg(i) var tb syscall.Utimbuf tb.Actime = (int32)(*newTime) tb.Modtime = (int32)(*newTime) var e = syscall.Utime(name, &tb) if (e != 0) && *create { fmt.Fprintf( os.Stderr, "touch: cannot touch `%s'\n", name) os.Exit(1) } f, err := os.Open(name, os.O_CREAT, 0666) if err != nil { fmt.Fprintf( os.Stderr, "touch: cannot touch `%s': %s\n", name, err.String()) os.Exit(1) } f.Close() e = syscall.Utime(name, &tb) if e != 0 { fmt.Fprintf( os.Stderr, "touch: cannot touch `%s'\n", name) os.Exit(1) } } os.Exit(0) }