home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine 1996 / ARCHIVE_96.iso / discs / shareware / share_46 / showdir / c / showdir
Text File  |  1991-11-24  |  5KB  |  136 lines

  1. /*
  2.  * Module:        showdir.c
  3.  * Created:       18th September, 1991.
  4.  * Last modified: 23rd September, 1991.
  5.  *
  6.  * Copyright (C) 1991 Neil Hoggarth.
  7.  *
  8.  * This text file is ANSI 'C' Source code.
  9.  *
  10.  *   The program 'showdir' is a utility for computers running the RISC-OS
  11.  * operating system. The program accepts one argument, the name of a
  12.  * directory. This directory is searched for applications. An application
  13.  * is defined as a directory which has '!' as the first character in its
  14.  * file name. If any applications exist the associated '!boot' files are
  15.  * run. If a '!boot' file does not exist but a '!sprite" file does then
  16.  * the sprite is merged into the system sprite area using the 'IconSprites'
  17.  * command.
  18.  *   The program therefore has the same effect as opening the given
  19.  * directory with the Desktop Filer but without displaying the contents in
  20.  * a window.
  21.  *
  22.  * When compiled this program should be linked to RISC_OSlib.o
  23.  *
  24.  *  This program is free software; you can redistribute it and/or modify
  25.  *  it under the terms of the GNU General Public License as published by
  26.  *  the Free Software Foundation; either version 1, or (at your option)
  27.  *  any later version.
  28.  *
  29.  *  This program is distributed in the hope that it will be useful,
  30.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  31.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  32.  *  GNU General Public License for more details.
  33.  *
  34.  *  You should have received a copy of the GNU General Public License
  35.  *  along with this program; if not, write to the Free Software
  36.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  37.  */
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41.  
  42. #include "os.h"
  43.  
  44. enum{OTYPE_NULL,OTYPE_FILE,OTYPE_DIR};
  45.  
  46. static struct FileRecord {
  47.   int load;                    /* Load address */
  48.   int exec;                    /* Execution address */
  49.   int leng;                    /* Length */
  50.   int attr;                    /* File attributes */
  51.   int type;                    /* Object type */
  52.   char name[11];               /* Filename */
  53. };
  54.  
  55. static int GetNextApp(char *dir,struct FileRecord *addr)
  56. /*
  57.  * This function uses a RISC_OSlib "os_gbpb" call to search the directory
  58.  * 'dir' for the next object which is an application. The function exits
  59.  * if it finds an object which meets the definition of an application or
  60.  * if the entire directory has been searched. The return value will be a
  61.  * positive integer if an application has been found and -1 if the
  62.  * directory search has been completed.
  63.  */
  64. {
  65.   static os_gbpbstr gbpbio={
  66.     10,                         /* Read entries from specified dir. */
  67.     0,                          /* ptr to directory name. */
  68.     0,                          /* ptr to buffer for results. */
  69.     1,                          /* number of entries to read. */
  70.     0,                          /* offset into directory. */
  71.     sizeof(struct FileRecord),  /* buffer length. */
  72.     "!*",                       /* Name to search for. */
  73.   };
  74.  
  75.   gbpbio.file_handle=(int)dir;
  76.   gbpbio.data_addr=addr;
  77.   (void)os_gbpb(&gbpbio);
  78.   while(!(gbpbio.number==1 && addr->type==OTYPE_DIR) && gbpbio.seq_point!=-1)
  79.     (void)os_gbpb(&gbpbio);
  80.   return(gbpbio.seq_point);
  81. }
  82.  
  83. static int ObjectType(char *fn)
  84. /*
  85.  * This function uses a RISC_OSlib "os_file" call to find the type of an
  86.  * object, given the filename of the object.
  87.  */
  88. {
  89.   os_filestr fileio;             /* control block for os_file call */
  90.  
  91.   fileio.action=17;              /* Read catalogue entry. */
  92.   fileio.name  =fn;              /* Name of object to search for. */
  93.  
  94.   (void)os_file(&fileio);
  95.  
  96.   return(fileio.action);
  97. }
  98.   
  99. int main(int argc,char *argv[])
  100. {
  101.   struct FileRecord buff;          /* data buffer for os_gbpb call */
  102.   char              bofile[256];   /* !boot filename */
  103.   char              spfile[256];   /* !sprite filename */
  104.   char              cmd[256];      /* command string */
  105.  
  106.   if (argc!=2)
  107.   {
  108.     (void)fprintf(stderr,"%s: wrong number of arguments.\n",argv[0]);
  109.     (void)fprintf(stderr,"useage - %s <directory name>\n",argv[0]);
  110.     exit(1);
  111.   }
  112.  
  113.   if (ObjectType(argv[1])!=OTYPE_DIR)
  114.   {
  115.     (void)fprintf(stderr,"%s: %s is not a directory.\n",argv[0],argv[1]);
  116.     exit(2);
  117.   }
  118.  
  119.   while (GetNextApp(argv[1],&buff)!=-1)
  120.   {
  121.     (void)sprintf(bofile,"%s.%s.!boot",argv[1],buff.name);
  122.     (void)sprintf(spfile,"%s.%s.!sprite",argv[1],buff.name);
  123.     if (ObjectType(bofile)==OTYPE_FILE)
  124.     {
  125.       (void)sprintf(cmd,"Run %s",bofile);
  126.       (void)system(cmd);
  127.     }
  128.     else if (ObjectType(spfile)==OTYPE_FILE)
  129.     {
  130.       (void)sprintf(cmd,"IconSprites %s",spfile);
  131.       (void)system(cmd);
  132.     }
  133.   }
  134.   return(0);
  135. }
  136.