home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3.4.17 [SPARC, PA-RISC] / nextstep33_risc.iso / usr / netware / lib / nuc / etc / dos2unix.c next >
Encoding:
C/C++ Source or Header  |  1993-07-13  |  2.8 KB  |  136 lines

  1.  
  2. /*
  3.  
  4. $Header: dos2unix.c
  5.  
  6. $Project: <NFS for Netware/386>, <DOS to UNIX translation util - DOS version>
  7.  
  8. $Creator: <Wen H. Chiu>    $
  9.  
  10. $Locker:  $
  11.  
  12. $Source: /proj/nwnfs2/REL1.2b/s_xlat/dos/RCS/dos2unix.c,v $
  13.  
  14.  
  15.  *****************************************************************************
  16.  *
  17.  *    (C) Copyright 1989, 1990 Novell, Inc.
  18.  *    All Rights Reserved.
  19.  *
  20.  *    This program is an unpublished copyrighted work which is proprietary
  21.  *    to Novell, Inc. and contains confidential information that is not
  22.  *    to be reproduced or disclosed to any other person or entity without
  23.  *    prior written consent from Novell, Inc. in each and every instance.
  24.  *
  25.  *    WARNING:  Unauthorized reproduction of this program as well as
  26.  *    unauthorized preparation of derivative works based upon the
  27.  *    program or distribution of copies by sale, rental, lease or
  28.  *    lending are violations of federal copyright laws and state trade
  29.  *    secret laws, punishable by civil and criminal penalties.
  30.  *
  31.  ****************************************************************************
  32.  
  33. */
  34.  
  35. #include <stdio.h>
  36. #include <fcntl.h>
  37.  
  38. #define START 0
  39. #define CRFOUND 1
  40.  
  41. #define RBYTES 4096
  42.  
  43. char CR = '\r';
  44. char rbuf[RBYTES];
  45. FILE *rfp;
  46.  
  47. print_argmsg()
  48. {
  49.     printf("\n  Usage: dos2unix source_file [ > dest_file]\n");
  50.     exit(-1);
  51. }
  52.  
  53. print_rmsg(file)
  54. char *file;
  55. {
  56.     printf("read error from source file %s\n", file);
  57.     fclose(rfp);
  58.     exit(-1);
  59. }
  60.  
  61. print_wmsg(file)
  62. char *file;
  63. {
  64.     printf("write error to destination file %s\n", file);
  65.     fclose(rfp);
  66.     exit(-1);
  67. }
  68.  
  69. main(argc, argv)
  70. int argc;
  71. char *argv[];
  72. {
  73.     int rlen, len, i;
  74.     int state = START;
  75.     char c;
  76.  
  77.     if (argc < 2)
  78.         print_argmsg();
  79.  
  80.     if (!(rfp=fopen(argv[1], "rb"))) {
  81.         printf("can't open source file %s\n", argv[1]);
  82.         exit(-1);
  83.     }
  84. #ifdef ON_DOS
  85.     /* set mode of stdout to binary */
  86.     if ((setmode(fileno(stdout), O_BINARY)) == 1) {
  87.         printf("can't set stdout to binary mode\n");
  88.         exit(-1);
  89.     }
  90. #endif
  91.  
  92.     do {
  93.         if ((rlen=fread(rbuf, 1, RBYTES, rfp)) == -1)
  94.             print_rmsg(argv[1]);
  95.  
  96.         if (rlen == 0)
  97.             break;
  98.  
  99.         len = rlen;
  100.         i = 0;
  101.         while (len--) {
  102.             c = rbuf[i++];
  103.             switch (c) {
  104.                 case '\r':
  105.                     if (state == CRFOUND)
  106.                         if (fwrite(&CR, 1, 1, stdout) < 1)
  107.                             print_wmsg(argv[2]);
  108.                     state = CRFOUND; 
  109.                     break;
  110.                 case '\n':
  111.                     if (state == CRFOUND) {
  112.                         if (fwrite(&c, 1, 1, stdout) < 1)
  113.                             print_wmsg(argv[2]);
  114.                         state = START;
  115.                     }
  116.                     break;
  117.                 default:
  118.                     if (state == CRFOUND) {
  119.                         state = START;
  120.                         if (fwrite(&CR, 1, 1, stdout) < 1)
  121.                             print_wmsg(argv[2]);
  122.                     }
  123.                     if (c == 0x1a)
  124.                         goto eof;
  125.                     if (fwrite(&c, 1, 1, stdout) < 1)
  126.                         print_wmsg(argv[2]);
  127.                     break;
  128.             };
  129.         };
  130.     } while (rlen == RBYTES);
  131.     
  132. eof:
  133.     fclose(rfp);
  134.     exit(0);
  135. }
  136.