home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 1 / FFMCD01.bin / bbs / libdisks / d700t799 / disk774.lha / ExtraCmds / src / Common.c next >
Encoding:
C/C++ Source or Header  |  1992-12-05  |  6.5 KB  |  254 lines

  1. /*
  2.  *
  3.  * Version 37.2 =TP= 18-Feb-92
  4.  * Compile with SAS/C 5.10 and link without startup code:
  5.  *      lc -cqfist -v -b0 -rr -O -ms Common
  6.  *      blink Common.o to Common sd sc
  7.  *      protect Common +p
  8.  *
  9.  * Copyright (c) 1992 Torsten Poulin
  10.  *
  11.  * Torsten Poulin
  12.  * Banebrinken 99, 2, lejlighed 77
  13.  * DK 2400  København NV
  14.  * DENMARK
  15.  */
  16.  
  17. /****** English:COMMON ****************************************************
  18. *
  19. *   FORMAT
  20. *       COMMON [FILE1] <file1> [[FILE2] <file2>] [-1] [-2] [-3]
  21. *
  22. *   TEMPLATE
  23. *       FILE1/A,FILE2,-1/S,-2/S,-3/S
  24. *
  25. *   PURPOSE
  26. *       To select or reject lines common to two sorted files.
  27. *
  28. *   SPECIFICATION
  29. *       The command Common reads <file1> and <file2>, which should be
  30. *       ordered in the collating sequence of SORT, and produces a
  31. *       three-column output: lines only in <file1>; lines only in
  32. *       <file2>; and lines in both files.
  33. *
  34. *       Switches -1, -2, or -3 suppress printing of the corresponding
  35. *       column.  Thus Common -1 -2 prints only the lines common to
  36. *       the two files; Common -2 -3 prints only the lines in the first
  37. *       file but not in the second; Common -1 -2 -3 is a no-op.
  38. *
  39. *   SEE ALSO
  40. *       SORT, UNIQUE
  41. *
  42. *   UNIX EQUIVALENT
  43. *       comm(BU_CMD)
  44. *
  45. ***************************************************************************
  46. *
  47. */
  48. /****** dansk:COMMON ******************************************************
  49. *
  50. *   FORMAT
  51. *       COMMON [FILE1] <fil1> [[FILE2] <fil2>] [-1] [-2] [-3]
  52. *
  53. *   SKABELON
  54. *       FILE1/A,FILE2,-1/S,-2/S,-3/S
  55. *
  56. *   FORMÅL
  57. *       At vælge eller fravælge linjer fælles for to sorterede filer.
  58. *
  59. *   SPECIFIKATION
  60. *       Kommandoen Common læser <fil1> og <fil2>, som skal være
  61. *       ordnet i rangfølgen brugt af SORT, og udskriver tre spalter:
  62. *       linjer der kun findes i <fil1>, linjer der kun findes i
  63. *       <fil2> og linjer der findes i begge filer.
  64. *
  65. *       Kontakterne -1, -2 og -3 undertrykker udskrivningen af de
  66. *       tilsvarende spalter.  Således udskriver Common -1 -2 kun de
  67. *       linjer som er fælles for de to filer; Common -2 -3 udskriver
  68. *       kun linjer som findes i den første fil, men ikke i den anden.
  69. *       Common -1 -2 -3 udskriver ingenting.
  70. *
  71. *   SE OGSÅ
  72. *       SORT, UNIQUE
  73. *
  74. *   TILSVARENDE I UNIX
  75. *       comm(BU_CMD)
  76. *
  77. ***************************************************************************
  78. *
  79. */
  80.  
  81.  
  82. #include <exec/types.h>
  83. #include <exec/libraries.h>
  84. #include <dos/dos.h>
  85. #include <dos/dostags.h>
  86. #include <proto/exec.h>
  87. #include <proto/dos.h>
  88. #include <string.h>
  89.  
  90. #define MAXLEN (512+1)
  91. #define TEMPLATE "FILE1/A,FILE2,-1/S,-2/S,-3/S"
  92. #define OPT_FILE1 0
  93. #define OPT_FILE2 1
  94. #define OPT_NOT1  2
  95. #define OPT_NOT2  3
  96. #define OPT_NOT3  4
  97.  
  98. char const *version = "\0$VER: Common 37.2 (18.2.92)\
  99.  ©1990,92 Torsten Poulin";
  100.  
  101. LONG Common(struct Library *SysBase, struct DosLibrary *DOSBase,
  102.             BPTR f1, BPTR f2, BOOL print1, BOOL print2, BOOL print3);
  103.  
  104.  
  105. LONG entrypoint(VOID)
  106. {
  107.     struct RDArgs     *args;
  108.     struct DosLibrary *DOSBase;
  109.     struct Library    *SysBase;
  110.     LONG  arg[5];
  111.     LONG  rc = RETURN_OK;
  112.     BPTR  f1, f2;
  113.     UBYTE *name1, *name2;
  114.     BOOL  print1, print2, print3;
  115.     
  116.     SysBase = *(struct Library **) 4L;
  117.     if(!(DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37L)))
  118.         return RETURN_FAIL;
  119.  
  120.     arg[OPT_FILE1] = arg[OPT_FILE2] = 0L;
  121.     arg[OPT_NOT1] = arg[OPT_NOT2] = arg[OPT_NOT3] = 0L;
  122.     
  123.     if(args = ReadArgs(TEMPLATE, arg, NULL))
  124.     {
  125.         name1 = (UBYTE *) arg[OPT_FILE1];
  126.         name2 = (UBYTE *) arg[OPT_FILE2];
  127.  
  128.         print1 = ! (BOOL) arg[OPT_NOT1];
  129.         print2 = ! (BOOL) arg[OPT_NOT2];
  130.         print3 = ! (BOOL) arg[OPT_NOT3];
  131.         
  132.         if(!(f1 = Open(name1, MODE_OLDFILE)))
  133.         {
  134.             LONG err = IoErr();
  135.             PrintFault(err, name1);
  136.             rc = RETURN_ERROR;
  137.         }
  138.         
  139.         if(!arg[OPT_FILE2])
  140.             f2 = Input();
  141.         else if(!(f2 = Open(name2, MODE_OLDFILE)))
  142.         {
  143.             LONG err = IoErr();
  144.             PrintFault(err, name2);
  145.             Close(f1);
  146.             rc = RETURN_ERROR;
  147.         }
  148.         
  149.         if(f1 && f2)
  150.         {
  151.             rc = Common(SysBase, DOSBase, f1, f2, print1, print2, print3);
  152.             Close(f1);
  153.             if(arg[OPT_FILE2])
  154.                 Close(f2);
  155.         }
  156.         FreeArgs(args);
  157.     }
  158.     else
  159.     {
  160.         LONG err = IoErr();
  161.         PrintFault(err, "Common");
  162.         rc = RETURN_ERROR;
  163.     }
  164.     CloseLibrary((struct Library *) DOSBase);
  165.     return rc;
  166. }
  167.  
  168.  
  169. LONG Common(struct Library *SysBase, struct DosLibrary *DOSBase,
  170.             BPTR f1, BPTR f2, BOOL print1, BOOL print2, BOOL print3)
  171. {
  172.     UBYTE *stat1, *stat2;
  173.     UBYTE line1[MAXLEN], line2[MAXLEN];
  174.     LONG  cmp_result;
  175.     LONG  err;
  176.     BPTR  out = Output();
  177.     
  178.     stat1 = FGets(f1, line1, MAXLEN);
  179.     stat2 = FGets(f2, line2, MAXLEN);
  180.     
  181.     while(stat1 != 0 && stat2 != 0)
  182.     {
  183.         cmp_result = strcmp(line1, line2);
  184.         if(cmp_result == 0)
  185.         {
  186.             if(print3)
  187.             {
  188.                 if(print1) FPutC(out, '\t');
  189.                 if(print2) FPutC(out, '\t');
  190.                 PutStr(line1);
  191.             }
  192.             stat1 = FGets(f1, line1, MAXLEN);
  193.             stat2 = FGets(f2, line2, MAXLEN);
  194.         }
  195.         else if(cmp_result < 0)
  196.         {
  197.             if(print1)
  198.                 PutStr(line1);
  199.             stat1 = FGets(f1, line1, MAXLEN);
  200.         }
  201.         else
  202.         {
  203.             if(print2)
  204.             {
  205.                 if(print1) FPutC(out, '\t');
  206.                 PutStr(line2);
  207.             }
  208.             stat2 = FGets(f2, line2, MAXLEN);
  209.         }
  210.         
  211.         if(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)
  212.         {
  213.             PrintFault(ERROR_BREAK, NULL);
  214.             return RETURN_WARN;
  215.         }
  216.     }
  217.     if(err = IoErr())
  218.     {
  219.         PrintFault(err, "Common");
  220.         return RETURN_ERROR;
  221.     }
  222.  
  223.     /* Output the remainder of the longest file */
  224.     if(stat1 || stat2)
  225.     {
  226.         if(print1 && stat1)
  227.             PutStr(line1);
  228.         if(print2 && stat2)
  229.         {
  230.             if(print1)
  231.                 FPutC(out, '\t');
  232.             PutStr(line2);
  233.         }
  234.         while(FGets(stat1 ? f1 : f2, line1, MAXLEN))
  235.         {
  236.             if(print1 && print2 && stat2)
  237.                 FPutC(out, '\t');
  238.             if(stat1 ? print1 : print2)
  239.                 PutStr(line1);
  240.  
  241.             if(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)
  242.             {
  243.                 PrintFault(ERROR_BREAK, NULL);
  244.                 return RETURN_WARN;
  245.             }
  246.         }
  247.         if(err = IoErr())
  248.         {
  249.             PrintFault(err, "Common");
  250.             return RETURN_ERROR;
  251.         }
  252.     }
  253. }
  254.