Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.sbin / sup / source / path.c
blobee115828b9b87dff35caed1372659a62ce0ad857
1 /* $NetBSD: path.c,v 1.3 1997/06/17 18:56:27 christos Exp $ */
3 /*
4 * Copyright (c) 1991 Carnegie Mellon University
5 * All Rights Reserved.
7 * Permission to use, copy, modify and distribute this software and its
8 * documentation is hereby granted, provided that both the copyright
9 * notice and this permission notice appear in all copies of the
10 * software, derivative works or modified versions, and any portions
11 * thereof, and that both notices appear in supporting documentation.
13 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
14 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
15 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 * Carnegie Mellon requests users of this software to return to
19 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
20 * School of Computer Science
21 * Carnegie Mellon University
22 * Pittsburgh PA 15213-3890
24 * any improvements or extensions that they make and grant Carnegie the rights
25 * to redistribute these changes.
27 /* path -- break filename into directory and file
29 * path (filename,direc,file);
30 * char *filename,*direc,*file;
31 * filename is input; direc and file are output (user-supplied).
32 * file will not have any trailing /; direc might.
34 * Note these rules:
35 * 1. trailing / are ignored (except as first character)
36 * 2. x/y is x;y where y contains no / (x may contain /)
37 * 3. /y is /;y where y contains no /
38 * 4. y is .;y where y contains no /
39 * 5. is .;. (null filename)
40 * 6. / is /;. (the root directory)
42 * Algorithm is this:
43 * 1. delete trailing / except in first position
44 * 2. if any /, find last one; change to null; y++
45 * else y = x; (x is direc; y is file)
46 * 3. if y is null, y = .
47 * 4. if x equals y, x = .
48 * else if x is null, x = /
50 * HISTORY
51 * 20-Nov-79 Steven Shafer (sas) at Carnegie-Mellon University
52 * Copied verbatim from PDP-11. Still as messy as ever.
53 * Some people have asked for a modification (I think that's a better
54 * idea than a new routine) which will change the directory name
55 * into an absolute pathname if it isn't one already. The change
56 * involves doing a getwd() and prepending that if appropriate, with
57 * a "/" in between that and the directory part of the path.
58 * If you want to be cute, you can also resolve ".."s at that time.
61 #include "supcdefs.h"
62 #include "supextern.h"
64 void
65 path(char *original, char *direc, char *file)
67 char *y;
68 /* x is direc */
69 char *p;
71 /* copy and note the end */
72 p = original;
73 y = direc;
74 while ((*y++ = *p++) != '\0'); /* copy string */
75 /* y now points to first char after null */
76 --y; /* y now points to null */
77 --y; /* y now points to last char of string before
78 * null */
80 /* chop off trailing / except as first character */
81 while (y > direc && *y == '/')
82 --y; /* backpedal past / */
83 /* y now points to char before first trailing / or null */
84 *(++y) = 0; /* chop off end of string */
85 /* y now points to null */
87 /* find last /, if any. If found, change to null and bump y */
88 while (y > direc && *y != '/')
89 --y;
90 /* y now points to / or direc. Note *direc may be / */
91 if (*y == '/') {
92 *y++ = 0;
94 /* find file name part */
95 if (*y)
96 strcpy(file, y);
97 else
98 strcpy(file, ".");
100 /* find directory part */
101 if (direc == y)
102 strcpy(direc, ".");
103 else if (*direc == 0)
104 strcpy(direc, "/");
105 /* else direc already has proper value */