File: lun2fn.c

package info (click to toggle)
dpm-postgres 1.7.4.7-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,788 kB
  • ctags: 10,782
  • sloc: ansic: 146,136; sh: 13,362; perl: 11,142; python: 5,529; cpp: 5,113; sql: 1,790; makefile: 955; fortran: 113
file content (116 lines) | stat: -rw-r--r-- 3,012 bytes parent folder | download | duplicates (8)
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
/*
 * $Id: lun2fn.c,v 1.1 2005/03/31 13:13:01 baud Exp $
 */

/*
 * Copyright (C) 1990-1999 by CERN/IT/PDP/DM
 * All rights reserved
 */

#ifndef lint
static char sccsid[] = "@(#)$RCSfile: lun2fn.c,v $ $Revision: 1.1 $ $Date: 2005/03/31 13:13:01 $ CERN/IT/PDP/DM Frederic Hemmer";
#endif /* not lint */

/* lun2fn.c     Remote File I/O - translate FORTRAN LUN to file name    */

/*
 * C bindings :
 *
 * assign syntax :
 *
 * fort.LUN:<filepath>[,<filepath>[,<filepath>...]]
 *
 * char *lun2fn(int lun)
 *
 */

#define RFIO_KERNEL     1       /* system part of Remote File I/O       */

#include "rfio.h"               /* remote file I/O definitions          */
#include <pwd.h>                /* password entry structure             */
#include <Cpwd.h>

extern char     *getenv();

char * DLL_DECL 
lun2fn(lun)                     /* find file name corresponding to lun  */
int     lun;
{
   char    *afile;         /* assign file name                     */
   FILE    *fp;            /* a file pointer                       */
   char    *p, *p1;        /* character pointers                   */
   int     clun;           /* current lun entry                    */
   static  char    buf[1024];  	   /* general purpose buffer     	        */
/*
 * Open the assign file, get the corresponding entry
 */
   INIT_TRACE("RFIO_TRACE");
   TRACE(1, "rfio", "lun2fn: looking environment for %s", "RFASSIGN");
   if ((p = getenv("RFASSIGN")) != NULL)   {
      afile = p;
   }
   else {  /* No RFASSIGN env var, so get the passwd entry         */
      TRACE(1, "rfio", "lun2fn: getting home directory name");
      if ((p = Cgetpwuid(getuid())->pw_dir) == NULL)   {
	 END_TRACE();
	 return(NULL);
      }
      sprintf(buf,"%s/%s",p,DEFASNFNAM);
      afile = buf;
   }

   TRACE(1, "rfio", "lun2fn: opening %s", afile);
   if ((fp = fopen(afile, "r")) == NULL)   {
      if (errno == ENOENT)    {
#if hpux
	 sprintf (buf, "ftn%02d", lun);
#else
	 sprintf (buf, "fort.%d", lun);
#endif
	 TRACE(1, "rfio", "lun2fn: assigning unit %d to %s", lun, buf);
	 END_TRACE();
	 return(buf);
      }
      else    {
	 END_TRACE();
	 return(NULL);
      }
   }

   for (;(p=fgets(buf, BUFSIZ, fp)) != NULL;)   {
      p = strchr(p,'.');
      p1 = strchr(p+1, ':');
      *(p1++)='\0';
      clun = atoi(p+1);
      TRACE(1, "rfio", "lun2fn: processing entry %d", clun);
      if (clun == lun) {      /* matching entry       */
	 p = p1;
	 /* The string is terminated by \n, \0 or \, */
	 if ((p1 = strpbrk(p,"\n\0,")) != NULL) {
	    *p1 = '\0';
	 }
	 break;
      }
      else    {
	 p = NULL;
      }
   }
   (void) fclose(fp);

   if (p == NULL)  {               /* no matching entry    */
#if hpux
      sprintf (buf, "ftn%d", lun);
#else
      sprintf (buf, "fort.%d", lun);
#endif

      TRACE(1, "rfio", "lun2fn: assigning unit %d to %s", lun, buf);
      END_TRACE();
      return(buf);
   }
   else    {
      TRACE(1, "rfio", "lun2fn: assigning unit %d to %s", lun, p);
      END_TRACE();
      return(p);
   }
}