2 * Copyright (c) 1990 Carnegie Mellon University
5 * Permission to use, copy, modify and distribute this software and its
6 * documentation is hereby granted, provided that both the copyright
7 * notice and this permission notice appear in all copies of the
8 * software, derivative works or modified versions, and any portions
9 * thereof, and that both notices appear in supporting documentation.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND CARNEGIE MELLON UNIVERSITY
12 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
13 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT
14 * SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY SPECIAL, DIRECT,
15 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
16 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
17 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * Users of this software agree to return to Carnegie Mellon any
21 * improvements or extensions that they make and grant Carnegie the
22 * rights to redistribute these changes.
24 * Export of this software is permitted only after complying with the
25 * regulations of the U.S. Deptartment of Commerce relating to the
26 * Export of Technical Data.
29 * setpath --- smart interface for setting path variables
31 * usage: setpath(paths, cmds, localsyspath, dosuffix, printerrors)
32 * char **paths, **cmds, *localsyspath;
33 * int dosuffix, printerrors;
35 * The 'paths' argument is a list of pointers to path lists of the
36 * form "name=value" where name is the name of the path and value
37 * is a colon separated list of directories. There can never be
38 * more than MAXDIRS (64) directories in a path.
40 * The 'cmds' argument may be a sequence of any of the following:
41 * -r reset path to default
42 * -i newpath insert newpath before localsyspath
43 * -ia oldpath newpath insert newpath after oldpath
44 * -ib oldpath newpath insert newpath before oldpath
45 * -i# newpath insert newpath at position #
46 * -d oldpath delete oldpath
47 * -d# delete path at position #
48 * -c oldpath newpath change oldpath to newpath
49 * -c# newpath change position # to newpath
51 * The "-i newpath" command is equivilent to "-ib 'localsyspath' newpath".
53 * If 'dosuffix' is true, the appropriate suffix will be added to
54 * all command operands for any system path in 'paths'.
56 * Both of the 'paths' and 'cmds' lists are terminated by a NULL
59 * if 'printerrors' is true, setpath will printf error diagnostics.
61 * WARNING !!!: Under no circumstances should anyone change this
62 * module without fully understanding the impact on the C shell.
63 * The C shell has it's own malloc and printf routines and this
64 * module was carefully written taking that into account. Do not
65 * use any stdio routines from this module except printf.
67 **********************************************************************
70 * Revision 1.4 90/12/11 17:58:44 mja
71 * Add copyright/disclaimer for distribution.
73 * 05-Jun-88 Glenn Marcy (gm0w) at Carnegie-Mellon University
74 * Make all non-entry points static.
76 * 30-Apr-88 Glenn Marcy (gm0w) at Carnegie-Mellon University
77 * Added -r switch to reset paths to their default values.
79 * 06-Jan-86 Glenn Marcy (gm0w) at Carnegie-Mellon University
80 * Created from old setpath program for the shell.
82 **********************************************************************
88 #define MAXDIRS 64 /* max directories on a path */
93 static int npaths; /* # pathlist arguments */
96 struct pelem *pnext; /* pointer to next path */
97 char *pname; /* name of pathlist */
98 char *psuf; /* suffix for pathlist */
99 char *pdef; /* default for pathlist */
100 int pdirs; /* # directories on each pathlist */
101 char *pdir[MAXDIRS]; /* directory names for each pathlist */
109 "PATH", "/bin", ":/usr/ucb:/bin:/usr/bin",
110 "CPATH", "/include", ":/usr/include",
111 "LPATH", "/lib", ":/lib:/usr/lib",
112 "MPATH", "/man", ":/usr/man",
113 "EPATH", "/maclib", "",
121 if (eflag) xprintf(CGETS(10, 1, \
122 "setpath: invalid command '%s'.\n"), cmd); \
128 if (eflag) xprintf(CGETS(10, 2, \
129 "setpath: insufficient arguments to command '%s'.\n"), cmd); \
134 static int initpaths (char **);
135 static void savepaths (char **);
136 static void freepaths (void);
137 static void tcsh_rcmd (char *);
138 static void icmd (char *, char *);
139 static void iacmd (char *, char *);
140 static void ibcmd (char *, char *);
141 static void incmd (char *, int);
142 static void insert (struct pelem *, int, char *);
143 static void dcmd (char *);
144 static void dncmd (int);
145 static void delete (struct pelem *, int);
146 static void ccmd (char *, char *);
147 static void cncmd (char *, int);
148 static void change (struct pelem *, int, char *);
149 static int locate (struct pelem *, char *);
154 setpath(char **paths, char **cmds, char *localsyspath, int dosuffix,
157 char *cmd, *cmd1, *cmd2;
162 if (initpaths(paths) < 0)
166 for (ncmd = 0; cmd = cmds[ncmd]; ncmd++) {
175 tcsh_rcmd(localsyspath);
178 if (cmd[2] == '\0') {
180 if (cmd1 == NULL) TOOFEW;
181 icmd(cmd1, localsyspath);
182 } else if (isdigit(cmd[2])) {
184 if (cmd1 == NULL) TOOFEW;
185 incmd(cmd1, atoi(cmd+2));
186 } else if (cmd[3] != '\0' || (cmd[2] != 'a' && cmd[2] != 'b')) {
190 if (cmd1 == NULL || cmd2 == NULL) TOOFEW;
198 if (cmd[2] == '\0') {
200 if (cmd1 == NULL) TOOFEW;
202 } else if (isdigit(cmd[2]))
209 if (cmd[2] == '\0') {
211 if (cmd1 == NULL || cmd2 == NULL) TOOFEW;
213 } else if (isdigit(cmd[2])) {
215 if (cmd1 == NULL) TOOFEW;
216 cncmd(cmd1, atoi(cmd+2));
231 initpaths(char **paths)
233 char *path, *val, *p, *q;
235 struct pelem *pe, *pathend;
238 for (npaths = 0; path = paths[npaths]; npaths++) {
239 val = index(path, '=');
243 "setpath: value missing in path '%s'\n"), path);
248 pe = xmalloc(sizeof(struct pelem));
249 setzero(pe, sizeof(struct pelem));
250 if (pathhead == NULL)
251 pathhead = pathend = pe;
260 for (i = 0; syspath[i].name; i++)
261 if (strcmp(pe->pname, syspath[i].name) == 0) {
262 pe->psuf = syspath[i].suffix;
263 pe->pdef = syspath[i].defalt;
268 q = index(p = q, ':');
273 pe->pdir[pe->pdirs] = p;
283 savepaths(char **paths)
289 for (npath = 0, pe = pathhead; pe; npath++, pe = pe->pnext) {
290 len = strlen(pe->pname) + 1;
293 else for (i = 0; i < pe->pdirs; i++)
294 len += strlen(pe->pdir[i]) + 1;
295 p = xmalloc((unsigned)len);
297 for (q = pe->pname; *p = *q; p++, q++);
299 if (pe->pdirs != 0) {
300 for (i = 0; i < pe->pdirs; i++) {
301 for (q = pe->pdir[i]; *p = *q; p++, q++);
317 if (npaths == 0 || pathhead == NULL)
319 while (pe = pathhead) {
321 for (i = 0; i < pe->pdirs; i++) {
322 if (pe->pdir[i] == NULL)
333 pathhead = pe->pnext;
339 /***********************************************
340 *** R E S E T A P A T H N A M E ***
341 ***********************************************/
344 tcsh_rcmd(char *localsyspath) /* reset path with localsyspath */
349 char newbuf[MAXPATHLEN+1];/*FIXBUF*/
351 for (pe = pathhead; pe; pe = pe->pnext) {
354 if (localsyspath != NULL) {
356 (void) strcpy(new + 1, localsyspath);
357 (void) strcat(new, pe->psuf);
359 (void) strcat(new, pe->pdef);
360 for (n = 0; n < pe->pdirs; n++) {
361 if (pe->pdir[n] == NULL)
369 new = index(p = new, ':');
370 done = (new == NULL);
374 pe->pdir[pe->pdirs] = p;
382 /***********************************************
383 *** I N S E R T A P A T H N A M E ***
384 ***********************************************/
387 icmd(char *path, char *localsyspath) /* insert path before localsyspath */
392 char newbuf[MAXPATHLEN+1];/*FIXBUF*/
394 for (pe = pathhead; pe; pe = pe->pnext) {
399 (void) strcpy(new, localsyspath);
400 (void) strcat(new, pe->psuf);
411 iacmd(char *inpath, char *path) /* insert path after inpath */
416 for (pe = pathhead; pe; pe = pe->pnext) {
417 n = locate(pe, inpath);
419 insert(pe, n + 1, path);
421 xprintf(CGETS(10, 4, "setpath: %s not found in %s\n"),
427 ibcmd(char *inpath, char *path) /* insert path before inpath */
432 for (pe = pathhead; pe; pe = pe->pnext) {
433 n = locate(pe, inpath);
437 xprintf(CGETS(10, 4, "setpath: %s not found in %s\n"),
443 incmd(char *path, int n) /* insert path at position n */
447 for (pe = pathhead; pe; pe = pe->pnext)
452 insert(struct pelem *pe, int loc, char *key)
456 char newbuf[2000];/*FIXBUF*/
458 if (sflag) { /* add suffix */
460 (void) strcpy(new, key);
461 (void) strcat(new, pe->psuf);
465 for (i = pe->pdirs; i > loc; --i)
466 pe->pdir[i] = pe->pdir[i-1];
473 /***********************************************
474 *** D E L E T E A P A T H N A M E ***
475 ***********************************************/
478 dcmd(char *path) /* delete path */
483 for (pe = pathhead; pe; pe = pe->pnext) {
484 n = locate(pe, path);
488 xprintf(CGETS(10, 4, "setpath: %s not found in %s\n"),
494 dncmd(int n) /* delete at position n */
498 for (pe = pathhead; pe; pe = pe->pnext) {
503 "setpath: %d not valid position in %s\n"),
509 delete(struct pelem *pe, int n)
513 xfree((ptr_t) (pe->pdir[n]));
514 for (d = n; d < pe->pdirs - 1; d++)
515 pe->pdir[d] = pe->pdir[d+1];
519 /***********************************************
520 *** C H A N G E A P A T H N A M E ***
521 ***********************************************/
524 ccmd(char *inpath, char *path) /* change inpath to path */
529 for (pe = pathhead; pe; pe = pe->pnext) {
530 n = locate(pe, inpath);
534 xprintf(CGETS(10, 4, "setpath: %s not found in %s\n"),
540 cncmd(char *path, int n) /* change at position n to path */
544 for (pe = pathhead; pe; pe = pe->pnext) {
549 "setpath: %d not valid position in %s\n"),
555 change(struct pelem *pe, int loc, char *key)
558 char newbuf[MAXPATHLEN+1];/*FIXBUF*/
560 if (sflag) { /* append suffix */
562 (void) strcpy(new, key);
563 (void) strcat(new, pe->psuf);
567 xfree((ptr_t) (pe->pdir[loc]));
571 /***************************************
572 *** F I N D P A T H N A M E ***
573 ***************************************/
576 locate(struct pelem *pe, char *key)
580 char keybuf[MAXPATHLEN+1];/*FIXBUF*/
584 (void) strcpy(realkey, key);
585 (void) strcat(realkey, pe->psuf);
588 for (i = 0; i < pe->pdirs; i++)
589 if (strcmp(pe->pdir[i], realkey) == 0)
591 return((i < pe->pdirs) ? i : -1);