home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari FTP
/
ATARI_FTP_0693.zip
/
ATARI_FTP_0693
/
Mint
/
mntlib32.zoo
/
crlf
/
crlf.c
next >
Wrap
C/C++ Source or Header
|
1993-02-23
|
5KB
|
297 lines
/* crlf.c 1.1 by entropy@terminator.rs.itd.umich.edu
PUBLIC DOMAIN -- NO RIGHTS RESERVED
NO WARRANTY -- USE AT YOUR OWN RISK!!!!!!!!!
strips/adds carriage returns from text files
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#ifdef __atarist__
#include <compiler.h>
#ifdef __TURBOC__
#include <sys\types.h>
#include <sys\stat.h>
#else /* not __TURBOC__ */
#include <sys/types.h>
#include <sys/stat.h>
#endif /* not __TURBOC__ */
#else /* not __atarist__ */
#include <sys/types.h>
#include <sys/stat.h>
#ifdef sun
#include <utime.h>
#endif /* sun */
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 2
#endif /* EXIT_FAILURE */
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif /* EXIT_SUCCESS */
#ifdef __STDC__
#ifndef __NO_PROTO__
#define __PROTO(x) x
#endif /* not __NO_PROTO__ */
#define __EXTERN
#else /* not __STDC__ */
#define __EXTERN extern
#define __PROTO(x) ()
#endif /* not __STDC__ */
#endif /* not __atarist__ */
__EXTERN int utime __PROTO((char *path, struct utimbuf *times));
extern char *optarg;
extern int optind, opterr;
#define CR_STRIP 0
#define CR_ADD 1
#define CR_NONE 2
char *program = NULL;
void perror2 __PROTO((char *msg1, char *msg2));
int crlf __PROTO((char *filename, int mode));
int simple_outchar __PROTO((FILE *ofp, int c));
int tounx_outchar __PROTO((FILE *ofp, int c));
int totos_outchar __PROTO((FILE *ofp, int c));
int smart_copy __PROTO((char *ifn, char *ofn,
int (*outcharfunc)(FILE *, int)));
void
perror2(msg1, msg2)
char *msg1;
char *msg2;
{
if (msg1 && *msg1)
{
fputs(msg1, stderr);
fputs(": ", stderr);
}
perror(msg2);
}
int
simple_outchar(ofp, c)
FILE *ofp;
int c;
{
fputc(c, ofp);
return (c);
}
int
totos_outchar(ofp, c)
FILE *ofp;
int c;
{
if (c == '\n')
{
fputc('\r', ofp);
}
fputc(c, ofp);
return (c);
}
int
tounx_outchar(ofp, c)
FILE *ofp;
int c;
{
if (c != '\r')
{
fputc(c, ofp);
}
return (c);
}
int
smart_copy(ifn, ofn, outcharfunc)
char *ifn;
char *ofn;
int (*outcharfunc)(FILE *, int);
{
FILE *ifp;
FILE *ofp;
int c;
if ((ifp = fopen(ifn, "rb")) == NULL)
{
perror2(program, ifn);
return (-1);
}
if ((ofp = fopen(ofn, "wb")) == NULL)
{
fclose(ifp);
perror2(program, ofn);
return (-1);
}
c = fgetc(ifp);
while (!(feof(ifp)) && !(ferror(ifp)) && !(ferror(ofp)))
{
(*outcharfunc)(ofp, c);
c = fgetc(ifp);
}
if (ferror(ifp))
{
perror2(program, ifn);
fclose(ifp);
fclose(ofp);
return (-1);
}
if (ferror(ofp))
{
perror2(program, ofn);
fclose(ifp);
fclose(ofp);
return (-1);
}
if (fclose(ifp) == EOF)
{
perror2(program, ifn);
fclose(ofp);
return (-1);
}
if (fclose(ofp) == EOF)
{
perror2(program, ofn);
return (-1);
}
return (0);
}
int
crlf(filename, mode)
char *filename;
int mode; /* 0 == strip CR's, 1 == add CR's */
{
char tempname[256];
int (*outcharfunc)(FILE *, int);
strcpy(tempname, "/tmp/crlfXXXXXX");
if (mktemp(tempname) == NULL)
{
fputs(program, stderr);
fputs(": could not get a temporary filename\n", stderr);
return (-1);
}
switch (mode)
{
case CR_ADD:
outcharfunc = totos_outchar;
break;
case CR_STRIP:
default:
outcharfunc = tounx_outchar;
break;
}
if (smart_copy(filename, tempname, outcharfunc))
{
return (-1);
}
if (rename(tempname, filename))
{
if (smart_copy(tempname, filename, simple_outchar))
{
return (-1);
}
if (unlink(tempname))
{
perror2(program, tempname);
return (-1);
}
}
return (0);
}
int
main(int argc, char *argv[], char *envp[])
{
char *fn = NULL;
int mode = CR_NONE;
int c;
int err = 0;
struct stat st;
struct utimbuf ut;
program = argv[0];
while ((c = getopt(argc, argv, "as")) != EOF)
{
switch (c)
{
case 'a':
if (mode == CR_NONE)
{
mode = CR_ADD;
}
else
{
err++;
}
break;
case 's':
if (mode == CR_NONE)
{
mode = CR_STRIP;
}
else
{
err++;
}
break;
case '?':
default:
err++;
break;
}
}
if (mode == CR_NONE)
{
err++;
}
if (err || (optind >= argc))
{
fputs("usage: ", stderr);
fputs(program, stderr);
fputs(" -s file [file2 [...]] (to strip carriage returns)\n", stderr);
fputs(" ", stderr);
fputs(program, stderr);
fputs(" -a file [file2 [...]] (to add carriage returns)\n", stderr);
exit(EXIT_FAILURE);
}
for ( ; optind < argc; optind++)
{
fn = argv[optind];
if (access(fn, 4))
{
err++;
perror2(program, fn);
continue;
}
if (stat(fn, &st))
{
err++;
perror2(program, fn);
}
ut.actime = st.st_atime;
ut.modtime = st.st_mtime;
if (crlf(fn, mode) != 0)
{
err++;
continue;
}
if (utime(fn, &ut))
{
err++;
perror2(program, fn);
}
}
if (err)
{
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
return (0);
}