OSEC

Neohapsis is currently accepting applications for employment. For more information, please visit our website www.neohapsis.com or email hr@neohapsis.com
 
[PATCH] "mnewer" command for ftp(1)

From: Fabio Olive Leite (foleiteyahoo.com.br)
Date: Sun Jul 25 2004 - 01:09:51 CDT


Hi there,

Inspired by "wget -m" and downloading snapshots every few days, I
implemented an "mnewer" command for the ftp client. It is basically
"mget" mixed with "newer", and allows one to easily download only the
files that were updated since the last download.

Downloading the latest snapshot most likely without taking time to
download the same XFree tarballs (since they get updated less often)
becomes simply:

echo "mnewer *" | ftp -i ftp.openbsd.org:/pub/OpenBSD/snapshots/$ARCH/

I hope someone else finds this usefull. Did not update the manpage,
though.

Index: usr.bin/ftp/cmds.c
===================================================================
RCS file: /usr/local/cvsroot_obsd/src/usr.bin/ftp/cmds.c,v
retrieving revision 1.47
diff -u -r1.47 cmds.c
--- usr.bin/ftp/cmds.c 20 Jul 2004 03:50:25 -0000 1.47
+++ usr.bin/ftp/cmds.c 25 Jul 2004 05:36:54 -0000
-609,6 +609,65
         mflag = 0;
 }
 
+/*
+ * Get multiple files if they are newer than their local copies.
+ */
+void
+mnewer(argc, argv)
+ int argc;
+ char *argv[];
+{
+ sig_t oldintr;
+ int ch, ointer;
+ char *cp, *tp, *tp2, tmpbuf[MAXPATHLEN];
+ char *args[3] = { "mnewer", };
+
+ if (argc < 2 && !another(&argc, &argv, "remote-files")) {
+ fprintf(ttyout, "usage: %s remote-files\n", argv[0]);
+ code = -1;
+ return;
+ }
+ mname = argv[0];
+ mflag = 1;
+ oldintr = signal(SIGINT, mabort);
+ (void)setjmp(jabort);
+ while ((cp = remglob(argv, proxy, NULL)) != NULL) {
+ if (*cp == '\0') {
+ mflag = 0;
+ continue;
+ }
+ if (mflag && confirm(argv[0], cp)) {
+ tp = cp;
+ if (mcase) {
+ for (tp2 = tmpbuf; (ch = *tp++) != 0; )
+ *tp2++ = isupper(ch) ? tolower(ch) : ch;
+ *tp2 = '\0';
+ tp = tmpbuf;
+ }
+ if (ntflag)
+ tp = dotrans(tp);
+ if (mapflag)
+ tp = domap(tp);
+ args[1] = cp;
+ args[2] = tp;
+ if (getit(3, args, -1, "w"))
+ fprintf(ttyout,
+ "Local file \"%s\" is newer than remote"
+ " file \"%s\".\n", tp, cp);
+ if (!mflag && fromatty) {
+ ointer = interactive;
+ interactive = 1;
+ if (confirm("Continue with", "mnewer")) {
+ mflag++;
+ }
+ interactive = ointer;
+ }
+ }
+ }
+ (void)signal(SIGINT, oldintr);
+ mflag = 0;
+}
+
 char *
 onoff(int bool)
 {
Index: usr.bin/ftp/cmdtab.c
===================================================================
RCS file: /usr/local/cvsroot_obsd/src/usr.bin/ftp/cmdtab.c,v
retrieving revision 1.17
diff -u -r1.17 cmdtab.c
--- usr.bin/ftp/cmdtab.c 16 Dec 2003 21:46:22 -0000 1.17
+++ usr.bin/ftp/cmdtab.c 13 Jul 2004 18:40:06 -0000
-76,6 +76,7
 char mgethelp[] = "get multiple files";
 char mkdirhelp[] = "make directory on the remote machine";
 char mlshelp[] = "list contents of multiple remote directories";
+char mnewerhelp[] = "get multiple files if they are newer than their local copies";
 char modehelp[] = "set file transfer mode";
 char modtimehelp[] = "show last modification time of remote file";
 char mputhelp[] = "send multiple files";
-171,6 +172,7
         { "mget", mgethelp, 1, 1, 1, CMPL(R) mget },
         { "mkdir", mkdirhelp, 0, 1, 1, CMPL(r) makedir },
         { "mls", mlshelp, 1, 1, 1, CMPL(R) mls },
+ { "mnewer", mnewerhelp, 1, 1, 1, CMPL(R) mnewer },
         { "mode", modehelp, 0, 1, 1, CMPL0 setftmode },
         { "modtime", modtimehelp, 0, 1, 1, CMPL(r) modtime },
         { "more", pagehelp, 1, 1, 1, CMPL(r) page },
Index: usr.bin/ftp/extern.h
===================================================================
RCS file: /usr/local/cvsroot_obsd/src/usr.bin/ftp/extern.h,v
retrieving revision 1.26
diff -u -r1.26 extern.h
--- usr.bin/ftp/extern.h 11 Aug 2003 21:23:58 -0000 1.26
+++ usr.bin/ftp/extern.h 13 Jul 2004 02:10:14 -0000
-118,6 +118,7
 void mdelete(int, char **);
 void mget(int, char **);
 void mls(int, char **);
+void mnewer(int, char **);
 void modtime(int, char **);
 void mput(int, char **);
 char *onoff(int);

--
I drowned in the universal pool of entropy
Eris has saved me, and she has set me free