File: miscvms.c

package info (click to toggle)
fsp 2.81.b3-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,072 kB
  • ctags: 1,264
  • sloc: ansic: 7,764; makefile: 357; sh: 312
file content (301 lines) | stat: -rw-r--r-- 6,808 bytes parent folder | download | duplicates (4)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* Some unix emulation procedures */
/* 1-NOV-1990 GJC@MITECH.COM */
/* 24-DEC-1992 modified for use with VMS-fsp, <S.A.Pechler@bdk.tue.nl> */
/* 05-MAR-1993 added some CPU-friendly fsp-patches */

#include <stdio.h>
#include <string.h>
#include <descrip.h>
#include <rms.h>
#include <ssdef.h>
#include <stat.h>

#include "pwd.h"

#define fexist(A) (!vms_access(A,0))

/* Ignore these functions on VMS.
 */
char *getlogin(void)
{
 return (char *)0;
}

struct passwd *getpwnam(char *dummy)
{
 return (struct passwd *)0;
}

struct passwd *getpwuid(uid_t dummy)
{ 
  return (struct passwd *)0;
}

/* strcasecmp */
int strcasecmp(char *name1, char *name2)
{
 return(strcmp(name1,name2));
}

/* vms_access
 * Just a simple patch for VMS-fsp, it checks for the existance of a
 * file, regardless of the 'dummy' parameter (was originally
 * protection-flag).
 */
int vms_access(file,dummy)
char *file;
int dummy;
{
 struct FAB fab;
 char filename[255];

 if (convfile(filename,file)) return 1;
 fab=cc$rms_fab;
 fab.fab$l_fna=filename;
 fab.fab$b_fns=strlen(filename);
 if (sys$open(&fab) != RMS$_NORMAL) return 1;
 else sys$close(&fab);
 return 0;
}

/* vms_stat
 * Just a simple patch, returns only filetype DIR or REG in struct stat. 
 */
int vms_stat(file,vstat)
char *file;
struct stat *vstat;
{
 struct FAB fab;
 struct NAM nam;
 char exp_str[NAM$C_MAXRSS];
 char res_str[NAM$C_MAXRSS];

 char filename[255];

 /* convert the filename to VMS-format */
 if (convfile(filename,file)) return 1;

 /* fill in nam-block */
 nam= cc$rms_nam;
 nam.nam$l_rsa= res_str;
 nam.nam$b_rss= NAM$C_MAXRSS;
 nam.nam$l_esa= exp_str;
 nam.nam$b_ess= NAM$C_MAXRSS;

 /* fill in fab-block */
 fab= cc$rms_fab;
 fab.fab$l_nam= &nam;
 fab.fab$l_fop= fab.fab$v_nam;
 fab.fab$l_fna= filename;
 fab.fab$b_fns= (unsigned char)strlen(filename);

 sys$parse(&fab); /* needed for completion nam-block */
 if (sys$search(&fab) != RMS$_NORMAL)
 {
  if (filename[strlen(filename)-1]==']')  /* kludge to check for dir-path */
  { vstat->st_mode=S_IFDIR;
    return 0;
  }
  else return 1;
 }

 /* if name ends on .DIR;1 then it's a dir (Will go wrong when you have
    files with a .DIR;1 extension). */
 if (strncmp(".DIR;1",nam.nam$l_type,6)) vstat->st_mode=S_IFREG;
 else vstat->st_mode=S_IFDIR;
 return 0;
}

/* isvariable
 * returns true if file is in variable record length format.
 */
int isvariable(sb)
struct stat sb;
{
return (int)(sb.st_fab_rfm==FAB$C_VAR || sb.st_fab_rfm==FAB$C_VFC);
}

#ifndef bcopy /* only for UCX systems */
bcopy(x,y,n)
     char *x,*y;
     long n;
{memmove(y,x,n);}	/* reverse order of arguments */
#endif

static char *getwd_tmp = NULL;

char *getwd(p)
     char *p;
{
 int c;
 char *root_dir,*l2;
 getcwd(p,512,0);	/* get current working directory in unix format*/

 root_dir = strstr ( p, "/000000" );
 if ( root_dir != NULL ) {
    /* trim root directory out of specification */
    if ( (strlen(root_dir) == 7) && 
	 (strpbrk(p+1,"/") == root_dir) ) *root_dir = '\0';
 }
 /* special kludge for "/" directory */
 if ( strcmp ( p, "/DEVICE_LIST_ROOT" ) == 0 ) strcpy  ( p, "/" );
 return(p);
}

/*
** HARDLINK emulation, just a filecopy on VMS.
*/
#ifndef link
long
link(source,target)
 char *source;
 char *target;

{
 FILE *ft, *fp;
 char buf[512];
 int bytes;

 if (fexist(target)) return(-1); /* target already exists */
#ifdef DEBUG
 printf("link: source: %s, target: %s\n",source,target);
#endif
 if(!(ft = fopen(source,"r","mbf=2","mbc=32"))) return(-1);
 if(!(fp = fopen(target,"w","mbf=2","mbc=32"))) { fclose(ft); return(-1); }
 /* copy source to target */
 while(bytes = fread(buf,1,sizeof(buf),ft)) fwrite(buf,1,bytes,fp);
 fclose(ft); fclose(fp);
 return(0);
}
#endif

rindex(p,c)
     char *p;
     int c;
{
 return(strrchr(p,c));
}

/*
 * redefinition of lstat
 */
int lstat(f,st)		/* fake a stat operation to return file type */
   char *f;
   stat_t *st;
{
    char *dirext, *name;
    int extlen;

    st->st_mode = S_IFREG;	/* default to normal file */
    name = strrchr ( f, '/' );	/* locate rightmost slash */
    if ( name == NULL ) name = f; else name++;

    dirext = strstr ( name, ".DIR" );
    if ( dirext != NULL ) {
	/* make it an exact match */
	extlen = strcspn(&dirext[1],".;");
        if ( (extlen == 0) || (extlen == 3) ) {
	    st->st_mode = S_IFDIR;
	    if ( strncmp ( name, "000000.", 7 ) == 0 ) return 0;
	    else return (stat ( f, st ));
	}
    }
    return 0;
}

do_vms_wildcard(pargc,pargv)
     int *pargc;
     char ***pargv;
{int j,vsize;
 int argc; char **argv;
 argc = *pargc;
 argv = *pargv;
 *pargc = 0;
 vsize = 3;
 *pargv = (char **) malloc(sizeof (char *) * vsize);
 for(j=0;j<argc;++j)
   vms_wild_putargs(argv[j],pargc,pargv,&vsize);
}

vms_wild_putargs(s,pargc,pargv,pvsize)
     char *s; int *pargc; char ***pargv; int *pvsize;
{if ( (!strchr(s,'*')) && (!strchr(s,'%')) )
   vms_wild_put_one(s,pargc,pargv,pvsize);
 else
   vms_wild_put_wild(s,pargc,pargv,pvsize);
}


vms_wild_put_one(s,pargc,pargv,pvsize)
     char *s; int *pargc; char ***pargv; int *pvsize;
{int nvsize,i;
 char ** nargv, *uname, *SHELL$TRANSLATE_VMS();
 if (*pargc == *pvsize)
   {nvsize = 2 * *pvsize;
    nargv = (char **) malloc(sizeof (char *) * nvsize);
    for(i=0;i < *pargc; ++i) nargv[i] = (*pargv)[i];
    free(*pargv);
    *pargv = nargv;
    *pvsize = nvsize;}
 if ( uname = SHELL$TRANSLATE_VMS ( s ) ) {
    /* printf("vms: '%s' -> unix: '%s'\n", s, uname ); */
    if ( strlen(s) >= strlen(uname) ) { strcpy(s,uname); free(uname); }
    else s = uname;  /* will lose s's old allocation */
 } 
 (*pargv)[(*pargc)++] = s;
}


set_dsc(x,buff,len)
 struct dsc$descriptor *x;
 char *buff;
 int len;
{
 (*x).dsc$w_length = len;
 (*x).dsc$a_pointer = buff;
 (*x).dsc$b_class = DSC$K_CLASS_S;
 (*x).dsc$b_dtype = DSC$K_DTYPE_T;
 return(x);
}

struct dsc$descriptor *
set_dsc_cst(x,buff)
 struct dsc$descriptor *x;
 char *buff;
{
 return(set_dsc(x,buff,strlen(buff)));
}


vms_wild_put_wild(s,pargc,pargv,pvsize)
 char *s; int *pargc; char ***pargv; int *pvsize;
{
 struct dsc$descriptor fnamed,foutd,rfnamed;
 char *ns,*p;
 int rval;
 long context;

 set_dsc_cst(&rfnamed,";");
 set_dsc_cst(&fnamed,s);
 set_dsc(&foutd,0,0);
 foutd.dsc$b_class = DSC$K_CLASS_D;
 context = 0;
 while(1)
  {rval = lib$find_file(&fnamed,&foutd,&context,0,&rfnamed,0,0);
   if (rval == RMS$_NMF) break;
   if (rval == RMS$_FNF) break;
   if (rval != RMS$_NORMAL) exit(rval);
   ns = (char *) malloc(foutd.dsc$w_length + 1);
   ns[foutd.dsc$w_length] = 0;
   memcpy(ns,foutd.dsc$a_pointer,foutd.dsc$w_length);
   /*if (p = strchr(ns,']')) ns = p+1;*/
   /* if (p = strchr(ns,';')) *p = 0; */
   vms_wild_put_one(ns,pargc,pargv,pvsize);
  }
 if (foutd.dsc$a_pointer) lib$sfree1_dd(&foutd);
 if (context)
 { rval = lib$find_file_end(&context);
   if (rval != SS$_NORMAL) exit(rval);
 }
}