/* $Header: util.c,v 4.3.1.2 85/05/15 14:44:27 lwall Exp $
* Revision 4.3.1.2 85/05/15 14:44:27 lwall
* Last arg of execl changed from 0 to Nullch [(char*)0].
* Revision 4.3.1.1 85/05/10 11:41:30 lwall
* Revision 4.3 85/05/01 11:51:44 lwall
* Baseline for release with 4.3bsd.
/* fork and exec a shell command */
register int (*istat
)(), (*qstat
)();
else if ((shell
= getenv("SHELL")) == Nullch
|| !*shell
)
if ((pid
= vfork()) == 0) {
execl(shell
, shell
, "-c", s
, Nullch
);
execl(shell
, shell
, Nullch
, Nullch
, Nullch
);
istat
= signal(SIGINT
, SIG_IGN
);
qstat
= signal(SIGQUIT
, SIG_IGN
);
while ((w
= wait(&status
)) != pid
&& w
!= -1)
sigset(SIGTSTP
,stop_catcher
);
sigset(SIGCONT
,cont_catcher
);
static char nomem
[] = "rn: out of memory!\n";
/* paranoid version of malloc */
ptr
= malloc(size
?size
:1); /* malloc(0) is NASTY on our system */
fputs(nomem
,stdout
) FLUSH
;
/* paranoid version of realloc */
ptr
= realloc(where
,size
?size
:1); /* realloc(0) is NASTY on our system */
fputs(nomem
,stdout
) FLUSH
;
/* safe version of string copy */
register char *dest
= to
;
for (len
--; len
&& (*dest
++ = *from
++); len
--) ;
/* safe version of string concatenate, with \n deletion and space padding */
register char *dest
= to
;
len
--; /* leave room for null */
while (len
&& *dest
++) len
--;
while (len
&& (*dest
++ = *from
++)) len
--;
/* copy a string up to some (non-backslashed) delimiter, if any */
register char *to
, *from
;
for (; *from
; from
++,to
++) {
if (*from
== '\\' && from
[1] == delim
)
/* return ptr to little string in big string, NULL if not found */
register char *t
, *s
, *x
;
for (x
=t
,s
=little
; *s
; x
++,s
++) {
mod
&= 7; /* remove extraneous garbage */
if (stat(filename
, &filestat
) < 0)
protection
= 7 & (filestat
.st_mode
>>
(filestat
.st_uid
== euid
? 6 :
(filestat
.st_gid
== getegid() ? 3 : 0)
if ((mod
& protection
) == mod
)
static struct stat d
, dd
;
static struct direct
*dir
;
if (d
.st_ino
==rino
&& d
.st_dev
==rdev
)
if ((dirp
= opendir(dotdot
)) == Null(DIR *))
prexit("getwd: cannot open ..\n");
if(d
.st_dev
== dd
.st_dev
) {
if(d
.st_ino
== dd
.st_ino
)
if ((dir
= readdir(dirp
)) == Null(struct direct
*))
prexit("getwd: read error in ..\n");
while (dir
->d_ino
!= d
.st_ino
);
if ((dir
= readdir(dirp
)) == Null(struct direct
*))
prexit("getwd: read error in ..\n");
} while(dd
.st_ino
!= d
.st_ino
|| dd
.st_dev
!= d
.st_dev
);
printf("getwd: can't cd back to %s\n",name
) FLUSH
;
while (dir
->d_name
[++i
] != 0);
name
[i
] = dir
->d_name
[i
];
write(2, cp
, strlen(cp
));
getwd(np
) /* shorter but slower */
FILE *pipefp
= popen("/bin/pwd","r");
printf("Can't run /bin/pwd\n") FLUSH
;
np
[strlen(np
)-1] = '\0'; /* wipe out newline */
/* just like fgets but will make bigger buffer as necessary */
get_a_line(original_buffer
,buffer_length
,fp
)
register int buffer_length
;
register char *some_buffer_or_other
= original_buffer
;
if (bufix
>= buffer_length
) {
if (some_buffer_or_other
== original_buffer
) {
some_buffer_or_other
= safemalloc((MEM_SIZE
)buffer_length
+1);
strncpy(some_buffer_or_other
,original_buffer
,buffer_length
/2);
else { /* just grow in place, if possible */
some_buffer_or_other
= saferealloc(some_buffer_or_other
,
(MEM_SIZE
)buffer_length
+1);
if ((nextch
= getc(fp
)) == EOF
)
some_buffer_or_other
[bufix
++] = (char) nextch
;
} while (nextch
&& nextch
!= '\n');
some_buffer_or_other
[bufix
] = '\0';
len_last_line_got
= bufix
;
return some_buffer_or_other
;
/* copy a string to a safe spot */
register char *newaddr
= safemalloc((MEM_SIZE
)(strlen(str
)+1));
makedir(dirname
,nametype
)
register char *tbptr
= tmpbuf
+5;
for (end
= dirname
; *end
; end
++) ; /* find the end */
if (nametype
== MD_FILE
) { /* not to create last component? */
for (--end
; end
!= dirname
&& *end
!= '/'; --end
) ;
return 0; /* nothing to make */
*end
= '\0'; /* isolate file name */
if (stat(dirname
,&filestat
) >= 0) {
/* does this much exist? */
*s
= '/'; /* mark this as existing */
s
= rindex(dirname
,'/'); /* shorten name */
if (!s
) /* relative path! */
break; /* hope they know what they are doing */
*s
= '\0'; /* mark as not existing */
for (s
=dirname
; s
<= end
; s
++) { /* this is grody but efficient */
if (!*s
) { /* something to make? */
sprintf(tbptr
," %s",dirname
);
tbptr
+= strlen(tbptr
); /* make it, sort of */
*s
= '/'; /* mark it made */
if (nametype
== MD_DIR
) /* don't need final slash unless */
*end
= '\0'; /* a filename follows the dir name */
return (tbptr
==tmpbuf
+5 ? 0 : doshell(sh
,tmpbuf
));
sprintf(cmd_buf
,"%s %s %d", filexp(DIRMAKER
), dirname
, nametype
);
return doshell(sh
,cmd_buf
);
static bool firstsetenv
= TRUE
;
register int i
=envix(nam
); /* where does it go? */
if (!environ
[i
]) { /* does not exist yet */
if (firstsetenv
) { /* need we copy environment? */
char **tmpenv
= (char**) /* point our wand at memory */
safemalloc((MEM_SIZE
) (i
+2) * sizeof(char*));
char **tmpenv
= Null(char **);
for (j
=0; j
<i
; j
++) /* copy environment */
environ
= tmpenv
; /* tell exec where it is now */
environ
= (char**) saferealloc((char*) environ
,
(MEM_SIZE
) (i
+2) * sizeof(char*));
/* just expand it a bit */
environ
[i
+1] = Nullch
; /* make sure it's null terminated */
environ
[i
] = safemalloc((MEM_SIZE
) strlen(nam
) + strlen(val
) + 2);
/* this may or may not be in */
/* the old environ structure */
sprintf(environ
[i
],"%s=%s",nam
,val
);/* all that work just for this */
register int i
, len
= strlen(nam
);
for (i
= 0; environ
[i
]; i
++) {
if (strnEQ(environ
[i
],nam
,len
) && environ
[i
][len
] == '=')
break; /* strnEQ must come first to avoid */
printf("\nNo room for feature \"%s\" on this machine.\n",feature
) FLUSH
;
if ((val
= getenv(nam
)) == Nullch
|| !*val
)
/* grow a static string to at least a certain length */
growstr(strptr
,curlen
,newlen
)
if (newlen
> *curlen
) { /* need more room? */
*strptr
= saferealloc(*strptr
,(MEM_SIZE
)newlen
);
*strptr
= safemalloc((MEM_SIZE
)newlen
);
if (*buffer
== ' ' || *buffer
== '\n')
if (*dflt
== '^' && isupper(dflt
[1]))