www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1999/04/15/04:45:47

Message-ID: <B0000083442@stargate.astr.lu.lv>
From: "Andris Pavenis" <pavenis AT lanet DOT lv>
To: djgpp-workers AT delorie DOT com
Date: Thu, 15 Apr 1999 11:43:46 +0300
MIME-Version: 1.0
Subject: Update to dtou.c
X-mailer: Pegasus Mail for Win32 (v3.02b14)
Reply-To: djgpp-workers AT delorie DOT com

--Message-Boundary-27839
Content-type: text/plain; charset=US-ASCII
Content-transfer-encoding: 7BIT
Content-description: Mail message body

Hi!

There are some possible problems with current version of dtou.c:

	- if writting output file is impossible due to some problem (eg. not enough
         space on disk) we may lose original file that is not nice. Therefore a check
         of results of write() must be done.

       - I would like to have the same utility also on other systems (usefull for poeple
         who are using cross-compilers to build DJGPP apps). There are some
         but it would be nice to be able to run the same shell scripts written for
         DJGPP. Therefore source should be cleaned from DOS only stuff.

Below is updated version of dtou.c where this is done. I tested it with 
DJGPP (of course), in Linux and on RS6000 (AIX-4.3.1). The same changes 
should be done also for utod.c  and may be some other utils which I didn't touch 
yet.

Andris



--Message-Boundary-27839
Content-type: text/plain; charset=US-ASCII
Content-disposition: inline
Content-description: Attachment information.

The following section of this message contains a file attachment
prepared for transmission using the Internet MIME message format.
If you are using Pegasus Mail, or any another MIME-compliant system,
you should be able to save it or view it from within your mailer.
If you cannot, please ask your system administrator for assistance.

   ---- File information -----------
     File:  DTOU.C
     Date:  15 Apr 1999, 11:39
     Size:  1529 bytes.
     Type:  Text

--Message-Boundary-27839
Content-type: Application/Octet-stream; name="DTOU.C"; type=Text
Content-disposition: attachment; filename="DTOU.C"

/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <utime.h>

#ifndef O_BINARY
#define O_BINARY 0
#endif

static int
dtou(char *fname)
{
  int i, k, k2, sf, df, l, l2=0, err=0;
  char buf[16384];
  char tfname[FILENAME_MAX], *bn, *w;
  struct stat st;
  struct utimbuf tim1;
  sf = open(fname, O_RDONLY|O_BINARY);
  if (sf < 1)
  {
    perror(fname);
    return 1;
  }
  
  fstat (sf,&st);
  tim1.actime = st.st_atime;
  tim1.modtime = st.st_mtime;

  strcpy (tfname, fname);
  for (bn=w=tfname; *w; w++) 
    if (*w=='/' || *w=='\\') 
      bn = w+1;  
  if (bn) *bn=0;
  strcat (tfname,"utod.tm$");
  
  df = open(tfname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
  if (df < 1)
  {
    perror(tfname);
    close(sf);
    return 1;
  }

  k2=0;
  while ((l=read(sf, buf, 16384)) > 0)
  { 
    for (i=k=0; i<l; i++) 
      if (buf[i]!=13)
        buf[k++]=buf[i];
    l2=write(df, buf, k);
    if (l2<0) break;
    if (l2!=k) { err=1; break; }
  }

  if (l<0) perror (fname);
  if (l2<0) perror (tfname);
  if (err) fprintf (stderr,"Cannot process file %s\n",fname);

  close(sf);
  close(df);

  if (l>=0 && l2>=0 && err==0)
  {
    remove(fname);
    rename(tfname, fname);
    utime(fname, &tim1);
  }
  else 
  {
    remove(tfname);
  }
  return 0;
}

int
main(int argc, char **argv)
{
  int rv = 0;
  for (argc--, argv++; argc; argc--, argv++)
    rv += dtou(*argv);
  return rv;
}

--Message-Boundary-27839--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019