1*41fbaed0Stron /* $NetBSD: dir_forest.c,v 1.1.1.1 2009/06/23 10:08:59 tron Exp $ */
2*41fbaed0Stron
3*41fbaed0Stron /*++
4*41fbaed0Stron /* NAME
5*41fbaed0Stron /* dir_forest 3
6*41fbaed0Stron /* SUMMARY
7*41fbaed0Stron /* file name to directory forest
8*41fbaed0Stron /* SYNOPSIS
9*41fbaed0Stron /* #include <dir_forest.h>
10*41fbaed0Stron /*
11*41fbaed0Stron /* char *dir_forest(buf, path, depth)
12*41fbaed0Stron /* VSTRING *buf;
13*41fbaed0Stron /* const char *path;
14*41fbaed0Stron /* int depth;
15*41fbaed0Stron /* DESCRIPTION
16*41fbaed0Stron /* This module implements support for directory forests: a file
17*41fbaed0Stron /* organization that introduces one or more levels of intermediate
18*41fbaed0Stron /* subdirectories in order to reduce the number of files per directory.
19*41fbaed0Stron /*
20*41fbaed0Stron /* dir_forest() maps a file basename to a directory forest and
21*41fbaed0Stron /* returns the resulting string: file name "abcd" becomes "a/b/"
22*41fbaed0Stron /* and so on. The number of subdirectory levels is adjustable.
23*41fbaed0Stron /*
24*41fbaed0Stron /* Arguments:
25*41fbaed0Stron /* .IP buf
26*41fbaed0Stron /* A buffer that is overwritten with the result. The result
27*41fbaed0Stron /* ends in "/" and is null terminated. If a null pointer is
28*41fbaed0Stron /* specified, the result is written to a private buffer that
29*41fbaed0Stron /* is overwritten upon each call.
30*41fbaed0Stron /* .IP path
31*41fbaed0Stron /* A null-terminated string of printable characters. Characters
32*41fbaed0Stron /* special to the file system are not permitted.
33*41fbaed0Stron /* The first subdirectory is named after the first character
34*41fbaed0Stron /* in \fIpath\fR, and so on. When the path is shorter than the
35*41fbaed0Stron /* desired number of subdirectory levels, directory names
36*41fbaed0Stron /* of '_' (underscore) are used as replacement.
37*41fbaed0Stron /* .IP depth
38*41fbaed0Stron /* The desired number of subdirectory levels.
39*41fbaed0Stron /* DIAGNOSTICS
40*41fbaed0Stron /* Panic: interface violations. Fatal error: out of memory.
41*41fbaed0Stron /* LICENSE
42*41fbaed0Stron /* .ad
43*41fbaed0Stron /* .fi
44*41fbaed0Stron /* The Secure Mailer license must be distributed with this software.
45*41fbaed0Stron /* AUTHOR(S)
46*41fbaed0Stron /* Wietse Venema
47*41fbaed0Stron /* IBM T.J. Watson Research
48*41fbaed0Stron /* P.O. Box 704
49*41fbaed0Stron /* Yorktown Heights, NY 10598, USA
50*41fbaed0Stron /*--*/
51*41fbaed0Stron
52*41fbaed0Stron /* System library. */
53*41fbaed0Stron
54*41fbaed0Stron #include <sys_defs.h>
55*41fbaed0Stron #include <ctype.h>
56*41fbaed0Stron
57*41fbaed0Stron /* Utility library. */
58*41fbaed0Stron
59*41fbaed0Stron #include "msg.h"
60*41fbaed0Stron #include "dir_forest.h"
61*41fbaed0Stron
62*41fbaed0Stron /* dir_forest - translate base name to directory forest */
63*41fbaed0Stron
dir_forest(VSTRING * buf,const char * path,int depth)64*41fbaed0Stron char *dir_forest(VSTRING *buf, const char *path, int depth)
65*41fbaed0Stron {
66*41fbaed0Stron const char *myname = "dir_forest";
67*41fbaed0Stron static VSTRING *private_buf = 0;
68*41fbaed0Stron int n;
69*41fbaed0Stron const char *cp;
70*41fbaed0Stron int ch;
71*41fbaed0Stron
72*41fbaed0Stron /*
73*41fbaed0Stron * Sanity checks.
74*41fbaed0Stron */
75*41fbaed0Stron if (*path == 0)
76*41fbaed0Stron msg_panic("%s: empty path", myname);
77*41fbaed0Stron if (depth < 1)
78*41fbaed0Stron msg_panic("%s: depth %d", myname, depth);
79*41fbaed0Stron
80*41fbaed0Stron /*
81*41fbaed0Stron * Your buffer or mine?
82*41fbaed0Stron */
83*41fbaed0Stron if (buf == 0) {
84*41fbaed0Stron if (private_buf == 0)
85*41fbaed0Stron private_buf = vstring_alloc(1);
86*41fbaed0Stron buf = private_buf;
87*41fbaed0Stron }
88*41fbaed0Stron
89*41fbaed0Stron /*
90*41fbaed0Stron * Generate one or more subdirectory levels, depending on the pathname
91*41fbaed0Stron * contents. When the pathname is short, use underscores instead.
92*41fbaed0Stron * Disallow non-printable characters or characters that are special to
93*41fbaed0Stron * the file system.
94*41fbaed0Stron */
95*41fbaed0Stron VSTRING_RESET(buf);
96*41fbaed0Stron for (cp = path, n = 0; n < depth; n++) {
97*41fbaed0Stron if ((ch = *cp) == 0) {
98*41fbaed0Stron ch = '_';
99*41fbaed0Stron } else {
100*41fbaed0Stron if (!ISPRINT(ch) || ch == '.' || ch == '/')
101*41fbaed0Stron msg_panic("%s: invalid pathname: %s", myname, path);
102*41fbaed0Stron cp++;
103*41fbaed0Stron }
104*41fbaed0Stron VSTRING_ADDCH(buf, ch);
105*41fbaed0Stron VSTRING_ADDCH(buf, '/');
106*41fbaed0Stron }
107*41fbaed0Stron VSTRING_TERMINATE(buf);
108*41fbaed0Stron
109*41fbaed0Stron if (msg_verbose > 1)
110*41fbaed0Stron msg_info("%s: %s -> %s", myname, path, vstring_str(buf));
111*41fbaed0Stron return (vstring_str(buf));
112*41fbaed0Stron }
113