File: Cns_selectsrvr.c

package info (click to toggle)
lfc-postgres 1.7.4.7-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,676 kB
  • ctags: 10,779
  • sloc: ansic: 146,136; sh: 13,176; perl: 11,142; python: 5,529; cpp: 5,113; sql: 1,790; makefile: 861; fortran: 113
file content (102 lines) | stat: -rw-r--r-- 2,643 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright (C) 2000-2004 by CERN/IT/PDP/DM
 * All rights reserved
 */

#ifndef lint
static char sccsid[] = "@(#)$RCSfile: Cns_selectsrvr.c,v $ $Revision: 1.2 $ $Date: 2005/11/24 09:57:14 $ CERN IT-PDP/DM Jean-Philippe Baud";
#endif /* not lint */

/*	Cns_selectsrvr - select the CASTOR Name Server hostname */

/*	The following rules apply:
 *	if the path is in the form server:pathname, "server" is used else
 *	if the environment variable CNS_HOST is set, its value is used else
 *	if CNS HOST is defined in /etc/shift.conf, the value is used else
 *	the second component of path is the domain name and the third component
 *	is prefixed by the value of NsHostPfx to give the host name or its alias
 */

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "Castor_limits.h"
#include "Cns_api.h"
#include "Cns.h"
#include "serrno.h"

#ifndef _WIN32
#if defined(_REENTRANT) || defined(_THREAD_SAFE)
#define strtok(X,Y) strtok_r(X,Y,&last)
#endif /* _REENTRANT || _THREAD_SAFE */
#endif /* _WIN32 */

int DLL_DECL
Cns_selectsrvr(path, current_directory_server, server, actual_path)
const char *path;
char *current_directory_server;
char *server;
char **actual_path;
{
	char buffer[CA_MAXPATHLEN+1];
	char *domain;
	char func[16];
	char *getconfent();
	char *getenv();
#ifndef _WIN32
#if defined(_REENTRANT) || defined(_THREAD_SAFE)
	char *last = NULL;
#endif  
#endif
	int n;
	char *p;
 
	strcpy (func, "Cns_selectsrvr");
	if (! path || ! server || ! actual_path) {
		serrno = EFAULT;
		return (-1);
	}

	if (*path != '/' && (p = strstr (path, ":/"))) {
		n = p - path;
		if (n > CA_MAXHOSTNAMELEN) {
			serrno = EINVAL;
			return (-1);
		}
		strncpy (server, path, n);
		*(server + n) = '\0';
		*actual_path = p + 1;
	} else {
		*actual_path = (char *) path;
		if ((p = getenv (CNS_HOST_ENV)) || (p = getconfent (CNS_SCE, "HOST", 0))) {
			if (strlen (p) > CA_MAXHOSTNAMELEN) {
				serrno = EINVAL;
				return (-1);
			}
			strcpy (server, p);
		} else {
			if (*path != '/') {	/* not a full path */
				if (*current_directory_server)	/* set by chdir */
					strcpy (server, current_directory_server);
				else
					server[0] = '\0';	/* use localhost */
				return (0);
			}
			strcpy (buffer, path);
			if (! strtok (buffer, "/") ||
			    (domain = strtok (NULL, "/")) == NULL ||
			    (p = strtok (NULL, "/")) == NULL) {
				server[0] = '\0';	/* use localhost */
				return (0);
			}
			if (strlen (CNSHOSTPFX) + strlen (p) + strlen (domain) +
			    1 > CA_MAXHOSTNAMELEN) {
				serrno = EINVAL;
				return (-1);
			}
			sprintf (server, "%s%s.%s", CNSHOSTPFX, p, domain);
		}
	}
	return (0);
}