2012/9/5 David Korn <d...@research.att.com>: [CC:ing ast-developers@research.att.com so that the patch gets archived] > cc: g...@research.att.com olga.kryzhanov...@gmail.com > Subject: Re: New syntax for paths relative to directory fds > (~{dirfd}/foo/bar.txt) ... >> [Mostly Olga's idea (who is away and can't post anyway here since the >> AT&T spam filters have a grudge against her native name... ;-( )] >> >> Woud the following syntax for paths relative to a directory fd "dirfd" >> be possible without violating POSIX (right now the only way to archive >> this is to use /dev/fd/${dirfd}/ ... which works OK but may not be >> suiteable for a new version of the POSIX standard since POSIX does not >> mandate any absolute paths): >> >> ~{dirfd}/foo/bar/txt >> >> (where "dirfd" is a directory fd and "foo/bar/txt" is a path relative >> to this fd) >> >> Example usage: >> -- snip -- >> # print contents of /etc/profile and /etc/ksh.kshrc >> redirect {dirfd}<"/etc" >> cat <~{dirfd}/profile >> cat <~{dirfd}/ksh.kshrc >> -- snip -- >> > > That could be done. The code is in sh/macro.c sh_tilde_expand2(). [snip]
Attached (as "astksh_tilde_fd001.diff.txt") is a prototype patch. Technically it works without problems... but I'm not happy with the |static| variable |devfdname| (short: Ugly |static| variable... which means: Global variable (with function-local scope). Not thread-safe). Question: Is it _valid_ to allocate space from |shp->stk| and return the string as return value of |sh_tilde()| ? Who or what is going to deallocate the memory chunk ? ---- Bye, Roland -- __ . . __ (o.\ \/ /.o) roland.ma...@nrubsig.org \__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer /O /==\ O\ TEL +49 641 3992797 (;O/ \/ \O;)
diff -r -u build_i386_64bit_debug/src/cmd/ksh93/sh/macro.c build_fdsyntax/src/cmd/ksh93/sh/macro.c --- src/cmd/ksh93/sh/macro.c 2012-08-30 22:33:50.000000000 +0200 +++ src/cmd/ksh93/sh/macro.c 2012-09-05 20:13:46.056881468 +0200 @@ -2679,6 +2679,56 @@ cp = nv_getval(sh_scoped(shp,OLDPWDNOD)); return(cp); } + if(c=='{') + { + static char devfdname[32+8]; + char fdnamebuf[PATH_MAX]; + int fd; + const char *s; + char *f; + + for(s=&string[1], f=fdnamebuf ; + (*s!='\0') && (*s!='}') && (s<(string+sizeof(fdnamebuf)-1)) ; + s++, f++) + *f=*s; + *f='\0'; + + if (*s != '}') + return(NIL(char*)); + + /* We assume that variable names cannot start with a digit */ + if (isdigit(fdnamebuf[0])) + { + errno=0; + fd=strtol(fdnamebuf, (char **)NULL, 10); + if(errno!=0) + return(NIL(char*)); + } + else + { + Namval_t *np; + np = nv_open(fdnamebuf, shp->var_tree, NV_VARNAME|NV_NOFAIL|NV_NOADD); + if (!np) + return(NIL(char*)); + fd = (int)nv_getnum(np); + nv_close(np); + } + + if (fd < 0) + return(NIL(char*)); + + /* + * We explicitly convert from string to integer and + * back to ensure that nooone can do any stunts to + * bypass security + */ +#ifdef __SunOS + snprintf(devfdname, sizeof(devfdname), "/proc/self/fd/%d/", fd); +#else + snprintf(devfdname, sizeof(devfdname), "/dev/fd/%d/", fd); +#endif + return (devfdname); + } #if _WINIX if(fcgetc(c)=='/') {
_______________________________________________ ast-developers mailing list ast-developers@research.att.com https://mailman.research.att.com/mailman/listinfo/ast-developers