home *** CD-ROM | disk | FTP | other *** search
- /* $Header: tyrathect:Development:Perl::RCS:missing.c,v 1.2 1994/05/04 02:12:43 neeri Exp $
- *
- * Copyright (c) 1991, 1992 Matthias Neeracher
- *
- * You may distribute under the terms of the Perl Artistic License,
- * as specified in the README file.
- *
- * $Log: missing.c,v $
- * Revision 1.2 1994/05/04 02:12:43 neeri
- * Added gmtime(), enabled utime(), reduced spinning.
- *
- * Revision 1.1 1994/03/01 23:38:17 neeri
- * Initial revision
- *
- */
-
- #define RESOLVE_MAC_CONFLICTS
-
- #include "EXTERN.h"
- #include "perl.h"
-
- #include <time.h>
-
- #include <Folders.h>
- #include <Events.h>
- #include <OSUtils.h>
- #include <GUSI.h>
-
- /* Allocate more stdio buffers */
-
- FILE _iob[64] = {
- 0, nil, nil, nil, 0, _IOREAD, 0,
- 0, nil, nil, nil, 0, _IOWRT, 1,
- 0, nil, nil, nil, 0, _IOWRT+_IOLBF, 2
- };
-
- FILE * _lastbuf = _iob + 64;
-
- /* Calls that don't exist on the mac */
-
- /* Borrowed from msdos.c
- * Just pretend that everyone is a superuser
- */
- #define ROOT_UID 0
- #define ROOT_GID 0
- int
- getuid(void)
- {
- return ROOT_UID;
- }
-
- int
- geteuid(void)
- {
- return ROOT_UID;
- }
-
- int
- getgid(void)
- {
- return ROOT_GID;
- }
-
- int
- getegid(void)
- {
- return ROOT_GID;
- }
-
- int
- setuid(int uid)
- {
- return (uid==ROOT_UID?0:-1);
- }
-
- int
- setgid(int gid)
- {
- return (gid==ROOT_GID?0:-1);
- }
-
- execv()
- {
- fatal("execv() not implemented on the Macintosh");
- }
-
- execvp()
- {
- fatal("execvp() not implemented on the Macintosh");
- }
-
- chmod()
- {
- }
-
- kill()
- {
- fatal("kill() not implemented on the Macintosh");
- }
-
- sleep(int seconds)
- {
- long ticks = TickCount() + seconds*60;
-
- while (TickCount() < ticks)
- SpinMacCursor();
- }
-
- do_aspawn()
- {
- fatal("do_aspawn() not implemented on the Macintosh");
- }
-
- do_spawn()
- {
- fatal("do_spawn() not implemented on the Macintosh");
- }
-
- char **environ;
- extern int StandAlone;
-
- char ** init_env(char ** env)
- {
- int envcnt = 0;
- int envsize = 0;
- int varlen;
- char * envpool;
- FILE * envfile = 0;
-
- for (envcnt = 0; env[envcnt]; envcnt++) {
- varlen = strlen(env[envcnt]);
- envsize += varlen+strlen(env[envcnt]+varlen+1)+2;
- }
-
- environ = (char **) malloc((envcnt+1)*sizeof(char *));
- envpool = (char *) malloc(envsize);
-
- for (envcnt = 0; env[envcnt]; envcnt++) {
- environ[envcnt] = envpool;
- varlen = strlen(env[envcnt]);
- strcpy(envpool, env[envcnt]);
- envpool += varlen+1;
- envpool[-1] = '=';
- strcpy(envpool, env[envcnt]+varlen+1);
- envpool += strlen(env[envcnt]+varlen+1)+1;
- }
-
- environ[envcnt] = 0;
-
- return environ;
- }
-
- typedef struct PD {
- struct PD * next;
- FILE * tempFile;
- FSSpec pipeFile;
- char * execute;
- } PipeDescr, *PipeDescrPtr;
-
- static PipeDescrPtr pipes = nil;
- static Boolean sweeper = false;
-
- void sweep()
- {
- while (pipes)
- mypclose(pipes->tempFile);
- }
-
- FILE * mypopen(char * command, char * mode)
- {
- PipeDescrPtr pipe;
-
- New(666, pipe, 1, PipeDescr);
-
- if (!pipe)
- return NULL;
-
- if (FSpMakeTempFile(&pipe->pipeFile))
- goto failed;
- pipe->execute = nil;
-
- switch(*mode) {
- case 'r':
- /* Ugh ! A hardcoded command ! */
-
- if (!strcmp(command, "pwd") || !strcmp(command, "Directory")) {
- char curdir[256];
-
- if (!(pipe->tempFile = fopen(FSp2FullPath(&pipe->pipeFile), "w")))
- goto delete;
- if (!getcwd(curdir, 256))
- goto delete;
-
- fprintf(pipe->tempFile, "%s\n", curdir);
- fclose(pipe->tempFile);
- } else if (SubLaunch(command, nil, &pipe->pipeFile, nil))
- goto delete;
-
- if (!(pipe->tempFile = fopen(FSp2FullPath(&pipe->pipeFile), "r")))
- goto delete;
- break;
- case 'w':
- New(667, pipe->execute, strlen(command)+1, char);
- if (!pipe->execute || !(pipe->tempFile = fopen(FSp2FullPath(&pipe->pipeFile), "w")))
- goto delete;
- strcpy(pipe->execute, command);
- break;
- }
-
- pipe->next = pipes;
- pipes = pipe;
-
- if (!sweeper) {
- atexit(sweep);
- sweeper = true;
- }
-
- return pipe->tempFile;
- delete:
- if (pipe->execute)
- Safefree(pipe->execute);
- HDelete(pipe->pipeFile.vRefNum, pipe->pipeFile.parID, pipe->pipeFile.name);
- failed:
- Safefree(pipe);
-
- return NULL;
- }
-
- int mypclose(FILE * f)
- {
- OSErr err;
- PipeDescrPtr * prev;
- PipeDescrPtr pipe;
-
- for (prev = (PipeDescrPtr *) &pipes; pipe = *prev; prev = &pipe->next)
- if (pipe->tempFile == f)
- break;
-
- if (!pipe)
- return -1;
-
- *prev = pipe->next;
-
- fclose(f);
-
- if (pipe->execute)
- err = SubLaunch(pipe->execute, &pipe->pipeFile, nil, nil);
- else
- err = noErr;
-
- HDelete(pipe->pipeFile.vRefNum, pipe->pipeFile.parID, pipe->pipeFile.name);
- if (pipe->execute)
- Safefree(pipe->execute);
- Safefree(pipe);
-
- return err?-1:0;
- }
-
- void SpinMacCursor()
- {
- static int delay = 0;
-
- if (StandAlone && ++delay & 31)
- return;
-
- (GUSIGetSpin())(SP_AUTO_SPIN, 1);
- }
-
- struct tm * gmtime(const time_t * timer)
- {
- MachineLocation loc;
- struct tm * swatch;
- long delta;
- time_t rolex;
-
- ReadLocation(&loc);
-
- if (!loc.latitude && !loc.longitude)
- return localtime(timer); /* This is incorrect unless you live in Greenwich */
-
- delta = loc.gmtFlags.gmtDelta & 0xFFFFFF;
-
- if (delta & 0x800000)
- delta = (long) ((unsigned long) delta | 0xFF000000);
-
- rolex = (unsigned long) ((long) *timer - delta);
-
- swatch = localtime(&rolex);
-
- swatch->tm_isdst = (loc.gmtFlags.dlsDelta & 0x80) != 0;
-
- return swatch;
- }
-
- void init_missing()
- {
- environ = nil;
- pipes = nil;
- }
-