home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / fixpath.icn < prev    next >
Text File  |  2000-07-29  |  2KB  |  63 lines

  1. ############################################################################
  2. #
  3. #    File:     fixpath.icn
  4. #
  5. #    Subject:  Program to replace path in a binary file
  6. #
  7. #    Author:   Gregg M. Townsend
  8. #
  9. #    Date:     November 14, 1994
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  Usage:  fixpath filename oldpath newpath
  18. #
  19. #  Fixpath changes file paths or other strings in a binary file by modifying
  20. #  the file in place.  Each null-terminated occurrence of "oldpath" is
  21. #  replaced by "newpath".
  22. #
  23. #  If the new path is longer than the old one, a warning is given and the
  24. #  old path is extended by null characters, which must be matched in the
  25. #  file for replacement to take place.  This is dangerous in general but
  26. #  allows repairing an errant fixpath command.
  27. #
  28. ############################################################################
  29.  
  30.  
  31. procedure main(args)
  32.    local fname, oldpath, newpath, f, pgm, n, p, s
  33.  
  34.    (*args == 3) | stop("usage: fixpath filename oldpath newpath")
  35.    fname := args[1]
  36.    oldpath := args[2]
  37.    newpath := args[3]
  38.    if *newpath > *oldpath then {
  39.       write(&errout, "warning: newpath is longer than oldpath")
  40.       oldpath := left(oldpath, *newpath, "\0")
  41.    }
  42.    oldpath ||:= "\0"
  43.    newpath := left(newpath, *oldpath, "\0")
  44.  
  45.    (f := open(fname, "rwu"))        | stop(fname, ": can't open")
  46.    pgm := ""
  47.    while pgm ||:= reads(f, 8192)
  48.    (*pgm > 0)                | stop(fname, ": empty file")
  49.    n := 0
  50.    pgm ? {
  51.       while tab(p := find(oldpath)) do {
  52.          seek(f, p)            | stop(fname, ": can't seek")
  53.          writes(f, s, newpath)        | stop(fname, ": can't write")
  54.          move(*newpath)
  55.          n +:= 1
  56.       }
  57.       (n > 0) | stop(fname, ": can't find string `", args[2], "'")
  58.    }
  59.    write("replaced ", n, " occurrence", if n>1 then "s" else "")
  60.  
  61. end
  62.  
  63.