diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2011-10-14 04:55:05 +0200 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2011-10-14 04:55:05 +0200 |
commit | 6aa0e0017d7d0cddc006da885946934b06949a91 (patch) | |
tree | 66b688ec32e2f91266db760b1762f2a50cc52036 /libslang/slsh/scripts/mv | |
parent | a966db5b71328f6adf9dd767e64b322a3bd7ed9c (diff) | |
download | erlang-slang-fork-6aa0e0017d7d0cddc006da885946934b06949a91.tar.gz |
include libslang-1.4.9 and automatically build it and link erlang-slang against it
few (erlang) people will still have libslang-1.4.9 installed or spend time
to get it to link against the driver
Diffstat (limited to 'libslang/slsh/scripts/mv')
-rwxr-xr-x | libslang/slsh/scripts/mv | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/libslang/slsh/scripts/mv b/libslang/slsh/scripts/mv new file mode 100755 index 0000000..47bc25f --- /dev/null +++ b/libslang/slsh/scripts/mv @@ -0,0 +1,143 @@ +#!/usr/bin/env slsh +% -*- slang -*- + +static variable Confirm_Move = 0; + +static define get_yn () +{ + variable args = __pop_args (_NARGS); + () = fprintf (stdout, __push_args (args)); + () = fflush (stdout); + + variable yn; + if (fgets (&yn, stdin) <= 0) + return -1; + + "y" == strlow (strtrim (yn)); +} + + +static define move_file (from, to) +{ + if (from == to) + { + () = fprintf (stderr, "%s: Cannot move a file to itself.\n", __argv[0]); + return -1; + } + + if (0 == rename (from, to)) + return 0; + + variable st = stat_file (to); + + if (st != NULL) + { + if (1 != get_yn ("%s exists. Overwrite? [y/n]", to)) + { + () = fputs ("Not Confirmed\n", stdout); + return -1; + } + () = remove (to); + } + + if (0 == rename (from, to)) + return 0; + + ()=fprintf (stderr, "Failed to rename %s to %s: %s\n", + from, to, errno_string (errno)); + + return -1; +} + +define move_files (from_files, to) +{ + variable st = stat_file (to); + if (st == NULL) + { + if (length (from_files) != 1) + { + () = fprintf (stderr, "%s must be a directory\n", to); + exit (1); + } + if (-1 == move_file (from_files[0], to)) + exit (1); + exit (0); + } + + !if (stat_is ("dir", st.st_mode)) + { + if (length (from_files) != 1) + { + () = fprintf (stderr, "%s must be a directory\n", to); + exit (1); + } + if (-1 == move_file (from_files[0], to)) + exit (1); + exit (0); + } + + + foreach (from_files) + { + variable old = (); + variable new = path_concat (to, path_basename (old)); + + if (NULL == stat_file (old)) + { + () = fprintf (stderr, "Unable to access %s\n", old); + continue; + } + + if (Confirm_Move) + { + if (1 != get_yn ("Move %s to %s/? [y/n]", old, to)) + { + () = fputs ("Not Confirmed\n", stdout); + continue; + } + } + + + () = move_file (old, new); + } +} + +static define usage () +{ + () = fprintf (stdout, "Usage: %s [-i] files ... dir\n", __argv[0]); + exit (1); +} + +define main (argc, argv) +{ + argc--; + argv = argv[[1:]]; + + while (argc > 1) + { + if (argv[0] == "-i") + { + Confirm_Move = 1; + argc--; + argv = argv[[1:]]; + continue; + } + break; + } + + if (argc < 2) + usage (); + + move_files (argv[[0:argc-2]], argv[argc-1]); +} + + +define slsh_main () +{ + main (__argc, __argv); +} + + + + + |