1/*$Header: /p/tcsh/cvsroot/tcsh/win32/ntport.h,v 1.13 2007/05/31 08:26:12 corinna Exp $*/
2/*-
3 * Copyright (c) 1980, 1991 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 *    may be used to endorse or promote products derived from this software
16 *    without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31// ntport.h
32// the main header.
33// -amol
34//
35//
36#ifndef NTPORT_H
37#define NTPORT_H
38#define WIN32_LEAN_AND_MEAN
39#include <windows.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <time.h>
43#include <sys/types.h>
44#include <sys/stat.h>
45#include <io.h>
46#include <direct.h>
47#include "dirent.h"
48#include "version.h"
49
50#ifndef WINDOWS_ONLY
51#define STRSAFE_NO_DEPRECATE
52#endif /* WINDOWS_ONLY*/
53#define STRSAFE_LIB
54#define STR_NO_CCH_FUNCTIONS
55#include <strsafe.h>
56
57// These needed for fork(), which controls the heap management.
58#pragma data_seg(".fusrdata")
59#define INIT_ZERO =0
60#define INIT_ZERO_STRUCT ={0}
61#define malloc fmalloc
62#define calloc fcalloc
63#define realloc frealloc
64#define free ffree
65#undef min
66#undef max
67
68#define heap_alloc(s) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(s))
69#define heap_free(p) HeapFree(GetProcessHeap(),0,(p))
70#define heap_realloc(p,s) HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(p),(s))
71
72#pragma warning(disable:4018)  //signed-unsigned mismatch
73#define HAVENOLIMIT
74
75/* os-dependent stuff. belongs in tc.os.h, but I never said this would be
76pretty */
77
78#define lstat stat
79#ifdef S_IFMT
80# if !defined(S_ISDIR) && defined(S_IFDIR)
81#  define S_ISDIR(a)	(((a) & S_IFMT) == S_IFDIR)
82# endif	/* ! S_ISDIR && S_IFDIR */
83# if !defined(S_ISCHR) && defined(S_IFCHR)
84#  define S_ISCHR(a)	(((a) & S_IFMT) == S_IFCHR)
85# endif /* ! S_ISCHR && S_IFCHR */
86# if !defined(S_ISBLK) && defined(S_IFBLK)
87#  define S_ISBLK(a)	(((a) & S_IFMT) == S_IFBLK)
88# endif	/* ! S_ISBLK && S_IFBLK */
89# if !defined(S_ISREG) && defined(S_IFREG)
90#  define S_ISREG(a)	(((a) & S_IFMT) == S_IFREG)
91# endif	/* ! S_ISREG && S_IFREG */
92# if !defined(S_ISFIFO) && defined(S_IFIFO)
93#  define S_ISFIFO(a)	(((a) & S_IFMT) == S_IFIFO)
94# endif	/* ! S_ISFIFO && S_IFIFO */
95# if !defined(S_ISNAM) && defined(S_IFNAM)
96#  define S_ISNAM(a)	(((a) & S_IFMT) == S_IFNAM)
97# endif	/* ! S_ISNAM && S_IFNAM */
98# if !defined(S_ISLNK) && defined(S_IFLNK)
99#  define S_ISLNK(a)	(((a) & S_IFMT) == S_IFLNK)
100# endif	/* ! S_ISLNK && S_IFLNK */
101# if !defined(S_ISSOCK) && defined(S_IFSOCK)
102#  define S_ISSOCK(a)	(((a) & S_IFMT) == S_IFSOCK)
103# endif	/* ! S_ISSOCK && S_IFSOCK */
104#endif /* S_IFMT */
105
106/* port defines */
107#define getpid                GetCurrentProcessId
108#define getpgrp               GetCurrentProcessId
109#define tcgetpgrp(a)          GetCurrentProcessId()
110#define tcsetpgrp(a,b)        0
111#define setpgid(a,b)          0
112
113
114#define close(a)              nt_close(a)
115#define execv(a,b)            nt_exec((a),(b))
116#define execve(a,b,c)         nt_execve((a),(b),(c))
117
118#define open                  nt_open
119#define read(f,b,n)           nt_read((f),(b),(n))
120#define write(f,b,n)          nt_write((f),(b),(n))
121#define creat(f,m)            nt_creat((f),(m))
122#define _exit(a)              ExitProcess((a))
123
124#define chdir(a)              nt_chdir(a)
125
126#undef putc
127#undef putchar
128#define fgetc(a)              nt_fgetc(a)
129#define fputs(a,b)            nt_fputs((a),(b))
130#define putc(a,b)             nt_putc((char)(a),(b))
131#define fflush(a)             nt_fflush((a))
132#define fputc(a,b)            nt_fputc((char)(a),(b))
133#define fprintf               nt_fprintf
134#define puts(a)               nt_puts(a)
135#define putchar(a)            nt_putchar((char)(a))
136#define fclose(p)             nt_fclose(p)
137#define _get_osfhandle        __nt_get_osfhandle
138#define _open_osfhandle       __nt_open_osfhandle
139#define clearerr              nt_clearerr
140#define dup2                  nt_dup2
141#define fdopen                nt_fdopen
142#define fgets                 nt_fgets
143#define fileno                nt_fileno
144#define fopen                 nt_fopen
145#define fread                 nt_fread
146#define fseek                 nt_fseek
147#define ftell                 nt_ftell
148#define fwrite                nt_fwrite
149#define isatty                nt_isatty
150#define lseek                 nt_lseek
151#define printf                nt_printf
152#define access                nt_access
153#define fstat(a,b)            nt_fstat((a),(b))
154#define stat(a,b)             nt_stat((a),(b))
155
156#define setvbuf(a,b,c,d)
157#define setpgrp(a,b) (-1)
158#define tcsetattr(a,b,c) 0
159
160#undef stdin
161#undef stdout
162#undef stderr
163#define stdin                 ((FILE*)my_stdin)
164#define stdout                ((FILE*)my_stdout)
165#define stderr                ((FILE*)my_stderr)
166
167#define dup(f)                nt_dup((f))
168#define sleep(a)              Sleep((a)*1000)
169
170#define getcwd(a,b)           forward_slash_get_cwd((a),(b))
171
172
173#define L_SET                 SEEK_SET
174#define L_XTND                SEEK_END
175#define L_INCR                SEEK_CUR
176#define S_IXUSR               S_IEXEC
177#define S_IXGRP               S_IEXEC
178#define S_IXOTH               S_IEXEC
179
180#define NOFILE                64
181#define ARG_MAX               1024
182#define MAXSIG                NSIG
183
184/*
185mode Value	Checks File For
186
18700	Existence only
18802 	Write permission
18904	Read permission
19006	Read and write permission
191*/
192#define F_OK 0
193#define X_OK 1
194#define W_OK 2
195#define R_OK 4
196#define XD_OK 9 //executable and not directory
197
198/* base key mappings + ctrl-key mappings + alt-key mappings */
199/* see nt.bind.c  to figure these out */
200/*  256 +
201	4*24 (fkeys) +
202	4*4 (arrow) +
203	4*2 (pgup/dn) +
204	4*2 (home/end) +
205	4*2 (ins/del)
206*/
207#define NT_NUM_KEYS	               392
208
209#define NT_SPECIFIC_BINDING_OFFSET 256 /* where our bindings start */
210
211#define KEYPAD_MAPPING_BEGIN       24 /* offset from NT_SPECIFIC
212										  where keypad mappings begin */
213#define INS_DEL_MAPPING_BEGIN      32
214
215#define SINGLE_KEY_OFFSET          0  /*if no ctrl or alt pressed */
216#define CTRL_KEY_OFFSET            34
217#define ALT_KEY_OFFSET             (34*2)
218#define SHIFT_KEY_OFFSET           (34*3)
219
220typedef int pid_t;
221typedef int speed_t;
222typedef unsigned char u_char;
223typedef size_t caddr_t;
224typedef int sig_atomic_t;
225typedef int mode_t;
226
227struct timeval{
228	long tv_sec;
229	long tv_usec;
230};
231struct termios;
232/*
233struct timezone{
234	int tz_minuteswest;
235	int dsttime;
236};
237*/
238struct rusage {
239
240	 struct timeval ru_utime; /* user time used */
241	 struct timeval ru_stime; /* system time used */
242	 long ru_maxrss;          /* maximum resident set size */
243	 long ru_ixrss;      /* integral shared memory size */
244	 long ru_idrss;      /* integral unshared data size */
245	 long ru_isrss;      /* integral unshared stack size */
246	 long ru_minflt;          /* page reclaims */
247	 long ru_majflt;          /* page faults */
248	 long ru_nswap;      /* swaps */
249	 long ru_inblock;         /* block input operations */
250	 long ru_oublock;         /* block output operations */
251	 long ru_msgsnd;          /* messages sent */
252	 long ru_msgrcv;          /* messages received */
253	 long ru_nsignals;        /* signals received */
254	 long ru_nvcsw;      /* voluntary context switches */
255	 long ru_nivcsw;          /* involuntary context switches */
256};
257typedef int uid_t;
258typedef int gid_t;
259typedef long ssize_t;
260
261struct passwd {
262	  char    *pw_name;       /* user name */
263	  char    *pw_passwd;     /* user password */
264	  uid_t   pw_uid;         /* user id */
265	  gid_t   pw_gid;         /* group id */
266	  char    *pw_gecos;      /* real name */
267	  char    *pw_dir;        /* home directory */
268	  char    *pw_shell;      /* shell program */
269};
270struct group {
271	  char    *gr_name;        /* group name */
272	  char    *gr_passwd;      /* group password */
273	  gid_t   gr_gid;          /* group id */
274	  char    **gr_mem;        /* group members */
275};
276
277#ifndef _INTPTR_T_DEFINED
278#ifdef  _WIN64
279typedef __int64             intptr_t;
280#else
281typedef int                 intptr_t;
282#endif
283#define _INTPTR_T_DEFINED
284#endif
285/* ntport.c */
286extern char *			 ttyname(int);
287extern struct passwd*    getpwuid(uid_t ) ;
288extern struct group *    getgrgid(gid_t ) ;
289extern struct passwd*    getpwnam(const char* ) ;
290extern struct group*     getgrnam(char* ) ;
291extern gid_t 			 getuid(void) ;
292extern gid_t 			 getgid(void) ;
293extern gid_t 			 geteuid(void) ;
294extern gid_t 			 getegid(void) ;
295
296#ifdef NTDBG
297extern void dprintf(char *,...);
298#define DBreak() __asm {int 3}
299#else
300#define dprintf (void)
301#endif NTDBG
302
303#define pipe(a) nt_pipe(a)
304
305
306/* support.c */
307extern void nt_init(void);
308extern int gethostname(char*,int);
309extern char* forward_slash_get_cwd(char *,size_t len );
310extern int  nt_chdir(char*);
311extern void  nt_execve(char *,char**,char**);
312extern void  nt_exec(char *,char**);
313extern int quoteProtect(char *, char *,unsigned long) ;
314extern char* fix_path_for_child(void) ;
315extern void restore_path(char *) ;
316extern int copy_quote_and_fix_slashes(char *,char *, int * );
317extern char* concat_args_and_quote(char **,char**,char **,unsigned int *, char **,
318	unsigned int *) ;
319
320
321extern int is_nt_executable(char*,char*);
322/* io.c */
323extern int  force_read(int, unsigned char*,size_t);
324extern int  nt_read(int, unsigned char*,size_t);
325extern int  nt_write(int, const unsigned char*,size_t);
326extern int stringtable_read(int,char*,size_t);
327
328/* tparse.c */
329extern int  tc_putc(char,FILE*);
330
331
332void nt_cleanup(void);
333
334/* stdio.c */
335extern int  nt_creat(const char*,int);
336extern int  nt_close(int);
337extern int  nt_open(const char*,int ,...);
338extern int  nt_pipe(int*);
339extern void restore_fds(void ) ;
340extern void copy_fds(void);
341extern void close_copied_fds(void ) ;
342extern int  nt_fgetc(FILE*);
343extern int	 nt_dup(int);
344extern int  nt_fputs(char*,FILE*);
345extern int  nt_putc(char,FILE*);
346extern int  nt_fflush(FILE*);
347extern int  nt_fputc(char, FILE*);
348extern int  nt_fprintf(FILE*,char*,...);
349extern int  nt_puts(char*);
350extern int  nt_putchar(char);
351extern int  nt_fclose(FILE*);
352extern int  nt_fputs(char *, FILE*);
353extern intptr_t  __nt_get_osfhandle(int);
354extern int __nt_open_osfhandle(intptr_t, int);
355extern int nt_clearerr(FILE*);
356extern int  nt_dup2(int,int );
357extern FILE* nt_fdopen(int,char*);
358extern char *  nt_fgets(char *,int, FILE*);
359extern int nt_fileno(FILE*);
360extern FILE *nt_fopen(char *,char*);
361extern int nt_fread(void *,size_t,size_t,FILE*);
362extern int nt_fwrite(void*,size_t,size_t,FILE*);
363extern int nt_fseek(FILE*,long,int);
364extern long nt_ftell(FILE*);
365extern int  nt_isatty(int);
366extern int  nt_lseek(int,long,int);
367extern int nt_printf(char*,...);
368extern int nt_access(char*,int);
369extern int nt_fstat(int, struct stat *) ;
370extern int nt_stat(const char *, struct stat *) ;
371extern void nt_close_on_exec(int , int);
372extern void init_stdio(void) ;
373extern int is_resource_file(int);
374#ifndef STDIO_C
375extern void *my_stdin,*my_stdout,*my_stderr;
376#endif STDIO_C
377
378
379/* nt.char.c */
380extern unsigned char oem_it(unsigned char );
381extern char *nt_cgets(int,int,char*);
382extern void nls_dll_init(void);
383extern void nls_dll_unload(void);
384extern void nt_autoset_dspmbyte(void);
385
386/* fork.c */
387extern int fork_init(void);
388extern int fork(void);
389extern void *sbrk(int);
390extern void *fmalloc(size_t);
391extern void ffree(void *);
392extern void *frealloc(void*,size_t);
393extern void *fcalloc(size_t,size_t);
394extern void set_stackbase(void*);
395
396/* console.c */
397extern void do_nt_cooked_mode(void );
398extern void do_nt_raw_mode(void ) ;
399extern int do_nt_check_cooked_mode(void);
400extern void set_cons_attr (char *);
401extern void NT_MoveToLineOrChar(int ,int ) ;
402extern void nt_term_init(void);
403extern void nt_term_cleanup(void);
404extern void nt_set_size(int,int);
405//extern DWORD set_cooked_mode(HANDLE);
406//extern void set_raw_mode(HANDLE);
407//extern void set_arbitrary_mode(HANDLE,DWORD);
408extern void set_attributes(const unsigned char *color);
409
410/* ../sh.exec.c */
411extern int nt_check_if_windir(char *);
412extern void nt_check_name_and_hash(int ,char *,int);
413
414
415/* clip.c */
416extern void cut_clip(void);
417extern int paste_clip(void);
418extern void init_clipboard(void);
419extern HANDLE create_clip_writer_thread(void) ;
420extern HANDLE create_clip_reader_thread(void) ;
421
422/* signal.c */
423extern int kill(int,int);
424extern int nice(int);
425extern void nt_init_signals(void) ;
426extern void nt_cleanup_signals(void) ;
427extern void start_sigchild_thread(HANDLE , DWORD ) ;
428
429/* nt.who.c */
430extern void start_ncbs(short **);
431extern void cleanup_netbios(void);
432
433/* ntfunc.c */
434struct command;
435extern void dostart(short **,struct command *);
436extern void docls(short **,struct command *);
437extern void dotitle(short **, struct command * ) ;
438extern void dostacksize(short**,struct command *);
439extern void dosourceresource(short **, struct command * ) ;
440extern void doprintresource(short **, struct command * ) ;
441#ifdef NTDBG
442extern void dodebugbreak(short **, struct command * ) ;
443#endif NTDBG
444extern void nt_set_env(const short *, const short*);
445extern char *hb_subst(char *) ;
446extern void init_hb_subst() ;
447extern void init_shell_dll(void) ;
448extern void try_shell_ex(char**,int,BOOL);
449extern int nt_try_fast_exec(struct command *);
450extern int nt_feed_to_cmd(char*,char**);
451extern short nt_translate_bindkey(const short*);
452
453extern struct biltins *nt_check_additional_builtins(short *);
454extern void nt_print_builtins(size_t);
455
456/* ps.c */
457extern void init_plister(void);
458extern void dops(short **,struct command *);
459extern void doshutdown(short **,struct command *);
460extern int kill_by_wm_close(int ) ;
461
462/* globals.c */
463extern int  is_gui(char*);
464extern int  is_9x_gui(char*);
465
466
467/* Global variables */
468extern unsigned short __nt_want_vcode,__nt_vcode,__nt_really_exec;
469extern int __dup_stdin;
470extern int __nt_only_start_exes;
471extern unsigned short __nt_child_nohupped;
472extern DWORD gdwPlatform,gdwVersion;
473extern int is_dev_clipboard_active;
474extern HANDLE ghdevclipthread;
475extern DWORD gdwStackSize;
476
477// bogus
478#define getppid() 0
479
480struct tms {
481	clock_t tms_utime;
482	clock_t tms_stime;
483	clock_t tms_cutime;
484	clock_t tms_cstime;
485};
486#define UT_UNKNOWN 0
487#define DEAD_PROCESS 7
488#define USER_PROCESS 8
489#define UT_LINESIZE  16
490#define UT_NAMESIZE  8
491#define UT_HOSTSIZE  16
492
493struct utmp {
494	short ut_type; /* type of login */
495	pid_t ut_pid;
496	char ut_line[UT_LINESIZE]; /* device pref'/dev/' */
497	char ut_id[2]; /*abbrev tty name */
498	time_t ut_time; /* login time */
499	char ut_user[UT_NAMESIZE]; /* user name */
500	char ut_host[UT_HOSTSIZE]; /* hostname for rlogin */
501	long ut_addr;  /*ipaddr of remote host */
502};
503
504
505#define ut_name ut_user
506#define killpg kill
507
508#endif NTPORT_H
509