home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / lan / soss.arj / D2X / D2X.C < prev    next >
C/C++ Source or Header  |  1991-04-10  |  4KB  |  182 lines

  1. /* DOS-to-Unix / Unix-to-DOS text file translation utility.     */
  2. /*  This program takes parameters the same way as 'copy', except */
  3. /*  that if no parameters are given it acts as a filter between  */
  4. /*  stdin and stdout.                         */
  5.  
  6. /* Written by Richard Braun @ Kronos, 19 March 1991.         */
  7. /*   Comments and bugs to rbraun@spdcc.com.             */
  8.  
  9. #include <stdio.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <string.h>
  13. #ifdef MSDOS
  14. #include <sys/utime.h>
  15. #else
  16. #include <utime.h>
  17. #endif
  18.  
  19. void usage (char *);
  20. void addCR (char *, FILE *, FILE *);
  21. void removeCR (char *, FILE *, FILE *);
  22.  
  23. extern int errno;
  24.  
  25. main (argc, argv)
  26. int argc;
  27. char *argv[];
  28. {
  29.     FILE *fpin, *fpout, *fopen();
  30.     struct stat statbuf;
  31.     struct utimbuf timebuf;
  32.     char outdir[200];
  33.     char **cur_file;
  34.  
  35.     outdir[0] = '\0';
  36.     fpout = (argc == 1) ? stdout : NULL;
  37.     fpin  = (argc == 1) ? stdin  : NULL;
  38.  
  39.     /* Get output directory, if given */
  40.  
  41.     if (argc >= 3) {
  42.     if (stat (argv[argc-1], &statbuf) == 0 &&
  43.         statbuf.st_mode & S_IFDIR) {
  44.         strcpy (outdir, argv[argc-1]);
  45.         argc--;
  46.         }
  47.     }
  48.     else if (argc == 2) {
  49.     strcpy (outdir, ".");
  50.     }
  51.     cur_file = &argv[1];
  52.     for (;;) {
  53.     char outfile [200];
  54.  
  55.     if (fpin != stdin && (fpin = fopen (*cur_file, "r")) == NULL) {
  56.         fprintf (stderr, "%s: couldn't open input file '%s'\n",
  57.              argv[0], *cur_file);
  58.         usage (argv[0]);
  59.     }
  60.  
  61.     if (fpout != stdout) {
  62.         struct stat instat, outstat;
  63.  
  64.         if (outdir[0]) {
  65.         char *ptr;
  66.  
  67.         strcpy (outfile, outdir);
  68.         strcat (outfile, "/");
  69.         strcat (outfile, (ptr = strrchr (*cur_file, '/')) ? ptr+1 :
  70.             *cur_file);
  71.         }
  72.         else
  73.           strcpy (outfile, argv[2]);
  74.  
  75.         /* Check to make sure we're not overwriting a file */
  76.         if (stat (*cur_file, &instat) == 0 &&
  77.         stat (outfile, &outstat) == 0 &&
  78.         outstat.st_dev == instat.st_dev &&
  79.         outstat.st_ino == instat.st_ino) {
  80.         fprintf (stderr, "%s error: input and output filespecs (%s) identical\n",
  81.              argv[0], *cur_file);
  82.         usage (argv[0]);
  83.         }
  84.  
  85.         /* Skip any directories in the input list, to make     */
  86.         /* "d2x * outdir" behave as expected.         */
  87.  
  88.         if (instat.st_mode & S_IFDIR) {
  89.         fclose (fpin);
  90.         if (outdir[0] == '\0' || --argc == 1)
  91.           break;
  92.         cur_file++;
  93.         continue;
  94.         }
  95.  
  96.         if ((fpout = fopen (outfile, "wb")) == NULL) {
  97.         fprintf (stderr, "%s: couldn't open output file '%s'\n",
  98.              argv[0], argv[2]);
  99.         usage (argv[0]);
  100.         }
  101.  
  102.         /* Preserve the modification time of the input file. */
  103.         timebuf.modtime = instat.st_mtime;
  104.     }
  105.  
  106.     removeCR (argv[0], fpin, fpout);
  107.     fclose (fpin);
  108.     fclose (fpout);
  109.  
  110.     /* Set the file's access time to now and the modification time  */
  111.     /* to that of the input file.                    */
  112.     time (&timebuf.actime);
  113.     (void) utime (outfile, &timebuf);
  114.  
  115.     if (outdir[0] == '\0' || --argc == 1)
  116.       break;
  117.     cur_file++;
  118.     }
  119.     exit (1);
  120. }
  121.  
  122.  
  123. void usage (program)
  124. char *program;
  125. {
  126.     fprintf (stderr, "Usage:  %s [infile [outfile]]\n", program);
  127.     fprintf (stderr, "        %s infile1 ... infileN outdir\n", program);
  128.     exit (1);
  129. }
  130.  
  131.  
  132. void addCR (program, fpin, fpout)
  133. char *program;
  134. FILE *fpin, *fpout;
  135. {
  136.     char str[500];
  137.  
  138.     /* Run a loop, appending "\r\n" to each line. */
  139.  
  140.     while (fgets (str, sizeof(str) - 1, fpin) != NULL) {
  141.     int len;
  142.  
  143.     len = strlen (str);
  144.     if (str[len-1] == '\n') {
  145.         str[len-1] = '\r';
  146.         str[len] = '\n';
  147.         str[len+1] = '\0';
  148.     }
  149.     if (fputs (str, fpout) == EOF) {
  150.         fprintf (stderr, "%s: error writing output\n", program);
  151.         usage (program);
  152.     }
  153.     }
  154. }
  155.  
  156.  
  157. void removeCR (program, fpin, fpout)
  158. char *program;
  159. FILE *fpin, *fpout;
  160. {
  161.     char str[500];
  162.  
  163.     /* Run a loop, stripping \r and ^Z characters */
  164.  
  165.     while (fgets (str, sizeof str, fpin) != NULL) {
  166.     char *ptrin, *ptrout;
  167.  
  168.     ptrin = str; ptrout = str;
  169.     while (*ptrin) {
  170.         if (*ptrin != '\032' && *ptrin != '\r')
  171.           *ptrout++ = *ptrin++;
  172.         else
  173.           ptrin++;
  174.     }
  175.     *ptrout = '\0';
  176.     if (fputs (str, fpout) == EOF) {
  177.         fprintf (stderr, "%s: error writing output\n", program);
  178.         usage (program);
  179.     }
  180.     }
  181. }
  182.