home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / printer / lp / source.lha / source / files.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-12  |  5.2 KB  |  222 lines

  1. /*
  2.  *  $RCSfile: files.c,v $ 
  3.  *  $Release$
  4.  *  $Revision: 1.2 $
  5.  *  $Date: 92/12/13 23:54:19 $
  6.  *  $Author: tf $
  7.  *  $State: Exp $
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <exec/memory.h>
  12. #include <stdio.h>
  13. #include <libraries/dos.h>
  14. #include <libraries/dosextens.h>
  15.  
  16. extern struct DosLibrary *DosBase;
  17.  
  18. struct FileLock *Lock(), *DupLock(), *CurrentDir(), *ParentDir();
  19.  
  20. unsigned long coptions=0L; /* is only accessed by: */
  21.  
  22. #define SETOPT(o) coptions |= (o)
  23. #define CLROPT(o) coptions &=~(o)
  24.  
  25. #define OPT_RECURSIVE (1L<<0)  /* enter sub-directories */
  26. #define OPT_NOICONS   (1L<<1)  /* skip .info files */
  27. #define OPT_QUOTE     (1L<<2)  /* skip .info files */
  28.  
  29. #define RECURSIVE ((coptions & OPT_RECURSIVE) !=NULL)
  30. #define NOICONS   ((coptions & OPT_NOICONS)   !=NULL)
  31. #define QUOTE     ((coptions & OPT_QUOTE)     !=NULL)
  32.  
  33. static char rcs_id[]= "$VER: $Id: files.c,v 1.2 92/12/13 23:54:19 tf Exp $";
  34.  
  35. #define BANNER &rcs_id[6]
  36.  
  37. void buythefarm(void)
  38. { exit(0);
  39. }
  40.  
  41. void _abort(void)
  42. { puts("^C\n\n***BREAK");
  43.   buythefarm();
  44. }
  45.  
  46. static char *whoami,      /* copy of argv[0] (for perror) */
  47.             *prefix="",   /* output line prefix */
  48.             *postfix="";  /* output line postfix */
  49.  
  50. #include <stdarg.h>
  51.  
  52. void perror(const char *fmt, ...)
  53. {
  54.   va_list argp;
  55.   va_start(argp,fmt);
  56.   fprintf(stderr,"\r%s: ",whoami);
  57.   vfprintf(stderr,fmt,argp);
  58.   fprintf(stderr,"\n");
  59.   fflush(stderr);
  60.   va_end(argp);
  61. }
  62.  
  63. /*
  64.  * getcdn() exreacts the current directory name from the CLI structure and
  65.  * copies it into the buffer.
  66.  */
  67.  
  68. int getcdn(char *buf, int len)
  69. { struct Process *proc= (struct Process *)FindTask(NULL);
  70.   BYTE l=0;
  71.   if(proc)
  72.   { char *cdn= (char *)
  73.       BADDR(((struct CommandLineInterface *)BADDR(proc->pr_CLI))->cli_SetName);
  74.     l=((*cdn<len)?*cdn:len);
  75.     while(l--) *buf++ = *++cdn;
  76.     *buf='\0';
  77.   }
  78.   return(l);
  79. }
  80.  
  81. /*
  82.  * Get full pathname out of the given path
  83.  */
  84.  
  85. char *getfpn(char *path)
  86. {
  87.   static char fpn[256];
  88.   struct FileLock *lock;
  89.   struct FileInfoBlock *fib= (struct FileInfoBlock *)
  90.     AllocMem(sizeof(struct FileInfoBlock),MEMF_PUBLIC);
  91.   char *s= &fpn[255];
  92.   *s= '\0';
  93.   if(fib)
  94.   { if(lock= Lock(path, SHARED_LOCK))
  95.     { while(lock)
  96.       { if(Examine(lock,fib))
  97.         { char *t= fib->fib_FileName;
  98.           int l=0;
  99.           *--s= (lock=ParentDir(lock))?'/':':';
  100.           while(*t)
  101.           { t++;
  102.             l++;
  103.           }
  104.           while(l--) *--s= *--t;
  105.         }
  106.       }
  107.       UnLock(lock);
  108.     }
  109.     else perror("Can't find %s\nError code %ld.",path,IoErr());
  110.     FreeMem(fib,sizeof(struct FileInfoBlock));
  111.   }
  112.   else perror("Error: Ran out of memory");
  113.   return(s);
  114. }
  115.  
  116. void perform(char *path)
  117. {
  118.   char fstr[512];
  119.   struct FileLock *lock;
  120.   if(lock= Lock(path,SHARED_LOCK))
  121.   { struct FileInfoBlock *fib= (struct FileInfoBlock *)
  122.        AllocMem(sizeof(struct FileInfoBlock),MEMF_PUBLIC);
  123.     if(fib)
  124.     { if(Examine(lock,fib)!=DOSFALSE)
  125.       { if(QUOTE) sprintf(fstr,"%s\"%s%%s\"%s\n",prefix,getfpn(path),postfix);
  126.         else sprintf(fstr,"%s%s%%s%s\n",prefix,getfpn(path),postfix);
  127.         while(ExNext(lock,fib)!=DOSFALSE)
  128.         { if(fib->fib_DirEntryType < 0)
  129.             printf(fstr,fib->fib_FileName);
  130.           else if(RECURSIVE)
  131.           { char temp[256];
  132.             sprintf(temp,"%s%s",getfpn(path),fib->fib_FileName);
  133.             perform(temp);
  134.           }
  135.         }
  136.       }
  137.       FreeMem(fib,sizeof(struct FileInfoBlock));
  138.     }
  139.     else perror("Error: Ran out of memory");
  140.     UnLock(lock);
  141.   }
  142.   else perror("Can't find %s\nError code %ld.",path,IoErr());
  143. }
  144.  
  145. void perform17(char *path)
  146. { char cdn[256];
  147.   struct FileLock *lock, *old;
  148.   if(lock= Lock(path, SHARED_LOCK))
  149.   { old= CurrentDir(lock);
  150.     getcdn(cdn,256);
  151.     puts(cdn);
  152.     /*lfiles(lock);*/
  153.     CurrentDir(old);
  154.     UnLock(lock);
  155.   }
  156.   else perror("Error code %ld.\n",IoErr());
  157. }
  158.  
  159.  
  160. void main(int argc, char *argv[])
  161. {
  162.   char *path= "";
  163.   onbreak(_abort);     /* this is DICE */
  164.   whoami= argv[0];     /* and this is me */
  165.   BOOL badopt= FALSE;  /* bad option? */
  166.  
  167.   if(argc>1)
  168.   { --argc;
  169.     ++argv;
  170.     while(argc>0 && !badopt)
  171.     { char *arg=argv[0];
  172.       if(*arg=='-')
  173.       { arg++;
  174.         switch(*arg)
  175.         { case 'r': case 'R': /* enter sub-dirs */
  176.             SETOPT(OPT_RECURSIVE);
  177.             break;
  178.           case 'i': case 'I': /* skip .info files */
  179.             SETOPT(OPT_NOICONS);
  180.             break;
  181.           case 'q': case 'Q': /* quote filename */
  182.             SETOPT(OPT_QUOTE);
  183.             break;
  184.           case 'p': /* prefix */
  185.             if(arg[1]) prefix= &arg[1];
  186.             else { ++argv;
  187.                    --argc;
  188.                    prefix= argv[0];
  189.                  }
  190.             break;
  191.           case 'P': /* postfix */
  192.             if(arg[1]) postfix= &arg[1];
  193.             else { ++argv;
  194.                    --argc;
  195.                    postfix= argv[0];
  196.                  }
  197.             break;
  198.           default:
  199.             perror("Bad option: -%c.",*arg);
  200.             badopt=TRUE;
  201.             break;
  202.         }
  203.       }
  204.       else if(*arg=='?'||*arg=='.')
  205.       { badopt= TRUE;
  206.         break;
  207.       }
  208.       else path=arg;
  209.       --argc;
  210.       ++argv;
  211.     }
  212.   }
  213.   if(!badopt)
  214.     perform(path);
  215.   else
  216.   { puts(BANNER);
  217.     puts("(c)Copyright 1990-92 by Tobias Ferber, All Rights Reserved");
  218.     puts("FILES [pathname] [-p prestr] [-P poststr] [-r] [-q]");
  219.   }
  220.   buythefarm();
  221. }
  222.