home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-2.iso / Files II / Prog / M / MacPerl 4.13 source.sit / Perl Source ƒ / Perl / missing.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-04  |  5.1 KB  |  300 lines  |  [TEXT/MPS ]

  1. /* $Header: tyrathect:Development:Perl::RCS:missing.c,v 1.2 1994/05/04 02:12:43 neeri Exp $
  2.  *
  3.  *    Copyright (c) 1991, 1992 Matthias Neeracher
  4.  *
  5.  *    You may distribute under the terms of the Perl Artistic License,
  6.  *    as specified in the README file.
  7.  *
  8.  * $Log: missing.c,v $
  9.  * Revision 1.2  1994/05/04  02:12:43  neeri
  10.  * Added gmtime(), enabled utime(), reduced spinning.
  11.  *
  12.  * Revision 1.1  1994/03/01  23:38:17  neeri
  13.  * Initial revision
  14.  *
  15.  */
  16.  
  17. #define RESOLVE_MAC_CONFLICTS
  18.  
  19. #include "EXTERN.h"
  20. #include "perl.h"
  21.  
  22. #include <time.h>
  23.  
  24. #include <Folders.h>
  25. #include <Events.h>
  26. #include <OSUtils.h>
  27. #include <GUSI.h>
  28.  
  29. /* Allocate more stdio buffers */
  30.  
  31. FILE _iob[64] = {
  32.     0, nil, nil, nil, 0, _IOREAD,         0,
  33.     0, nil, nil, nil, 0, _IOWRT,          1,
  34.     0, nil, nil, nil, 0, _IOWRT+_IOLBF,    2
  35. };
  36.     
  37. FILE * _lastbuf = _iob + 64;
  38.  
  39. /* Calls that don't exist on the mac */
  40.  
  41. /* Borrowed from msdos.c
  42.  * Just pretend that everyone is a superuser
  43.  */
  44. #define ROOT_UID    0
  45. #define ROOT_GID    0
  46. int
  47. getuid(void)
  48. {
  49.     return ROOT_UID;
  50. }
  51.  
  52. int
  53. geteuid(void)
  54. {
  55.     return ROOT_UID;
  56. }
  57.  
  58. int
  59. getgid(void)
  60. {
  61.     return ROOT_GID;
  62. }
  63.  
  64. int
  65. getegid(void)
  66. {
  67.     return ROOT_GID;
  68. }
  69.  
  70. int
  71. setuid(int uid)
  72.     return (uid==ROOT_UID?0:-1);
  73. }
  74.  
  75. int
  76. setgid(int gid)
  77.     return (gid==ROOT_GID?0:-1); 
  78. }
  79.  
  80. execv()
  81. {
  82.     fatal("execv() not implemented on the Macintosh");
  83. }
  84.  
  85. execvp()
  86. {
  87.     fatal("execvp() not implemented on the Macintosh");
  88. }
  89.  
  90. chmod()
  91. {
  92. }
  93.  
  94. kill()
  95. {
  96.     fatal("kill() not implemented on the Macintosh");
  97. }
  98.  
  99. sleep(int seconds)
  100. {
  101.     long    ticks    =    TickCount() + seconds*60;
  102.     
  103.     while (TickCount() < ticks)
  104.         SpinMacCursor();
  105. }
  106.  
  107. do_aspawn()
  108. {
  109.     fatal("do_aspawn() not implemented on the Macintosh");
  110. }
  111.  
  112. do_spawn()
  113. {
  114.     fatal("do_spawn() not implemented on the Macintosh");
  115. }
  116.  
  117. char **environ;
  118. extern int StandAlone;
  119.  
  120. char ** init_env(char ** env)
  121. {
  122.     int        envcnt    =    0;
  123.     int        envsize    =    0;
  124.     int        varlen;
  125.     char *    envpool;
  126.     FILE *    envfile    =    0;
  127.     
  128.     for (envcnt = 0; env[envcnt]; envcnt++)    {
  129.         varlen    = strlen(env[envcnt]);
  130.         envsize    +=    varlen+strlen(env[envcnt]+varlen+1)+2;
  131.     }
  132.     
  133.     environ = (char **)     malloc((envcnt+1)*sizeof(char *));
  134.     envpool = (char *)     malloc(envsize);
  135.     
  136.     for (envcnt = 0; env[envcnt]; envcnt++)    {
  137.         environ[envcnt]     = envpool;
  138.         varlen                = strlen(env[envcnt]);
  139.         strcpy(envpool, env[envcnt]);
  140.         envpool              += varlen+1;
  141.         envpool[-1]            = '=';
  142.         strcpy(envpool, env[envcnt]+varlen+1);
  143.         envpool              += strlen(env[envcnt]+varlen+1)+1;
  144.     }
  145.  
  146.     environ[envcnt] = 0;
  147.     
  148.     return environ;
  149. }    
  150.  
  151. typedef struct PD {
  152.     struct PD *    next;
  153.     FILE *        tempFile;
  154.     FSSpec        pipeFile;
  155.     char *        execute;
  156. } PipeDescr, *PipeDescrPtr;
  157.  
  158. static PipeDescrPtr    pipes        =    nil;
  159. static Boolean            sweeper    =    false;
  160.  
  161. void sweep()
  162. {
  163.     while (pipes)
  164.         mypclose(pipes->tempFile);
  165. }
  166.  
  167. FILE * mypopen(char * command, char * mode)
  168. {
  169.     PipeDescrPtr    pipe;
  170.     
  171.     New(666, pipe, 1, PipeDescr);
  172.     
  173.     if (!pipe)
  174.         return NULL;
  175.         
  176.     if (FSpMakeTempFile(&pipe->pipeFile))
  177.         goto failed;
  178.     pipe->execute    =    nil;
  179.     
  180.     switch(*mode)    {
  181.     case 'r':
  182.         /* Ugh ! A hardcoded command  ! */
  183.         
  184.         if (!strcmp(command, "pwd") || !strcmp(command, "Directory")) {
  185.             char curdir[256];
  186.             
  187.             if (!(pipe->tempFile    = fopen(FSp2FullPath(&pipe->pipeFile), "w")))
  188.                 goto delete;
  189.             if (!getcwd(curdir, 256)) 
  190.                 goto delete;
  191.             
  192.             fprintf(pipe->tempFile, "%s\n", curdir);
  193.             fclose(pipe->tempFile);
  194.         } else if (SubLaunch(command, nil, &pipe->pipeFile, nil))
  195.             goto delete;
  196.             
  197.         if (!(pipe->tempFile    = fopen(FSp2FullPath(&pipe->pipeFile), "r")))
  198.             goto delete;
  199.         break;
  200.     case 'w':
  201.         New(667, pipe->execute, strlen(command)+1, char);
  202.         if (!pipe->execute || !(pipe->tempFile    = fopen(FSp2FullPath(&pipe->pipeFile), "w")))
  203.             goto delete;
  204.         strcpy(pipe->execute, command);
  205.         break;
  206.     }
  207.     
  208.     pipe->next    =    pipes;
  209.     pipes            =    pipe;
  210.     
  211.     if (!sweeper)    {
  212.         atexit(sweep);
  213.         sweeper    =    true;
  214.     }
  215.  
  216.     return pipe->tempFile;
  217. delete:
  218.     if (pipe->execute)
  219.         Safefree(pipe->execute);
  220.     HDelete(pipe->pipeFile.vRefNum, pipe->pipeFile.parID, pipe->pipeFile.name);
  221. failed:
  222.     Safefree(pipe);
  223.     
  224.     return NULL;
  225. }
  226.  
  227. int mypclose(FILE *    f)
  228. {
  229.     OSErr                err;
  230.     PipeDescrPtr *    prev;
  231.     PipeDescrPtr    pipe;
  232.     
  233.     for (prev = (PipeDescrPtr *) &pipes; pipe = *prev; prev = &pipe->next)
  234.         if (pipe->tempFile == f)
  235.             break;
  236.     
  237.     if (!pipe)
  238.         return -1;
  239.     
  240.     *prev = pipe->next;
  241.     
  242.     fclose(f);
  243.     
  244.     if (pipe->execute)
  245.         err = SubLaunch(pipe->execute, &pipe->pipeFile, nil, nil);
  246.     else
  247.         err = noErr;
  248.         
  249.     HDelete(pipe->pipeFile.vRefNum, pipe->pipeFile.parID, pipe->pipeFile.name);
  250.     if (pipe->execute)
  251.         Safefree(pipe->execute);
  252.     Safefree(pipe);
  253.     
  254.     return err?-1:0;    
  255. }
  256.  
  257. void SpinMacCursor()
  258. {
  259.     static int delay = 0;
  260.     
  261.     if (StandAlone && ++delay & 31)
  262.         return;
  263.         
  264.     (GUSIGetSpin())(SP_AUTO_SPIN, 1);
  265. }
  266.  
  267. struct tm * gmtime(const time_t * timer)
  268. {
  269.     MachineLocation     loc;
  270.     struct tm *            swatch;
  271.     long                    delta;
  272.     time_t                rolex;
  273.     
  274.     ReadLocation(&loc);
  275.     
  276.     if (!loc.latitude && !loc.longitude)
  277.         return localtime(timer);    /* This is incorrect unless you live in Greenwich */
  278.     
  279.     delta = loc.gmtFlags.gmtDelta & 0xFFFFFF;
  280.     
  281.     if (delta & 0x800000)
  282.         delta = (long) ((unsigned long) delta | 0xFF000000);
  283.         
  284.     rolex = (unsigned long) ((long) *timer - delta);
  285.     
  286.     swatch = localtime(&rolex);
  287.     
  288.     swatch->tm_isdst = (loc.gmtFlags.dlsDelta & 0x80) != 0;
  289.     
  290.     return swatch;
  291.  }
  292.  
  293. void init_missing()
  294. {
  295.     environ         =    nil;
  296.     pipes            =    nil;
  297. }
  298.