home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / disks / disk402.lzh / StdFile / Test.c < prev    next >
C/C++ Source or Header  |  1990-11-17  |  2KB  |  87 lines

  1. #include <intuition/intuitionbase.h>
  2. #include <intuition/intuition.h>
  3. #include <libraries/dos.h>
  4. #include <exec/memory.h>
  5. #include <stdio.h>
  6.  
  7. struct IntuitionBase *IntuitionBase;
  8. struct GfxBase *GfxBase;
  9.  
  10. #define SECSPERDAY (60*60*24)
  11. #define SECSPERMIN 60
  12. #define TICKSPERSEC TICKS_PER_SECOND
  13.  
  14. #define MAXNAME 128
  15.  
  16. char *stdfile();
  17. extern char *Copyright;
  18.  
  19. main()
  20. {
  21.     char name[MAXNAME];
  22.  
  23.     if(!(IntuitionBase = OpenLibrary("intuition.library", 1))) {
  24.         printf("Couldn't open intuition.library.\n");
  25.         exit(20);
  26.     }
  27.     if(!(GfxBase = OpenLibrary("graphics.library", 1))) {
  28.         printf("Couldn't open graphics.library.\n");
  29.         CloseLibrary(IntuitionBase);
  30.         exit(20);
  31.     }
  32.  
  33.     printf("Testing STDFILE standard file requestor.\n%s\n", Copyright);
  34.  
  35.     name[0] = 0;
  36.     while(stdfile("Display file", name, 0, name)) {
  37.         BPTR stdlock;
  38.         struct FileInfoBlock *stdfib;
  39.         FILE *fp;
  40.         long datestamp;
  41.         int c;
  42.  
  43.         stdfib = AllocMem(sizeof(struct FileInfoBlock), MEMF_PUBLIC);
  44.         if(!stdfib) {
  45.             printf("Out of memory!\n");
  46.             break;
  47.         }
  48.  
  49.         if(!(stdlock = Lock(name, ACCESS_READ))) {
  50.             printf("Can't obtain lock for %s\n", name);
  51.             FreeMem(stdfib, sizeof(struct FileInfoBlock));
  52.             continue;
  53.         }
  54.         if(!(Examine(stdlock, stdfib))) {
  55.             printf("Can't examine %s\n", name);
  56.             UnLock(stdlock);
  57.             FreeMem(stdfib, sizeof(struct FileInfoBlock));
  58.             continue;
  59.         }
  60.         UnLock(stdlock);
  61.  
  62.         if(stdfib->fib_DirEntryType >= 0) {
  63.             printf("%s is a directory.\n", name);
  64.             FreeMem(stdfib, sizeof(struct FileInfoBlock));
  65.             continue;
  66.         }
  67.         if(!(fp = fopen(name, "r"))) {
  68.             perror(name);
  69.             FreeMem(stdfib, sizeof(struct FileInfoBlock));
  70.             continue;
  71.         }
  72.  
  73.         datestamp = stdfib->fib_Date.ds_Days*SECSPERDAY +
  74.                   stdfib->fib_Date.ds_Minute*SECSPERMIN +
  75.                   stdfib->fib_Date.ds_Tick/TICKSPERSEC;
  76.         printf("\n%s, %s\n", stdfib->fib_FileName,
  77.             ctime(&datestamp));
  78.         while((c = getc(fp)) != EOF)
  79.             putchar(c);
  80.         fclose(fp);
  81.         FreeMem(stdfib, sizeof(struct FileInfoBlock));
  82.     }
  83.  
  84.     CloseLibrary(IntuitionBase);
  85.     CloseLibrary(GfxBase);
  86. }
  87.