File: cluni0.c

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (105 lines) | stat: -rw-r--r-- 3,019 bytes parent folder | download | duplicates (2)
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
/* Copyright INRIA/ENPC */
#include <stdio.h>
#include <string.h>

#include "../graphics/Math.h"
#include "Os_specific.h"

#define MAX_ENV 256 

static char *SCI_a[] = {  "SCI/", "sci/", "$SCI", "SCI\\", "sci\\", (char *) 0 };
static char *HOME_a[] = {  "HOME/", "home/", "~/" , "HOME\\", "home\\", "~\\" ,"$HOME", (char *) 0};
static char *TMP_a[] = {  "TMPDIR/", "tmpdir/","TMPDIR\\", "tmpdir\\", "$TMPDIR", (char *) 0};
void GetenvB __PARAMS(( char *name,char *env, int len));
static int Cluni0 __PARAMS((char *env,char **alias,char* in_name,char *out_name, long int lin));

extern int UpdateEnvVar; /* see setenvc.c */
static int n=0;
/************************************************
 * expand  in_name to produce out_name 
 *       
 ************************************************/

int C2F(cluni0)(char *in_name, char *out_name, int *out_n, long int lin, long int lout)
{
  int  nc= MAX_ENV;
  static char SCI[MAX_ENV],HOME[MAX_ENV],TMP[MAX_ENV];
  static int k;
  
  if ( ( n==0 ) || (UpdateEnvVar == 1) )
  {
	  GetenvB("SCI",SCI,nc);
	  GetenvB("HOME",HOME,nc);
      GetenvB("TMPDIR",TMP,nc);
	  n=n+1;
	  UpdateEnvVar=0;
  }
  /* in_name[lin]='\0';*/
  if ( Cluni0(SCI,SCI_a,in_name,out_name,lin) == 0 )
    if ( Cluni0(HOME,HOME_a,in_name,out_name,lin) == 0 )
      if ( Cluni0(TMP,TMP_a,in_name,out_name,lin) == 0 ){
	strncpy(out_name,in_name,(size_t)lin);
	out_name[lin]='\0';
      }
  /*strncpy(out_name,in_name,lout); */
  *out_n = strlen(out_name);
#if defined(THINK_C)||defined(__MWERKS__)
  for (k=0 ; k < *out_n ;k++) if ( out_name[k]=='/') out_name[k]=':';
#else
#if defined(WIN32)
  for (k=0 ; k < *out_n ;k++) if ( out_name[k]=='/') out_name[k]='\\';
#else
  for (k=0 ; k < *out_n ;k++) if ( out_name[k]=='\\') out_name[k]='/';
#endif
#endif


  /** sciprint( "out [%s] [%d]\r\n",out_name,*out_n); **/
  return(0);
}

/************************************************
 * getenv + squash trailing white spaces 
 ************************************************/

void GetenvB(char *name, char *env, int len)
{
  int ierr,un=1;
  C2F(getenvc)(&ierr,name,env,&len,&un);
  if( ierr == 0) 
    {
      char *last = &env[len-1];
      while ( *last == ' ' ) { last = '\0' ; } last--;
    }
  else 
    {
      env[0] = '\0' ;
    }  
}

/************************************************
 * expand in_name to produce out_name 
 *     try to find alias[i] at the begining of in_name 
 *     and replaces it by env in out_name 
 *     out_name must be large enough to get the result 
 ************************************************/

static int Cluni0(char *env, char **alias, char *in_name, char *out_name, long int lin)
{
  int i=0;
  if ( env[0] == '\0' ) return(0);
  while ( alias[i] != (char *) 0) 
    {
      if ( strncmp(alias[i],in_name,strlen(alias[i])) == 0)
	{
	  strcpy(out_name,env);
	  strncat(out_name,in_name+strlen(alias[i])-1,
		  (size_t)(lin-strlen(alias[i]+1)));
	  /*sprintf(out_name,"%s/%s",env,in_name+strlen(alias[i]));*/
	  return(1);
	}
      i++;
    }
  return(0);
}