Debian Codegraph @

COSMIC/Sopenv.c

raw
1 /* Sopenv.c : this function attempts to open a Socket based on an
2 * environment variable containing machine names separated by colons.
3 * Authors: Charles E. Campbell, Jr.
4 * Terry McRoberts
5 * Copyright: Copyright (C) 1999-2005 Charles E. Campbell, Jr. {{{1
6 * Permission is hereby granted to use and distribute this code,
7 * with or without modifications, provided that this copyright
8 * notice is copied with it. Like anything else that's free,
9 * Sopenv.c is provided *as is* and comes with no warranty
10 * of any kind, either expressed or implied. By using this
11 * software, you agree that in no event will the copyright
12 * holder be liable for any damages resulting from the use
13 * of this software.
14 * Date: Aug 22, 2005
15 */
16 #include <stdio.h>
17 #include <ctype.h>
18 #include "xtdio.h"
19 #include "sockets.h"
20
21 /* -----------------------------------------------------------------------
22 * Definitions:
23 */
24 #define MACHINE_SEP ':'
25 #define BUFSIZE 256
26
27 /* -----------------------------------------------------------------------
28 * Source Code:
29 */
30
31 /* Sopenv: opens Socket for server/client based on environment variable
32 */
33 #ifdef __PROTOTYPE__
34 Socket *Sopenv(
35 char *sktname, /* name of server */
36 char *ctrl, /* "s", "c" */
37 char *env_var) /* environment variable to be used */
38 #else
39 Socket *Sopenv(sktname,ctrl,env_var)
40 char *sktname; /* name of server */
41 char *ctrl; /* "s", "c" */
42 char *env_var; /* environment variable to be used */
43 #endif
44 {
45 char *at = NULL;
46 char *env = NULL;
47 char *s = NULL;
48 int more = 0;
49 Socket *skt = NULL;
50 static char buf[BUFSIZE];
51
52
53 /* see if Socket can be opened on current machine, first */
54 skt= Sopen(sktname,ctrl);
55 if(skt) {
56 return skt;
57 }
58
59 /* cannot open server on a different host
60 * ie. using Sopenv to open a server doesn't give any
61 * value added because it doesn't make any sense to
62 * "search" a path of machines to do it.
63 */
64 else if(!strcmp(ctrl,"s") || !strcmp(ctrl,"S")) {
65 return (Socket *) NULL;
66 }
67
68 /* look for '@' in string and null it */
69 at= (char *) strchr(sktname,'@');
70 if(at) *at= '\0';
71
72 /* check out the environment variable */
73 if(!env_var || !*env_var) env_var= "SKTPATH";
74 env= getenv(env_var);
75
76 /* attempt to open connection to Socket server using environment search */
77 if(env) while(*env) { /* while there's environment-variable info left... */
78 for(s= env; *s && *s != MACHINE_SEP; ++s);
79 more= *s == MACHINE_SEP;
80 *s = '\0';
81 sprintf(buf,"%s@%s",sktname,env);
82
83 /* attempt to open Socket given machine name */
84 skt= Sopen(buf,ctrl);
85 if(more) *s= MACHINE_SEP;
86
87 if(skt) { /* successfully opened Socket */
88 if(at) *at= '@';
89 return skt;
90 }
91 if(more) { /* another path to search */
92 *s = MACHINE_SEP;
93 env= s + 1;
94 }
95 else env= s;
96 }
97
98 /* restore '@' if it was present */
99 if(at) *at= '@';
100
101 /* return NULL pointer, thereby modestly indicating a lack of success */
102 return (Socket *) NULL;
103 }
104
105 /* =======================================================================
106 * Test Code:
107 */
108 #ifdef DEBUG_TEST
109 #ifdef unix
110 void *sbrk(int);
111 #endif
112
113 /* main: */
114 int main(int argc,char **argv)
115 {
116 Socket *skt = NULL;
117 #ifdef unix
118 char *memstart = NULL;
119 char *memory = NULL;
120 #endif
121
122 rdcolor();
123
124
125 #ifdef unix
126 memstart= NULL;
127 #endif
128
129 do {
130 skt= Sopenv("SOPENV","c","SKTPATH");
131 ++cnt;
132 #ifdef unix
133 memory= sbrk(0);
134 if(!memstart) memstart= memory;
135 else if(memory > memstart)
136 error(XTDIO_WARNING,"memory leak: ([memory=%dd] - [memstart=%dd])= %d\n",
137 (int) memory,(int) memstart,memory - memstart);
138 #endif
139 } while(!skt);
140 Sclose(skt);
141 printf("took %d tries to open the SOPENV client\n",cnt);
142
143 return 0;
144 }
145 #endif /* DEBUG_TEST */
146
147 /* ---------------------------------------------------------------------
148 * vim: ts=4
149 */