home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / ils / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-28  |  2.9 KB  |  146 lines

  1. /**************************************************************************
  2. ** main() routine for ils.
  3. **
  4. ** ils (c) copyright 1991 by Jack Alexander
  5. **
  6. ** NO WARRANTY OF ANY KIND IS ASSOCIATED WITH THIS PROGRAM, NOT EVEN
  7. ** FOR FITNESS OF PURPOSE
  8. **
  9. **/
  10. #include    <curses.h>
  11. #include    <stdio.h>
  12. #include    "ils.h"
  13.  
  14. char    *pnam;
  15. char    *patterns[ILS_MAX_PATS];
  16.  
  17. main(argc,argv)
  18. int    argc;
  19. char    *argv[];
  20. {
  21.     int    rows=0, cols=0, i, j, pats, modes=0,errs=0,skip,l;
  22.     char    curdir[MAXPATH], *title, c;
  23.  
  24.     pnam = argv[0];        /* globnal pointer to program name */
  25.     title = (char *) NULL;    /* no title, yet */
  26.  
  27.     /* extract any modes that may be part of a default dure to filename */
  28.     switch(argv[0][strlen(argv[0])-1]) {
  29.         case 'a':
  30.             modes |= ILS_ALL;
  31.             break;
  32.         case 'f':
  33.             modes |= ILS_F_TYPE;
  34.             break;
  35.         case 'l':
  36.             modes |= ILS_LONG;
  37.             break;
  38.     }
  39.     for(i=1,pats=0;i<argc;i++) {    /* look at all arguments passed */
  40.         skip=0;
  41.         switch(argv[i][0]) {
  42.             case '-':    /* handle the option */
  43.                 l = strlen(argv[i]);
  44.                 for(j=1;j<l;j++) {
  45.                     switch(argv[i][j]) {
  46.                         case 'r':
  47.                         /* change number of rows */
  48.                             j++;
  49.                             c = argv[i][j];
  50.                             while(c>='0' && c<='9' && j<l) {
  51.                                 rows *= 10;
  52.                                 rows += c-'0';
  53.                                 j++;
  54.                                 c = argv[i][j];
  55.                             }
  56.                             j--;
  57.                             break;
  58.                         case 'c':
  59.                         /* change number of columns */
  60.                             j++;
  61.                             c = argv[i][j];
  62.                             while(c>='0' && c<='9' && j<l) {
  63.                                 cols *= 10;
  64.                                 cols += c-'0';
  65.                                 j++;
  66.                                 c = argv[i][j];
  67.                             }
  68.                             j--;
  69.                             break;
  70.                         case 'a':
  71.                         /* list ALL mode (. files) */
  72.                             modes |= ILS_ALL;
  73.                             break;
  74.                         case 'F':
  75.                         /* show * and / */
  76.                             modes |= ILS_F_TYPE;
  77.                             break;
  78.                         case 'l':
  79.                         /* long listing format */
  80.                             modes |= ILS_LONG;
  81.                             break;
  82.                         case 'e':
  83.                         /* echo commands */
  84.                             modes |= ILS_ECHO_COMS;
  85.                             break;
  86.                         case 'T':
  87.                         /* window to have title */
  88.                             if((i+1)>=argc) {
  89.                                 fprintf(stderr,"%s: -T option must be followed with a title\n",pnam);
  90.                                 fprintf(stderr,"    example: %s -T \"This is a title\"\n",pnam);
  91.                                 errs++;
  92.                             }
  93.                             else {
  94.                                 title = argv[i+1];
  95.                                 skip=1;
  96.                             }
  97.                             break;
  98.                     }
  99.                 }
  100.                 break;
  101.             default:
  102.                 patterns[pats++] = argv[i];
  103.         }
  104.         i+=skip;
  105.         skip=0;
  106.     }
  107.     if(errs) {
  108.         fprintf(stderr,"%s: errors in parameters\n",pnam);
  109.         exit(0);
  110.     }
  111.     patterns[pats] = (char *) NULL;
  112.  
  113.     initscr();        /* init curses */
  114.     scrollok(stdscr,TRUE);
  115.     if(!rows)
  116.         rows = LINES;
  117.     if(!cols)
  118.         cols = COLS;
  119.  
  120.     if(get_curdir(curdir)) {
  121.         endwin();
  122.         exit(0);
  123.     }
  124.     ils(patterns,curdir,0,0,cols,rows,modes,title);
  125. #ifdef BSD
  126.     clear();
  127.     printw("Leaving %s...\n",pnam);
  128.     refresh();
  129. #endif
  130.     endwin();        /* end curses */
  131. }
  132.  
  133. get_curdir(path)
  134. char    path[];
  135. {
  136.     FILE    *fp;
  137.  
  138.     if((fp=popen(PWD,"r"))==NULL) {
  139.         fprintf(stderr,"%s: popen returns error\n",pnam);
  140.         return(1);
  141.     };
  142.     fscanf(fp,"%s",path);
  143.     pclose(fp);
  144.     return(0);
  145. }
  146.