Deleted Added
full compact
ma.setp.c (131962) ma.setp.c (145479)
1/*
2 * Copyright (c) 1990 Carnegie Mellon University
3 * All Rights Reserved.
4 *
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

--- 68 unchanged lines hidden (view full) ---

77 * Added -r switch to reset paths to their default values.
78 *
79 * 06-Jan-86 Glenn Marcy (gm0w) at Carnegie-Mellon University
80 * Created from old setpath program for the shell.
81 *
82 **********************************************************************
83 */
84#include "sh.h"
1/*
2 * Copyright (c) 1990 Carnegie Mellon University
3 * All Rights Reserved.
4 *
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

--- 68 unchanged lines hidden (view full) ---

77 * Added -r switch to reset paths to their default values.
78 *
79 * 06-Jan-86 Glenn Marcy (gm0w) at Carnegie-Mellon University
80 * Created from old setpath program for the shell.
81 *
82 **********************************************************************
83 */
84#include "sh.h"
85RCSID("$Id: ma.setp.c,v 1.13 2003/05/26 07:11:06 christos Exp $")
85RCSID("$Id: ma.setp.c,v 1.14 2004/08/04 17:12:28 christos Exp $")
86
87#ifdef MACH
88
89#define MAXDIRS 64 /* max directories on a path */
90#ifndef NULL
91# define NULL 0
92#endif
93

--- 54 unchanged lines hidden (view full) ---

148static void cncmd __P((char *, int));
149static void change __P((struct pelem *, int, char *));
150static int locate __P((struct pelem *, char *));
151
152
153
154int
155setpath(paths, cmds, localsyspath, dosuffix, printerrors)
86
87#ifdef MACH
88
89#define MAXDIRS 64 /* max directories on a path */
90#ifndef NULL
91# define NULL 0
92#endif
93

--- 54 unchanged lines hidden (view full) ---

148static void cncmd __P((char *, int));
149static void change __P((struct pelem *, int, char *));
150static int locate __P((struct pelem *, char *));
151
152
153
154int
155setpath(paths, cmds, localsyspath, dosuffix, printerrors)
156register char **paths, **cmds, *localsyspath;
156char **paths, **cmds, *localsyspath;
157int dosuffix, printerrors;
158{
157int dosuffix, printerrors;
158{
159 register char *cmd, *cmd1, *cmd2;
160 register int ncmd;
159 char *cmd, *cmd1, *cmd2;
160 int ncmd;
161
162 sflag = dosuffix;
163 eflag = printerrors;
164 if (initpaths(paths) < 0)
165 return(-1);
166 if (npaths == 0)
167 return(0);
168 for (ncmd = 0; cmd = cmds[ncmd]; ncmd++) {

--- 57 unchanged lines hidden (view full) ---

226 }
227 savepaths(paths);
228 freepaths();
229 return(0);
230}
231
232static int
233initpaths(paths)
161
162 sflag = dosuffix;
163 eflag = printerrors;
164 if (initpaths(paths) < 0)
165 return(-1);
166 if (npaths == 0)
167 return(0);
168 for (ncmd = 0; cmd = cmds[ncmd]; ncmd++) {

--- 57 unchanged lines hidden (view full) ---

226 }
227 savepaths(paths);
228 freepaths();
229 return(0);
230}
231
232static int
233initpaths(paths)
234register char **paths;
234char **paths;
235{
235{
236 register char *path, *val, *p, *q;
237 register int i, done;
238 register struct pelem *pe, *pathend;
236 char *path, *val, *p, *q;
237 int i, done;
238 struct pelem *pe, *pathend;
239
240 freepaths();
241 for (npaths = 0; path = paths[npaths]; npaths++) {
242 val = index(path, '=');
243 if (val == NULL) {
244 if (eflag)
245 xprintf(CGETS(10, 3,
246 "setpath: value missing in path '%s'\n"), path);

--- 32 unchanged lines hidden (view full) ---

279 break;
280 }
281 }
282 return(0);
283}
284
285static void
286savepaths(paths)
239
240 freepaths();
241 for (npaths = 0; path = paths[npaths]; npaths++) {
242 val = index(path, '=');
243 if (val == NULL) {
244 if (eflag)
245 xprintf(CGETS(10, 3,
246 "setpath: value missing in path '%s'\n"), path);

--- 32 unchanged lines hidden (view full) ---

279 break;
280 }
281 }
282 return(0);
283}
284
285static void
286savepaths(paths)
287register char **paths;
287char **paths;
288{
288{
289 register char *p, *q;
290 register int npath, i, len;
291 register struct pelem *pe;
289 char *p, *q;
290 int npath, i, len;
291 struct pelem *pe;
292
293 for (npath = 0, pe = pathhead; pe; npath++, pe = pe->pnext) {
294 len = strlen(pe->pname) + 1;
295 if (pe->pdirs == 0)
296 len++;
297 else for (i = 0; i < pe->pdirs; i++)
298 len += strlen(pe->pdir[i]) + 1;
299 p = xmalloc((unsigned)len);

--- 9 unchanged lines hidden (view full) ---

309 }
310 *p = '\0';
311 }
312}
313
314static void
315freepaths()
316{
292
293 for (npath = 0, pe = pathhead; pe; npath++, pe = pe->pnext) {
294 len = strlen(pe->pname) + 1;
295 if (pe->pdirs == 0)
296 len++;
297 else for (i = 0; i < pe->pdirs; i++)
298 len += strlen(pe->pdir[i]) + 1;
299 p = xmalloc((unsigned)len);

--- 9 unchanged lines hidden (view full) ---

309 }
310 *p = '\0';
311 }
312}
313
314static void
315freepaths()
316{
317 register char *p;
318 register int i;
319 register struct pelem *pe;
317 char *p;
318 int i;
319 struct pelem *pe;
320
321 if (npaths == 0 || pathhead == NULL)
322 return;
323 while (pe = pathhead) {
324 if (pe->pname) {
325 for (i = 0; i < pe->pdirs; i++) {
326 if (pe->pdir[i] == NULL)
327 continue;

--- 15 unchanged lines hidden (view full) ---

343/***********************************************
344 *** R E S E T A P A T H N A M E ***
345 ***********************************************/
346
347static void
348rcmd(localsyspath) /* reset path with localsyspath */
349char *localsyspath;
350{
320
321 if (npaths == 0 || pathhead == NULL)
322 return;
323 while (pe = pathhead) {
324 if (pe->pname) {
325 for (i = 0; i < pe->pdirs; i++) {
326 if (pe->pdir[i] == NULL)
327 continue;

--- 15 unchanged lines hidden (view full) ---

343/***********************************************
344 *** R E S E T A P A T H N A M E ***
345 ***********************************************/
346
347static void
348rcmd(localsyspath) /* reset path with localsyspath */
349char *localsyspath;
350{
351 register int n, done;
352 register char *new, *p;
353 register struct pelem *pe;
351 int n, done;
352 char *new, *p;
353 struct pelem *pe;
354 char newbuf[MAXPATHLEN+1];
355
356 for (pe = pathhead; pe; pe = pe->pnext) {
357 new = newbuf;
358 *new = '\0';
359 if (localsyspath != NULL) {
360 *new = ':';
361 (void) strcpy(new + 1, localsyspath);

--- 25 unchanged lines hidden (view full) ---

387/***********************************************
388 *** I N S E R T A P A T H N A M E ***
389 ***********************************************/
390
391static void
392icmd(path, localsyspath) /* insert path before localsyspath */
393char *path, *localsyspath;
394{
354 char newbuf[MAXPATHLEN+1];
355
356 for (pe = pathhead; pe; pe = pe->pnext) {
357 new = newbuf;
358 *new = '\0';
359 if (localsyspath != NULL) {
360 *new = ':';
361 (void) strcpy(new + 1, localsyspath);

--- 25 unchanged lines hidden (view full) ---

387/***********************************************
388 *** I N S E R T A P A T H N A M E ***
389 ***********************************************/
390
391static void
392icmd(path, localsyspath) /* insert path before localsyspath */
393char *path, *localsyspath;
394{
395 register int n;
396 register char *new;
397 register struct pelem *pe;
395 int n;
396 char *new;
397 struct pelem *pe;
398 char newbuf[MAXPATHLEN+1];
399
400 for (pe = pathhead; pe; pe = pe->pnext) {
401 if (sflag)
402 new = localsyspath;
403 else {
404 new = newbuf;
405 (void) strcpy(new, localsyspath);

--- 6 unchanged lines hidden (view full) ---

412 insert(pe, 0, path);
413 }
414}
415
416static void
417iacmd(inpath, path) /* insert path after inpath */
418char *inpath, *path;
419{
398 char newbuf[MAXPATHLEN+1];
399
400 for (pe = pathhead; pe; pe = pe->pnext) {
401 if (sflag)
402 new = localsyspath;
403 else {
404 new = newbuf;
405 (void) strcpy(new, localsyspath);

--- 6 unchanged lines hidden (view full) ---

412 insert(pe, 0, path);
413 }
414}
415
416static void
417iacmd(inpath, path) /* insert path after inpath */
418char *inpath, *path;
419{
420 register int n;
421 register struct pelem *pe;
420 int n;
421 struct pelem *pe;
422
423 for (pe = pathhead; pe; pe = pe->pnext) {
424 n = locate(pe, inpath);
425 if (n >= 0)
426 insert(pe, n + 1, path);
427 else
428 xprintf(CGETS(10, 4, "setpath: %s not found in %s\n"),
429 inpath, pe->pname);
430 }
431}
432
433static void
434ibcmd(inpath, path) /* insert path before inpath */
435char *inpath, *path;
436{
422
423 for (pe = pathhead; pe; pe = pe->pnext) {
424 n = locate(pe, inpath);
425 if (n >= 0)
426 insert(pe, n + 1, path);
427 else
428 xprintf(CGETS(10, 4, "setpath: %s not found in %s\n"),
429 inpath, pe->pname);
430 }
431}
432
433static void
434ibcmd(inpath, path) /* insert path before inpath */
435char *inpath, *path;
436{
437 register int n;
438 register struct pelem *pe;
437 int n;
438 struct pelem *pe;
439
440 for (pe = pathhead; pe; pe = pe->pnext) {
441 n = locate(pe, inpath);
442 if (n >= 0)
443 insert(pe, n, path);
444 else
445 xprintf(CGETS(10, 4, "setpath: %s not found in %s\n"),
446 inpath, pe->pname);
447 }
448}
449
450static void
451incmd(path, n) /* insert path at position n */
452char *path;
453int n;
454{
439
440 for (pe = pathhead; pe; pe = pe->pnext) {
441 n = locate(pe, inpath);
442 if (n >= 0)
443 insert(pe, n, path);
444 else
445 xprintf(CGETS(10, 4, "setpath: %s not found in %s\n"),
446 inpath, pe->pname);
447 }
448}
449
450static void
451incmd(path, n) /* insert path at position n */
452char *path;
453int n;
454{
455 register struct pelem *pe;
455 struct pelem *pe;
456
457 for (pe = pathhead; pe; pe = pe->pnext)
458 insert(pe, n, path);
459}
460
461static void
462insert(pe, loc, key)
456
457 for (pe = pathhead; pe; pe = pe->pnext)
458 insert(pe, n, path);
459}
460
461static void
462insert(pe, loc, key)
463register struct pelem *pe;
464register int loc;
465register char *key;
463struct pelem *pe;
464int loc;
465char *key;
466{
466{
467 register int i;
468 register char *new;
467 int i;
468 char *new;
469 char newbuf[2000];
470
471 if (sflag) { /* add suffix */
472 new = newbuf;
473 (void) strcpy(new, key);
474 (void) strcat(new, pe->psuf);
475 } else
476 new = key;

--- 9 unchanged lines hidden (view full) ---

486/***********************************************
487 *** D E L E T E A P A T H N A M E ***
488 ***********************************************/
489
490static void
491dcmd(path) /* delete path */
492char *path;
493{
469 char newbuf[2000];
470
471 if (sflag) { /* add suffix */
472 new = newbuf;
473 (void) strcpy(new, key);
474 (void) strcat(new, pe->psuf);
475 } else
476 new = key;

--- 9 unchanged lines hidden (view full) ---

486/***********************************************
487 *** D E L E T E A P A T H N A M E ***
488 ***********************************************/
489
490static void
491dcmd(path) /* delete path */
492char *path;
493{
494 register int n;
495 register struct pelem *pe;
494 int n;
495 struct pelem *pe;
496
497 for (pe = pathhead; pe; pe = pe->pnext) {
498 n = locate(pe, path);
499 if (n >= 0)
500 delete(pe, n);
501 else
502 xprintf(CGETS(10, 4, "setpath: %s not found in %s\n"),
503 path, pe->pname);
504 }
505}
506
507static void
508dncmd(n) /* delete at position n */
509int n;
510{
496
497 for (pe = pathhead; pe; pe = pe->pnext) {
498 n = locate(pe, path);
499 if (n >= 0)
500 delete(pe, n);
501 else
502 xprintf(CGETS(10, 4, "setpath: %s not found in %s\n"),
503 path, pe->pname);
504 }
505}
506
507static void
508dncmd(n) /* delete at position n */
509int n;
510{
511 register struct pelem *pe;
511 struct pelem *pe;
512
513 for (pe = pathhead; pe; pe = pe->pnext) {
514 if (n < pe->pdirs)
515 delete(pe, n);
516 else
517 xprintf(CGETS(10, 5,
518 "setpath: %d not valid position in %s\n"),
519 n, pe->pname);
520 }
521}
522
523static void
524delete(pe, n)
512
513 for (pe = pathhead; pe; pe = pe->pnext) {
514 if (n < pe->pdirs)
515 delete(pe, n);
516 else
517 xprintf(CGETS(10, 5,
518 "setpath: %d not valid position in %s\n"),
519 n, pe->pname);
520 }
521}
522
523static void
524delete(pe, n)
525register struct pelem *pe;
525struct pelem *pe;
526int n;
527{
526int n;
527{
528 register int d;
528 int d;
529
530 xfree((ptr_t) (pe->pdir[n]));
531 for (d = n; d < pe->pdirs - 1; d++)
532 pe->pdir[d] = pe->pdir[d+1];
533 --pe->pdirs;
534}
535
536/***********************************************
537 *** C H A N G E A P A T H N A M E ***
538 ***********************************************/
539
540static void
541ccmd(inpath, path) /* change inpath to path */
542char *inpath, *path;
543{
529
530 xfree((ptr_t) (pe->pdir[n]));
531 for (d = n; d < pe->pdirs - 1; d++)
532 pe->pdir[d] = pe->pdir[d+1];
533 --pe->pdirs;
534}
535
536/***********************************************
537 *** C H A N G E A P A T H N A M E ***
538 ***********************************************/
539
540static void
541ccmd(inpath, path) /* change inpath to path */
542char *inpath, *path;
543{
544 register int n;
545 register struct pelem *pe;
544 int n;
545 struct pelem *pe;
546
547 for (pe = pathhead; pe; pe = pe->pnext) {
548 n = locate(pe, inpath);
549 if (n >= 0)
550 change(pe, n, path);
551 else
552 xprintf(CGETS(10, 4, "setpath: %s not found in %s\n"),
553 inpath, pe->pname);
554 }
555}
556
557static void
558cncmd(path, n) /* change at position n to path */
559char *path;
560int n;
561{
546
547 for (pe = pathhead; pe; pe = pe->pnext) {
548 n = locate(pe, inpath);
549 if (n >= 0)
550 change(pe, n, path);
551 else
552 xprintf(CGETS(10, 4, "setpath: %s not found in %s\n"),
553 inpath, pe->pname);
554 }
555}
556
557static void
558cncmd(path, n) /* change at position n to path */
559char *path;
560int n;
561{
562 register struct pelem *pe;
562 struct pelem *pe;
563
564 for (pe = pathhead; pe; pe = pe->pnext) {
565 if (n < pe->pdirs)
566 change(pe, n, path);
567 else
568 xprintf(CGETS(10, 5,
569 "setpath: %d not valid position in %s\n"),
570 n, pe->pname);
571 }
572}
573
574static void
575change(pe, loc, key)
563
564 for (pe = pathhead; pe; pe = pe->pnext) {
565 if (n < pe->pdirs)
566 change(pe, n, path);
567 else
568 xprintf(CGETS(10, 5,
569 "setpath: %d not valid position in %s\n"),
570 n, pe->pname);
571 }
572}
573
574static void
575change(pe, loc, key)
576register struct pelem *pe;
577register int loc;
578register char *key;
576struct pelem *pe;
577int loc;
578char *key;
579{
579{
580 register char *new;
580 char *new;
581 char newbuf[MAXPATHLEN+1];
582
583 if (sflag) { /* append suffix */
584 new = newbuf;
585 (void) strcpy(new, key);
586 (void) strcat(new, pe->psuf);
587 } else
588 new = key;
589 new = strsave(new);
590 xfree((ptr_t) (pe->pdir[loc]));
591 pe->pdir[loc] = new;
592}
593
594/***************************************
595 *** F I N D P A T H N A M E ***
596 ***************************************/
597
598static int
599locate(pe, key)
581 char newbuf[MAXPATHLEN+1];
582
583 if (sflag) { /* append suffix */
584 new = newbuf;
585 (void) strcpy(new, key);
586 (void) strcat(new, pe->psuf);
587 } else
588 new = key;
589 new = strsave(new);
590 xfree((ptr_t) (pe->pdir[loc]));
591 pe->pdir[loc] = new;
592}
593
594/***************************************
595 *** F I N D P A T H N A M E ***
596 ***************************************/
597
598static int
599locate(pe, key)
600register struct pelem *pe;
601register char *key;
600struct pelem *pe;
601char *key;
602{
602{
603 register int i;
604 register char *realkey;
603 int i;
604 char *realkey;
605 char keybuf[MAXPATHLEN+1];
606
607 if (sflag) {
608 realkey = keybuf;
609 (void) strcpy(realkey, key);
610 (void) strcat(realkey, pe->psuf);
611 } else
612 realkey = key;
613 for (i = 0; i < pe->pdirs; i++)
614 if (strcmp(pe->pdir[i], realkey) == 0)
615 break;
616 return((i < pe->pdirs) ? i : -1);
617}
618#endif
605 char keybuf[MAXPATHLEN+1];
606
607 if (sflag) {
608 realkey = keybuf;
609 (void) strcpy(realkey, key);
610 (void) strcat(realkey, pe->psuf);
611 } else
612 realkey = key;
613 for (i = 0; i < pe->pdirs; i++)
614 if (strcmp(pe->pdir[i], realkey) == 0)
615 break;
616 return((i < pe->pdirs) ? i : -1);
617}
618#endif