TIP: Click on subject to list as thread! ANSI
echo: muffin
to: Roy J. Tellason
from: andrew clarke
date: 2003-06-13 15:00:48
subject: Maximus at UNIX

Thu 2003-06-12 22:12, Wes Garland (1:106/2000) wrote to Roy J. Tellason:

 WG> #! /bin/sh
 WG>  
 WG> for file in /dir/filespec.*
 WG> do
 WG>   dos2unix  ${file}.tmp
 WG>   mv -f ${file}.tmp ${file}
 WG> done

dos2unix does the temporary file creation itself, so you should be able to just run:

dos2unix /dir/filespec/*

Unless you don't have dos2unix (*) then you could use:

#!/bin/sh
for file in /dir/filespec.*
do
  tr -d "\r\032"  ${file}.tmp
  mv -f ${file}.tmp ${file}
done

Or if you prefer csh:

#!/bin/csh
foreach file (/dir/filespec.*)
  tr -d "\r\032"  ${file}.tmp
  mv -f ${file}.tmp ${file}
end

I don't think this will work very well with filenames with spaces in them
though.  dos2unix is probably the better option.


(*) Now you do.  :-)

/*
 *  dos2unix.c
 *
 *  A utility to convert text files from DOS to UNIX format.  Bytes values
 *  of 0x0d (CR) and 0x1a (EOF or Ctrl+Z) are stripped.
 *
 *  Copyright (c) December 1998 Clem Dye 
 *  Copyright (c) June 2003 Andrew Clarke 
 */

/*
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/*
 *  The author of the original source code is unknown.
 *
 *  Modified in December 1998 by Clem Dye to compile cleanly using Microsoft
 *  Visual C.  Added exit(1) statements in main() to improve error reporting
 *  when the program is used in batch scripts.
 *
 *  Modified in June 2003 by Andrew Clarke  to compile
 *  cleanly with several other compilers.  General code clean-up and better
 *  error handling.
 */

#define PROGNAME "dos2unix"

#include 
#include 
#include 
#include 
#include 
#include 

#ifdef __GNUC__
#include 
#include 
#else
#include 
#include 
#endif

static struct stat s_buf;

static int dos2u(char *path)
{
    FILE *ifp, *ofp;
    int ch, rval;
    char temppath[250];
    struct utimbuf ut_buf;

    strcpy(temppath, "./clntmp");
    strcat(temppath, "XXXXXX");
    mktemp(temppath);

    ifp = fopen(path, "rb");

    if (ifp == NULL)
    {
        perror(path);
        return EXIT_FAILURE;
    }

    ofp = fopen(temppath, "wb");

    if (ofp == NULL)
    {
        perror(temppath);
        fclose(ifp);
        return EXIT_FAILURE;
    }

    rval = EXIT_SUCCESS;

#define CR 0x0d
#define CZ 0x1a

    ch = getc(ifp);

    while (ch != EOF)
    {
        if (ch != CR && ch != CZ)
        {
            if (putc(ch, ofp) == EOF)
            {
                perror(temppath);
                rval = EXIT_FAILURE;
                break;
            }
        }

        ch = getc(ifp);
    }

    if (fclose(ifp) != 0)
    {
        perror(path);
        rval = EXIT_FAILURE;
    }

    if (fclose(ofp) != 0)
    {
        perror(temppath);
        rval = EXIT_FAILURE;
    }

    ut_buf.actime = s_buf.st_atime;
    ut_buf.modtime = s_buf.st_mtime;

    if (utime(temppath, &ut_buf) != 0)
    {
        perror(temppath);
        rval = EXIT_FAILURE;
    }

    if (remove(path) != 0)
    {
        perror(path);
        rval = EXIT_FAILURE;
    }

    if (rval != EXIT_SUCCESS)
    {
        remove(temppath);
        return rval;
    }

    if (rename(temppath, path) != 0)
    {
        fprintf(stderr, PROGNAME ": Problems renaming '%s' to '%s':
%s\n", temppath, path, strerror(errno));
        fprintf(stderr, "          However, file '%s'
remains.\n", temppath);
        exit(EXIT_FAILURE);
    }

    remove(temppath);

    return rval;
}

int main(int argc, char **argv)
{
    char *path;

    while (--argc > 0)
    {
        path = *++argv;

        if (stat(path, &s_buf) != 0)
        {
            fprintf(stderr, PROGNAME ": Can't stat '%s': %s\n",
path, strerror(errno));
            return EXIT_FAILURE;
        }

        printf(PROGNAME ": Processing file '%s' ...\n", path);

        if (dos2u(path) != EXIT_SUCCESS)
        {
            fprintf(stderr, PROGNAME ": Problems processing file
'%s'.\n", path);
            return EXIT_FAILURE;
        }
    }

    return EXIT_SUCCESS;
}


/*
 *  unix2dos.c
 *
 *  A utility to convert text files from UNIX to DOS format.
 *  Replaces 0x0a (LF) with 0x0a 0x0d (CR LF) sequence.  Existing
 *  CR LF sequences are left alone.
 *
 *  Copyright (c) December 1998 Clem Dye 
 *  Copyright (c) August 2000 Johannes Herzig 
 *  Copyright (c) June 2003 Andrew Clarke 
 */

/*
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/*
 *  The author of the original source code is unknown.
 *
 *  Modified in December 1998 by Clem Dye to compile cleanly using Microsoft
 *  Visual C.  Added exit(1) statements in main() to improve error reporting
 *  when the program is used in batch scripts.
 *
 *  Modified in August 2000 by Johannes Herzig.  Decoding loop modified to
 *  add CR only once in the situation that unix2dos finds CR LF combination
 *  in files with a mix of DOS and UNIX text file formats.
 *
 *  Modified in June 2003 by Andrew Clarke  to compile
 *  cleanly with several other compilers.  General code clean-up and better
 *  error handling.
 */

#define PROGNAME "unix2dos"

#include 
#include 
#include 
#include 
#include 
#include 

#ifdef __GNUC__
#include 
#include 
#else
#include 
#include 
#endif

static struct stat s_buf;

static int u2dos(char *path)
{
    FILE *ifp, *ofp;
    int ch, prev_ch, rval;
    char temppath[250];
    struct utimbuf ut_buf;

    strcpy(temppath, "./clntmp");
    strcat(temppath, "XXXXXX");
    mktemp(temppath);

    ifp = fopen(path, "rb");

    if (ifp == NULL)
    {
        perror(path);
        return EXIT_FAILURE;
    }

    ofp = fopen(temppath, "wb");

    if (ofp == NULL)
    {
        perror(temppath);
        fclose(ifp);
        return EXIT_FAILURE;
    }

    rval = EXIT_SUCCESS;

#define LF 0x0a
#define CR 0x0d

    prev_ch = '\0';

    ch = getc(ifp);

    while (ch != EOF)
    {
        if (ch == LF && prev_ch != CR)
        {
            if (putc(CR, ofp) == EOF)
            {
                perror(temppath);
                rval = EXIT_FAILURE;
                break;
            }
        }

        if (putc(ch, ofp) == EOF)
        {
            perror(temppath);
            rval = EXIT_FAILURE;
            break;
        }

        prev_ch = ch;

        ch = getc(ifp);
    }

    if (fclose(ifp) != 0)
    {
        perror(path);
        rval = EXIT_FAILURE;
    }

    if (fclose(ofp) != 0)
    {
        perror(temppath);
        rval = EXIT_FAILURE;
    }

    ut_buf.actime = s_buf.st_atime;
    ut_buf.modtime = s_buf.st_mtime;

    if (utime(temppath, &ut_buf) != 0)
    {
        perror(temppath);
        rval = EXIT_FAILURE;
    }

    if (remove(path) != 0)
    {
        perror(path);
        rval = EXIT_FAILURE;
    }

    if (rval != EXIT_SUCCESS)
    {
        remove(temppath);
        return EXIT_FAILURE;
    }

    if (rename(temppath, path) != 0)
    {
        fprintf(stderr, PROGNAME ": Problems renaming '%s' to '%s':
%s\n", temppath, path, strerror(errno));
        fprintf(stderr, "          However, file '%s'
remains.\n", temppath);
        exit(EXIT_FAILURE);
    }

    remove(temppath);

    return rval;
}

int main(int argc, char **argv)
{
    char *path;

    while (--argc > 0)
    {
        path = *++argv;

        if (stat(path, &s_buf) != 0)
        {
            fprintf(stderr, PROGNAME ": Can't stat '%s': %s\n",
path, strerror(errno));
            return EXIT_FAILURE;
        }

        printf(PROGNAME ": Processing file '%s' ...\n", path);

        if (u2dos(path) != EXIT_SUCCESS)
        {
            fprintf(stderr, PROGNAME ": Problems processing file
'%s'.\n", path);
            return EXIT_FAILURE;
        }
    }

    return EXIT_SUCCESS;
}

-- mail{at}ozzmosis.com

--- timEd/FreeBSD 1.11.b1
* Origin: Blizzard of Ozz, Mt Eliza, Melbourne, Australia (3:633/267)
SEEN-BY: 633/267 270
@PATH: 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.