home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / lib / c / files.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-04  |  3.3 KB  |  121 lines

  1. /* ACE linked library module for the FILES command
  2. ** which gives a directory listing.
  3. ** Copyright (C) 1998 David Benn
  4. ** 
  5. ** This program is free software; you can redistribute it and/or
  6. ** modify it under the terms of the GNU General Public License
  7. ** as published by the Free Software Foundation; either version 2
  8. ** of the License, or (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program; if not, write to the Free Software
  17. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19.    ACE syntax: FILES [TO <storefile>] [,<target>]
  20.  
  21.    If <storefile> is specified, the listing will be
  22.    captured by that file.
  23.  
  24.    If <storefile> is omitted, a valid _stdout is 
  25.    assumed for the listing to be displayed. 
  26.  
  27.    The <target> argument can be a file, directory or
  28.    an AmigaDOS device name.
  29.  
  30.    Author: David J Benn
  31.      Date: 18th,21st May 1993,
  32.        10th September 1994
  33. */
  34.  
  35. #include <exec/types.h>
  36. #include <exec/memory.h>
  37. #include <libraries/dos.h>
  38. #include <libraries/dosextens.h>
  39.  
  40. extern struct FileHandle *stdout;
  41.  
  42. void show_info(info,out)
  43. struct FileInfoBlock *info;
  44. struct FileHandle *out;
  45. {
  46. /* show info about file/directory */
  47. long entry_type;
  48. char *name;
  49.  
  50.  /* get info */
  51.  entry_type = info->fib_DirEntryType;
  52.  name = info->fib_FileName;
  53.  
  54.  /* show or store info */ 
  55.  if (entry_type > 0) 
  56.     fprintf(out,"[%s]\n",name); /* directory name in brackets */
  57.  else
  58.     fprintf(out,"%s\n",name);
  59. }
  60.  
  61. void files(target,store)
  62. char *target,*store;
  63. {
  64. /* FILES [TO <storefile>] [,<target>] */
  65. struct Process *process;
  66. struct FileLock *lock;
  67. struct FileHandle *store_fh;
  68. struct FileInfoBlock *f_info;
  69.  
  70.  /* allocate memory for FIB -- must be long-word aligned */
  71.  if ((f_info=(struct FileInfoBlock *)
  72.     AllocMem(sizeof(struct FileInfoBlock),MEMF_ANY | MEMF_CLEAR))==NULL)
  73.  return;
  74.  
  75.  /* get lock on directory */
  76.  if (target)
  77.  {
  78.   /* target directory/file specified */
  79.   lock = (struct FileLock *)Lock(target,ACCESS_READ);
  80.  }
  81.  else
  82.  {
  83.   /* no target specified -> get current directory for CLI/Wb command */
  84.   process = (struct Process *)FindTask(0L);
  85.   lock = (struct FileLock *)process->pr_CurrentDir;   
  86.  }
  87.  
  88.  /* quit if lock is NULL */
  89.  if (lock == NULL) 
  90.     { FreeMem(f_info,sizeof(struct FileInfoBlock)); return; }
  91.  
  92.  /* set up file storage? */
  93.  if (store)
  94.  {
  95.   if ((store_fh=(struct FileHandle *)Open(store,MODE_NEWFILE))==NULL)
  96.      { FreeMem(f_info,sizeof(struct FileInfoBlock)); return; }
  97.  }
  98.  else
  99.      store_fh = stdout;
  100.  
  101.  /* examine first entry */
  102.  if (Examine(lock,f_info) == NULL)
  103.     { FreeMem(f_info,sizeof(struct FileInfoBlock)); return; }
  104.  
  105.  /* show details of first entry */
  106.  fprintf(store_fh,"Directory of: ");
  107.  show_info(f_info,store_fh);
  108.  
  109.  /* if directory, examine contents */
  110.  if (f_info->fib_DirEntryType > 0)
  111.  {
  112.   while (ExNext(lock,f_info) != NULL) 
  113.         show_info(f_info,store_fh);
  114.  }
  115.  
  116.  /* clean up */
  117.  if (target) UnLock(lock);  /* don't free CLI/Wb lock! */ 
  118.  FreeMem(f_info,sizeof(struct FileInfoBlock));
  119.  if (store_fh != stdout) Close(store_fh);
  120. }
  121.