[comp.os.minix] undos & rename

dono@killer.DALLAS.TX.US (Don OConnell) (01/09/89)

Here are a couple of little programs that I shar'd together.
Undos - An ascii translation program
	I find this one to be an almost invaluable program that I have running
	on the SysV machine, Minix, and dos - read the man page.

rename(3) - the library program
	I needed this one for a program that I was porting to MINIX.

----------------------------Cut Here----------------------------------------
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  rename.3 rename.c rename.man undos.c undos.man
# Wrapped by dono@killer on Sun Jan  8 22:23:57 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f rename.3 -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"rename.3\"
else
echo shar: Extracting \"rename.3\" \(2283 characters\)
sed "s/^X//" >rename.3 <<'END_OF_rename.3'
X.TH RENAME 3 "Standard Extension"
X.SH NAME
Xrename \- change the name of a file
X.SH SYNOPSIS
X.ft B
X.nf
Xrename(from, to)
Xchar *from, *to;
X.fi
X.ft R
X.SH DESCRIPTION
X.I Rename
Xcauses the link named
X.I from
Xto be renamed as
X.IR to .
XIf 
X.I to
Xexists, then it is first removed.
XBoth 
X.I from
Xand
X.I to
Xmust be of the same type (i.e., both directories or both
Xnon-directories), and must reside on the same file system.
X.PP
X.I Rename
Xguarantees that an instance of
X.I to
Xwill always exist, even if the system should crash in
Xthe middle of the operation.
X.SH CAVEAT
XThe system can deadlock if a loop in the file system graph is present.
XThis loop takes the form of an entry in directory \*(lqa\*(rq,
Xsay \*(lqa/foo\*(rq,
Xbeing a hard link to directory \*(lqb\*(rq, and an entry in
Xdirectory \*(lqb\*(rq, say \*(lqb/bar\*(rq, being a hard link
Xto directory \*(lqa\*(rq.
XWhen such a loop exists and two separate processes attempt to
Xperform \*(lqrename a/foo b/bar\*(rq and \*(lqrename b/bar a/foo\*(rq,
Xrespectively, 
Xthe system may deadlock attempting to lock
Xboth directories for modification.
XOn systems with a symbolic link capability, hard links to directories should be
Xreplaced by symbolic links by the system administrator.
X.SH "RETURN VALUE"
XA 0 value is returned if the operation succeeds, otherwise
X.I rename
Xreturns \-1 and the global variable
X.I errno
Xindicates the reason for the failure.
X.SH "ERRORS
X.I Rename
Xwill fail and nothing will change if any of the following are true:
X.TP 15
X[ENOTDIR]
XA component of either path prefix is not a directory.
X.TP 15
X[ENOENT]
XA component of either path prefix does not exist.
X.TP 15
X[EACCES]
XA component of either path prefix denies search permission.
X.TP 15
X[ENOENT]
XThe file named by \fIfrom\fP does not exist.
X.TP 15
X[EXDEV]
XThe link named by \fIto\fP and the file named by \fIfrom\fP
Xare on different logical devices (file systems).  Note that this error
Xcode will not be returned if the implementation permits cross-device
Xlinks.
X.TP 15
X[EACCES]
XThe requested link requires writing in a directory with a mode
Xthat denies write permission.
X.TP 15
X[EROFS]
XThe requested link requires writing in a directory on a read-only file
Xsystem.
X.TP 15
X[EFAULT]
X.I Path
Xpoints outside the process's allocated address space.
X.SH "SEE ALSO"
Xopen(2)
END_OF_rename.3
if test 2283 -ne `wc -c <rename.3`; then
    echo shar: \"rename.3\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f rename.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"rename.c\"
else
echo shar: Extracting \"rename.c\" \(377 characters\)
sed "s/^X//" >rename.c <<'END_OF_rename.c'
X/* rename.c -- file renaming routine for systems without rename(2)
X *
X * Written by reading the System V Interface Definition, not the code.
X *
X * Totally public domain.
X *
X */
X/* LINTLIBRARY */
X
Xint rename(from,to)
Xregister char *from, *to;
X{
X    (void) unlink(to);
X    if (link(from, to) < 0)
X	return(-1);
X
X    (void) unlink(from);
X    return(0);
X}
X
X/* rename.c ends here */
END_OF_rename.c
if test 377 -ne `wc -c <rename.c`; then
    echo shar: \"rename.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f rename.man -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"rename.man\"
else
echo shar: Extracting \"rename.man\" \(3070 characters\)
sed "s/^X//" >rename.man <<'END_OF_rename.man'
X
X
X
X     RENAME(3)         UNIX 5.0 (Standard Extension)         RENAME(3)
X
X
X
X     NAME
X          rename - change the name of a file
X
X     SYNOPSIS
X          rename(from, to)
X          char *from, *to;
X
X     DESCRIPTION
X          Rename causes the link named from to be renamed as to.  If
X          to exists, then it is first removed.  Both from and to must
X          be of the same type (i.e., both directories or both non-
X          directories), and must reside on the same file system.
X
X          Rename guarantees that an instance of to will always exist,
X          even if the system should crash in the middle of the
X          operation.
X
X     CAVEAT
X          The system can deadlock if a loop in the file system graph
X          is present.  This loop takes the form of an entry in
X          directory a, say a/foo, being a hard link to directory b,
X          and an entry in directory b, say b/bar, being a hard link to
X          directory a.  When such a loop exists and two separate
X          processes attempt to perform rename a/foo b/bar and rename
X          b/bar a/foo, respectively, the system may deadlock
X          attempting to lock both directories for modification.  On
X          systems with a symbolic link capability, hard links to
X          directories should be replaced by symbolic links by the
X          system administrator.
X
X     RETURN VALUE
X          A 0 value is returned if the operation succeeds, otherwise
X          rename returns -1 and the global variable errno indicates
X          the reason for the failure.
X
X     ERRORS
X          Rename will fail and nothing will change if any of the
X          following are true:
X
X          [ENOTDIR]      A component of either path prefix is not a
X                         directory.
X
X          [ENOENT]       A component of either path prefix does not
X                         exist.
X
X          [EACCES]       A component of either path prefix denies
X                         search permission.
X
X          [ENOENT]       The file named by from does not exist.
X
X          [EXDEV]        The link named by to and the file named by
X                         from are on different logical devices (file
X
X
X
X     Page 1                                           (printed 1/8/89)
X
X
X
X
X
X
X     RENAME(3)         UNIX 5.0 (Standard Extension)         RENAME(3)
X
X
X
X                         systems).  Note that this error code will not
X                         be returned if the implementation permits
X                         cross-device links.
X
X          [EACCES]       The requested link requires writing in a
X                         directory with a mode that denies write
X                         permission.
X
X          [EROFS]        The requested link requires writing in a
X                         directory on a read-only file system.
X
X          [EFAULT]       Path points outside the process's allocated
X                         address space.
X
X     SEE ALSO
X          open(2)
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X     Page 2                                           (printed 1/8/89)
X
X
X
X
END_OF_rename.man
if test 3070 -ne `wc -c <rename.man`; then
    echo shar: \"rename.man\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f undos.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"undos.c\"
else
echo shar: Extracting \"undos.c\" \(3544 characters\)
sed "s/^X//" >undos.c <<'END_OF_undos.c'
X/*% cc -O -K % -o undos
X *
X * Undos - change DOS format files to Unix, etc.
X */
Xchar ID[] =
X "Undos Rev 12-07-85 (C)Copyright Omen Technology Inc All Rights Reserved\n";
X/*
X * This program and documentation may be copied, used, or modified
X *  by Professional-YAM and POWERCOMM licensees provided these notices are
X * not removed.  Others may use this program for non-profit purposes only.
X */
X
X#include <stdio.h>
X#include <sys/types.h>
X#include <sys/stat.h>
X
X#define LL 1024
X#define SUB 032
X
Xchar Lbuf[LL];
Xchar *Progname;
Xint Todos = 0;
Xint Tocpm = 0;
Xint Tomac = 0;
Xint Unmac = 0;
Xint Strip = 0;
X
Xmain(argc, argv)
Xchar **argv;
X{
X	Progname = *argv;
X	if (! strcmp(Progname, "tocpm"))
X		Todos = Tocpm = 1;
X	if (! strcmp(Progname, "todos"))
X		Todos = 1;
X	if (! strcmp(Progname, "unmac"))
X		Unmac = 1;
X	if (! strcmp(Progname, "tomac"))
X		Tomac = 1;
X
X	if (! strcmp(argv[1], "-s")) {
X		++Strip; --argc; ++argv;
X	}
X
X
X	if (argc<2 || *argv[1]== '-')
X		usage();
X	while (--argc >= 1)
X		chngfmt(*++argv);
X	exit(0);
X}
Xusage()
X{
X	fprintf(stderr, ID);
X	fprintf(stderr, "Usage: {undos|tounix|todos|tocpm|unmac} [-s] file ...\n");
X	fprintf(stderr, "	-s Strip parity bit, ignore bytes < 007\n");
X	exit(1);
X}
X
X
Xchngfmt(name)
Xchar *name;
X{
X	register c;
X	register char *p;
X	register n;
X	register long fpos;
X	struct stat st;
X	FILE *fin, *fout;
X	int linno = 0;
X	long ftell();
X	char *mktemp();
X	char outnam[64];
X
X	if (stat(name, &st)) {
X		xperror(name); return;
X	}
X	if ((st.st_mode & S_IFMT) != S_IFREG) {
X		fprintf(stderr, "%s: %s is not a regular file\n", Progname, name);
X		return;
X	}
X	if ((fin = fopen(name, "r")) == NULL) {
X		xperror(name); return;
X	}
X	strcpy(outnam, "undosXXXXXX");
X	mktemp(outnam);
X	if ((fout = fopen(outnam, "w")) == NULL) {
X		xperror(outnam); exit(1);
X	}
X
X	for (;;) {
X		++linno;
X		for (p=Lbuf, n=LL; --n>0; ) {
Xignore:
X			if ((c = getc(fin)) == EOF)
X				break;
X			if ( !c)
X				goto ignore;
X			if (c < '\7' || (c & 0200)) {
X				if (Strip) {
X					if ((c &= 0177) < 7)
X						goto ignore;
X				} else
X					goto thisbin; 
X			}
X			if (c == SUB)
X				break;
X			if (c == '\r' && Unmac)
X				c = '\n';
X			*p++ = c;
X			if (c == '\n')
X				break;
X		}
X		*p = '\0';
X
X		if (n == 0) {
X	thisbin:
X			if (n) {
X				fprintf(stderr, "%s: %s is a binary file", Progname, name);
X				fprintf(stderr, " line=%d char =%2X\n", linno, c);
X			} else
X				fprintf(stderr, "%s: %s has no linefeeds: try unmac?\n", Progname, name);
X			fclose(fout);
X			unlink(outnam);
X			return;
X		}
X
X		if (Todos) {
X			if (*--p == '\n' && p[-1] != '\r') {
X				*p++ = '\r'; *p++ = '\n'; *p = 0;
X			}
X		} else if (Tomac) {
X			if (*--p == '\n') {
X				if (p[-1] == '\r')
X					--p;
X				*p++ = '\r'; *p = 0;
X			}
X		} else {
X			if (*--p == '\n' && *--p == '\r') {
X				*p++ = '\n'; *p = 0;
X			}
X		}
X		if (fputs(Lbuf, fout) == EOF) {
X			xperror(outnam); exit(1);
X		}
X		switch (c) {
X		case EOF:
X			if (ferror(fin)) {
X				xperror(name); exit(0200);
X			}
X		case SUB:
X			if (Tocpm) {
X				fpos = ftell(fout);
X				do {
X					putc(SUB, fout);
X				} while (++fpos & 127);
X			}
X			fclose(fout); fclose(fin);
X			if (st.st_nlink > 1) 
X				sprintf(Lbuf, "cp %s %s", outnam, name);
X			else
X				sprintf(Lbuf, "mv %s %s", outnam, name);
X			system(Lbuf);
X			utime(name, (struct utimbuf *) &st.st_atime);
X			if (st.st_nlink > 1) 
X				unlink(outnam);
X			return;
X		}
X	}
X}
X
Xxperror(s)
Xchar *s;
X{
X	register char *p;
X	extern int sys_nerr;
X	extern char *sys_errlist[];
X	extern errno;
X
X	if (errno >= sys_nerr)
X		p = "Gloryovsky: a New Error!";
X	else
X		p = sys_errlist[errno];
X	fprintf(stderr, "%s: %s: %s\n", Progname, s, p);
X}
END_OF_undos.c
if test 3544 -ne `wc -c <undos.c`; then
    echo shar: \"undos.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f undos.man -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"undos.man\"
else
echo shar: Extracting \"undos.man\" \(2326 characters\)
sed "s/^X//" >undos.man <<'END_OF_undos.man'
X
X
X
X                                  - 1 -
X
X
X
X       undos,tounix,todos,tocpm,tomac,unmac - Change ASCII file
X       format for target operating system undos [ -s ] file ...
X       tounix [ -s ] file ...
X       todos [ -s ] file ...
X       tocpm [ -s ] file ...
X       unmac [ -s ] file ...
X       tomac [ -s ] file ...  Undos and tounix convert DOS or CP/M
X       format source files to Unix format by deleting carriage
X       returns preceding linefeeds and eliminating characters
X       starting with CPMEOF (^Z).  Todos converts Unix format
X       source files to DOS format by adding a carriage return (if
X       not already present) before each linefeed, and eliminates
X       characters starting with CPMEOF (^Z).  Tocpm additionally
X       appends CPMEOF (^Z) characters to the resulting file to make
X       the file length a multiple of the 128 byte CP/M record
X       length.
X
X       Any combination of undos,todos, or tocpm (without flags) may
X       be applied to a proper ASCII file without destroying
X       information.  Lone carriage returns used to force
X       overprinting are not translated to CR/LF pairs.
X
X       Unmac converts files with lines terminated only by carriage
X       return to Unix format.  Unmac should only be used to
X       translate files whose lines are terminated by lone carriage
X       returns.
X
X       Tomac converts Unix format files to Macintosh format (lines
X       terminated by carriage return only).
X
X       The optional flag -s Strips the parity bit on all characters
X       and discards all resulting characters with values less than
X       7.
X
X       The access and modification times of the modified files are
X       set to those of the original files.  Refuses to translate
X       files in which "binary" characters (less than 7 or greater
X       than 127) are seen before CPMEOF.  Refuses to translate
X       files with ultra long lines.  Refuses to translate special
X       files.  Should be executed with the current directory in the
X       same filesystem as the target files for minimum disk i/o.
X       Does not detect short files without linefeeds.  Unmac and
X       tomac cannot handle files with CR-only overprinting.  (ASCII
X       allows either LF or CR/LF to delimit lines, but not CR.)
X       lar(1), yam(1), sq(1), usq(1), rb(omen), sb(omen)
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
END_OF_undos.man
if test 2326 -ne `wc -c <undos.man`; then
    echo shar: \"undos.man\" unpacked with wrong size!
fi
# end of overwriting check
fi
echo shar: End of shell archive.
exit 0