a7601c638d75717698790da4ba53b54cbeceb3c7
[debian/amanda] / common-src / getcwd.c
1 /* 
2  * getcwd.c --
3  *
4  *      This file provides an implementation of the getcwd procedure
5  *      that uses getwd, for systems with getwd but without getcwd.
6  *
7  * Copyright (c) 1993 The Regents of the University of California.
8  * All rights reserved.
9  *
10  * Permission is hereby granted, without written agreement and without
11  * license or royalty fees, to use, copy, modify, and distribute this
12  * software and its documentation for any purpose, provided that the
13  * above copyright notice and the following two paragraphs appear in
14  * all copies of this software.
15  * 
16  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
17  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
18  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
19  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20  *
21  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
22  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
24  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
25  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
26  */
27
28 /* $Id: getcwd.c,v 1.4 2002/02/11 01:32:10 jrjackson Exp $ */
29
30 #ifndef lint
31 static char rcsid[] = "$Header: /cvsroot/amanda/amanda/common-src/getcwd.c,v 1.4 2002/02/11 01:32:10 jrjackson Exp $ SPRITE (Berkeley)";
32 #endif /* not lint */
33
34 #include <stdio.h>
35 #include <errno.h>
36 #include <sys/param.h>
37
38 extern char *getwd();
39 extern int errno;
40
41 char *
42 getcwd(buf, size)
43     char *buf;                  /* Where to put path for current directory. */
44     size_t size;                        /* Number of bytes at buf. */
45 {
46     char realBuffer[MAXPATHLEN+1];
47     int length;
48
49     if (getwd(realBuffer) == NULL) {
50         /*
51          * There's not much we can do besides guess at an errno to
52          * use for the result (the error message in realBuffer isn't
53          * much use...).
54          */
55
56         errno = EACCES;
57         return NULL;
58     }
59     length = strlen(realBuffer);
60     if (length >= size) {
61         errno = ERANGE;
62         return NULL;
63     }
64     strncpy(buf, realBuffer, size-1);
65     buf[size-1] = '\0';
66     return buf;
67 }