home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip540.zip / amiga / stat.c < prev    next >
C/C++ Source or Header  |  1998-01-21  |  8KB  |  286 lines

  1. /* Here we have a handmade stat() function because Aztec's c.lib stat() */
  2. /* does not support an st_mode field, which we need... also a chmod().  */
  3.  
  4. /* This stat() is by Paul Wells, modified by Paul Kienitz. */
  5. /* Originally for use with Aztec C >= 5.0 and Lattice C <= 4.01  */
  6. /* Adapted for SAS/C 6.5x by Haidinger Walter */
  7.  
  8. /* POLICY DECISION: We will not attempt to remove global variables from */
  9. /* this source file for Aztec C.  These routines are essentially just   */
  10. /* augmentations of Aztec's c.lib, which is itself not reentrant.  If   */
  11. /* we want to produce a fully reentrant UnZip, we will have to use a    */
  12. /* suitable startup module, such as purify.a for Aztec by Paul Kienitz. */
  13.  
  14. #ifndef __amiga_stat_c
  15. #define __amiga_stat_c
  16.  
  17. #include <exec/types.h>
  18. #include <exec/memory.h>
  19. #include "amiga/z-stat.h"             /* fake version of stat.h */
  20. #include <string.h>
  21.  
  22. #ifdef AZTEC_C
  23. #  include <libraries/dos.h>
  24. #  include <libraries/dosextens.h>
  25. #  include <clib/exec_protos.h>
  26. #  include <clib/dos_protos.h>
  27. #  include <pragmas/exec_lib.h>
  28. #  include <pragmas/dos_lib.h>
  29. #endif
  30. #ifdef __SASC
  31. #  include <sys/dir.h>               /* SAS/C dir function prototypes */
  32. #  include <sys/types.h>
  33. #  include <proto/exec.h>
  34. #  include <proto/dos.h>
  35. #endif
  36.  
  37. #ifndef SUCCESS
  38. #  define SUCCESS (-1)
  39. #  define FAILURE (0)
  40. #endif
  41.  
  42.  
  43. void close_leftover_open_dirs(void);    /* prototype */
  44.  
  45. static DIR *dir_cleanup_list = NULL;    /* for resource tracking */
  46.  
  47. /* CALL THIS WHEN HANDLING CTRL-C OR OTHER UNEXPECTED EXIT! */
  48. void close_leftover_open_dirs(void)
  49. {
  50.     while (dir_cleanup_list)
  51.         closedir(dir_cleanup_list);
  52. }
  53.  
  54.  
  55. unsigned short disk_not_mounted;
  56.  
  57. extern int stat(char *file,struct stat *buf);
  58.  
  59. stat(file,buf)
  60. char *file;
  61. struct stat *buf;
  62. {
  63.  
  64.         struct FileInfoBlock *inf;
  65.         BPTR lock;
  66.         time_t ftime;
  67.         struct tm local_tm;
  68.  
  69.         if( (lock = Lock(file,SHARED_LOCK))==0 )
  70.                 /* file not found */
  71.                 return(-1);
  72.  
  73.         if( !(inf = (struct FileInfoBlock *)AllocMem(
  74.                 (long)sizeof(struct FileInfoBlock),MEMF_PUBLIC|MEMF_CLEAR)) )
  75.         {
  76.                 UnLock(lock);
  77.                 return(-1);
  78.         }
  79.  
  80.         if( Examine(lock,inf)==FAILURE )
  81.         {
  82.                 FreeMem((char *)inf,(long)sizeof(*inf));
  83.                 UnLock(lock);
  84.                 return(-1);
  85.         }
  86.  
  87.         /* fill in buf */
  88.         buf->st_dev         =
  89.         buf->st_nlink       =
  90.         buf->st_uid         =
  91.         buf->st_gid         =
  92.         buf->st_rdev        = 0;
  93.         buf->st_ino         = inf->fib_DiskKey;
  94.         buf->st_blocks      = inf->fib_NumBlocks;
  95.         buf->st_size        = inf->fib_Size;
  96.  
  97.         /* now the date.  AmigaDOS has weird datestamps---
  98.          *      ds_Days is the number of days since 1-1-1978;
  99.          *      however, as Unix wants date since 1-1-1970...
  100.          */
  101.  
  102.         ftime =
  103.                 (inf->fib_Date.ds_Days * 86400 )                +
  104.                 (inf->fib_Date.ds_Minute * 60 )                 +
  105.                 (inf->fib_Date.ds_Tick / TICKS_PER_SECOND )     +
  106.                 (86400 * 8 * 365 )                              +
  107.                 (86400 * 2 );  /* two leap years */
  108.  
  109.         tzset();
  110.         /* ftime += timezone; */
  111.         local_tm = *gmtime(&ftime);
  112.         local_tm.tm_isdst = -1;
  113.         ftime = mktime(&local_tm);
  114.  
  115.         buf->st_ctime =
  116.         buf->st_atime =
  117.         buf->st_mtime = ftime;
  118.  
  119.         buf->st_mode = (inf->fib_DirEntryType < 0 ? S_IFREG : S_IFDIR);
  120.  
  121.         /* lastly, throw in the protection bits */
  122.         buf->st_mode |= ((inf->fib_Protection ^ 0xF) & 0xFF);
  123.  
  124.         FreeMem((char *)inf, (long)sizeof(*inf));
  125.         UnLock((BPTR)lock);
  126.  
  127.         return(0);
  128.  
  129. }
  130.  
  131. int fstat(int handle, struct stat *buf)
  132. {
  133.     /* fake some reasonable values for stdin */
  134.     buf->st_mode = (S_IREAD|S_IWRITE|S_IFREG);
  135.     buf->st_size = -1;
  136.     buf->st_mtime = time(&buf->st_mtime);
  137.     return 0;
  138. }
  139.  
  140.  
  141. /* opendir(), readdir(), closedir(), rmdir(), and chmod() by Paul Kienitz. */
  142.  
  143. DIR *opendir(char *path)
  144. {
  145.     DIR *dd = AllocMem(sizeof(DIR), MEMF_PUBLIC);
  146.     if (!dd) return NULL;
  147.     if (!(dd->d_parentlock = Lock(path, MODE_OLDFILE))) {
  148.         disk_not_mounted = IoErr() == ERROR_DEVICE_NOT_MOUNTED;
  149.         FreeMem(dd, sizeof(DIR));
  150.         return NULL;
  151.     } else
  152.         disk_not_mounted = 0;
  153.     if (!Examine(dd->d_parentlock, &dd->d_fib) || dd->d_fib.fib_EntryType < 0) {
  154.         UnLock(dd->d_parentlock);
  155.         FreeMem(dd, sizeof(DIR));
  156.         return NULL;
  157.     }
  158.     dd->d_cleanuplink = dir_cleanup_list;       /* track them resources */
  159.     if (dir_cleanup_list)
  160.         dir_cleanup_list->d_cleanupparent = &dd->d_cleanuplink;
  161.     dd->d_cleanupparent = &dir_cleanup_list;
  162.     dir_cleanup_list = dd;
  163.     return dd;
  164. }
  165.  
  166. void closedir(DIR *dd)
  167. {
  168.     if (dd) {
  169.         if (dd->d_cleanuplink)
  170.             dd->d_cleanuplink->d_cleanupparent = dd->d_cleanupparent;
  171.         *(dd->d_cleanupparent) = dd->d_cleanuplink;
  172.         if (dd->d_parentlock)
  173.             UnLock(dd->d_parentlock);
  174.         FreeMem(dd, sizeof(DIR));
  175.     }
  176. }
  177.  
  178. struct dirent *readdir(DIR *dd)
  179. {
  180.     return (ExNext(dd->d_parentlock, &dd->d_fib) ? (struct dirent *)dd : NULL);
  181. }
  182.  
  183.  
  184. #ifdef AZTEC_C
  185.  
  186. int rmdir(char *path)
  187. {
  188.     return (DeleteFile(path) ? 0 : IoErr());
  189. }
  190.  
  191. int chmod(char *filename, int bits)     /* bits are as for st_mode */
  192. {
  193.     long protmask = (bits & 0xFF) ^ 0xF;
  194.     return !SetProtection(filename, protmask);
  195. }
  196.  
  197.  
  198. /* This here removes unnecessary bulk from the executable with Aztec: */
  199. void _wb_parse(void)  { }
  200.  
  201. /* fake a unix function that does not apply to amigados: */
  202. int umask(void)  { return 0; }
  203.  
  204.  
  205. #  include <signal.h>
  206.  
  207. /* C library signal() messes up debugging yet adds no actual usefulness */
  208. typedef void (*__signal_return_type)(int);
  209. __signal_return_type signal()  { return SIG_ERR; }
  210.  
  211.  
  212. /* The following replaces Aztec's argv-parsing function for compatibility with
  213. Unix-like syntax used on other platforms.  It also fixes the problem the
  214. standard _cli_parse() has of accepting only lower-ascii characters. */
  215.  
  216. int _argc, _arg_len;
  217. char **_argv, *_arg_lin;
  218.  
  219. void _cli_parse(struct Process *pp, long alen, register UBYTE *aptr)
  220. {
  221.     register UBYTE *cp;
  222.     register struct CommandLineInterface *cli;
  223.     register short c;
  224.     register short starred = 0;
  225. #  ifdef PRESTART_HOOK
  226.     void Prestart_Hook(void);
  227. #  endif
  228.  
  229.     cli = (struct CommandLineInterface *) (pp->pr_CLI << 2);
  230.     cp = (UBYTE *) (cli->cli_CommandName << 2);
  231.     _arg_len = cp[0] + alen + 2;
  232.     if (!(_arg_lin = AllocMem((long) _arg_len, 0L)))
  233.         return;
  234.     c = cp[0];
  235.     strncpy(_arg_lin, cp + 1, c);
  236.     _arg_lin[c] = 0;
  237.     for (cp = _arg_lin + c + 1; alen && (*aptr < '\n' || *aptr > '\r'); alen--)
  238.         *cp++ = *aptr++;
  239.     *cp = 0;
  240.     aptr = cp = _arg_lin + c + 1;
  241.     for (_argc = 1; ; _argc++) {
  242.         while (*cp == ' ' || *cp == '\t')
  243.             cp++;
  244.         if (!*cp)
  245.             break;
  246.         if (*cp == '"') {
  247.             cp++;
  248.             while (c = *cp++) {
  249.                 if (c == '"' && !starred) {
  250.                     *aptr++ = 0;
  251.                     starred = 0;
  252.                     break;
  253.                 } else if (c == '\\' && !starred)
  254.                     starred = 1;
  255.                 else {
  256.                     *aptr++ = c;
  257.                     starred = 0;
  258.                 }
  259.             }
  260.         } else {
  261.             while ((c = *cp++) && c != ' ' && c != '\t')
  262.                 *aptr++ = c;
  263.             *aptr++ = 0;
  264.         }
  265.         if (c == 0)
  266.             --cp;
  267.     }
  268.     *aptr = 0;
  269.     if (!(_argv = AllocMem((_argc + 1) * sizeof(*_argv), 0L))) {
  270.         _argc = 0;
  271.         return;
  272.     }
  273.     for (c = 0, cp = _arg_lin; c < _argc; c++) {
  274.         _argv[c] = cp;
  275.         cp += strlen(cp) + 1;
  276.     }
  277.     _argv[c] = NULL;
  278. #  ifdef PRESTART_HOOK
  279.     Prestart_Hook();
  280. #  endif
  281. }
  282.  
  283. #endif /* AZTEC_C */
  284.  
  285. #endif /* __amiga_stat_c */
  286.