Hi!

----

Attached is a small patch ("ksh93_libcmd_killglobalvars001.diff.txt")
for ast-ksh.2006-09-12 which significantly reduces the size of the
global variables in libcmd  - there are still some |static| buffers and
"cp.c" causes headaches since I don't know how I can pass a pointer to
the child function of |ftwalk()| (the only global variable is now a
pointer to the state structure) ...

Comments/ideas/rants welcome...

----

Bye,
Roland

-- 
  __ .  . __
 (o.\ \/ /.o) roland.mainz at nrubsig.org
  \__\/\/__/  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
  /O /==\ O\  TEL +49 641 7950090
 (;O/ \/ \O;)
-------------- next part --------------
Index: src/lib/libcmd/common/cp.c
===================================================================
--- src/lib/libcmd/common/cp.c  (revision 502)
+++ src/lib/libcmd/common/cp.c  (working copy)
@@ -140,7 +140,7 @@
 #define BAK_number     2               /* append .suffix number suffix */
 #define BAK_simple     3               /* append suffix                */
 
-static struct                          /* program state                */
+struct progstate       /* program state        */
 {
        int             backup;         /* BAK_* type                   */
        int             directory;      /* destination is directory     */
@@ -153,12 +153,12 @@
        int             missmode;       /* default missing dir mode     */
        int             official;       /* move to next view            */
        int             op;             /* {CP,LN,MV}                   */
-       int             pathsiz;        /* state.path buffer size       */
+       int             pathsiz;        /* state->path buffer size      */
        int             perm;           /* permissions to preserve      */
-       int             postsiz;        /* state.path post index        */
+       int             postsiz;        /* state->path post index       */
        int             preserve;       /* preserve { id mode time }    */
        int             recursive;      /* subtrees too                 */
-       int             suflen;         /* strlen(state.suffix)         */
+       int             suflen;         /* strlen(state->suffix)        */
        int             sync;           /* fsync() each file after copy */
        int             uid;            /* caller uid                   */
        int             update;         /* replace only if newer        */
@@ -168,14 +168,16 @@
        int             (*stat)(const char*, struct stat*);     /* stat */
 
        char*           path;           /* to pathname buffer           */
-       char*           opname;         /* state.op message string      */
+       char*           opname;         /* state->op message string     */
        char*           suffix;         /* backup suffix                */
 
        Sfio_t*         tmp;            /* tmp string stream            */
 
        char            text[PATH_MAX]; /* link text buffer             */
-} state;
+};
 
+struct progstate *state;
+
 static char    dot[2] = { '.' };
 
 /*
@@ -190,7 +192,7 @@
        if (tmxtouch(path, tmxgetatime(os), tmxgetmtime(os), TMX_NOTIME, 0))
                error(ERROR_SYSTEM|2, "%s: cannot reset access and modify 
times", path);
        n = ((ns->st_uid != os->st_uid) << 1) | (ns->st_gid != os->st_gid);
-       if (n && chown(state.path, os->st_uid, os->st_gid))
+       if (n && chown(state->path, os->st_uid, os->st_gid))
                switch (n)
                {
                case 01:
@@ -206,7 +208,7 @@
 }
 
 /*
- * visit a single file and state.op to the destination
+ * visit a single file and state->op to the destination
  */
 
 static int
@@ -231,7 +233,7 @@
 
        static int      presiz;
 
-       if (state.interrupt)
+       if (state->interrupt)
                return -1;
        if (ftw->info == FTW_DC)
        {
@@ -243,7 +245,7 @@
        {
                base = ftw->name;
                len = ftw->namelen;
-               if (state.hierarchy)
+               if (state->hierarchy)
                        presiz = -1;
                else
                {
@@ -260,7 +262,7 @@
                        while (base > ftw->name && *(base - 1) != '/')
                                base--;
                        len -= base - ftw->name;
-                       if (state.directory)
+                       if (state->directory)
                                presiz -= len + 1;
                }
        }
@@ -270,29 +272,29 @@
                len = ftw->pathlen - presiz - 1;
        }
        len++;
-       if (state.directory)
+       if (state->directory)
        {
-               if ((state.postsiz + len) > state.pathsiz && !(state.path = 
newof(state.path, char, state.pathsiz = roundof(state.postsiz + len, 
PATH_CHUNK), 0)))
+               if ((state->postsiz + len) > state->pathsiz && !(state->path = 
newof(state->path, char, state->pathsiz = roundof(state->postsiz + len, 
PATH_CHUNK), 0)))
                        error(3, "out of space");
-               if (state.hierarchy && ftw->level == 0 && strchr(base, '/'))
+               if (state->hierarchy && ftw->level == 0 && strchr(base, '/'))
                {
-                       s = state.path + state.postsiz;
+                       s = state->path + state->postsiz;
                        memcpy(s, base, len);
                        while (e = strchr(s, '/'))
                        {
                                *e = 0;
-                               if (access(state.path, F_OK))
+                               if (access(state->path, F_OK))
                                {
-                                       st.st_mode = state.missmode;
+                                       st.st_mode = state->missmode;
                                        if (s = strrchr(s, '/'))
                                        {
                                                *s = 0;
-                                               stat(state.path, &st);
+                                               stat(state->path, &st);
                                                *s = '/';
                                        }
-                                       if (mkdir(state.path, st.st_mode & 
S_IPERM))
+                                       if (mkdir(state->path, st.st_mode & 
S_IPERM))
                                        {
-                                               error(ERROR_SYSTEM|2, "%s: 
cannot create directory -- %s ignored", state.path, ftw->path);
+                                               error(ERROR_SYSTEM|2, "%s: 
cannot create directory -- %s ignored", state->path, ftw->path);
                                                ftw->status = FTW_SKIP;
                                                return 0;
                                        }
@@ -305,32 +307,32 @@
        switch (ftw->info)
        {
        case FTW_DP:
-               if (state.preserve && state.op != LN || ftw->level > 0 && 
(ftw->statb.st_mode & S_IRWXU) != S_IRWXU)
+               if (state->preserve && state->op != LN || ftw->level > 0 && 
(ftw->statb.st_mode & S_IRWXU) != S_IRWXU)
                {
                        if (len && ftw->level > 0)
-                               memcpy(state.path + state.postsiz, base, len);
+                               memcpy(state->path + state->postsiz, base, len);
                        else
-                               state.path[state.postsiz] = 0;
-                       if (stat(state.path, &st))
-                               error(ERROR_SYSTEM|2, "%s: cannot stat", 
state.path);
+                               state->path[state->postsiz] = 0;
+                       if (stat(state->path, &st))
+                               error(ERROR_SYSTEM|2, "%s: cannot stat", 
state->path);
                        else
                        {
-                               if ((ftw->statb.st_mode & S_IPERM) != 
(st.st_mode & S_IPERM) && chmod(state.path, ftw->statb.st_mode & S_IPERM))
-                                       error(ERROR_SYSTEM|2, "%s: cannot reset 
directory mode to %s", state.path, fmtmode(st.st_mode & S_IPERM, 0) + 1);
-                               if (state.preserve)
-                                       preserve(state.path, &st, &ftw->statb);
+                               if ((ftw->statb.st_mode & S_IPERM) != 
(st.st_mode & S_IPERM) && chmod(state->path, ftw->statb.st_mode & S_IPERM))
+                                       error(ERROR_SYSTEM|2, "%s: cannot reset 
directory mode to %s", state->path, fmtmode(st.st_mode & S_IPERM, 0) + 1);
+                               if (state->preserve)
+                                       preserve(state->path, &st, &ftw->statb);
                        }
                }
                return 0;
        case FTW_DNR:
        case FTW_DNX:
        case FTW_D:
-               if (!state.recursive)
+               if (!state->recursive)
                {
                        ftw->status = FTW_SKIP;
-                       if (state.op == CP)
+                       if (state->op == CP)
                                error(1, "%s: directory -- copying as plain 
file", ftw->path);
-                       else if (state.link == link && !state.force)
+                       else if (state->link == link && !state->force)
                        {
                                error(2, "%s: cannot link directory", 
ftw->path);
                                return 0;
@@ -346,32 +348,32 @@
                        ftw->status = FTW_SKIP;
                        /*FALLTHROUGH*/
                case FTW_D:
-                       if (state.directory)
-                               memcpy(state.path + state.postsiz, base, len);
-                       if (!(*state.stat)(state.path, &st))
+                       if (state->directory)
+                               memcpy(state->path + state->postsiz, base, len);
+                       if (!(*state->stat)(state->path, &st))
                        {
                                if (!S_ISDIR(st.st_mode))
                                {
-                                       error(2, "%s: not a directory -- %s 
ignored", state.path, ftw->path);
+                                       error(2, "%s: not a directory -- %s 
ignored", state->path, ftw->path);
                                        return 0;
                                }
                        }
-                       else if (mkdir(state.path, (ftw->statb.st_mode & 
S_IPERM)|(ftw->info == FTW_D ? S_IRWXU : 0)))
+                       else if (mkdir(state->path, (ftw->statb.st_mode & 
S_IPERM)|(ftw->info == FTW_D ? S_IRWXU : 0)))
                        {
-                               error(ERROR_SYSTEM|2, "%s: cannot create 
directory -- %s ignored", state.path, ftw->path);
+                               error(ERROR_SYSTEM|2, "%s: cannot create 
directory -- %s ignored", state->path, ftw->path);
                                ftw->status = FTW_SKIP;
                        }
-                       if (!state.directory)
+                       if (!state->directory)
                        {
-                               state.directory = 1;
-                               state.path[state.postsiz++] = '/';
+                               state->directory = 1;
+                               state->path[state->postsiz++] = '/';
                                presiz--;
                        }
                        return 0;
                }
                break;
        case FTW_NS:
-               if (state.link != pathsetlink)
+               if (state->link != pathsetlink)
                {
                        error(2, "%s: not found", ftw->path);
                        return 0;
@@ -379,7 +381,7 @@
                break;
 #if 0
        case FTW_SL:
-               if (state.op == CP)
+               if (state->op == CP)
                {
                        error(2, "%s: cannot copy non-terminal symbolic link", 
ftw->path);
                        return 0;
@@ -387,16 +389,16 @@
                break;
 #endif
        }
-       if (state.directory)
-               memcpy(state.path + state.postsiz, base, len);
-       if ((*state.stat)(state.path, &st))
+       if (state->directory)
+               memcpy(state->path + state->postsiz, base, len);
+       if ((*state->stat)(state->path, &st))
                st.st_mode = 0;
-       else if (state.update && !S_ISDIR(st.st_mode) && (unsigned 
long)ftw->statb.st_mtime < (unsigned long)st.st_mtime)
+       else if (state->update && !S_ISDIR(st.st_mode) && (unsigned 
long)ftw->statb.st_mtime < (unsigned long)st.st_mtime)
        {
                ftw->status = FTW_SKIP;
                return 0;
        }
-       else if (!state.fs3d || !iview(&st))
+       else if (!state->fs3d || !iview(&st))
        {
                /*
                 * target is in top 3d view
@@ -404,47 +406,47 @@
 
                if (st.st_dev == ftw->statb.st_dev && st.st_ino == 
ftw->statb.st_ino)
                {
-                       if (state.op == MV)
+                       if (state->op == MV)
                        {
                                /*
                                 * let rename() handle it
                                 */
 
-                               if (state.verbose)
-                                       sfputr(sfstdout, state.path, '\n');
+                               if (state->verbose)
+                                       sfputr(sfstdout, state->path, '\n');
                                goto operate;
                        }
-                       if (!state.official)
-                               error(2, "%s: identical to %s", state.path, 
ftw->path);
+                       if (!state->official)
+                               error(2, "%s: identical to %s", state->path, 
ftw->path);
                        return 0;
                }
                if (S_ISDIR(st.st_mode))
                {
-                       error(2, "%s: cannot %s existing directory", 
state.path, state.opname);
+                       error(2, "%s: cannot %s existing directory", 
state->path, state->opname);
                        return 0;
                }
-               if (state.verbose)
-                       sfputr(sfstdout, state.path, '\n');
-               rm = state.op == LN || ftw->info == FTW_SL;
-               if (!rm || !state.force)
+               if (state->verbose)
+                       sfputr(sfstdout, state->path, '\n');
+               rm = state->op == LN || ftw->info == FTW_SL;
+               if (!rm || !state->force)
                {
-                       if ((n = open(state.path, O_RDWR|O_BINARY)) >= 0)
+                       if ((n = open(state->path, O_RDWR|O_BINARY)) >= 0)
                        {
                                close(n);
-                               if (state.force)
+                               if (state->force)
                                        /* ok */;
-                               else if (state.interactive)
+                               else if (state->interactive)
                                {
-                                       if (astquery(-1, "%s %s? ", 
state.opname, state.path))
+                                       if (astquery(-1, "%s %s? ", 
state->opname, state->path))
                                                return 0;
                                }
-                               else if (state.op == LN)
+                               else if (state->op == LN)
                                {
-                                       error(2, "%s: cannot %s existing file", 
state.path, state.opname);
+                                       error(2, "%s: cannot %s existing file", 
state->path, state->opname);
                                        return 0;
                                }
                        }
-                       else if (state.force)
+                       else if (state->force)
                                rm = 1;
                        else
                        {
@@ -452,93 +454,93 @@
 #ifdef ETXTBSY
                                    errno == ETXTBSY ? "``running program''" : 
 #endif
-                                   st.st_uid != state.uid ? "``not owner''" :
+                                   st.st_uid != state->uid ? "``not owner''" :
                                    fmtmode(st.st_mode & 
(S_IRWXU|S_IRWXG|S_IRWXO), 0) + 1;
-                               if (state.interactive)
+                               if (state->interactive)
                                {
-                                       if (astquery(-1, "override protection 
%s for %s? ", protection, state.path))
+                                       if (astquery(-1, "override protection 
%s for %s? ", protection, state->path))
                                                return 0;
                                        rm = 1;
                                }
                                else if (!rm)
                                {
-                                       error(2, "%s: cannot %s %s protection", 
state.path, state.opname, protection);
+                                       error(2, "%s: cannot %s %s protection", 
state->path, state->opname, protection);
                                        return 0;
                                }
                        }
                }
-               switch (state.backup)
+               switch (state->backup)
                {
                case BAK_existing:
                case BAK_number:
                        v = 0;
-                       if (s = strrchr(state.path, '/'))
+                       if (s = strrchr(state->path, '/'))
                        {
-                               e = state.path;
+                               e = state->path;
                                *s++ = 0;
                        }
                        else
                        {
                                e = dot;
-                               s = state.path;
+                               s = state->path;
                        }
                        n = strlen(s);
                        if (fts = fts_open((char**)e, 
FTS_NOCHDIR|FTS_ONEPATH|FTS_PHYSICAL|FTS_NOPOSTORDER|FTS_NOSTAT|FTS_NOSEEDOTDIR,
 NiL))
                        {
                                while (ent = fts_read(fts))
                                {
-                                       if (strneq(s, ent->fts_name, n) && 
ent->fts_name[n] == '.' && strneq(ent->fts_name + n + 1, state.suffix, 
state.suflen) && (m = strtol(ent->fts_name + n + state.suflen + 1, &e, 10)) && 
streq(e, state.suffix) && m > v)
+                                       if (strneq(s, ent->fts_name, n) && 
ent->fts_name[n] == '.' && strneq(ent->fts_name + n + 1, state->suffix, 
state->suflen) && (m = strtol(ent->fts_name + n + state->suflen + 1, &e, 10)) 
&& streq(e, state->suffix) && m > v)
                                                v = m;
                                        if (ent->fts_level)
                                                fts_set(NiL, ent, FTS_SKIP);
                                }
                                fts_close(fts);
                        }
-                       if (s != state.path)
+                       if (s != state->path)
                                *--s = '/';
-                       if (v || state.backup == BAK_number)
+                       if (v || state->backup == BAK_number)
                        {
-                               sfprintf(state.tmp, "%s.%s%d%s", state.path, 
state.suffix, v + 1, state.suffix);
+                               sfprintf(state->tmp, "%s.%s%d%s", state->path, 
state->suffix, v + 1, state->suffix);
                                goto backup;
                        }
                        /*FALLTHROUGH*/
                case BAK_simple:
-                       sfprintf(state.tmp, "%s%s", state.path, state.suffix);
+                       sfprintf(state->tmp, "%s%s", state->path, 
state->suffix);
                backup:
-                       s = sfstruse(state.tmp);
-                       if (rename(state.path, s))
+                       s = sfstruse(state->tmp);
+                       if (rename(state->path, s))
                        {
-                               error(ERROR_SYSTEM|2, "%s: cannot backup to 
%s", state.path, s);
+                               error(ERROR_SYSTEM|2, "%s: cannot backup to 
%s", state->path, s);
                                return 0;
                        }
                        break;
                default:
-                       if (rm && remove(state.path))
+                       if (rm && remove(state->path))
                        {
-                               error(ERROR_SYSTEM|2, "%s: cannot remove", 
state.path);
+                               error(ERROR_SYSTEM|2, "%s: cannot remove", 
state->path);
                                return 0;
                        }
                        break;
                }
        }
  operate:
-       switch (state.op)
+       switch (state->op)
        {
        case MV:
                for (;;)
                {
-                       if (!rename(ftw->path, state.path))
+                       if (!rename(ftw->path, state->path))
                                return 0;
                        if (errno == ENOENT)
                                rm = 1;
-                       else if (!rm && st.st_mode && !remove(state.path))
+                       else if (!rm && st.st_mode && !remove(state->path))
                        {
                                rm = 1;
                                continue;
                        }
                        if (errno != EXDEV && (rm || 
S_ISDIR(ftw->statb.st_mode)))
                        {
-                               error(ERROR_SYSTEM|2, "%s: cannot rename to 
%s", ftw->path, state.path);
+                               error(ERROR_SYSTEM|2, "%s: cannot rename to 
%s", ftw->path, state->path);
                                return 0;
                        }
                        else
@@ -548,28 +550,28 @@
        case CP:
                if (S_ISLNK(ftw->statb.st_mode))
                {
-                       if ((n = pathgetlink(ftw->path, state.text, 
sizeof(state.text) - 1)) < 0)
+                       if ((n = pathgetlink(ftw->path, state->text, 
sizeof(state->text) - 1)) < 0)
                        {
                                error(ERROR_SYSTEM|2, "%s: cannot read symbolic 
link text", ftw->path);
                                return 0;
                        }
-                       state.text[n] = 0;
-                       if (pathsetlink(state.text, state.path))
+                       state->text[n] = 0;
+                       if (pathsetlink(state->text, state->path))
                        {
-                               error(ERROR_SYSTEM|2, "%s: cannot copy symbolic 
link to %s", ftw->path, state.path);
+                               error(ERROR_SYSTEM|2, "%s: cannot copy symbolic 
link to %s", ftw->path, state->path);
                                return 0;
                        }
                }
-               else if (state.op == CP || S_ISREG(ftw->statb.st_mode) || 
S_ISDIR(ftw->statb.st_mode))
+               else if (state->op == CP || S_ISREG(ftw->statb.st_mode) || 
S_ISDIR(ftw->statb.st_mode))
                {
                        if (ftw->statb.st_size > 0 && (rfd = open(ftw->path, 
O_RDONLY|O_BINARY)) < 0)
                        {
                                error(ERROR_SYSTEM|2, "%s: cannot read", 
ftw->path);
                                return 0;
                        }
-                       else if ((wfd = open(state.path, 
O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, ftw->statb.st_mode & state.perm)) < 0)
+                       else if ((wfd = open(state->path, 
O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, ftw->statb.st_mode & state->perm)) < 0)
                        {
-                               error(ERROR_SYSTEM|2, "%s: cannot write", 
state.path);
+                               error(ERROR_SYSTEM|2, "%s: cannot write", 
state->path);
                                if (ftw->statb.st_size > 0)
                                        close(rfd);
                                return 0;
@@ -578,7 +580,7 @@
                        {
                                if (!(ip = sfnew(NiL, NiL, SF_UNBOUND, rfd, 
SF_READ)))
                                {
-                                       error(ERROR_SYSTEM|2, "%s: %s read 
stream error", ftw->path, state.path);
+                                       error(ERROR_SYSTEM|2, "%s: %s read 
stream error", ftw->path, state->path);
                                        close(rfd);
                                        close(wfd);
                                }
@@ -587,7 +589,7 @@
                                        n = 0;
                                        if (!(op = sfnew(NiL, NiL, SF_UNBOUND, 
wfd, SF_WRITE)))
                                        {
-                                               error(ERROR_SYSTEM|2, "%s: %s 
write stream error", ftw->path, state.path);
+                                               error(ERROR_SYSTEM|2, "%s: %s 
write stream error", ftw->path, state->path);
                                                close(wfd);
                                                sfclose(ip);
                                        }
@@ -597,12 +599,12 @@
                                                        n |= 3;
                                                if (!sfeof(ip))
                                                        n |= 1;
-                                               if (sfsync(op) || state.sync && 
fsync(wfd) || sfclose(op))
+                                               if (sfsync(op) || state->sync 
&& fsync(wfd) || sfclose(op))
                                                        n |= 2;
                                                if (sfclose(ip))
                                                        n |= 1;
                                                if (n)
-                                                       error(ERROR_SYSTEM|2, 
"%s: %s %s error", ftw->path, state.path, n == 1 ? ERROR_translate(0, 0, 0, 
"read") : n == 2 ? ERROR_translate(0, 0, 0, "write") : ERROR_translate(0, 0, 0, 
"io"));
+                                                       error(ERROR_SYSTEM|2, 
"%s: %s %s error", ftw->path, state->path, n == 1 ? ERROR_translate(0, 0, 0, 
"read") : n == 2 ? ERROR_translate(0, 0, 0, "write") : ERROR_translate(0, 0, 0, 
"io"));
                                        }
                                }
                        }
@@ -611,9 +613,9 @@
                }
                else if (S_ISBLK(ftw->statb.st_mode) || 
S_ISCHR(ftw->statb.st_mode) || S_ISFIFO(ftw->statb.st_mode))
                {
-                       if (mknod(state.path, ftw->statb.st_mode, 
idevice(&ftw->statb)))
+                       if (mknod(state->path, ftw->statb.st_mode, 
idevice(&ftw->statb)))
                        {
-                               error(ERROR_SYSTEM|2, "%s: cannot copy special 
file to %s", ftw->path, state.path);
+                               error(ERROR_SYSTEM|2, "%s: cannot copy special 
file to %s", ftw->path, state->path);
                                return 0;
                        }
                }
@@ -622,26 +624,26 @@
                        error(2, "%s: cannot copy -- unknown file type 0%o", 
ftw->path, S_ITYPE(ftw->statb.st_mode));
                        return 0;
                }
-               if (state.preserve)
+               if (state->preserve)
                {
                        if (ftw->info != FTW_SL)
                        {
-                               if (stat(state.path, &st))
-                                       error(ERROR_SYSTEM|2, "%s: cannot 
stat", state.path);
+                               if (stat(state->path, &st))
+                                       error(ERROR_SYSTEM|2, "%s: cannot 
stat", state->path);
                                else
                                {
-                                       if ((ftw->statb.st_mode & state.perm) 
!= (st.st_mode & state.perm) && chmod(state.path, ftw->statb.st_mode & 
state.perm))
-                                               error(ERROR_SYSTEM|2, "%s: 
cannot reset mode to %s", state.path, fmtmode(st.st_mode & state.perm, 0) + 1);
-                                       preserve(state.path, &st, &ftw->statb);
+                                       if ((ftw->statb.st_mode & state->perm) 
!= (st.st_mode & state->perm) && chmod(state->path, ftw->statb.st_mode & 
state->perm))
+                                               error(ERROR_SYSTEM|2, "%s: 
cannot reset mode to %s", state->path, fmtmode(st.st_mode & state->perm, 0) + 
1);
+                                       preserve(state->path, &st, &ftw->statb);
                                }
                        }
-                       if (state.op == MV && remove(ftw->path))
+                       if (state->op == MV && remove(ftw->path))
                                error(ERROR_SYSTEM|1, "%s: cannot remove", 
ftw->path);
                }
                break;
        case LN:
-               if ((*state.link)(ftw->path, state.path))
-                       error(ERROR_SYSTEM|2, "%s: cannot link to %s", 
ftw->path, state.path);
+               if ((*state->link)(ftw->path, state->path))
+                       error(ERROR_SYSTEM|2, "%s: cannot link to %s", 
ftw->path, state->path);
                break;
        }
        return 0;
@@ -658,136 +660,138 @@
        int             path_resolve;
        int             standard;
        struct stat     st;
+       struct progstate pg;
+       state = &pg;
 
        if (argc < 0)
        {
-               state.interrupt = 1;
+               state->interrupt = 1;
                return -1;
        }
-       memset(&state, 0, sizeof(state));
+       memset(state, 0, sizeof(struct progstate));
        cmdinit(argv, context, ERROR_CATALOG, ERROR_NOTIFY);
        backup_type = 0;
-       state.flags = FTW_DOT|FTW_MULTIPLE|FTW_TWICE|FTW_NOSEEDOTDIR;
-       state.uid = geteuid();
-       if (!(state.tmp = sfstropen()))
+       state->flags = FTW_DOT|FTW_MULTIPLE|FTW_TWICE|FTW_NOSEEDOTDIR;
+       state->uid = geteuid();
+       if (!(state->tmp = sfstropen()))
                error(ERROR_SYSTEM|3, "out of space [tmp string]");
-       sfputr(state.tmp, usage_head, -1);
+       sfputr(state->tmp, usage_head, -1);
        standard = !strcmp(astconf("CONFORMANCE", NiL, NiL), "standard");
        switch (error_info.id[0])
        {
        case 'c':
        case 'C':
-               sfputr(state.tmp, usage_cp, -1);
-               state.op = CP;
-               state.stat = stat;
+               sfputr(state->tmp, usage_cp, -1);
+               state->op = CP;
+               state->stat = stat;
                path_resolve = -1;
                break;
        case 'l':
        case 'L':
-               sfputr(state.tmp, usage_ln, -1);
-               state.op = LN;
-               state.flags |= FTW_PHYSICAL;
-               state.link = link;
-               state.stat = lstat;
+               sfputr(state->tmp, usage_ln, -1);
+               state->op = LN;
+               state->flags |= FTW_PHYSICAL;
+               state->link = link;
+               state->stat = lstat;
                path_resolve = 1;
                break;
        case 'm':
        case 'M':
-               sfputr(state.tmp, usage_mv, -1);
-               state.op = MV;
-               state.flags |= FTW_PHYSICAL;
-               state.preserve = 1;
-               state.stat = lstat;
+               sfputr(state->tmp, usage_mv, -1);
+               state->op = MV;
+               state->flags |= FTW_PHYSICAL;
+               state->preserve = 1;
+               state->stat = lstat;
                path_resolve = 1;
                break;
        default:
                error(3, "not implemented");
                break;
        }
-       sfputr(state.tmp, usage_tail, -1);
-       usage = sfstruse(state.tmp);
-       state.opname = state.op == CP ? ERROR_translate(0, 0, 0, "overwrite") : 
ERROR_translate(0, 0, 0, "replace");
+       sfputr(state->tmp, usage_tail, -1);
+       usage = sfstruse(state->tmp);
+       state->opname = state->op == CP ? ERROR_translate(0, 0, 0, "overwrite") 
: ERROR_translate(0, 0, 0, "replace");
        for (;;)
        {
                switch (optget(argv, usage))
                {
                case 'a':
-                       state.flags |= FTW_PHYSICAL;
-                       state.preserve = 1;
-                       state.recursive = 1;
+                       state->flags |= FTW_PHYSICAL;
+                       state->preserve = 1;
+                       state->recursive = 1;
                        path_resolve = 1;
                        continue;
                case 'b':
-                       state.backup = 1;
+                       state->backup = 1;
                        continue;
                case 'f':
-                       state.force = 1;
-                       if (state.op != CP || !standard)
-                               state.interactive = 0;
+                       state->force = 1;
+                       if (state->op != CP || !standard)
+                               state->interactive = 0;
                        continue;
                case 'h':
-                       state.hierarchy = 1;
+                       state->hierarchy = 1;
                        continue;
                case 'i':
-                       state.interactive = 1;
-                       if (state.op != CP || !standard)
-                               state.force = 0;
+                       state->interactive = 1;
+                       if (state->op != CP || !standard)
+                               state->force = 0;
                        continue;
                case 'l':
-                       state.op = LN;
-                       state.link = link;
-                       state.stat = lstat;
+                       state->op = LN;
+                       state->link = link;
+                       state->stat = lstat;
                        continue;
                case 'p':
-                       state.preserve = 1;
+                       state->preserve = 1;
                        continue;
                case 'r':
-                       state.recursive = 1;
+                       state->recursive = 1;
                        if (path_resolve < 0)
                                path_resolve = 0;
                        continue;
                case 's':
-                       state.op = LN;
-                       state.link = pathsetlink;
-                       state.stat = lstat;
+                       state->op = LN;
+                       state->link = pathsetlink;
+                       state->stat = lstat;
                        continue;
                case 'u':
-                       state.update = 1;
+                       state->update = 1;
                        continue;
                case 'v':
-                       state.verbose = 1;
+                       state->verbose = 1;
                        continue;
                case 'x':
-                       state.flags |= FTW_MOUNT;
+                       state->flags |= FTW_MOUNT;
                        continue;
                case 'F':
 #if _lib_fsync
-                       state.sync = 1;
+                       state->sync = 1;
 #else
                        error(1, "%s not implemented on this system", 
opt_info.name);
 #endif
                        continue;
                case 'H':
-                       state.flags |= FTW_META|FTW_PHYSICAL;
+                       state->flags |= FTW_META|FTW_PHYSICAL;
                        path_resolve = 1;
                        continue;
                case 'L':
-                       state.flags &= ~FTW_PHYSICAL;
+                       state->flags &= ~FTW_PHYSICAL;
                        path_resolve = 1;
                        continue;
                case 'P':
-                       state.flags &= ~FTW_META;
-                       state.flags |= FTW_PHYSICAL;
+                       state->flags &= ~FTW_META;
+                       state->flags |= FTW_PHYSICAL;
                        path_resolve = 1;
                        continue;
                case 'R':
-                       state.recursive = 1;
-                       state.flags &= ~FTW_META;
-                       state.flags |= FTW_PHYSICAL;
+                       state->recursive = 1;
+                       state->flags &= ~FTW_META;
+                       state->flags |= FTW_PHYSICAL;
                        path_resolve = 1;
                        continue;
                case 'S':
-                       state.suffix = opt_info.arg;
+                       state->suffix = opt_info.arg;
                        continue;
                case 'V':
                        backup_type = opt_info.arg;
@@ -817,10 +821,10 @@
                argc++;
                argv[1] = dot;
        }
-       if (state.backup)
+       if (state->backup)
        {
                if (!(file = backup_type) && !(backup_type = 
getenv("VERSION_CONTROL")))
-                       state.backup = BAK_existing;
+                       state->backup = BAK_existing;
                else
                        switch (strkey(backup_type))
                        {
@@ -832,7 +836,7 @@
                        case HASHKEY1('e'):
                        case HASHKEY3('n','i','l'):
                        case HASHKEY2('n','i'):
-                               state.backup = BAK_existing;
+                               state->backup = BAK_existing;
                                break;
                        case HASHKEY5('n','e','v','e','r'):
                        case HASHKEY4('n','e','v','e'):
@@ -844,7 +848,7 @@
                        case HASHKEY3('s','i','m'):
                        case HASHKEY2('s','i'):
                        case HASHKEY1('s'):
-                               state.backup = BAK_simple;
+                               state->backup = BAK_simple;
                                break;
                        case HASHKEY6('n','u','m','b','e','r'):
                        case HASHKEY5('n','u','m','b','e'):
@@ -852,21 +856,21 @@
                        case HASHKEY3('n','u','m'):
                        case HASHKEY2('n','u'):
                        case HASHKEY1('t'):
-                               state.backup = BAK_number;
+                               state->backup = BAK_number;
                                break;
                        default:
                                if (file)
                                        error(2, "%s: unknown backup type", 
backup_type);
                                break;
                        }
-               if (!state.suffix && !(state.suffix = 
getenv("SIMPLE_BACKUP_SUFFIX")))
-                       state.suffix = "~";
-               state.suflen = strlen(state.suffix);
+               if (!state->suffix && !(state->suffix = 
getenv("SIMPLE_BACKUP_SUFFIX")))
+                       state->suffix = "~";
+               state->suflen = strlen(state->suffix);
        }
        if (argc <= 0 || error_info.errors)
                error(ERROR_USAGE|4, "%s", optusage(NiL));
        if (!path_resolve)
-               state.flags |= ftwflags();
+               state->flags |= ftwflags();
        file = argv[argc];
        argv[argc] = 0;
        if (s = strrchr(file, '/'))
@@ -877,29 +881,29 @@
                        s = 0;
        }
        pathcanon(file, 0);
-       if (!(state.directory = !stat(file, &st) && S_ISDIR(st.st_mode)) && 
argc > 1)
+       if (!(state->directory = !stat(file, &st) && S_ISDIR(st.st_mode)) && 
argc > 1)
                error(ERROR_USAGE|4, "%s", optusage(NiL));
-       if (s && !state.directory)
+       if (s && !state->directory)
                error(3, "%s: not a directory", file);
-       if ((state.fs3d = fs3d(FS3D_TEST)) && strmatch(file, "...|*/...|.../*"))
-               state.official = 1;
-       state.postsiz = strlen(file);
-       state.pathsiz = roundof(state.postsiz + 2, PATH_CHUNK);
-       if (!(state.path = newof(0, char, state.pathsiz, 0)))
+       if ((state->fs3d = fs3d(FS3D_TEST)) && strmatch(file, 
"...|*/...|.../*"))
+               state->official = 1;
+       state->postsiz = strlen(file);
+       state->pathsiz = roundof(state->postsiz + 2, PATH_CHUNK);
+       if (!(state->path = newof(0, char, state->pathsiz, 0)))
                error(3, "out of space");
-       memcpy(state.path, file, state.postsiz + 1);
-       if (state.directory && state.path[state.postsiz - 1] != '/')
-               state.path[state.postsiz++] = '/';
-       if (state.hierarchy)
+       memcpy(state->path, file, state->postsiz + 1);
+       if (state->directory && state->path[state->postsiz - 1] != '/')
+               state->path[state->postsiz++] = '/';
+       if (state->hierarchy)
        {
-               if (!state.directory)
+               if (!state->directory)
                        error(3, "%s: last argument must be a directory", file);
-               state.missmode = st.st_mode;
+               state->missmode = st.st_mode;
        }
-       state.perm = state.uid ? S_IPERM : (S_IPERM & ~S_ISVTX);
-       if (!state.recursive)
-               state.flags |= FTW_TOP;
-       ftwalk((char*)argv, visit, state.flags, NiL);
-       free(state.path);
+       state->perm = state->uid ? S_IPERM : (S_IPERM & ~S_ISVTX);
+       if (!state->recursive)
+               state->flags |= FTW_TOP;
+       ftwalk((char*)argv, visit, state->flags, NiL);
+       free(state->path);
        return error_info.errors != 0;
 }
Index: src/lib/libcmd/common/cat.c
===================================================================
--- src/lib/libcmd/common/cat.c (revision 502)
+++ src/lib/libcmd/common/cat.c (working copy)
@@ -95,14 +95,12 @@
 
 #define printof(c)     ((c)^0100)
 
-static char            states[UCHAR_MAX+1];
-
 /*
  * called for any special output processing
  */
 
 static int
-vcat(Sfio_t *fdin, Sfio_t *fdout, int flags)
+vcat(Sfio_t *fdin, Sfio_t *fdout, int flags, char *states)
 {
        register unsigned char* cp;
        register unsigned char* cpold;
@@ -233,7 +231,8 @@
        char*                   mode;
        int                     att;
        int                     dovcat=0;
-
+       char                    states[UCHAR_MAX+1];
+       
        NoP(argc);
        cmdinit(argv, context, ERROR_CATALOG, 0);
        att = !strcmp(astconf("UNIVERSE", NiL, NiL), "att");
@@ -354,7 +353,7 @@
                if (flags&U_FLAG)
                        sfsetbuf(fp, (void*)fp, -1);
                if (dovcat)
-                       n = vcat(fp, sfstdout, flags);
+                       n = vcat(fp, sfstdout, flags, states);
                else if (sfmove(fp, sfstdout, SF_UNBOUND, -1) >= 0 && sfeof(fp))
                        n = 0;
                else
Index: src/lib/libcmd/common/tee.c
===================================================================
--- src/lib/libcmd/common/tee.c (revision 502)
+++ src/lib/libcmd/common/tee.c (working copy)
@@ -89,7 +89,7 @@
        return(n);
 }
 
-static Sfdisc_t tee_disc = { 0, tee_write, 0, 0, 0 };
+static const Sfdisc_t tee_disc = { 0, tee_write, 0, 0, 0 };
 
 int
 b_tee(int argc, register char** argv, void* context)
Index: src/lib/libcmd/common/expr.c
===================================================================
--- src/lib/libcmd/common/expr.c        (revision 502)
+++ src/lib/libcmd/common/expr.c        (working copy)
@@ -140,10 +140,10 @@
 
 #define numeric(np)    ((np)->type&T_NUM)
 
-static struct Optable_s
+static const struct Optable_s
 {
-       char    opname[3];
-       int     op;
+       const char      opname[3];
+       const int       op;
 }
 optable[] =
 {
Index: src/lib/libcmd/common/fold.c
===================================================================
--- src/lib/libcmd/common/fold.c        (revision 502)
+++ src/lib/libcmd/common/fold.c        (working copy)
@@ -86,9 +86,7 @@
 #define T_SP   5
 #define T_RET  6
 
-static char cols[1<<CHAR_BIT];
-
-static void fold(Sfio_t *in, Sfio_t *out, register int width, const char 
*cont, size_t contsize)
+static void fold(Sfio_t *in, Sfio_t *out, register int width, const char 
*cont, size_t contsize, char *cols)
 {
        register char *cp, *first;
        register int n, col=0, x=0;
@@ -176,6 +174,7 @@
        register char *cp;
        char *cont="\n";
        size_t contsize = 1;
+       char cols[1<<CHAR_BIT];
 
        cmdinit(argv, context, ERROR_CATALOG, 0);
        cols['\t'] = T_TAB;
@@ -233,7 +232,7 @@
                        error_info.errors = 1;
                        continue;
                }
-               fold(fp,sfstdout,width,cont,contsize);
+               fold(fp,sfstdout,width,cont,contsize,cols);
                if(fp!=sfstdin)
                        sfclose(fp);
        }
Index: src/lib/libcmd/common/cut.c
===================================================================
--- src/lib/libcmd/common/cut.c (revision 502)
+++ src/lib/libcmd/common/cut.c (working copy)
@@ -101,8 +101,6 @@
 #define C_NOCHOP       16
 #define C_NONEWLINE    32
 
-static int seqno;
-
 /*
  * compare the first of an array of integers
  */
@@ -112,7 +110,7 @@
        return(*((int*)a) - *((int*)b));
 }
 
-static Cut_t *cutinit(int mode,char *str,int wdelim,int ldelim,size_t reclen)
+static Cut_t *cutinit(int mode,char *str,int wdelim,int ldelim,size_t reclen, 
int *seqno_ptr)
 {
        register int *lp, c, n=0;
        register int range = 0;
@@ -126,7 +124,7 @@
        cuthdr->wdelim = wdelim;
        cuthdr->ldelim = ldelim;
        cuthdr->reclen = reclen;
-       cuthdr->seqno = ++seqno;
+       cuthdr->seqno = ++(*seqno_ptr);
        lp = cuthdr->list;
        while(1) switch(c= *cp++)
        {
@@ -412,6 +410,7 @@
        int     wdelim = '\t';
        int     ldelim = '\n';
        size_t  reclen = 0;
+       int     seqno;
 
        NoP(argc);
        cmdinit(argv, context, ERROR_CATALOG, 0);
@@ -478,7 +477,7 @@
                error(3, "non-empty b, c or f option must be specified");
        if((mode & (C_FIELDS|C_SUPRESS)) == C_SUPRESS)
                error(3, "s option requires f option");
-       cuthdr = cutinit(mode,cp,wdelim,ldelim,reclen);
+       cuthdr = cutinit(mode,cp,wdelim,ldelim,reclen, &seqno);
        if(cp = *argv)
                argv++;
        do

Reply via email to