File: misc.c

package info (click to toggle)
scheme48 1.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 14,980 kB
  • ctags: 14,127
  • sloc: lisp: 76,272; ansic: 71,514; sh: 3,026; makefile: 637
file content (159 lines) | stat: -rw-r--r-- 3,382 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
/* Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees.

   See file COPYING. */

#include <stdio.h>
#include <stdlib.h>		/* for getenv(), etc. (POSIX?/ANSI) */
#include <string.h>		/* for strncpy(), etc. (POSIX/ANSI) */
#include <errno.h>

#include <windows.h>

/*
   Expanding Windows filenames
   Windows Sucks.  Unix Sucks.  C Sucks.
   Richard Kelsey Wed Jan 17 21:40:26 EST 1990 (for the Unix version)
   Later rewritten by others who wish to remain anonymous.

   Expands ~/ in string `name', leaving the result in `buffer'.
   `buffer_len' is the length of `buffer'.
*/

char *s48_expand_file_name (char *name, char *buffer, int buffer_len)
{
  char *drive = NULL, *path = NULL, *dir = NULL;
  int dir_len;
  int name_len = strlen(name);

  if ((name_len >= 2) && (name[0] == '~') && ((name[1] == '\\' || (name[1] == '/'))))
    {
      drive = getenv("HOMEDRIVE");
      /* HOMEPATH is usually \.
       * I have no idea if it will have trailing \ for a subdirectory.
       */
      path  = getenv("HOMEPATH");
      name += 1;
      name_len -= 2;
    }
  else if ((name_len >= 3) && (name[0] == '%'))
    {
      char *pos = strchr(name + 2, '%');
      if (pos)
	{
	  *pos = '\0';
	  dir = getenv(name+1);
	  name = pos + 1;
	  name_len -= (pos - name) + 1;
	}
    }

  if ((drive && path) || dir)
    {
      if (drive && path)
	dir_len = strlen(drive) + strlen(path);
      else
	dir_len = strlen(dir);

      if ((name_len + dir_len + 1) > buffer_len)
	{
	  fprintf(stderr, "\ns48_expand_file_name: supplied buffer is too small\n");
	  return(NULL);
	};
      if (drive && path)
	{
	  int drive_length = strlen(drive);

	  strcpy(buffer, drive);
	  strcpy(buffer + drive_length, path);
	  strcpy(buffer + dir_len, name);
	}
      else
	{
	  strcpy(buffer, dir);
	  strcpy(buffer + dir_len, name);
	}
    }
  else
    {
      if ((name_len + 1) > buffer_len)
	{
	  fprintf(stderr, "\ns48_expand_file_name: supplied buffer is too small\n");
	  return NULL;
	};
      strcpy(buffer, name);
    }

  return(buffer);
}

/* test routine */
/*
main(argc, argv)
  int argc;
  char *argv[];
{
  char buffer[512];
  s48_expand_file_name(argv[1], buffer, 512);
  printf("%s\n", buffer);
  return(0);
}
/* */


/* Driver loop for tail-recursive calls */

long s48_return_value;

long
s48_run_machine(long (*proc) (void))
{
  while (proc != 0)
    proc = (long (*) (void)) (*proc)();
  return s48_return_value;
}

unsigned char *
ps_error_string(long the_errno)
{
  DWORD id = the_errno;
  /* the VM is responsible for copying this */
  static char buf[512];

  for (;;)
    {
      if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
			NULL, /* lpSource */
			(DWORD) id,
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
			buf, sizeof(buf)-1,
			NULL)) /* arguments ... */
	return buf;
      else
	/* risky, but we assume some amount of sanity on the side of
	   the Windows implementors---haha */
	id = GetLastError();
    }
}

/* Getting the length of a file. */

long
s48_get_file_size(unsigned char *name)
{
  HANDLE handle = CreateFile(name, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
			     OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

  if (handle == INVALID_HANDLE_VALUE)
    return -1;
  
  {
    DWORD dwSize = GetFileSize(handle, NULL);
    
    CloseHandle(handle);
    
    if (dwSize == 0xFFFFFFFF)
      return -1;
    else
      return (long) dwSize;
  }
}