home *** CD-ROM | disk | FTP | other *** search
/ BUG 14 / BUGCD1998_05.ISO / internet / tcp4u / tcp4u330.exe / tcp4u.330 / u2d / u2d.c next >
C/C++ Source or Header  |  1998-03-05  |  3KB  |  86 lines

  1. /*
  2.  * u2d v 1.10         Last Revision 02/03/1998  1.10
  3.  *
  4.  *===========================================================================
  5.  *
  6.  * Project: u2d,      Unix To Dos converter
  7.  * File:    u2d.c
  8.  * Purpose: u2d source code. 
  9.  * Compilation vith microsoft C compiler:  
  10.  *         cl -O1 u2d.c setargv.obj 
  11.  *
  12.  *===========================================================================
  13.  * 
  14.  * This software is Copyright (c) 1996-1998 by Philippe Jounin
  15.  *
  16.  *  This program is free software; you can redistribute it and/or modify
  17.  *  it under the terms of the GNU General Public License as published by
  18.  *  the Free Software Foundation; either version 2 of the License, or
  19.  *  (at your option) any later version.
  20.  *  
  21.  *  This program is distributed in the hope that it will be useful,
  22.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  *  GNU General Public License for more details.
  25.  *  
  26.  *  You should have received a copy of the GNU General Public License
  27.  *  along with this program; if not, write to the Free Software
  28.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  *  
  30.  */   
  31.  
  32. #include <stdio.h>
  33.  
  34.  
  35. int Convert (char *szFile)
  36. {
  37. #define XX_RETURN(s,x) \
  38. {  printf ("%s\n", s) ; if (pFIn!=NULL) fclose(pFIn) ; \
  39.    if (pFOut!=NULL)   fclose(pFOut);                   \
  40.    if (TmpFile!=NULL) remove(TmpFile);                 \
  41.    return x;   }
  42.    
  43. char *TmpFile=NULL;
  44. int   c;
  45. FILE *pFIn=NULL, *pFOut=NULL;
  46.  
  47.    printf ("%s", szFile);  fflush (stdout);
  48.    TmpFile = tmpnam(NULL); 
  49.    if (TmpFile==NULL) XX_RETURN ("\tCan not create temporary file", -1);
  50.    pFOut = fopen (TmpFile, "w");
  51.    if (pFOut==NULL) XX_RETURN ("\tCan not create temporary file", -2);
  52.    pFIn = fopen (szFile, "r");
  53.    if (pFIn==NULL) XX_RETURN ("\tCan not open input file", -3);
  54.    
  55.    while ( (c=fgetc (pFIn)) != EOF)
  56.      { if (fputc (c, pFOut)==EOF)  XX_RETURN ("\tCan not write into output file", -4); }
  57.    if (!feof (pFIn))  XX_RETURN ("\tCan not read from input file", -5);
  58.    
  59.    fclose (pFIn); fclose (pFOut);
  60.    if (remove (szFile))  XX_RETURN ("\tCan not rename output file", -6);
  61.    if (rename (TmpFile, szFile)==-1)
  62.    { printf ("\tinput file %s removed, temp file %s can not be removed\n", 
  63.                  szFile, TmpFile);
  64.      return -8; }
  65.    printf ("\n");   
  66. return 0;
  67.    
  68. } /* Convert */
  69.  
  70.  
  71.  
  72. int main (int argc, char *argv[])
  73. {
  74.   if (argc<2) 
  75.     { 
  76.        printf ("Usage: u2d file [file [...]]\n"
  77.                "Translate a unix text file. Existing file is overwritten\n");
  78.     }
  79.   else
  80.   {int Ark;
  81.      for (Ark=1 ;  Ark<argc ; Ark++)
  82.         Convert (argv[Ark]);
  83.   }
  84. return 0;       
  85. } /* main */
  86.