home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume9 / pc-mail-nfs / dosunix.c next >
C/C++ Source or Header  |  1989-11-26  |  3KB  |  120 lines

  1. /*++
  2. /* NAME
  3. /*    dosunix 3
  4. /* SUMMARY
  5. /*    UNIX <-> MS-DOS text format conversion
  6. /* PROJECT
  7. /*    pc-mail
  8. /* PACKAGE
  9. /*    nfs
  10. /* SYNOPSIS
  11. /*    #include <stdio.h>
  12. /*    #include "dosunix.h"
  13. /*
  14. /*    int dos2unix(ifp, ofp)
  15. /*    FILE *ifp;
  16. /*    FILE *ofp;
  17. /*
  18. /*    int unix2dos(ifp, ofp)
  19. /*    FILE *ifp;
  20. /*    FILE *ofp;
  21. /*
  22. /*    char *dosgets(buf, len, fp)
  23. /*    char *buf;
  24. /*    unsigned len;
  25. /*    FILE *fp;
  26. /* DESCRIPTION
  27. /*    dos2unix() converts an MS-DOS text stream (with cr/lf-delimited
  28. /*    lines) to a UNIX style stream (with lf-delimited lines).
  29. /*
  30. /*    unix2dos() performs the opposite function as dos2unix().
  31. /*
  32. /*    dosgets() reads one line from the designated stream and strips
  33. /*    off any cr of lf characters.
  34. /* DIAGNOSTICS
  35. /*    dos2unix(), unix2dos() return a nonzero value if an error was detected.
  36. /*
  37. /*    dosgets() returns (char *) 0 if it could not read any data.
  38. /* BUGS
  39. /*    Very long lines will be broken; Ctrl-Z in MS-DOS files is not
  40. /*    given special treatment, nor will it be added to the end of
  41. /*    MS-DOS files.
  42. /* AUTHOR(S)
  43. /*    Wietse Z. Venema
  44. /*    Eindhoven University of Technology
  45. /*    Department of Mathematics and Computer Science
  46. /*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  47. /* CREATION DATE
  48. /*    Sun Oct 29 16:41:50 MET 1989
  49. /* LAST MODIFICATION
  50. /*    10/29/89 22:30:00
  51. /* VERSION/RELEASE
  52. /*    1.1
  53. /*--*/
  54.  
  55. #ifndef lint
  56. static char sccsid[] = "@(#) dosunix.c 1.1 10/29/89 22:30:00";
  57.  
  58. #endif
  59.  
  60. #include <stdio.h>
  61. #include "dosunix.h"            /* consistency check */
  62.  
  63. extern char *strchr();
  64. extern char *fgets();
  65.  
  66. /* unix2dos - copy UNIX-format stream to MS-DOS-format stream */
  67.  
  68. int     unix2dos(ifp, ofp)
  69. FILE   *ifp;
  70. FILE   *ofp;
  71. {
  72.     static char buf[BUFSIZ];
  73.     register int end;
  74.  
  75.     while (fgets(buf, sizeof(buf), ifp)) {
  76.     if (buf[end = strlen(buf) - 1] == '\n') {
  77.         buf[end] = '\r';
  78.         (void) fputs(buf, ofp);
  79.         (void) putc('\n', ofp);
  80.     } else {
  81.         (void) fputs(buf, ofp);
  82.     }
  83.     }
  84.     return (fflush(ofp) || ferror(ofp) || feof(ofp));
  85. }
  86.  
  87. /* dos2unix - copy MS-DOS-format text stream to UNIX-format text stream */
  88.  
  89. int     dos2unix(dfp, pfp)
  90. register FILE *dfp;
  91. register FILE *pfp;
  92. {
  93.     static char msgbuf[BUFSIZ];        /* copy buffer */
  94.  
  95.     while (dosgets(msgbuf, sizeof(msgbuf), dfp)) {
  96.     (void) fputs(msgbuf, pfp);
  97.     (void) putc('\n', pfp);
  98.     }
  99.     return (fflush(pfp) || ferror(pfp) || feof(pfp));
  100. }
  101.  
  102. /* dosgets - read one line from DOS-format text stream; strip cr and lf */
  103.  
  104. char   *dosgets(buf, len, fp)
  105. register char *buf;
  106. unsigned len;
  107. register FILE *fp;
  108. {
  109.     register char *cp;
  110.     register char *ret;
  111.  
  112.     /* Lines with >= len characters will be broken */
  113.  
  114.     if ((ret = fgets(buf, len, fp))
  115.     && ((cp = strchr(buf, '\r')) || (cp = strchr(buf, '\n'))))
  116.     *cp = '\0';                /* strip cr or lf */
  117.     return (ret);
  118. }
  119.  
  120.