home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
x
/
volume10
/
contool
/
part02
/
misc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-10-30
|
6KB
|
267 lines
/************************************************************************/
/* Copyright 1988-1990 by Chuck Musciano and Harris Corporation */
/* */
/* Permission to use, copy, modify, and distribute this software */
/* and its documentation for any purpose and without fee is */
/* hereby granted, provided that the above copyright notice */
/* appear in all copies and that both that copyright notice and */
/* this permission notice appear in supporting documentation, and */
/* that the name of Chuck Musciano and Harris Corporation not be */
/* used in advertising or publicity pertaining to distribution */
/* of the software without specific, written prior permission. */
/* Chuck Musciano and Harris Corporation make no representations */
/* about the suitability of this software for any purpose. It is */
/* provided "as is" without express or implied warranty. This */
/* software may not be sold without the prior explicit permission */
/* of Harris Corporation. */
/************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <pwd.h>
#include "manifest.h"
#include "contool.h"
/************************************************************************/
EXPORT int getline(stream, string, max)
FILE *stream;
char *string;
int max;
{ register int i, j;
i = (int) fgets(string, max, stream);
if (i == NULL)
return(EOF);
j = strlen(string);
if (j > 0 && string[j - 1] == '\n')
string[--j] = '\0';
return(j);
}
/************************************************************************/
PRIVATE delarg(argc, argv)
int *argc;
char **argv;
{ char *p;
while (*argv = *(argv+1))
argv++;
(*argc)--;
}
/************************************************************************/
EXPORT char getopt(argc, argv, opts, parm)
int *argc;
char **argv;
char *opts;
char **parm;
{ char c, *p, *strcpy(), *index();
int killed;
*parm = NULL;
while (*argv && ((**argv != '-') || (*(*argv+1) == '\0')))
argv++;
if (*argv == NULL)
return(EOF);
c = *(*argv+1);
*++(*argv) = '-';
if (killed = (*(*argv+1) == '\0'))
delarg(argc, argv);
if ((p = index(opts, c)) == NULL)
c = '\0';
else if (*(p+1) == ':') {
*parm = killed ? *argv : *argv+1;
delarg(argc, argv);
}
return(c);
}
/************************************************************************/
EXPORT int verify(source, valid)
char *source;
char *valid;
{ register char *s;
for ( ; *source; source++) {
for (s = valid; *s && *s != *source; s++)
;
if (*s == '\0')
return(0);
}
return(1);
}
/************************************************************************/
EXPORT char **saveargs(argc, argv)
int argc;
char **argv;
{ int i;
char **copy;
copy = (char **) malloc((argc + 1) * sizeof(char *));
for (i = 0; i < argc; i++)
strcpy(copy[i] = (char *) malloc(strlen(argv[i]) + 1), argv[i]);
copy[i] = (char *) 0;
return(copy);
}
#define SIZE_INCREMENT 8
/************************************************************************/
EXPORT char **tokenize(line, argc)
char *line;
int *argc;
{ char match, **argv, *buf, *p;
int limit;
buf = (char *) malloc(strlen(line) + 1);
*argc = 0;
argv = (char **) malloc((limit = SIZE_INCREMENT) * sizeof(char *));
while (*line) {
while (isspace(*line))
line++;
switch (*line) {
case '"' :
case '\'' : match = *line++; /* remove the quote mark */
for (p = buf; *line && (*line != match); ) {
if (*line == '\\')
line++;
if (*line)
*p++ = *line++;
}
if (*line)
line++; /* wipe out quote mark */
break;
default : for (p = buf; *line && !isspace(*line) && (*line != '"') && (*line != '\''); )
*p++ = *line++;
break;
}
*p = '\0';
if (*buf) {
argv[(*argc)++] = strsave(buf);
if (*argc == limit)
argv = (char **) realloc(argv, (limit += SIZE_INCREMENT) * sizeof(char *));
}
}
free(buf);
argv[*argc] = (char *) 0;
return(argv);
}
#define P_POS 5
#define L_POS 8
#define D_POS 9
#define PATH "/dev/ptyp0"
#define LETTERS "pqr"
#define DIGITS "0123456789abcdef"
PRIVATE char path[12];
/************************************************************************/
EXPORT char *open_psuedo_tty(master, m_mode, slave, s_mode)
FILE **master;
char *m_mode;
FILE **slave;
char *s_mode;
{ char *s, *t;
strcpy(path, PATH);
for (s = LETTERS; *s && *master == NULL; s++) {
path[L_POS] = *s;
for (t = DIGITS; *t && *master == NULL; t++) {
path[D_POS] = *t;
*master = fopen(path, m_mode);
}
}
if (*master != NULL) {
path[P_POS] = 't';
*slave = fopen(path, s_mode);
path[P_POS] = 'p';
}
return(path);
}
/************************************************************************/
EXPORT char *expand_tilde(path)
char *path;
{ char s[1024], *p;
struct passwd *pw;
if (*path == '~') {
if (path[1] == '/' || path[1] == '\0') {
strcpy(s, getenv("HOME"));
strcat(s, path + 1);
}
else {
if ((p = index(path, '/')) != NULL)
*p = '\0';
if ((pw = getpwnam(path + 1)) != NULL) {
strcpy(s, pw->pw_dir);
if (p != NULL) {
strcat(s, "/");
strcat(s, p + 1);
}
}
else {
if (p != NULL)
*p = '/';
strcpy(s, path);
}
}
return(strsave(s));
}
else
return(strsave(path));
}
/************************************************************************/
EXPORT int is_empty(s)
char *s;
{
if (s == NULL)
return(TRUE);
for (; *s; s++)
if (!isspace(*s))
return(FALSE);
return(TRUE);
}
/************************************************************************/
EXPORT char *check_escapes(s)
char *s;
{ static char buf[1024];
char *p;
for (p = buf; *s; s++, p++)
if (*s == '\\' || *s == '"') {
*p++ = '\\';
*p = *s;
}
else
*p = *s;
*p = '\0';
return(buf);
}