home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / utilities / shell / tlist13.lha / tlist.c next >
Encoding:
C/C++ Source or Header  |  1994-09-27  |  11.1 KB  |  412 lines

  1. ;/* Execute to compile
  2.  sc tlist.c OPTIMIZE OPTINLOCAL OPTTIME OPTSCHED IGNORE 73 LINK
  3.  quit
  4.  */
  5.  
  6. /* typelist.c
  7.  * Copyright © 1994 Uwe Röhm
  8.  * Thanks to David N. Junod for his examinedt.c
  9.  *
  10.  * Scans all given File- and Dirnames and
  11.  * prints their name, size, date and type (if possible).
  12.  *
  13.  */
  14.  
  15. #include <exec/types.h>
  16. #include <exec/memory.h>
  17. #include <exec/libraries.h>
  18. #include <dos/dos.h>
  19. #include <dos/datetime.h>
  20. #include <dos/dosextens.h>
  21. #include <datatypes/datatypes.h>
  22. #include <datatypes/datatypesclass.h>
  23. #include <libraries/iffparse.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <ctype.h>
  27.  
  28. #include <clib/dos_protos.h>
  29. #include <clib/exec_protos.h>
  30. #include <clib/datatypes_protos.h>
  31. #include <clib/iffparse_protos.h>
  32.  
  33. #include <pragmas/dos_pragmas.h>
  34. #include <pragmas/exec_pragmas.h>
  35. #include <pragmas/datatypes_pragmas.h>
  36. #include <pragmas/iffparse_pragmas.h>
  37. void __main(void);
  38.  
  39.  
  40. #define TEMPLATE    "NAME/M,ALL/S,FILES/S,DIRS/S,NOSIZE/S,NOPROT/S,NODATE/S,NOCOMMENT/S,GROUP/S,HEADER/S,TOOLS/S,TAGS/S,CODE/S"
  41.  
  42. extern struct Library *SysBase, *DOSBase;
  43. struct Library *DataTypesBase, *IFFParseBase;
  44.  
  45. UBYTE Version[] = "\0$VER: tlist 1.3 (24.09.94) (c) 1994 by Uwe Röhm";
  46.  
  47. /* String for leading spaces for sub-dirs, max. 40 subdirs allowed! */
  48. UBYTE SubDirSpaces[81] = "";
  49.  
  50. LONG *Options[13] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  51. #define OPT_NAMES    0
  52. #define PrintAll     (Options[1])
  53. #define PrintFile    (Options[2] || !Options[3])
  54. #define PrintDir     (Options[3] || !Options[2])
  55. #define PrintSize    (!Options[4])
  56. #define PrintBits    (!Options[5])
  57. #define PrintDate    (!Options[6])
  58. #define PrintComment (!Options[7])
  59. #define PrintGroup   (Options[8])
  60. #define PrintHeader  (Options[9])
  61. #define PrintTools   (Options[10])
  62. #define PrintTags    (Options[11])
  63. #define PrintCode    (Options[12])
  64.  
  65. #define IsDir(x)     (x->ap_Info.fib_DirEntryType > 0)
  66. #define IsFile(x)    (x->ap_Info.fib_DirEntryType < 0)
  67. #define DidDir(x)    (x->ap_Flags & APF_DIDDIR)
  68. #define IsWild(x)    (x->ap_Flags & APF_ITSWILD)
  69.  
  70. /* Value for hidden-protection-bit, which isn't defined in dos/dos.h */
  71. #define FIBF_HIDDEN  (1 << 7)
  72.  
  73. /*
  74.  * Help functions
  75.  */
  76. char *dump_mask( WORD *ptr, WORD len )
  77. {
  78.    static char buffer[81];
  79.    char        c;
  80.    WORD        i;
  81.  
  82.    for ( i = 0; i < len; i++)
  83.    {
  84.       c = (char) *ptr;
  85.       if ( isprint(c) )
  86.          buffer[i] = c;
  87.       else
  88.          buffer[i] = '.';
  89.       ptr++;
  90.  
  91.    } /* for i */
  92.    buffer[len] = '\0';
  93.  
  94.    return buffer;
  95. }
  96.  
  97.  
  98. /*
  99.  *  Print Contents of Datatype-Header
  100.  */
  101. void print_dtyp_dthd ( struct DataType *dtype )
  102. {
  103.    UBYTE buf[8];
  104.  
  105.    Printf(" %-9s", dtype->dtn_Header->dth_BaseName);
  106.    Printf(" %s", IDtoStr(dtype->dtn_Header->dth_ID, buf));
  107.    Printf(" %ld",  dtype->dtn_Header->dth_Priority);
  108.  
  109.    if ( !dtype->dtn_Header->dth_Flags )
  110.       PutStr(" none");
  111.    if ( dtype->dtn_Header->dth_Flags & DTF_BINARY )
  112.       PutStr(" BINARY");
  113.    if ( dtype->dtn_Header->dth_Flags & DTF_ASCII )
  114.       PutStr(" ASCII");
  115.    if ( dtype->dtn_Header->dth_Flags & DTF_IFF )
  116.       PutStr(" IFF");
  117.    if ( dtype->dtn_Header->dth_Flags & DTF_MISC )
  118.       PutStr(" MISC");
  119.    if ( dtype->dtn_Header->dth_Flags & DTF_CASE )
  120.       PutStr(" CASE");
  121.    if ( dtype->dtn_Header->dth_Flags & DTF_SYSTEM1 )
  122.       PutStr(" SYSTEM1");
  123.  
  124.    if ( dtype->dtn_Header->dth_Pattern )
  125.       Printf(",%s", dtype->dtn_Header->dth_Pattern);
  126.    if ( dtype->dtn_Header->dth_MaskLen )
  127.       Printf(",%ld: %s", dtype->dtn_Header->dth_MaskLen, dump_mask(dtype->dtn_Header->dth_Mask, dtype->dtn_Header->dth_MaskLen));
  128. }
  129.  
  130. /*
  131.  *  Print Contents of Datatype-Tools-List
  132.  */
  133. void print_dtyp_tool ( struct DataType *dtype )
  134. {
  135.    struct ToolNode *tn;
  136.  
  137.    if ( IsListEmpty(&dtype->dtn_ToolList) )
  138.       return;
  139.    else for ( tn = (struct ToolNode *) dtype->dtn_ToolList.lh_Head;
  140.               tn->tn_Node.ln_Succ;
  141.               tn = (struct ToolNode *) tn->tn_Node.ln_Succ)
  142.    {
  143.       PutStr(SubDirSpaces);
  144.       switch ( tn->tn_Tool.tn_Which)
  145.       {
  146.          case TW_INFO:
  147.             PutStr("[INFO: ");
  148.             break;
  149.          case TW_BROWSE:
  150.             PutStr("[BROWSE: ");
  151.             break;
  152.          case TW_EDIT:
  153.             PutStr("[EDIT: ");
  154.             break;
  155.          case TW_PRINT:
  156.             PutStr("[PRINT: ");
  157.             break;
  158.          case TW_MAIL:
  159.             PutStr("[MAIL: ");
  160.             break;
  161.          default:
  162.             PutStr("[unknown: ");
  163.             break;
  164.       }
  165.  
  166.       PutStr(tn->tn_Tool.tn_Program);
  167.  
  168.       PutStr(" (Flags: ");
  169.       if ( tn->tn_Tool.tn_Flags == TF_RX )
  170.          PutStr("RX ");
  171.       if ( tn->tn_Tool.tn_Flags == TF_SHELL )
  172.          PutStr("SHELL ");
  173.       if ( tn->tn_Tool.tn_Flags == TF_WORKBENCH )
  174.          PutStr("WORKBENCH ");
  175.       if ( !tn->tn_Tool.tn_Flags )
  176.          PutStr("none ");
  177.       Printf("Length %ld)]\n", tn->tn_Length);
  178.    }
  179. }
  180.  
  181. /*
  182.  *  Print Contents of Datatype-Attributes-List
  183.  */
  184. void print_dtyp_tags ( struct DataType *dtype )
  185. {
  186.    struct TagItem *tag;
  187.  
  188.    for ( tag = dtype->dtn_AttrList; tag && tag->ti_Tag; tag++ )
  189.       Printf("%s[TAG %lx DATA %lx]\n", SubDirSpaces, tag->ti_Tag, tag->ti_Data);
  190. }
  191.  
  192. /*
  193.  *  Print Contents of Datatype-Code
  194.  */
  195. void print_dtyp_code ( struct DataType *dtype )
  196. {
  197.    if ( dtype->dtn_FunctionName )
  198.       Printf("%s[Function: %s]\n", SubDirSpaces, dtype->dtn_FunctionName);
  199.    if ( dtype->dtn_Length )
  200.       Printf("[Code length: %ld]\n", dtype->dtn_Length);
  201. }
  202.  
  203.  
  204. void print_datatype_info ( BPTR dir, char *filename )
  205. {
  206.    struct DataTypeHeader *dth;
  207.    struct DataType       *dtn;
  208.    BPTR                   home, lock;
  209.  
  210.  
  211.    home = CurrentDir(dir);
  212.    if ( lock = Lock(filename, ACCESS_READ) )
  213.    {
  214.       if (dtn = ObtainDataTypeA (DTST_FILE, (APTR) lock, NULL))
  215.       {
  216.          dth = dtn->dtn_Header;
  217.          Printf (" %s ", dth->dth_Name);
  218.  
  219.          if ( PrintGroup )
  220.             Printf(" (%s, %s)", GetDTString (dth->dth_GroupID), GetDTString ((dth->dth_Flags & DTF_TYPE_MASK) + DTMSG_TYPE_OFFSET) );
  221.  
  222.          if ( PrintHeader )
  223.              print_dtyp_dthd( dtn );
  224.  
  225.          PutStr("\n");
  226.  
  227.          if ( PrintTools )
  228.              print_dtyp_tool( dtn );
  229.  
  230.          if ( PrintTags )
  231.              print_dtyp_tags( dtn );
  232.  
  233.          if ( PrintCode )
  234.              print_dtyp_code( dtn );
  235.  
  236.          ReleaseDataType (dtn);
  237.  
  238.       } /* if ObtainDataTypeA() */
  239.       else
  240.          Printf (" %s\n", GetDTString(DTERROR_UNKNOWN_DATATYPE));
  241.  
  242.       UnLock(lock);
  243.  
  244.    } /* if Lock() */
  245.    else
  246.       PutStr("\n"); /* finish current line */
  247.  
  248.    CurrentDir(home);
  249. }
  250.  
  251. void print_protection_bits ( ULONG protection )
  252. {
  253.    static char buffer[10]=" --------";
  254.  
  255.    buffer[1] = (protection & FIBF_HIDDEN)  ? 'h' : '-';
  256.    buffer[2] = (protection & FIBF_SCRIPT)  ? 's' : '-';
  257.    buffer[3] = (protection & FIBF_PURE)    ? 'p' : '-';
  258.    buffer[4] = (protection & FIBF_ARCHIVE) ? 'a' : '-';
  259.    buffer[5] =!(protection & FIBF_READ)    ? 'r' : '-';
  260.    buffer[6] =!(protection & FIBF_WRITE)   ? 'w' : '-';
  261.    buffer[7] =!(protection & FIBF_EXECUTE) ? 'e' : '-';
  262.    buffer[8] =!(protection & FIBF_DELETE)  ? 'd' : '-';
  263.  
  264.    PutStr(buffer);
  265. }
  266.  
  267. void print_date ( struct DateStamp *date )
  268. {
  269.    struct DateTime  dtime;
  270.    UBYTE            buffer[12];
  271.  
  272.    dtime.dat_Stamp  = *date;                   /* datestamp to be converted */
  273.    dtime.dat_Format = FORMAT_DOS;              /* Format: dd-mmm-yy */
  274.    dtime.dat_Flags  = DTF_SUBST;               /* create dayname if possible */
  275.    dtime.dat_StrDate= buffer;                  /* only create datestring */
  276.    dtime.dat_StrTime= NULL;
  277.    dtime.dat_StrDay = NULL;
  278.  
  279.    DateToStr(&dtime);
  280.    Printf(" %9s", buffer);
  281. }
  282.  
  283.  
  284. void scan_dir ( char *name )
  285. {
  286.    struct AnchorPath *anchor;
  287.    LONG               indent = 0, error = 0;
  288.  
  289.  
  290.    if ( anchor = (struct AnchorPath *) AllocMem(sizeof(struct AnchorPath), MEMF_CLEAR) )
  291.    {
  292.       anchor->ap_BreakBits = SIGBREAKF_CTRL_C;
  293.  
  294.       if ( (error = MatchFirst(name, anchor)) == 0 )
  295.       {
  296.          /* if the user gave a directory name without wildcard - enter it */
  297.          if ( IsDir(anchor) && !IsWild(anchor) )
  298.          {
  299.             anchor->ap_Flags |= APF_DODIR;
  300.             indent = 2;
  301.             strcat(SubDirSpaces, "  ");
  302.          }
  303.  
  304.          while ( !error )
  305.          {
  306.             /*
  307.              * APF_DIDDIR indicates that we used returned from a
  308.              * directory. In that case we clear both APF_DIDDIR and
  309.              * APF_DODIR, so we can start afresh with the next one.
  310.              */
  311.             if ( anchor->ap_Flags & APF_DIDDIR )
  312.             {
  313.                anchor->ap_Flags &= ~(APF_DODIR | APF_DIDDIR);
  314.                indent -= 2;
  315.                SubDirSpaces[indent] = '\0';
  316.             }
  317.             else
  318.             if ( (IsDir(anchor) && PrintDir) || (IsFile(anchor) && PrintFile) )
  319.             {
  320.                /* again test for the case of a directory without wildcards */
  321.                if ( !*anchor->ap_Info.fib_FileName )
  322.                   Printf("%-25s", name);
  323.                else
  324.                   Printf("%s%-25s", SubDirSpaces, anchor->ap_Info.fib_FileName);
  325.  
  326.                if ( PrintSize )
  327.                   Printf("%7ld", anchor->ap_Info.fib_Size);
  328.  
  329.                if ( PrintBits )
  330.                   print_protection_bits(anchor->ap_Info.fib_Protection);
  331.  
  332.                if ( PrintDate )
  333.                   print_date(&anchor->ap_Info.fib_Date);
  334.  
  335.                print_datatype_info(anchor->ap_Current->an_Lock, anchor->ap_Info.fib_FileName);
  336.  
  337.                if ( PrintComment && anchor->ap_Info.fib_Comment[0] )
  338.                   Printf("%s: %s\n", SubDirSpaces, anchor->ap_Info.fib_Comment);
  339.  
  340.  
  341.                /*
  342.                 * If the ALL keyword has been used and this is a directory
  343.                 * enter it by setting the APF_DODIR flag.
  344.                 */
  345.                if (IsDir(anchor) && PrintAll)
  346.                {
  347.                   anchor->ap_Flags |= APF_DODIR;
  348.                   indent += 2;                  /* indent sub-dirs  */
  349.                   strcat(SubDirSpaces, "  ");
  350.                }
  351.  
  352.             } /* if PrintDir || PrintFile */
  353.  
  354.             error = MatchNext(anchor);
  355.  
  356.          } /* while !error */
  357.  
  358.          if (error != ERROR_NO_MORE_ENTRIES)
  359.             PrintFault(error, NULL);
  360.  
  361.          MatchEnd(anchor);
  362.  
  363.       } /* if MatchFirst() */
  364.       else if (error != ERROR_NO_MORE_ENTRIES)
  365.          PrintFault(error, NULL);
  366.  
  367.       FreeMem(anchor, sizeof(struct AnchorPath));
  368.  
  369.    } /* if AllocMem() */
  370.    else
  371.       PrintFault(ERROR_NO_FREE_STORE, NULL);
  372. }
  373.  
  374.  
  375. void __main ()
  376. {
  377.    struct RDArgs *rdargs;
  378.    char         **filename;
  379.  
  380.  
  381.    if ( rdargs = ReadArgs(TEMPLATE, (LONG *) Options, NULL) )
  382.    {
  383.       if (DataTypesBase = OpenLibrary ("datatypes.library", 39))
  384.       {
  385.          if (IFFParseBase = OpenLibrary ("iffparse.library", 39))
  386.          {
  387.             /* scan all given dirs */
  388.             filename = (char **) Options[OPT_NAMES];
  389.             if ( filename == NULL )
  390.                scan_dir("#?");
  391.             else
  392.                while ( *filename )
  393.                   scan_dir(*filename++);
  394.  
  395.             CloseLibrary (IFFParseBase);
  396.  
  397.          } /* if OpenLibrary() */
  398.          else
  399.             PutStr("Couldn't open iffparse.library V39\n");
  400.  
  401.          CloseLibrary (DataTypesBase);
  402.       }
  403.       else
  404.          PutStr("Couldn't open datatypes.library V39\n");
  405.  
  406.       FreeArgs(rdargs);
  407.  
  408.    } /* if ReadArgs() */
  409.    else
  410.       PrintFault(IoErr(), NULL); /* prints the appropriate error message */
  411. }
  412.