Skip to content

Commit

Permalink
Merge pull request wryun#128 from jpco/posix
Browse files Browse the repository at this point in the history
Posix and portability improvements
  • Loading branch information
jpco authored Nov 3, 2024
2 parents 535dd9c + 4dbae43 commit a91f68a
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 178 deletions.
5 changes: 5 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ jobs:
name: Build
command: make 'ADDCFLAGS=-DGCDEBUG=1 -DREF_ASSERTIONS=1'

- run:
name: Build test helper
command: make testrun

- persist_to_workspace:
root: .
paths:
- es
- testrun

test:
docker:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ sigmsgs.c
token.h
es
esdump
testrun
7 changes: 5 additions & 2 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ esdump : ${OFILES} dump.o
${CC} -o esdump ${LDFLAGS} ${OFILES} dump.o ${LIBS}

clean :
rm -f es ${OFILES} ${GEN} dump.o initial.o
rm -f es testrun ${OFILES} ${GEN} dump.o initial.o

distclean: clean
rm -f config.cache config.log config.h Makefile cscope.out tags TAGS core cs.out config.status ltmain.sh
Expand All @@ -94,7 +94,10 @@ install : es
$(MKDIR_P) $(DESTDIR)$(datadir)/es
$(INSTALL_DATA) $(srcdir)/share/* $(DESTDIR)$(datadir)/es

test : es $(testdir)/test.es
testrun : $(testdir)/testrun.c
${CC} -o testrun $(testdir)/testrun.c

test : es testrun $(testdir)/test.es
./es -s < $(testdir)/test.es $(testdir)/tests/*

src :
Expand Down
59 changes: 31 additions & 28 deletions access.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,29 @@
#define GROUP 3
#define OTHER 0

#define IFREG 1
#define IFDIR 2
#define IFCHR 3
#define IFBLK 4
#define IFLNK 5
#define IFSOCK 6
#define IFIFO 7

/* ingroupset -- determine whether gid lies in the user's set of groups */
static Boolean ingroupset(gidset_t gid) {
#ifdef NGROUPS
int i;
static int ngroups;
static gidset_t gidset[NGROUPS];
static gidset_t *gidset;
static Boolean initialized = FALSE;
if (!initialized) {
initialized = TRUE;
ngroups = getgroups(NGROUPS, gidset);
ngroups = getgroups(0, gidset);
gidset = ealloc(ngroups * sizeof(gidset_t));
getgroups(ngroups, gidset);
}
for (i = 0; i < ngroups; i++)
if (gid == gidset[i])
return TRUE;
#endif
return FALSE;
}

Expand Down Expand Up @@ -56,16 +64,17 @@ static int testperm(struct stat *stat, unsigned int perm) {

static int testfile(char *path, unsigned int perm, unsigned int type) {
struct stat st;
#ifdef S_IFLNK
if (type == S_IFLNK) {
if (lstat(path, &st) == -1)
return errno;
} else
#endif
if (stat(path, &st) == -1)
return errno;
if (type != 0 && (st.st_mode & S_IFMT) != type)
return EACCES; /* what is an appropriate return value? */
if ((type == IFLNK ? lstat(path, &st) : stat(path, &st)) == -1)
return errno;
/* is EACCES the right return value? */
switch(type) {
case IFREG: if (!S_ISREG(st.st_mode)) return EACCES; break;
case IFDIR: if (!S_ISDIR(st.st_mode)) return EACCES; break;
case IFBLK: if (!S_ISBLK(st.st_mode)) return EACCES; break;
case IFLNK: if (!S_ISLNK(st.st_mode)) return EACCES; break;
case IFSOCK: if (!S_ISSOCK(st.st_mode)) return EACCES; break;
case IFIFO: if (!S_ISFIFO(st.st_mode)) return EACCES; break;
}
return testperm(&st, perm);
}

Expand Down Expand Up @@ -114,19 +123,13 @@ PRIM(access) {
case 'r': perm |= READ; break;
case 'w': perm |= WRITE; break;
case 'x': perm |= EXEC; break;
case 'f': type = S_IFREG; break;
case 'd': type = S_IFDIR; break;
case 'c': type = S_IFCHR; break;
case 'b': type = S_IFBLK; break;
#ifdef S_IFLNK
case 'l': type = S_IFLNK; break;
#endif
#ifdef S_IFSOCK
case 's': type = S_IFSOCK; break;
#endif
#ifdef S_IFIFO
case 'p': type = S_IFIFO; break;
#endif
case 'f': type = IFREG; break;
case 'd': type = IFDIR; break;
case 'c': type = IFCHR; break;
case 'b': type = IFBLK; break;
case 'l': type = IFLNK; break;
case 's': type = IFSOCK; break;
case 'p': type = IFIFO; break;
default:
esoptend();
fail("$&access", "access -%c is not supported on this system", c);
Expand Down Expand Up @@ -177,6 +180,6 @@ extern Dict *initprims_access(Dict *primdict) {
}

extern char *checkexecutable(char *file) {
int err = testfile(file, EXEC, S_IFREG);
int err = testfile(file, EXEC, IFREG);
return err == 0 ? NULL : esstrerror(err);
}
10 changes: 1 addition & 9 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ rm -f conftest*

AC_CANONICAL_HOST

case "$host" in
*sun5* | *solaris2*)
AC_DEFINE(SOLARIS, [], [Are we Solaris?])
;;
esac

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CPP
Expand Down Expand Up @@ -67,12 +61,10 @@ AC_TYPE_SIZE_T

dnl Checks for library functions.
AC_TYPE_GETGROUPS
AC_PROG_GCC_TRADITIONAL
AC_FUNC_MMAP

AC_FUNC_WAIT3
AC_CHECK_FUNCS(strerror strtol lstat setrlimit sigrelse sighold sigaction \
sysconf setsid sigsetjmp)
sysconf setsid sigsetjmp getrusage)

AC_CACHE_CHECK(for an abused getenv, es_cv_abused_getenv,
AC_RUN_IFELSE([AC_LANG_SOURCE([[
Expand Down
10 changes: 0 additions & 10 deletions esconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,6 @@
#endif
#endif /* sgi */


/* SunOS 4.x defaults */

#if sun && !SOLARIS
#ifndef INITIAL_PATH
#define INITIAL_PATH "/usr/ucb", "/usr/bin", ""
#endif
#endif /* sun */


/* HP/UX 9.0.1 -- from [email protected] (Rich $alz) and haahr*/

#if HPUX
Expand Down
2 changes: 1 addition & 1 deletion glob.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static List *dirmatch(const char *prefix, const char *dirname, const char *patte
* is necessary (sigh); the check is done here instead of with the
* opendir to handle a trailing slash.
*/
if (stat(dirname, &s) == -1 || (s.st_mode & S_IFMT) != S_IFDIR)
if (stat(dirname, &s) == -1 || !S_ISDIR(s.st_mode))
return NULL;

if (!haswild(pattern, quote)) {
Expand Down
14 changes: 5 additions & 9 deletions prim-sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#if BSD_LIMITS || BUILTIN_TIME
#include <sys/time.h>
#include <sys/resource.h>
#if !HAVE_WAIT3
#if !HAVE_GETRUSAGE
#include <sys/times.h>
#include <limits.h>
#endif
Expand All @@ -28,17 +28,15 @@ PRIM(newpgrp) {
fail("$&newpgrp", "usage: newpgrp");
pid = getpid();
setpgrp(pid, pid);
#ifdef TIOCSPGRP
{
Sigeffect sigtstp = esignal(SIGTSTP, sig_ignore);
Sigeffect sigttin = esignal(SIGTTIN, sig_ignore);
Sigeffect sigttou = esignal(SIGTTOU, sig_ignore);
ioctl(2, TIOCSPGRP, &pid);
tcsetpgrp(2, pid);
esignal(SIGTSTP, sigtstp);
esignal(SIGTTIN, sigttin);
esignal(SIGTTOU, sigttou);
}
#endif
return ltrue;
}

Expand Down Expand Up @@ -296,8 +294,7 @@ PRIM(limit) {

#if BUILTIN_TIME
PRIM(time) {

#if HAVE_WAIT3
#if HAVE_GETRUSAGE

int pid, status;
time_t t0, t1;
Expand Down Expand Up @@ -326,7 +323,7 @@ PRIM(time) {
RefEnd(lp);
return mklist(mkstr(mkstatus(status)), NULL);

#else /* !HAVE_WAIT3 */
#else /* !HAVE_GETRUSAGE */

int pid, status;
Ref(List *, lp, list);
Expand Down Expand Up @@ -370,8 +367,7 @@ PRIM(time) {
RefEnd(lp);
return mklist(mkstr(mkstatus(status)), NULL);

#endif /* !HAVE_WAIT3 */

#endif /* !HAVE_GETRUSAGE */
}
#endif /* BUILTIN_TIME */

Expand Down
Loading

0 comments on commit a91f68a

Please sign in to comment.