018b0a4
diff -up ksh20120801/src/cmd/ksh93/sh/subshell.c.orig ksh20120801/src/cmd/ksh93/sh/subshell.c
018b0a4
--- ksh20120801/src/cmd/ksh93/sh/subshell.c.orig	2012-07-17 23:54:21.000000000 +0200
018b0a4
+++ ksh20120801/src/cmd/ksh93/sh/subshell.c	2012-10-24 15:03:44.436870792 +0200
018b0a4
@@ -40,14 +40,6 @@
018b0a4
 #   define PIPE_BUF	512
018b0a4
 #endif
018b0a4
 
018b0a4
-#ifndef O_SEARCH
018b0a4
-#   ifdef O_PATH
018b0a4
-#	define O_SEARCH	O_PATH
018b0a4
-#   else
018b0a4
-#	define O_SEARCH	0
018b0a4
-#   endif
018b0a4
-#endif
018b0a4
-
018b0a4
 /*
018b0a4
  * Note that the following structure must be the same
018b0a4
  * size as the Dtlink_t structure
018b0a4
@@ -84,7 +76,7 @@ static struct subshell
018b0a4
 	char		*pwd;	/* present working directory */
018b0a4
 	const char	*shpwd;	/* saved pointer to sh.pwd */
018b0a4
 	void		*jobs;	/* save job info */
018b0a4
-	int		pwdfd;	/* file descritor for pwd */
018b0a4
+	int		shpwdfd;/* fd for present working directory */
018b0a4
 	mode_t		mask;	/* saved umask */
018b0a4
 	short		tmpfd;	/* saved tmp file descriptor */
018b0a4
 	short		pipefd;	/* read fd if pipe is created */
018b0a4
@@ -101,7 +93,6 @@ static struct subshell
018b0a4
 	int		subdup;
018b0a4
 	char		subshare;
018b0a4
 	char		comsub;
018b0a4
-	char		pwdclose;
018b0a4
 #if SHOPT_COSHELL
018b0a4
 	void		*coshell;
018b0a4
 #endif /* SHOPT_COSHELL */
018b0a4
@@ -518,7 +509,6 @@ Sfio_t *sh_subshell(Shell_t *shp,Shnode_
018b0a4
 		shp->pathinit = 0;
018b0a4
 	}
018b0a4
 	sp->pathlist = path_dup((Pathcomp_t*)shp->pathlist);
018b0a4
-	sp->pwdfd = -1;
018b0a4
 	if(!shp->pwd)
018b0a4
 		path_pwd(shp,0);
018b0a4
 	sp->bckpid = shp->bckpid;
018b0a4
@@ -531,39 +521,14 @@ Sfio_t *sh_subshell(Shell_t *shp,Shnode_
018b0a4
 	shp->subshare = comsub==2 ||  (comsub==1 && sh_isoption(SH_SUBSHARE));
018b0a4
 	if(comsub)
018b0a4
 		shp->comsub = comsub;
018b0a4
+	sp->shpwdfd=-1;
018b0a4
 	if(!comsub || !shp->subshare)
018b0a4
 	{
018b0a4
-		struct subshell *xp;
018b0a4
 		sp->shpwd = shp->pwd;
018b0a4
-#ifdef _lib_fchdir
018b0a4
-		for(xp=sp->prev; xp; xp=xp->prev) 
018b0a4
-		{
018b0a4
-			if(xp->pwdfd>0 && strcmp(xp->pwd,shp->pwd)==0)
018b0a4
-			{
018b0a4
-				sp->pwdfd = xp->pwdfd;
018b0a4
-				break;
018b0a4
-			}
018b0a4
-		}
018b0a4
-		if(sp->pwdfd<0)
018b0a4
-		{
018b0a4
-			int n = open(".",O_RDONLY);
018b0a4
-			if(O_SEARCH && errno==EACCES)
018b0a4
-				n =  open(".",O_RDONLY);
018b0a4
-			if(n>=0)
018b0a4
-			{
018b0a4
-				sp->pwdfd = n;
018b0a4
-				if(n<10)
018b0a4
-				{
ebf9b31
-					sp->pwdfd = sh_fcntl(n,F_DUPFD,10);
018b0a4
-					close(n);
018b0a4
-				}
018b0a4
-				if(sp->pwdfd>0)
018b0a4
-				{
018b0a4
-					fcntl(sp->pwdfd,F_SETFD,FD_CLOEXEC);
018b0a4
-					sp->pwdclose = 1;
018b0a4
-				}
018b0a4
-			}
018b0a4
-		}
018b0a4
+		sp->shpwdfd=((shp->pwdfd >= 0))?sh_fcntl(shp->pwdfd, F_dupfd_cloexec, 10):-1;
018b0a4
+#ifdef O_SEARCH
018b0a4
+		if(sp->shpwdfd<0)
018b0a4
+			errormsg(SH_DICT,ERROR_system(1), "Can't obtain directory fd.");
018b0a4
 #endif
018b0a4
 		sp->pwd = (shp->pwd?strdup(shp->pwd):0);
018b0a4
 		sp->mask = shp->mask;
018b0a4
@@ -741,14 +706,11 @@ Sfio_t *sh_subshell(Shell_t *shp,Shnode_
018b0a4
 			Namval_t *pwdnod = sh_scoped(shp,PWDNOD);
018b0a4
 			if(shp->pwd)
018b0a4
 			{
018b0a4
-				if(sp->pwdfd >=0)
018b0a4
-				{
018b0a4
-					if(fchdir(sp->pwdfd)<0)
018b0a4
-						chdir(sp->pwd);
018b0a4
-				}
018b0a4
-				else
018b0a4
-					chdir(sp->pwd);
018b0a4
 				shp->pwd=sp->pwd;
018b0a4
+#ifndef O_SEARCH
018b0a4
+				if (sp->shpwdfd < 0)
018b0a4
+					chdir(shp->pwd);
018b0a4
+#endif
018b0a4
 				path_newdir(shp,shp->pathlist);
018b0a4
 			}
018b0a4
 			if(nv_isattr(pwdnod,NV_NOFREE))
018b0a4
@@ -762,8 +724,6 @@ Sfio_t *sh_subshell(Shell_t *shp,Shnode_
018b0a4
 		}
018b0a4
 		else
018b0a4
 			free((void*)sp->pwd);
018b0a4
-		if(sp->pwdclose)
018b0a4
-			close(sp->pwdfd);
018b0a4
 		if(sp->mask!=shp->mask)
018b0a4
 			umask(shp->mask=sp->mask);
018b0a4
 		if(shp->coutpipe!=sp->coutpipe)
018b0a4
@@ -775,6 +735,13 @@ Sfio_t *sh_subshell(Shell_t *shp,Shnode_
018b0a4
 		shp->cpipe[1] = sp->cpipe;
018b0a4
 		shp->coutpipe = sp->coutpipe;
018b0a4
 	}
018b0a4
+	if(sp->shpwdfd >=0)
018b0a4
+	{
018b0a4
+		if(shp->pwdfd >=0)
018b0a4
+			sh_close(shp->pwdfd);
018b0a4
+		shp->pwdfd=sp->shpwdfd;
018b0a4
+		fchdir(shp->pwdfd);
018b0a4
+	}
018b0a4
 	shp->subshare = sp->subshare;
018b0a4
 	shp->comsub = sp->comsub;
018b0a4
 	shp->subdup = sp->subdup;
018b0a4
diff -up ksh-20120801/src/cmd/ksh93/bltins/cd_pwd.c.orig ksh-20120801/src/cmd/ksh93/bltins/cd_pwd.c
018b0a4
--- ksh-20120801/src/cmd/ksh93/bltins/cd_pwd.c.orig	2012-08-02 16:50:40.000000000 +0200
018b0a4
+++ ksh-20120801/src/cmd/ksh93/bltins/cd_pwd.c	2012-10-24 15:37:46.814469045 +0200
018b0a4
@@ -38,6 +38,10 @@
018b0a4
 #include	"builtins.h"
018b0a4
 #include	<ls.h>
018b0a4
 
018b0a4
+#ifndef EINTR_REPEAT
018b0a4
+#   define EINTR_REPEAT(expr) while((expr) && (errno == EINTR)) errno=0;
018b0a4
+#endif
018b0a4
+
018b0a4
 /*
018b0a4
  * Invalidate path name bindings to relative paths
018b0a4
  */
018b0a4
@@ -49,6 +53,95 @@ static void rehash(register Namval_t *np
018b0a4
 		_nv_unset(np,0);
018b0a4
 }
018b0a4
 
018b0a4
+/*
018b0a4
+ * Obtain a file handle to the directory "path" relative to directory
018b0a4
+ * "dir", or open a NFSv4 xattr directory handle for file dir/path.
018b0a4
+ */
018b0a4
+int sh_diropenat(Shell_t *shp, int dir, const char *path, bool xattr)
018b0a4
+{
018b0a4
+	int fd,shfd;
018b0a4
+	int savederrno=errno;
018b0a4
+#ifndef AT_FDCWD
018b0a4
+	NOT_USED(dir);
018b0a4
+#endif
018b0a4
+#ifndef O_XATTR
018b0a4
+	NOT_USED(xattr);
018b0a4
+#endif
018b0a4
+
018b0a4
+#ifdef O_XATTR
018b0a4
+	if(xattr)
018b0a4
+	{
018b0a4
+		int apfd; /* attribute parent fd */
018b0a4
+		/* open parent node... */
018b0a4
+		EINTR_REPEAT((apfd = openat(dir, path, O_RDONLY|O_NONBLOCK|O_cloexec)) < 0);
018b0a4
+		if(apfd < 0)
018b0a4
+			return -1;
018b0a4
+
018b0a4
+		/* ... and then open a fd to the attribute directory */
018b0a4
+		 EINTR_REPEAT((fd = openat(apfd, e_dot, O_XATTR|O_cloexec)) < 0);
018b0a4
+
018b0a4
+		savederrno = errno;
018b0a4
+		EINTR_REPEAT(close(apfd) < 0);
018b0a4
+		errno = savederrno;
018b0a4
+	}
018b0a4
+	else
018b0a4
+#endif
018b0a4
+	{
018b0a4
+#ifdef AT_FDCWD
018b0a4
+		/*
018b0a4
+		 * Open directory. First we try without |O_SEARCH| and
018b0a4
+		 * if this fails with EACCESS we try with |O_SEARCH|
018b0a4
+		 * again.
018b0a4
+		 * This is required ...
018b0a4
+		 * - ... because some platforms may require that it can
018b0a4
+		 * only be used for directories while some filesystems
018b0a4
+		 * (e.g. Reiser4 or HSM systems) allow a |fchdir()| into
018b0a4
+		 * files, too)
018b0a4
+		 * - ... to preserve the semantics of "cd", e.g.
018b0a4
+		 * otherwise "cd" would return [No access] instead of
018b0a4
+		 * [Not a directory] for files on filesystems which do
018b0a4
+		 * not allow a "cd" into files.
018b0a4
+		 * - ... to allow that a
018b0a4
+		 * $ redirect {n}
018b0a4
+		 * platforms.
018b0a4
+		 */
018b0a4
+		EINTR_REPEAT((fd = openat(dir, path, O_RDONLY|O_NONBLOCK|O_cloexec)) < 0);
018b0a4
+#   ifdef O_SEARCH
018b0a4
+		if((fd < 0) && (errno == EACCES))
018b0a4
+		{
018b0a4
+			EINTR_REPEAT((fd = openat(dir, path, O_SEARCH|O_cloexec)) < 0)
018b0a4
+		}
018b0a4
+#   endif
018b0a4
+#else
018b0a4
+		/*
018b0a4
+		 * Version of openat() call above for systems without
018b0a4
+		 * openat API. This only works because we basically
018b0a4
+		 * gurantee that |dir| is always the same place as
018b0a4
+		 * |cwd| on such machines (but this won't be the case
018b0a4
+		 * in the future).
018b0a4
+		 */
018b0a4
+		/*
018b0a4
+		 * This |fchdir()| call is not needed (yet) since
018b0a4
+		 * all consumers do not use |dir| when |AT_FDCWD|
018b0a4
+		 * is not available.
018b0a4
+		 *
018b0a4
+		 * fchdir(dir);
018b0a4
+		 */
018b0a4
+		EINTR_REPEAT((fd = open(path, O_cloexec)) < 0);
018b0a4
+#endif
018b0a4
+	}
018b0a4
+
018b0a4
+	if(fd < 0)
018b0a4
+		return fd;
018b0a4
+
018b0a4
+	/* Move fd to a number > 10 and *register* the fd number with the shell */
018b0a4
+	shfd = sh_fcntl(fd, F_dupfd_cloexec, 10);
018b0a4
+	savederrno=errno;
018b0a4
+	sh_close(fd);
018b0a4
+	errno=savederrno;
018b0a4
+	return(shfd);
018b0a4
+}
018b0a4
+
018b0a4
 int	b_cd(int argc, char *argv[],Shbltin_t *context)
018b0a4
 {
018b0a4
 	register char *dir;
018b0a4
@@ -56,18 +149,20 @@ int	b_cd(int argc, char *argv[],Shbltin_
018b0a4
 	register const char *dp;
018b0a4
 	register Shell_t *shp = context->shp;
018b0a4
 	int saverrno=0;
018b0a4
-	int rval,flag=0;
018b0a4
+	int rval;
018b0a4
+	bool flag=false,xattr=false;
018b0a4
 	char *oldpwd;
018b0a4
+	int newdirfd;
018b0a4
 	Namval_t *opwdnod, *pwdnod;
018b0a4
 	if(sh_isoption(SH_RESTRICTED))
018b0a4
 		errormsg(SH_DICT,ERROR_exit(1),e_restricted+4);
018b0a4
 	while((rval = optget(argv,sh_optcd))) switch(rval)
018b0a4
 	{
018b0a4
 		case 'L':
018b0a4
-			flag = 0;
018b0a4
+			flag = false;
018b0a4
 			break;
018b0a4
 		case 'P':
018b0a4
-			flag = 1;
018b0a4
+			flag = true;
018b0a4
 			break;
018b0a4
 		case ':':
018b0a4
 			errormsg(SH_DICT,2, "%s", opt_info.arg);
018b0a4
@@ -179,14 +274,72 @@ int	b_cd(int argc, char *argv[],Shbltin_
018b0a4
 					continue;
018b0a4
 #endif /* SHOPT_FS_3D */
018b0a4
 		}
018b0a4
+		rval = newdirfd = sh_diropenat(shp, shp->pwdfd,
018b0a4
+			path_relative(shp,stakptr(PATH_OFFSET)), xattr);
018b0a4
+		if(newdirfd >=0)
018b0a4
+		{
018b0a4
+			/* chdir for directories on HSM/tapeworms may take minutes */
018b0a4
+			if(fchdir(newdirfd) >= 0)
018b0a4
+			{
018b0a4
+				if(shp->pwdfd >= 0)
018b0a4
+					sh_close(shp->pwdfd);
018b0a4
+				shp->pwdfd=newdirfd;
018b0a4
+				goto success;
018b0a4
+			}
018b0a4
+		}
018b0a4
+#ifndef O_SEARCH
018b0a4
+		else
018b0a4
+		{
018b0a4
 		if((rval=chdir(path_relative(shp,stakptr(PATH_OFFSET)))) >= 0)
018b0a4
-			goto success;
018b0a4
-		if(errno!=ENOENT && saverrno==0)
018b0a4
+			{
018b0a4
+				if(shp->pwdfd >= 0)
018b0a4
+				{
018b0a4
+					sh_close(shp->pwdfd);
018b0a4
+#ifdef AT_FDCWD
018b0a4
+					shp->pwdfd = AT_FDCWD;
018b0a4
+#else
018b0a4
+					shp->pwdfd = -1;
018b0a4
+#endif
018b0a4
+				}
018b0a4
+			}
018b0a4
+		}
018b0a4
+#endif
018b0a4
+		if(saverrno==0)
018b0a4
 			saverrno=errno;
018b0a4
+		if(newdirfd >=0)
018b0a4
+			sh_close(newdirfd);
018b0a4
 	}
018b0a4
 	while(cdpath);
018b0a4
 	if(rval<0 && *dir=='/' && *(path_relative(shp,stakptr(PATH_OFFSET)))!='/')
018b0a4
-		rval = chdir(dir);
018b0a4
+	{
018b0a4
+		rval = newdirfd = sh_diropenat(shp,
018b0a4
+			shp->pwdfd,
018b0a4
+			dir, xattr);
018b0a4
+		if(newdirfd >=0)
018b0a4
+		{
018b0a4
+			/* chdir for directories on HSM/tapeworms may take minutes */
018b0a4
+			if(fchdir(newdirfd) >= 0)
018b0a4
+			{
018b0a4
+				if(shp->pwdfd >= 0)
018b0a4
+					sh_close(shp->pwdfd);
018b0a4
+				shp->pwdfd=newdirfd;
018b0a4
+				goto success;
018b0a4
+			}
018b0a4
+		}
018b0a4
+#ifndef O_SEARCH
018b0a4
+		else
018b0a4
+		{
018b0a4
+			if(chdir(dir) >=0)
018b0a4
+			{
018b0a4
+				if(shp->pwdfd >= 0)
018b0a4
+				{
018b0a4
+					sh_close(shp->pwdfd);
018b0a4
+					shp->pwdfd=-1;
018b0a4
+				}
018b0a4
+			}
018b0a4
+		}
018b0a4
+#endif
018b0a4
+	}
018b0a4
 	/* use absolute chdir() if relative chdir() fails */
018b0a4
 	if(rval<0)
018b0a4
 	{
018b0a4
@@ -213,7 +366,7 @@ success:
018b0a4
 	if(*dir != '/')
018b0a4
 		return(0);
018b0a4
 	nv_putval(opwdnod,oldpwd,NV_RDONLY);
018b0a4
-	flag = strlen(dir);
018b0a4
+	flag = (strlen(dir)>0)?true:false;
018b0a4
 	/* delete trailing '/' */
018b0a4
 	while(--flag>0 && dir[flag]=='/')
018b0a4
 		dir[flag] = 0;
018b0a4
diff -up ksh-20120801/src/cmd/ksh93/include/shell.h.orig ksh-20120801/src/cmd/ksh93/include/shell.h
018b0a4
--- ksh-20120801/src/cmd/ksh93/include/shell.h.orig	2012-07-17 22:07:40.000000000 +0200
018b0a4
+++ ksh-20120801/src/cmd/ksh93/include/shell.h	2012-10-24 15:42:10.756987230 +0200
018b0a4
@@ -145,6 +145,7 @@ struct Shell_s
018b0a4
 	unsigned char	trapnote;	/* set when trap/signal is pending */
018b0a4
 	char		shcomp;		/* set when runing shcomp */
018b0a4
 	short		subshell;	/* set for virtual subshell */
018b0a4
+	int		pwdfd;		/* file descriptor for pwd */
018b0a4
 #ifdef _SH_PRIVATE
018b0a4
 	_SH_PRIVATE
018b0a4
 #endif /* _SH_PRIVATE */
018b0a4
diff -up ksh-20120801/src/cmd/ksh93/sh/init.c.orig ksh-20120801/src/cmd/ksh93/sh/init.c
018b0a4
--- ksh-20120801/src/cmd/ksh93/sh/init.c.orig	2012-05-11 19:19:10.000000000 +0200
018b0a4
+++ ksh-20120801/src/cmd/ksh93/sh/init.c	2012-10-24 15:31:59.659485151 +0200
018b0a4
@@ -1365,6 +1365,18 @@ Shell_t *sh_init(register int argc,regis
018b0a4
 		}
018b0a4
 	}
018b0a4
 	sh_ioinit(shp);
018b0a4
+#ifdef AT_FDCWD
018b0a4
+       shp->pwdfd = sh_diropenat(shp, AT_FDCWD, e_dot, false);
018b0a4
+#else
018b0a4
+       /* Systems without AT_FDCWD/openat() do not use the |dir| argument */
018b0a4
+       shp->pwdfd = sh_diropenat(shp, -1, e_dot, false);
018b0a4
+#endif
018b0a4
+#ifdef O_SEARCH
018b0a4
+       /* This should _never_ happen, guranteed by design and goat sacrifice */
018b0a4
+       if(shp->pwdfd < 0)
018b0a4
+               errormsg(SH_DICT,ERROR_system(1), "Can't obtain directory fd.");
018b0a4
+#endif
018b0a4
+
018b0a4
 	/* initialize signal handling */
018b0a4
 	sh_siginit(shp);
018b0a4
 	stakinstall(NIL(Stak_t*),nospace);
018b0a4
diff -up ksh-20120801/src/cmd/ksh93/sh/xec.c.orig ksh-20120801/src/cmd/ksh93/sh/xec.c
018b0a4
--- ksh-20120801/src/cmd/ksh93/sh/xec.c.orig	2012-07-23 16:49:32.000000000 +0200
018b0a4
+++ ksh-20120801/src/cmd/ksh93/sh/xec.c	2012-10-24 15:35:02.209539671 +0200
018b0a4
@@ -1348,8 +1348,12 @@ int sh_exec(register const Shnode_t *t,
018b0a4
 						{
018b0a4
 							if(!shp->pwd)
018b0a4
 								path_pwd(shp,0);
018b0a4
-							if(shp->pwd)
018b0a4
-								stat(".",&statb);
018b0a4
+#ifndef O_SEARCH
018b0a4
+							else if (shp->pwdfd>=0)
018b0a4
+								fstat(shp->pwdfd,&statb);
018b0a4
+							else if (shp->pwd)
018b0a4
+								stat(e_dot,&statb);
018b0a4
+#endif
018b0a4
 							sfsync(NULL);
018b0a4
 							share = sfset(sfstdin,SF_SHARE,0);
018b0a4
 							sh_onstate(SH_STOPOK);
018b0a4
@@ -1428,14 +1432,32 @@ int sh_exec(register const Shnode_t *t,
018b0a4
 						sh_offstate(SH_NOFORK);
018b0a4
 					if(!(nv_isattr(np,BLT_ENV)))
018b0a4
 					{
018b0a4
-						if(shp->pwd)
018b0a4
+#ifdef O_SEARCH
018b0a4
+						while((fchdir(shp->pwdfd) < 0) && errno==EINTR)
018b0a4
+							errno = 0;
018b0a4
+#else
018b0a4
+						if(shp->pwd || (shp->pwdfd >= 0))
018b0a4
 						{
018b0a4
 							struct stat stata;
018b0a4
 							stat(".",&stata);
018b0a4
 							/* restore directory changed */
018b0a4
 							if(statb.st_ino!=stata.st_ino || statb.st_dev!=stata.st_dev)
018b0a4
-								chdir(shp->pwd);
018b0a4
+							{
018b0a4
+								/* chdir for directories on HSM/tapeworms may take minutes */
018b0a4
+								int err=errno;
018b0a4
+								if(shp->pwdfd >= 0)
018b0a4
+								{
018b0a4
+									 while((fchdir(shp->pwdfd) < 0) && errno==EINTR)
018b0a4
+										errno = err;
018b0a4
+								}
018b0a4
+								else
018b0a4
+								{
018b0a4
+									 while((chdir(shp->pwd) < 0) && errno==EINTR)
018b0a4
+										errno = err;
018b0a4
+								}
018b0a4
+							}
018b0a4
 						}
018b0a4
+#endif /* O_SEARCH */
018b0a4
 						sh_offstate(SH_STOPOK);
018b0a4
 						if(share&SF_SHARE)
018b0a4
 							sfset(sfstdin,SF_PUBLIC|SF_SHARE,1);
018b0a4
diff -up ksh-20120801/src/lib/libast/features/common.orig ksh-20120801/src/lib/libast/features/common
018b0a4
--- ksh-20120801/src/lib/libast/features/common.orig	2011-12-12 20:55:33.000000000 +0100
018b0a4
+++ ksh-20120801/src/lib/libast/features/common	2012-10-24 15:54:35.433885131 +0200
018b0a4
@@ -463,6 +463,66 @@ typ uintptr_t stdint.h inttypes.h no{
018b0a4
 	typedef unsigned _ast_int4_t uintptr_t;
018b0a4
 	#endif
018b0a4
 }end
018b0a4
+typ _Bool = uint8_t
018b0a4
+cat{
018b0a4
+	#if defined(_STDC_C99) || __STDC_VERSION__ >= 199901L
018b0a4
+	#include <stdbool.h>
018b0a4
+	#else
018b0a4
+	#define bool	_Bool
018b0a4
+	#define false	0
018b0a4
+	#define true	1
018b0a4
+	#endif
018b0a4
+}end
018b0a4
+tst key __thread -lpthread note{ __thread keyword exists and works with -lpthread }end execute{
018b0a4
+	#include	<pthread.h>
018b0a4
+	
018b0a4
+	#define INITIAL		1
018b0a4
+	#define LOOP		100
018b0a4
+	
018b0a4
+	static __thread int	specific = INITIAL;
018b0a4
+	static int		global = 0;
018b0a4
+	
018b0a4
+	static void* worker(void* arg)
018b0a4
+	{
018b0a4
+		int	k;
018b0a4
+		int	v;
018b0a4
+		v = (int)(arg - 0);
018b0a4
+		for (k = 0; k < LOOP; ++k)
018b0a4
+		{	
018b0a4
+			specific += v;
018b0a4
+			usleep(1);
018b0a4
+		}
018b0a4
+		if (specific != (INITIAL + LOOP * v)) 
018b0a4
+			global = 1;
018b0a4
+		return 0;
018b0a4
+	}
018b0a4
+	int main()
018b0a4
+	{
018b0a4
+		pthread_t	th[2];
018b0a4
+	
018b0a4
+		if (pthread_create(&th[0], 0, worker, (void*)0 + 5) ||
018b0a4
+		    pthread_create(&th[1], 0, worker, (void*)0 + 7))
018b0a4
+		{
018b0a4
+			NOTE("pthread_create failed");
018b0a4
+			return 1;
018b0a4
+		}
018b0a4
+		pthread_join(th[0], 0);
018b0a4
+		pthread_join(th[1], 0);
018b0a4
+		if (global)
018b0a4
+		{
018b0a4
+			NOTE("__thread variable not thread specific");
018b0a4
+			return 1;
018b0a4
+		}
018b0a4
+		if (specific != INITIAL)
018b0a4
+		{
018b0a4
+			NOTE("main __thread variable changed by another thread");
018b0a4
+			return 1;
018b0a4
+		}
018b0a4
+		return 0;
018b0a4
+	}
018b0a4
+}end no{
018b0a4
+	#define __thread		/* __thread keyword does not exist or does not work with -lpthread */
018b0a4
+}end
018b0a4
 
018b0a4
 tst	- -DTRY=1 - -DTRY=1 -Dvoid=char - -DTRY=2 - -DTRY=3 - -DTRY=4 output{
018b0a4
 	#if _STD_ && _hdr_stdarg