#!/bin/csh #****************************************************************************** # MPIA - MIDI project # # "@(#) $Id: $" # # who when what # ---------- ---------- ---------------------------------------------- # mathar 2000-10-13 created # mathar 2000-11-22 added loop over multi-file arguments # mathar 2001-08-14 removed bug attributed to 20 Kbytes tail(1) buffer under HP-UX 10.20 # #************************************************************************ # NAME # ftruncate - delete leading portions of Unix file # # SYNOPSIS # ftruncate [ ...] # # DESCRIPTION # Remove the leading part of the files until only 'bytes' or less # bytes are left, making sure that # (i) only complete lines are left (no lines are broken, only entire lines # kept) # (ii) no local, temporary file is used (to let it work even if file # systems are full) by using only pipes and the like # The (2nd command line argument) is modified on return unless # is smaller or equal to the file's byte count. # # RETURN VALUE # exit status 0 (OK) or 1 (failed) # # ENVIRONMENT # TMPDIR as used by ed(1) # # CAUTIONS # Whereas ftruncate(2) removes bytes at the end of the file, this # shell script here removes bytes at the beginning, leaving the # most recent information intact. # # SEE ALSO # ftruncate(2) wc(1) head(1) # # BUGS # #------------------------------------------------------------------------ if ( ${#argv} >= 2 ) then foreach file ( ${argv[2-]} ) # Do nothing if less than bytes_to_remain in file set ball=`cat ${file} | wc -c` if ( ${ball} <= ${argv[1]} ) continue # Number of bytes to be deleted at minimum @ bdel = ${ball} - ${argv[1]} # Number lines to delete from the SOF set ldel = `head -c -n ${bdel} ${file} | wc -l` # call line editor in silent mode to delete lines 1 thru ${ldel} ed -s ${file} <<++ 1,${ldel}d w q ++ end exit 0 else exit 1 endif # ___oOo___