home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fish 'n' More 2
/
fishmore-publicdomainlibraryvol.ii1991xetec.iso
/
fish
/
languages
/
northc_384
/
clibs
/
cstartup.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-09-09
|
5KB
|
200 lines
/*
(c) 1990 S.Hawtin.
Permission is granted to copy this file provided
1) It is not used for commercial gain
2) This notice is included in all copies
3) Altered copies are marked as such
No liability is accepted for the contents of the file.
Cstartup.c within Public Domain c.lib
The high level startup code for all C programs.
*/
/* This file splits the command arguments into seperate strings and
passes an array of strings to the main routine. */
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <workbench/startup.h>
extern APTR _stdin; /* Standard file handles */
extern APTR _stdout;
extern long _cmndlen;
extern char *_cmndstr;
extern long _cmndnlen;
extern char *_cmndnam;
extern long _fromWB;
extern struct WBStartup *_WBmsg;
extern int stdoutUnbuffered;
char cmndname[16] = ""; /* How do we get the name of this command? */
char *WBargv[3] = {cmndname,NULL,NULL};
FILE *_iob[_NFILE];
_main()
{/* The initial entry point, called once cmndlen and cmndstr have
been set up */
register int argc;
char **argv;
register int count,space,arg_num;
char *cmnd_cpy;
char ch;
register FILE *f_blocks;
f_blocks = (FILE *)calloc(sizeof(FILE),2);
if(!f_blocks)
_libc_error(MALLOC_ZERO);
/* First set up the file handles */
f_blocks->_file = _stdin;
f_blocks->_flag = _IOREAD | _IONBF;
_iob[0] = f_blocks++;
f_blocks->_file = _stdout;
if(stdoutUnbuffered)
f_blocks->_flag = _IOWRT | _IONBF;
else
{f_blocks->_flag = _IOWRT | _IOLBF;
f_blocks->_base = (unsigned char *)malloc(256);
if(f_blocks->_base == 0)
_libc_error(MALLOC_ZERO);
f_blocks->_bsiz = 256;
}
/* Make stderr and stdout the same file pointer */
_iob[1] = f_blocks;
_iob[2] = f_blocks;
/* Now split the command string into arguments */
if(_fromWB)
{/* called from workbench, we just grab the file name argument
if we were called as a project */
struct WBArg *args;
char *arg1;
argv = (char **)calloc(sizeof(char *),_WBmsg->sm_NumArgs);
if(argv == 0)
_libc_error(MALLOC_ZERO);
args = _WBmsg->sm_ArgList;
for(argc=0;argc < _WBmsg->sm_NumArgs;argc++)
{
argv[argc] = args->wa_Name;
CurrentDir(args->wa_Lock);
args++;
}
main(argc,argv);
}
else if(_cmndlen<=1 || _cmndstr==0)
/* Called with no arguments */
main(1,WBargv);
else
{/* We must split the buffer into arguments */
/* Copy the command string to where we can fiddle with it */
cmnd_cpy = malloc((int)(_cmndlen+1));
strncpy(cmnd_cpy,_cmndstr,(int)_cmndlen);
cmnd_cpy[_cmndlen]='\0';
/* Work out the number of arguments */
argc = 1;
space = 1;
for(count=0;count<_cmndlen;count++)
{register char this;
this = cmnd_cpy[count];
if(this=='\"')
{/* Quoted strings are a single argument */
cmnd_cpy[count]='\0';
count++;
while(cmnd_cpy[count]!='\"' && cmnd_cpy[count]!='\0')
count++;
cmnd_cpy[count]='\0';
argc++;
space=1;
}
else if(this==' ' || this=='\t' || this=='\0' ||
this=='\n' || this=='\r')
{if (!space)
space = 1;
cmnd_cpy[count] = '\0';
}
else
{if (space)
{space = 0;
argc++;
}
}
}
/* argc now counts the number of arguments we have */
argv = (char **)calloc(argc+1,sizeof(char *));
space = 1;
arg_num = 1;
argv[0] = cmndname;
for(count=0;count<_cmndlen;count++)
{if(cmnd_cpy[count]=='\0')
{if (!space)
space = 1;
}
else
{if (space)
{space = 0;
argv[arg_num++] = &cmnd_cpy[count];
}
}
}
argv[arg_num] = NULL;
main(argc,argv);
}
exit(0);
}
typedef void (*voidfun)();
#define MAX_EXIT 32
static voidfun exit_funs[MAX_EXIT];
static int exit_fun_count = 0;
exit(code)
int code;
{/* Quit from the program */
register int count;
/* Call the exit functions */
for(count=exit_fun_count-1;count>=0;count--)
(*(exit_funs[count]))();
/* Shut any open files, stdin & stdout are shut by _exit */
for(count=3;count<_NFILE;count++)
{/* Close any open files */
if(_iob[count] && _iob[count]->_file)
fclose(_iob[count]);
}
if(stderr->_file != stdout->_file)
fclose(stderr);
fflush(stdout);
_exit((long)code);
}
int
atexit(func)
voidfun func;
{/* If there is still space register the function */
if(exit_fun_count<MAX_EXIT)
{exit_funs[exit_fun_count] = func;
exit_fun_count++;
return(-1);
}
else
/* Ran out of function slots */
return(0);
}