home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 066.lha / c / d.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-20  |  10.5 KB  |  356 lines

  1. /* D  :  Directory replacement by Gregg Reno 5/28/87
  2.  
  3.   Format:   D  [spec] [option] [option] [option] [option]
  4.    where
  5.       spec   = file or directory spec.  Wild cards accepted (Ex: #?.c)
  6.       option = one of the following:
  7.                   -d,-D    list directories only
  8.                   -f,-F    list files only
  9.                   -n,-N    list files with no file extensions only
  10.                   -l,-L    long format including #bytes, #blocks, protection,
  11.                            timestamp and comments
  12.                   -s,-S    Sort entries
  13.  
  14.    If no option is specified, entries are entered into three columns.
  15. */
  16. #include "stdio.h"
  17. #include "error.h"
  18. #include "dos.h"
  19.  
  20. #define lf     '\n'
  21. #define esc    27
  22. #define ff     12
  23. #define space  ' '
  24. #define DTYPE  1
  25. #define FTYPE  2
  26. #define MAXFILES 100     /* Change this if you have more files */
  27.  
  28. typedef struct JULDATE {
  29.     short  year;
  30.     short  days;
  31.     };
  32.  
  33. short days_before[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  34.  
  35. long int lseconds,nfiles=0,nbytes=0,nblocks=0,curpos,ndirs=0;
  36. char  *files[MAXFILES],*curfile;
  37. char type[MAXFILES];
  38. char  *month_str[12] = { "Jan ","Feb ","Mar ","Apr ","May ","Jun ","Jul ",
  39.                          "Aug ","Sep ","Oct ","Nov ","Dec "};
  40.  
  41. main(argc,argv)
  42. int   argc;
  43. char  *argv[];
  44.    {
  45.  
  46.    int   error,len,temp;
  47.    struct FILEINFO info;
  48.    struct JULDATE jdate;
  49.    char  tstring[80],tstring2[80], column, i, opt,
  50.          prot[7], files_only, dir_only,
  51.          lformat, sort, wildc, noex;
  52.  
  53.    /* Default settings */
  54.    files_only=FALSE;                /* Files and directories */
  55.    dir_only = FALSE;
  56.    lformat = FALSE;                 /* No long format */
  57.    noex = FALSE;                    /* No file extension check */
  58.    sort=FALSE;                      /* No sorting */
  59.    wildc = FALSE;                   /* No wildcards found in string */
  60.    scpy(tstring2, "#?");            /* Default is wildcard */
  61.  
  62.    for (i=1; i < argc; ++i) {
  63.       if (argv[i][0] == '-') {            /* Option selected */
  64.          opt = argv[i][1];
  65.          if (opt >= 'a' && opt <= 'z') opt = opt - 'a' + 'A';  /* Upper case */
  66.          switch (opt) {
  67.             case 'D':   dir_only = TRUE;
  68.                         break;
  69.             case 'F':   files_only = TRUE;
  70.                         break;
  71.             case 'L':   lformat = TRUE;
  72.                         break;
  73.             case 'S':   sort = TRUE;
  74.                         break;
  75.             case 'N':   noex = TRUE;
  76.                         break;
  77.             default:    ps("  ** Invalid option: -",22);
  78.                         pc(argv[i][1]);
  79.                         pc(lf);
  80.                         exit();
  81.             }
  82.          }
  83.       else
  84.          scpy(tstring2, argv[i]);
  85.       }
  86.  
  87.    if (lformat) {
  88.       pc(lf);
  89.       ps("  File Name                Bytes   Blocks  Prot  Time Stamp",70);
  90.       pc(lf);
  91.       ps("  ------------------------ ------- ------  ----  --------------------",70);
  92.       pc(lf);
  93.       }
  94.    len = strlen(tstring2);
  95.    curpos = 0;
  96.    for (i=0; i < len; ++i) {
  97.       if (tstring2[i] == '.' && (i==0 || i==(len-1))) { /* Dot at beg or end */
  98.             if (i==(len-1)) tstring[curpos++] = '.';
  99.             tstring[curpos++] = '#';
  100.             tstring[curpos++] = '?';
  101.             if (i==0) tstring[curpos++] = '.';
  102.             wildc = TRUE;
  103.             }
  104.       else if (tstring2[i] == ':' && i==len-1) {         /* Colon at end */
  105.             tstring[curpos++] = ':';
  106.             tstring[curpos++] = '#';
  107.             tstring[curpos++] = '?';
  108.             wildc = TRUE;
  109.             }
  110.       else if (tstring2[i] == '#' || tstring2[i] == '?') {
  111.             wildc = TRUE;
  112.             tstring[curpos++] = tstring2[i];
  113.             }
  114.       else if (tstring2[i] == '*') {
  115.          tstring[curpos++] = '#';
  116.          tstring[curpos++] = '?';
  117.          wildc = TRUE;
  118.          }
  119.       else
  120.          tstring[curpos++] = tstring2[i];
  121.       }
  122.    tstring[curpos] = '\0';
  123.    len = curpos;
  124.  
  125.    error = dfind(&info, tstring, 1);
  126.  
  127.    /* Check to see if they specified a directory */
  128.    if (error==0 && info.fib_DirEntryType>0 && len >0 && wildc == FALSE) {
  129.       scpy(&tstring[len], "/#?");
  130.       error = dfind(&info, tstring, 1);
  131.       }
  132.  
  133.    while (error==0) {
  134.       if (noex)                          /* Search for file extension */
  135.          for (i=0; info.fib_FileName[i] != 0; ++i)
  136.             if (info.fib_FileName[i]=='.') goto next;
  137.  
  138.       if (files[nfiles]==0) {             /* Get memory */
  139.          if (lformat) files[nfiles] = calloc(1,190);
  140.          else files[nfiles] = calloc(1,35);
  141.          }
  142.       curfile = files[nfiles];
  143.       curpos = 0;
  144.  
  145.       /* Entry Name */
  146.       if (info.fib_DirEntryType > 0) {     /* Directory entry */
  147.          if (files_only == TRUE) goto next;
  148.          type[nfiles] = DTYPE;
  149.          pmc(ff);
  150.          pms(info.fib_FileName,25);
  151.          ++ndirs;                         /* Number of directories */
  152.          }
  153.       else {                               /* File entry */
  154.          if (dir_only == TRUE) goto next;
  155.          type[nfiles] = FTYPE;
  156.          pms(info.fib_FileName,25);
  157.          }
  158.  
  159.       if (lformat) {                     /* Long format */
  160.          if (type[nfiles] == DTYPE)
  161.             pms(" ",16);
  162.          else {
  163.             nbytes += info.fib_Size;
  164.             stcl_d(tstring, info.fib_Size);        /* # bytes */
  165.             pms(tstring,8);
  166.             nblocks += info.fib_NumBlocks;
  167.             stcl_d(tstring, info.fib_NumBlocks);   /* # blocks */
  168.             pms(tstring,8);
  169.             }
  170.          scpy(prot, "----  ");                  /* protection */
  171.          if ((info.fib_Protection & 8) == 0) prot[0] = 'r';
  172.          if ((info.fib_Protection & 4) == 0) prot[1] = 'w';
  173.          if ((info.fib_Protection & 2) == 0) prot[2] = 'e';
  174.          if ((info.fib_Protection & 1) == 0) prot[3] = 'd';
  175.          pms(prot,6);
  176.          Get_Date(&jdate, &info.fib_Date);
  177.          JultoGreg(&jdate);
  178.          pms("  ",2);
  179.          temp = info.fib_Date.ds_Minute/60;              /* Hour */
  180.          if (temp<10) pmc('0');
  181.          stcl_d(tstring, temp);   /* # blocks */
  182.          pms(tstring,0);
  183.          temp = info.fib_Date.ds_Minute - (temp * 60);   /* Minute */
  184.          pmc(':');
  185.          if (temp<10) pmc('0');
  186.          stcl_d(tstring, temp);
  187.          pms(tstring,0);
  188.          pmc(lf);
  189.          if (info.fib_Comment[0] != '\0') {
  190.             pms(" ",5);
  191.             pms(info.fib_Comment,70);
  192.             pmc(lf);
  193.             }
  194.          }
  195.       ++nfiles;
  196. next:
  197.       error = dnext(&info);
  198.       }
  199.    if (sort)
  200.       strsrt(files, nfiles);
  201.    column = 1;
  202.    for (i=0; i<nfiles; ++i) {
  203.       if (column==1) ps("  ",2);
  204.       if (sort== TRUE && i < ndirs){   /* If we are sorting , all directory */
  205.          pc(esc);                      /* files are at the beginning */
  206.          ps("[0;33;40m",9);
  207.          ps(files[i],0);
  208.          pc(esc);
  209.          ps("[0;31;40m",9);
  210.          }
  211.       else if (sort == FALSE && type[i]==DTYPE) {
  212.          pc(esc);
  213.          ps("[0;33;40m",9);
  214.          ps(files[i],0);
  215.          pc(esc);
  216.          ps("[0;31;40m",9);
  217.          }
  218.       else
  219.          ps(files[i],0);
  220.       free(files[i]);
  221.       if (lformat==FALSE) {
  222.          ++column;
  223.          if (column>3) {
  224.             pc(lf);
  225.             column=1;
  226.             }
  227.          }
  228.       }
  229.    if (lformat) {
  230.       ps("  ------------------------ ------- ------",0);
  231.       pc(lf);
  232.       }
  233.    else if (column > 1) pc(lf);
  234.    stcl_d(tstring, nfiles);   /* # files */
  235.    ps("  ",2);
  236.    ps(tstring,3);
  237.    ps(" files",22);
  238.    if (lformat) {
  239.       stcl_d(tstring, nbytes);   /* # bytes */
  240.       ps(tstring,8);
  241.       stcl_d(tstring, nblocks);  /* # blocks */
  242.       ps(tstring,8);
  243.       }
  244.    pc(lf);
  245.    }
  246.  
  247. ps(string,len)          /* Replace printf to shrink program */
  248.    char  string[];
  249.    {
  250.    int   i,j;
  251.    i=0;
  252.    while (string[i] != '\0') {
  253.       if (string[i] != ff) putchar(string[i]);
  254.       ++i;
  255.       }
  256.    if (len != 0) for (j=i; j<len; ++j) putchar(space);
  257.    }
  258.  
  259. pc(ch)
  260.    char  ch;
  261.    {
  262.    putchar(ch);
  263.    }
  264.  
  265. pms(string,len)          /* put string in memory */
  266.    char  string[];
  267.    {
  268.    int   i,j;
  269.    i=0;
  270.    while (string[i] != '\0') {
  271.       curfile[curpos] = string[i];
  272.       ++i;
  273.       ++curpos;
  274.       }
  275.    for (j=i; j<len; ++j, ++curpos) curfile[curpos] = space;
  276.    }
  277.  
  278. pmc(ch)                 /* Put character in memory */
  279.    char  ch;
  280.    {
  281.    curfile[curpos] = ch;
  282.    ++curpos;
  283.    }
  284.  
  285. scpy(to_string,from_string)            /* Replace strcpy to shrink program */
  286.    char to_string[],from_string[];
  287.    {
  288.    int   i;
  289.    for (i=0; from_string[i] != '\0'; ++i)
  290.       to_string[i] = from_string[i];
  291.    to_string[i] = '\0';
  292.    }
  293.  
  294. /*
  295.     The following routine accepts one argument, a pointer to a JULDATE
  296.     structure.  It returns the current date in Julian date form.
  297. */
  298. Get_Date(date,datestmp)
  299. struct JULDATE *date;
  300. long datestmp[3];
  301. {
  302.     int year_span, years_left, remainder, leapdays;
  303.  
  304.     datestmp[0] += 1;                  /*  add 1 for Jan. 1, 1978  */
  305.     year_span = datestmp[0] / 366;     /*  approx. year  */
  306.     date->days = datestmp[0] % 366;    /*  # of days left over  */
  307.     leapdays = year_span / 4;          /*  approx. # of leap days  */
  308.     date->year = 1978 + year_span;     /*  current year  */
  309.     years_left = year_span % 4;        /*  years since last leap yr  */
  310.     remainder = date->year % 4;        /*  # of years since last leap  */
  311.     if (remainder <= years_left && remainder > 0)
  312.          leapdays += 1;      /*  recent year was a leap year  */
  313.     date->days += year_span - leapdays;     /*  correct for leap days  */
  314.     if (remainder > 0 && date->days > 365)
  315.          {
  316.          date->year += 1;
  317.          date->days -= 365;
  318.          }
  319.     else if (remainder == 0 && date->days > 366)
  320.          {
  321.          date->year += 1;
  322.          date->days -= 366;
  323.          }
  324.  
  325. }
  326.  
  327. /*
  328.     This function accepts two arguments:  a string pointer and a pointer to a
  329.     JULDATE structure.  The routine will convert the Julian date to Gregorian
  330.     (mm-dd-yyyy) format.  The string pointer must point to a space at least
  331.     11 characters long.
  332. */
  333. JultoGreg(date)
  334. struct JULDATE *date;
  335. {
  336.     int length, year, month, day;
  337.     short julday;
  338.     char string[6];
  339.  
  340.     year = date->year;
  341.     julday = date->days;
  342.     if ( !(year % 4) && julday > 60)  julday -= 1;
  343.          /*  subtract 1 if we are in a leap year & past Feb.  */
  344.     for (month = 0; (days_before[month] < julday) && (month < 12); month++);
  345.          /*  find month  */
  346.     day = julday - days_before[month-1];    /*  get current day  */
  347.  
  348.     pms(month_str[month-1],4);
  349.     length = stci_d(string, day, 6);
  350.     if (day<10) pmc('0');
  351.     pms(string,0);         /*  add day to greg_date  */
  352.     pmc( ',');
  353.     length = stci_d(string, year, 6);
  354.     pms(string,0);                 /*  add year to greg_date  */
  355. }
  356.