home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / psh / c / history < prev    next >
Encoding:
Text File  |  1995-05-08  |  864 b   |  52 lines

  1. /* vi:tabstop=4:shiftwidth=4:smartindent
  2.  *
  3.  * history.c - Lists the history list, either the whole list
  4.  *             or the last n.
  5.  *
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <history.h>
  10. #include "psh.h"
  11.  
  12. int sh_history(int argc, char **argv)
  13. {
  14.     int            i, j;
  15.     int            first = 0;
  16.     HIST_ENTRY    **hlist;
  17.  
  18.     /* If there were args, then try to read the number
  19.      */
  20.     if (argc > 1)
  21.     {
  22.         if ((sscanf(argv[1], "%d", &first) != 1) || (first >= 0))
  23.         {
  24.             fprintf(stderr, "Usage: history -n\n");
  25.             return 1;
  26.         }
  27.     }
  28.  
  29.     /* Read the history list
  30.      */
  31.     hlist = history_list();
  32.     if (hlist == NULL)
  33.     {
  34.         printf("No history stored\n");
  35.     }
  36.     else
  37.     {
  38.         /* Read the list length
  39.          */
  40.         for (j=0; hlist[j]; j++);
  41.  
  42.         /* Print the list
  43.          */
  44.         first = (first == 0) ? first : j+first;
  45.         for (i=first; hlist[i]; i++)
  46.         {
  47.             printf("%5d   %s\n", history_base + i, hlist[i]->line);
  48.         }
  49.     }
  50.     return 0;
  51. }
  52.