home *** CD-ROM | disk | FTP | other *** search
- /*
- * update.exe
- * copy files with time stamp considering
- *
- * for Turbo C 1.5ja (SPL) / compile with COMPACT memory model
- * (C) 1988 Sey
- * 1988/4/27 1.0
- * 9/3 1.1 kanji file name available
- * 10/20 1.2 disk full check / default destination directory
- * 11/16 1.2a 1 bug fixed
- * 12/16 1.3 drive or directory only is available for source
- * 1989/ 3/ 2 1.4 disk space check before copying
- * free location of option in arguments
- * 5/ 9 1.5 verify mode added(-v)
- */
- #define VERSION "1.5 "
-
- #include <dos.h>
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <dir.h>
- #include <io.h>
- #include <fcntl.h>
- #include <alloc.h>
- #include <stat.h>
- #include <ctype.h>
-
- #define MAXARG 256
- #define MAXBUFF 65534
- #define MINBUFF 1024
- char *buff[10];
- unsigned buffsize[10],buffuse[10];
-
- void
- usage()
- {
- printf("UPDATE - copy files when source is newer ver.%s (C)"
- "1988 Sey\n"
- "usage : update [-{ev}] <source> [<source>...] [<destination>]\n"
- "\t<source> is file or directory\n"
- "\t<destination> is directory (default current)\n"
- "\t-e limits sources to that alredy Exist in <destination>\n"
- "\t-v sets Verify flag\n"
- ,VERSION);
- exit(1);
- }
- /* end usage */
-
- void
- fatal(char *msg)
- {
- printf("update : %s\n",msg);
- exit(1);
- }
- /* end fatal */
-
- int
- isdir(char *pathname) /* is directory ? */
- {
- struct ffblk ffblk;
-
- if( jstrmatch(pathname,"*?") != NULL ) /* wild cards are not */
- return(0); /* available in directory */
- if( strcmp(pathname,"\\") == 0 ) /* root directory */
- return(2);
- if( strlen(pathname) == 3 && /* root directory */
- *(pathname + 1) == ':' &&
- *(pathname + 2) == '\\' )
- return(2);
- if( pathname[strlen(pathname)-1] == '\\' )
- pathname[strlen(pathname)-1] = '\0';
- if( !findfirst(pathname,&ffblk,FA_DIREC) &&
- ffblk.ff_attrib & FA_DIREC )
- return(1);
- else
- return(0);
- }
- /* end isdir */
-
- long
- dfree(int drive)
- {
- struct dfree df;
- long dfree;
-
- getdfree( drive,&df );
- dfree = df.df_avail;
- dfree *= df.df_sclus;
- dfree *= df.df_bsec;
- return( dfree );
- }
- /* end dfree */
-
- long
- fileclususe(int handle,int drive)
- {
- struct dfree df;
- long clussize;
-
- getdfree( drive,&df );
- clussize = (long)df.df_sclus * df.df_bsec;
- return( ((filelength(handle) + clussize - 1) / clussize) * clussize );
- }
- /* end fileclususe */
-
- long
- spend(char *src,char *dstdir,int copy_if_exist)
- {
- char dst[MAXPATH];
- char sdrive[MAXDRIVE],sdir[MAXDIR],sname[MAXFILE],sext[MAXEXT];
- int shandle,dhandle;
- struct ftime stime,dtime;
- int ddrive;
- long spend;
-
- ddrive = dstdir[1] == ':' ? toupper(dstdir[0]) - '@' : 0;
- if( (shandle = open(src,O_RDONLY)) == -1 )
- return(0);
- getftime(shandle,&stime);
- jfnsplit(src,sdrive,sdir,sname,sext);
- strcpy(dst,dstdir);
- if( *(dst + strlen(dst) - 1) != '\\' )
- strcat(dst,"\\");
- strcat(dst,sname);
- strcat(dst,sext);
- if( (dhandle = open(dst,O_WRONLY)) == -1 )
- if( errno == ENOENT && !copy_if_exist )
- spend = fileclususe(shandle,ddrive);
- else
- spend = 0;
- else
- {
- getftime(dhandle,&dtime);
- if( *((unsigned long *)&dtime) >= *((unsigned long *)&stime) )
- spend = 0;
- else
- spend = fileclususe(shandle,ddrive)
- - fileclususe(dhandle,ddrive);
- }
- close(shandle);
- close(dhandle);
- return(spend);
- }
- /* end spend */
-
- int
- update(char *src,char *dstdir,int copy_if_exist)
- {
- char dst[MAXPATH];
- char sdrive[MAXDRIVE],sdir[MAXDIR],sname[MAXFILE],sext[MAXEXT];
- struct ftime stime,dtime;
- int shandle,dhandle;
- int ddrive;
-
- ddrive = dstdir[1] == ':' ? toupper(dstdir[0]) - '@' : 0;
- if( (shandle = open(src,O_RDONLY)) == -1 )
- {
- printf("cannot open %s\n",src);
- return(1);
- }
- getftime(shandle,&stime);
-
- jfnsplit(src,sdrive,sdir,sname,sext);
- strcpy(dst,dstdir);
- if( *(dst + strlen(dst) - 1) != '\\' )
- strcat(dst,"\\");
- strcat(dst,sname);
- strcat(dst,sext);
- if( (dhandle = open(dst,O_RDONLY)) == -1 )
- {
- if( copy_if_exist )
- {
- /* printf("%s -x-> %s not exist\n",src,dst); */
- close(shandle);
- return(0);
- }
- else
- {
- if( filelength(shandle) > dfree(ddrive) )
- {
- printf("%s -x-> %s not enough disk space\n"
- ,src,dst);
- close(shandle);
- return(1);
- }
- if( (dhandle = open(dst
- ,O_CREAT|O_BINARY,S_IREAD|S_IWRITE)) == -1 )
- {
- printf("%s -x-> %s cannot create\n",src,dst);
- close(shandle);
- return(1);
- }
- }
- }
- else
- {
- getftime(dhandle,&dtime);
- if( *((unsigned long *)&dtime) >= *((unsigned long *)&stime) )
- {
- close(shandle);
- close(dhandle);
- return(0);
- }
- if( filelength(shandle)
- > dfree(ddrive) + fileclususe(dhandle,ddrive))
- {
- printf("%s -x-> %s not enough disk space\n",src,dst);
- close(shandle);
- close(dhandle);
- return(1);
- }
- close(dhandle);
- if( (dhandle = open(dst,O_WRONLY|O_TRUNC|O_BINARY)) == -1 )
- {
- printf("%s -x-> %s protected\n",src,dst);
- close(shandle);
- return(1);
- }
- }
- printf("%s -> %s\n",src,dst);
- {
- int fin;
- unsigned j;
-
- for( fin = 0 ; fin == 0 ; )
- {
- for( j = 0; buffsize[j] > 0 && j < 10; j++ )
- if( (buffuse[j]
- = (unsigned)_read(shandle,buff[j],buffsize[j]))
- == 0 )
- {
- fin = 1;
- break;
- }
- for( j = 0; buffuse[j] > 0 && j < 10; j++ )
- if( (unsigned)_write(dhandle,buff[j],buffuse[j])
- < buffuse[j] )
- {
- unlink(dst);
- printf("sorry! may be diskfull,%s is destroyed"
- ,dst);
- return(1);
- }
- }
- }
- setftime(dhandle,&stime);
- close(shandle);
- close(dhandle);
- return(0);
- }
- /* end update */
-
- char
- *strmerge(char *a,char *b)
- {
- char *p;
-
- if( (p = (char *)malloc(strlen(a)+strlen(b)+1)) == NULL )
- fatal("not enough memory");
- strcpy(p,a);
- strcat(p,b);
- return(p);
- }
- /* end strmerge */
-
- char
- *dirchck(char *path)
- {
- int isdir_;
- char *p;
-
- if( (isdir_ = isdir(path)) == 1 )
- path = strmerge(path,"\\*.*");
- else if( isdir_ == 2 )
- path = strmerge(path,"*.*");
- else if( path[1] == ':' && path[2] == '\0' )
- path = strmerge(path,"*.*");
- return(path);
- }
- /* end dirchck */
-
- main(int ac,char *av[])
- {
- int oac,aac;
- char **oav,**aav,*p;
- int xac,j,isdir_,missdst = 0,copy_if_exist = 0,failcopy = 0;
- int verify;
- char *xav[MAXARG],dstdir[MAXPATH+2],work[MAXPATH+2];
- unsigned long buffall = 0;
- long dspend;
- int ddrive;
-
- if( ac < 2 )
- usage();
- verify = getverify();
- setverify(0);
- optarg(ac,av,&oac,&oav,&aac,&aav,"-");
- for( j = 0; j < oac; j++ )
- {
- for( p = oav[j] + 1; *p; p++ )
- switch( *p )
- {
- case 'e': case 'E':
- copy_if_exist = 1; break;
- case 'v': case 'V':
- setverify(1); break;
- default:
- fatal("unknown option");
- }
- }
- if( aac == 2 )
- {
- getcwd( dstdir,MAXPATH+2 );
- missdst = 1;
- }
- else
- {
- strcpy(dstdir,aav[aac-1]);
- if( !isdir(dstdir))
- {
- if( dstdir[1] == ':' && dstdir[2] == '\0' )
- {
- getcurdir( toupper(dstdir[0]) - '@',work);
- strcat( dstdir,"\\" );
- strcat( dstdir,work );
- }
- else
- {
- getcwd( dstdir,MAXPATH+2 );
- missdst = 1;
- }
- }
- }
- ddrive = dstdir[1] == ':' ? toupper(dstdir[0]) - '@' : 0;
-
- for( j = 0; j < 10; j++ )
- {
- if( (buffsize[j] = (unsigned)min(MAXBUFF,coreleft())) == 0 )
- break;
- if( (buff[j] = (char *)malloc(buffsize[j])) == NULL )
- {
- buffsize[j] = 0;
- break;
- }
- buffall += buffsize[j];
- }
- if( buffsize[0] < MINBUFF )
- fatal("not enough memory");
- printf("using %lu bytes of buffer\n",buffall);
-
- for( j = 1 ; j < aac - 1 + missdst; j++ )
- aav[j] = dirchck(aav[j]);
-
- xac = exargs(aac-1+missdst,aav,xav,MAXARG);
-
- for( dspend = 0,j = 0 ; j < xac ; j++ )
- dspend += spend(xav[j],dstdir,copy_if_exist);
- if( dspend > dfree(ddrive) )
- fatal("not enough disk space");
- for( j = 0 ; j < xac ; j++ )
- failcopy |= update(xav[j],dstdir,copy_if_exist);
-
- setverify(verify);
- free(buff);
- exit(failcopy);
- }
- /* end main */
-
- /* end source update.c */