From 8b73b7881d350ae48fe53b661cf3299980434bf1 Mon Sep 17 00:00:00 2001 From: Alexander Fomin Date: Sun, 15 Jun 2014 18:25:00 +0400 Subject: [PATCH] GNU sync implementation --- Makefile | 11 +++++---- README.md | 1 - sync/sync.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 sync/sync.rs diff --git a/Makefile b/Makefile index cd6e617c6..91f9215ad 100644 --- a/Makefile +++ b/Makefile @@ -43,17 +43,18 @@ PROGS := \ tail \ UNIX_PROGS := \ + groups \ hostid \ hostname \ + id \ kill \ logname \ - users \ - whoami \ + sync \ tty \ - groups \ - id \ + uname \ uptime \ - uname + users \ + whoami ifneq ($(OS),Windows_NT) PROGS := $(PROGS) $(UNIX_PROGS) diff --git a/README.md b/README.md index fe8aea782..a8529ab0b 100644 --- a/README.md +++ b/README.md @@ -171,7 +171,6 @@ To do - stat - stdbuf - stty (in progress) -- sync - tail (not all features implemented) - test - timeout diff --git a/sync/sync.rs b/sync/sync.rs new file mode 100644 index 000000000..53f91dc63 --- /dev/null +++ b/sync/sync.rs @@ -0,0 +1,66 @@ +#![crate_id(name="sync", vers="1.0.0", author="Alexander Fomin")] +/* + * This file is part of the uutils coreutils package. + * + * (c) Alexander Fomin + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + + /* Last synced with: sync (GNU coreutils) 8.13 */ + +extern crate getopts; +extern crate libc; + +use std::os; +use getopts::{optflag, getopts, usage}; + +extern { + fn sync() -> libc::c_void; +} + +#[allow(dead_code)] +fn main () { os::set_exit_status(uumain(os::args())); } + +pub fn uumain(args: Vec) -> int { + let program = args.get(0); + + let options = [ + optflag("h", "help", "display this help and exit"), + optflag("V", "version", "output version information and exit") + ]; + + let matches = match getopts(args.tail(), options) { + Ok(m) => { m } + _ => { help(program.as_slice(), options); return 0 } + }; + + if matches.opt_present("h") { + help(program.as_slice(), options); + return 0 + } + + if matches.opt_present("V") { + version(); + return 0 + } + + unsafe { + sync() + }; + + 0 +} + +fn version() { + println!("sync (uutils) 1.0.0"); + println!("The MIT License"); + println!(""); + println!("Author -- Alexander Fomin."); +} + +fn help(program: &str, options: &[getopts::OptGroup]) { + println!("Usage: {:s} [OPTION]", program); + print!("{:s}", usage("Force changed blocks to disk, update the super block.", options)); +}