Source

emacs / src / w32.c

   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
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
/* Utility and Unix shadow routines for GNU Emacs on Windows NT.
   Copyright (C) 1994, 1995 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.

   Geoff Voelker (voelker@cs.washington.edu)                         7-29-94
*/


/* Define stat before including config.h.  */
#include <string.h>
#include <sys/stat.h>
#include <malloc.h>

static int is_toplevel_share_name (char *);
static int stat_toplevel_share (char *, void *);

int
nt_stat (char *filename, struct stat *statbuf)
{
  int l = strlen (filename);
  char *str = NULL;

  /* stat has a bug when passed a name of a directory with a trailing
     backslash (but a trailing forward slash works fine).  */
  if (filename[l - 1] == '\\') 
    {
      str = (char *) alloca (l + 1);
      strcpy (str, filename);
      str[l - 1] = '/';
      return stat (str, statbuf);
    }

  if (stat (filename, statbuf) == 0)
    return 0;
  else if (is_toplevel_share_name (filename))
    return stat_toplevel_share (filename, statbuf);
  else
    return -1;
}

/* Place a wrapper around the NT version of ctime.  It returns NULL
   on network directories, so we handle that case here.  
   Define it before including config.h.  (Ulrich Leodolter, 1/11/95).  */
char *
nt_ctime (const time_t *t)
{
  char *str = (char *) ctime (t);
  return (str ? str : "Sun Jan 01 00:00:00 1970");
}

#include <config.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <ctype.h>

#define getwd _getwd
#include "lisp.h"
#undef getwd

#include <pwd.h>

#include "ndir.h"
#include "ntheap.h"

extern int report_file_error (char *, Lisp_Object);

/* Routines for extending stat above.  */
static int
get_unassigned_drive_letter ()
{
  int i;
  unsigned int mask;
 
  mask = GetLogicalDrives ();
  for (i = 0; i < 26; i++)
    {
      if (mask & (1 << i))
	continue;
      break;
    }
  return (i == 26 ? -1 : 'A' + i);
}

void dostounix_filename (char *);

/* Return nonzero if NAME is of the form \\host\share (forward slashes
   also valid), otherwise return 0.  */
static int
is_toplevel_share_name (char *filename)
{
  int len;
  char *name;
  char *host;
  char *share;
  char *suffix;

  len = strlen (filename);
  name = alloca (len + 1);
  strcpy (name, filename);

  dostounix_filename (name);
  if (name[0] != '/' || name[1] != '/')
    return 0;

  host = strtok (&name[2], "/");
  share = strtok (NULL, "/");
  suffix = strtok (NULL, "/");
  if (!host || !share || suffix)
    return 0;

  return 1;
}


/* FILENAME is of the form \\host\share, and stat can't handle names
   of this form.  But stat can handle \\host\share if it's been
   assigned a drive letter.  So we create a network connection to this
   share, assign it a drive letter, stat the drive letter, and
   disconnect from the share.  Hassle... */
static int
stat_toplevel_share (char *filename, void *statbuf)
{
  NETRESOURCE net;
  int drive_letter;
  char drive[4];
  int result;

  drive_letter = get_unassigned_drive_letter ();
  if (drive_letter < 0)
    return -1;
  
  drive[0] = drive_letter;
  drive[1] = ':';
  drive[2] = '\0';
  net.dwType = RESOURCETYPE_DISK;
  net.lpLocalName = drive;
  net.lpRemoteName = filename;
  net.lpProvider = NULL;
  
  switch (WNetAddConnection2 (&net, NULL, NULL, 0))
    {
    case NO_ERROR:
      break;
    case ERROR_ALREADY_ASSIGNED:
    default:
      return -1;
    }
  
  /* Name the toplevel directory on the drive letter. */
  drive[2] = '/';
  drive[3] = '\0';
  result = stat (drive, (void *) statbuf);
  
  /* Strip the slash so we can disconnect. */
  drive[2] = '\0';
  if (WNetCancelConnection2 (drive, 0, TRUE) != NO_ERROR)
    result = -1;

  return result;
}


/* Get the current working directory.  */
int
getwd (char *dir)
{
  return GetCurrentDirectory (MAXPATHLEN, dir);
}

/* Emulate gethostname.  */
int
gethostname (char *buffer, int size)
{
  /* NT only allows small host names, so the buffer is 
     certainly large enough.  */
  return !GetComputerName (buffer, &size);
}

/* Emulate getloadavg.  */
int
getloadavg (double loadavg[], int nelem)
{
  int i;

  /* A faithful emulation is going to have to be saved for a rainy day.  */
  for (i = 0; i < nelem; i++) 
    {
      loadavg[i] = 0.0;
    }
  return i;
}

/* Emulate sleep...we could have done this with a define, but that
   would necessitate including windows.h in the files that used it.
   This is much easier.  */
void
nt_sleep (int seconds)
{
  Sleep (seconds * 1000);
}

/* Emulate rename. */

#ifndef ENOENT
#define ENOENT 2
#endif
#ifndef EXDEV
#define EXDEV 18
#endif
#ifndef EINVAL
#define EINVAL 22
#endif

int
rename (const char *oldname, const char *newname)
{
#ifdef WINDOWS95
  int i, len, len0, len1;
  char *dirs[2], *names[2], *ptr;

  /* A bug in MoveFile under Windows 95 incorrectly renames files in
     some cases.  If the old name is of the form FILENAME or
     FILENAME.SUF, and the new name is of the form FILENAME~ or
     FILENAME.SUF~, and both the source and target are in the same
     directory, then MoveFile renames the long form of the filename to
     FILENAME~ (FILENAME.SUF~) but leaves the DOS short form as
     FILENAME (FILENAME.SUF).  The result is that the two different
     filenames refer to the same file.  In this case, rename the
     source to a temporary name that can then successfully be renamed
     to the target.  */

  dirs[0] = names[0] = oldname;
  dirs[1] = names[1] = newname;
  for (i = 0; i < 2; i++)
    {
      /* Canonicalize and remove prefix.  */
      len = strlen (names[i]);
      for (ptr = names[i] + len - 1; ptr > names[i]; ptr--)
	{
	  if (IS_ANY_SEP (ptr[0]) && ptr[1] != '\0')
	    {
	      names[i] = ptr + 1;
	      break;
	    }
	}
    }

  len0 = strlen (names[0]);
  len1 = strlen (names[1]);

  /* The predicate is whether the file is being renamed to a filename
     with ~ appended.  This is conservative, but should be correct.  */
  if ((len0 == len1 - 1)
      && (names[1][len0] == '~')
      && (!strnicmp (names[0], names[1], len0)))
    {
      /* Rename the source to a temporary name that can succesfully be
	 renamed to the target.  The temporary name is in the directory
	 of the target.  */
      char *tmp, *fulltmp;

      tmp = "eXXXXXX";
      fulltmp = alloca (strlen (dirs[1]) + strlen (tmp) + 1);
      fulltmp[0] = '\0';
      if (dirs[1] != names[1])
	{
	  len = names[1] - dirs[1];
	  strncpy (fulltmp, dirs[1], len);
	  fulltmp[len] = '\0';
	}
      strcat (fulltmp, tmp);
      mktemp (fulltmp);

      if (rename (oldname, fulltmp) < 0)
	return -1;
      
      oldname = fulltmp;
    }
#endif

  if (!MoveFile (oldname, newname))
    {
      switch (GetLastError ())
	{
	case ERROR_FILE_NOT_FOUND:
	  errno = ENOENT;
	  break;
	case ERROR_ACCESS_DENIED:
	  /* This gets returned when going across devices.  */
	  errno = EXDEV;
	  break;
	case ERROR_FILE_EXISTS:
	case ERROR_ALREADY_EXISTS:
	default:
	  errno = EINVAL;
	  break;
	}
      return -1;
    }
  errno = 0;
  return 0;
}

/* Emulate the Unix directory procedures opendir, closedir, 
   and readdir.  We can't use the procedures supplied in sysdep.c,
   so we provide them here.  */

struct direct dir_static;       /* simulated directory contents */
static int    dir_finding;
static HANDLE dir_find_handle;

DIR *
opendir (char *filename)
{
  DIR *dirp;

  /* Opening is done by FindFirstFile.  However, a read is inherent to
     this operation, so we have a flag to handle the open at read
     time.  This flag essentially means "there is a find-handle open and
     it needs to be closed."  */

  if (!(dirp = (DIR *) malloc (sizeof (DIR)))) 
    {
      return 0;
    }

  dirp->dd_fd = 0;
  dirp->dd_loc = 0;
  dirp->dd_size = 0;

  /* This is tacky, but we need the directory name for our
     implementation of readdir.  */
  strncpy (dirp->dd_buf, filename, DIRBLKSIZ);
  return dirp;
}

void
closedir (DIR *dirp)
{
  /* If we have a find-handle open, close it.  */
  if (dir_finding) 
    {
      FindClose (dir_find_handle);
      dir_finding = 0;
    }
  xfree ((char *) dirp);
}

struct direct *
readdir (DIR *dirp)
{
  WIN32_FIND_DATA find_data;
  
  /* If we aren't dir_finding, do a find-first, otherwise do a find-next. */
  if (!dir_finding) 
    {
      char filename[MAXNAMLEN + 3];
      int ln;

      strncpy (filename, dirp->dd_buf, MAXNAMLEN);
      ln = strlen (filename)-1;
      if (!IS_ANY_SEP (filename[ln]))
	strcat (filename, "\\");
      strcat (filename, "*.*");

      dir_find_handle = FindFirstFile (filename, &find_data);

      if (dir_find_handle == INVALID_HANDLE_VALUE) 
	return NULL;

      dir_finding = 1;
    } 
  else 
    {
      if (!FindNextFile (dir_find_handle, &find_data))
	return NULL;
    }
  
  /* NT's unique ID for a file is 64 bits, so we have to fake it here.  
     This should work as long as we never use 0.  */
  dir_static.d_ino = 1;
  
  dir_static.d_reclen = sizeof (struct direct) - MAXNAMLEN + 3 +
    dir_static.d_namlen - dir_static.d_namlen % 4;
  
  dir_static.d_namlen = strlen (find_data.cFileName);
  strncpy (dir_static.d_name, find_data.cFileName, MAXNAMLEN);
  
  return &dir_static;
}

/* Emulate getpwuid and getpwnam.  */

int getuid ();	/* forward declaration */

#define PASSWD_FIELD_SIZE 256

static char the_passwd_name[PASSWD_FIELD_SIZE];
static char the_passwd_passwd[PASSWD_FIELD_SIZE];
static char the_passwd_gecos[PASSWD_FIELD_SIZE];
static char the_passwd_dir[PASSWD_FIELD_SIZE];
static char the_passwd_shell[PASSWD_FIELD_SIZE];

static struct passwd the_passwd = 
{
  the_passwd_name,
  the_passwd_passwd,
  0,
  0,
  0,
  the_passwd_gecos,
  the_passwd_dir,
  the_passwd_shell,
};

struct passwd *
getpwuid (int uid)
{
  int size = PASSWD_FIELD_SIZE;
  
  if (!GetUserName (the_passwd.pw_name, &size))
    return NULL;

  the_passwd.pw_passwd[0] = '\0';
  the_passwd.pw_uid = 0;
  the_passwd.pw_gid = 0;
  strcpy (the_passwd.pw_gecos, the_passwd.pw_name);
  the_passwd.pw_dir[0] = '\0';
  the_passwd.pw_shell[0] = '\0';

  return &the_passwd;
}

struct passwd *
getpwnam (char *name)
{
  struct passwd *pw;
  
  pw = getpwuid (getuid ());
  if (!pw)
    return pw;

  if (strcmp (name, pw->pw_name))
    return NULL;

  return pw;
}


/* We don't have scripts to automatically determine the system configuration
   for Emacs before it's compiled, and we don't want to have to make the
   user enter it, so we define EMACS_CONFIGURATION to invoke this runtime
   routine.  */

static char configuration_buffer[32];

char *
get_emacs_configuration (void)
{
  char *arch, *oem, *os;

  /* Determine the processor type.  */
  switch (get_processor_type ()) 
    {

#ifdef PROCESSOR_INTEL_386
    case PROCESSOR_INTEL_386:
    case PROCESSOR_INTEL_486:
    case PROCESSOR_INTEL_PENTIUM:
      arch = "i386";
      break;
#endif

#ifdef PROCESSOR_INTEL_860
    case PROCESSOR_INTEL_860:
      arch = "i860";
      break;
#endif

#ifdef PROCESSOR_MIPS_R2000
    case PROCESSOR_MIPS_R2000:
    case PROCESSOR_MIPS_R3000:
    case PROCESSOR_MIPS_R4000:
      arch = "mips";
      break;
#endif

#ifdef PROCESSOR_ALPHA_21064
    case PROCESSOR_ALPHA_21064:
      arch = "alpha";
      break;
#endif

    default:
      arch = "unknown";
      break;
    }

  /* Let oem be "*" until we figure out how to decode the OEM field.  */
  oem = "*";

#ifdef WINDOWS95
  os = "win";
#else
  os = "nt";
#endif

  sprintf (configuration_buffer, "%s-%s-%s%d.%d", arch, oem, os,
	   get_nt_major_version (), get_nt_minor_version ());
  return configuration_buffer;
}

/* Conjure up inode and device numbers that will serve the purpose
   of Emacs.  Return 1 upon success, 0 upon failure.  */
int
get_inode_and_device_vals (Lisp_Object filename, Lisp_Object *p_inode, 
			   Lisp_Object *p_device)
{
  /* File uids on NT are found using a handle to a file, which
     implies that it has been opened.  Since we want to be able
     to stat an arbitrary file, we must open it, get the info,
     and then close it.
     
     Also, NT file uids are 64-bits.  This is a problem.  */

  HANDLE handle;
  BOOL result;
  DWORD attrs;
  BY_HANDLE_FILE_INFORMATION info;

  /* We have to stat files and directories differently, so check
     to see what filename references.  */
  attrs = GetFileAttributes (XSTRING (filename)->data);
  if (attrs == 0xFFFFFFFF) {
    return 0;
  }
  if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
    /* Conjure up bogus, but unique, values.  */
    attrs = GetTickCount ();
    *p_inode = make_number (attrs);
    *p_device = make_number (attrs);
    return 1;
  }

  /* FIXME:  It shouldn't be opened without READ access, but NT on x86
     doesn't allow GetFileInfo in that case (NT on mips does).  */
     
  handle = CreateFile (XSTRING (filename)->data,
		       GENERIC_READ,
		       FILE_SHARE_READ | FILE_SHARE_WRITE,
		       NULL,
		       OPEN_EXISTING,
		       FILE_ATTRIBUTE_NORMAL,
		       NULL);
  if (handle == INVALID_HANDLE_VALUE)
    return 0;

  result = GetFileInformationByHandle (handle, &info);
  CloseHandle (handle);
  if (!result)
    return 0;

  *p_inode = make_number (info.nFileIndexLow);	        /* use the low value */
  *p_device = make_number (info.dwVolumeSerialNumber);

  return 1;
}

/* The following pipe routines are used to support our fork emulation.
   Since NT's crt dup always creates inherited handles, we
   must be careful in setting up pipes.  First create 
   non-inherited pipe handles, then create an inherited handle
   to the write end by dup-ing it, and then close the non-inherited
   end that was just duped.  This gives us one non-inherited handle
   on the read end and one inherited handle to the write end.  As
   the parent, we close the inherited handle to the write end after
   spawning the child.  */

/* From callproc.c  */
extern Lisp_Object Vbinary_process_input;
extern Lisp_Object Vbinary_process_output;

void
pipe_with_inherited_out (int fds[2])
{
  int inherit_out;
  unsigned int flags = _O_NOINHERIT;

  if (!NILP (Vbinary_process_output))
    flags |= _O_BINARY;

  _pipe (fds, 0, flags);
  inherit_out = dup (fds[1]);
  close (fds[1]);
  fds[1] = inherit_out;
}

void
pipe_with_inherited_in (int fds[2])
{
  int inherit_in;
  unsigned int flags = _O_NOINHERIT;

  if (!NILP (Vbinary_process_input))
    flags |= _O_BINARY;

  _pipe (fds, 0, flags);
  inherit_in = dup (fds[0]);
  close (fds[0]);
  fds[0] = inherit_in;
}

/* The following two routines are used to manipulate stdin, stdout, and
   stderr of our child processes.

   Assuming that in, out, and err are inherited, we make them stdin,
   stdout, and stderr of the child as follows:

   - Save the parent's current standard handles.
   - Set the parent's standard handles to the handles being passed in.
     (Note that _get_osfhandle is an io.h procedure that 
     maps crt file descriptors to NT file handles.)
   - Spawn the child, which inherits in, out, and err as stdin,
     stdout, and stderr. (see Spawnve)
   - Reset the parent's standard handles to the saved handles.
     (see reset_standard_handles)
   We assume that the caller closes in, out, and err after calling us.  */

void
prepare_standard_handles (int in, int out, int err, HANDLE handles[4])
{
  HANDLE parent, stdin_save, stdout_save, stderr_save, err_handle;

#ifdef WINDOWS95
  /* The Win95 beta doesn't set the standard handles correctly.
     Handicap subprocesses until we get a version that works correctly.  
     Undefining the subprocesses macro reveals other incompatibilities,
     so, since we're expecting subprocs to work in the near future, 
     disable them here.  */
  report_file_error ("Subprocesses currently disabled on Win95", Qnil);
#endif

  parent = GetCurrentProcess ();
  stdin_save = GetStdHandle (STD_INPUT_HANDLE);
  stdout_save = GetStdHandle (STD_OUTPUT_HANDLE);
  stderr_save = GetStdHandle (STD_ERROR_HANDLE);

#ifndef HAVE_NTGUI
  if (!DuplicateHandle (parent, 
		       GetStdHandle (STD_INPUT_HANDLE), 
		       parent,
		       &stdin_save, 
		       0, 
		       FALSE, 
		       DUPLICATE_SAME_ACCESS))
    report_file_error ("Duplicating parent's input handle", Qnil);
  
  if (!DuplicateHandle (parent,
		       GetStdHandle (STD_OUTPUT_HANDLE),
		       parent,
		       &stdout_save,
		       0,
		       FALSE,
		       DUPLICATE_SAME_ACCESS))
    report_file_error ("Duplicating parent's output handle", Qnil);
  
  if (!DuplicateHandle (parent,
		       GetStdHandle (STD_ERROR_HANDLE),
		       parent,
		       &stderr_save,
		       0,
		       FALSE,
		       DUPLICATE_SAME_ACCESS))
    report_file_error ("Duplicating parent's error handle", Qnil);
#endif /* !HAVE_NTGUI */
  
  if (!SetStdHandle (STD_INPUT_HANDLE, (HANDLE) _get_osfhandle (in)))
    report_file_error ("Changing stdin handle", Qnil);
  
  if (!SetStdHandle (STD_OUTPUT_HANDLE, (HANDLE) _get_osfhandle (out)))
    report_file_error ("Changing stdout handle", Qnil);
  
  /* We lose data if we use the same handle to the pipe for stdout and
     stderr, so make a duplicate.  This took a while to find.  */
  if (out == err) 
    {
      if (!DuplicateHandle (parent,
			   (HANDLE) _get_osfhandle (err),
			   parent,
			   &err_handle,
			   0,
			   TRUE,
			   DUPLICATE_SAME_ACCESS))
	report_file_error ("Duplicating out handle to make err handle.",
			  Qnil);
    } 
  else 
    {
      err_handle = (HANDLE) _get_osfhandle (err);
    }

  if (!SetStdHandle (STD_ERROR_HANDLE, err_handle))
    report_file_error ("Changing stderr handle", Qnil);

  handles[0] = stdin_save;
  handles[1] = stdout_save;
  handles[2] = stderr_save;
  handles[3] = err_handle;
}

void
reset_standard_handles (int in, int out, int err, HANDLE handles[4])
{
  HANDLE stdin_save = handles[0];
  HANDLE stdout_save = handles[1];
  HANDLE stderr_save = handles[2];
  HANDLE err_handle = handles[3];
  int i;

#ifndef HAVE_NTGUI
  if (!SetStdHandle (STD_INPUT_HANDLE, stdin_save))
    report_file_error ("Resetting input handle", Qnil);
  
  if (!SetStdHandle (STD_OUTPUT_HANDLE, stdout_save))
    {
      i = GetLastError ();
      report_file_error ("Resetting output handle", Qnil);
    }
  
  if (!SetStdHandle (STD_ERROR_HANDLE, stderr_save))
    report_file_error ("Resetting error handle", Qnil);
#endif /* !HAVE_NTGUI */
  
  if (out == err) 
    {
      /* If out and err are the same handle, then we duplicated out
	 and stuck it in err_handle.  Close the duplicate to clean up.  */
      if (!CloseHandle (err_handle))
	report_file_error ("Closing error handle duplicated from out.", 
			  Qnil);
    }
}

int
random ()
{
  /* rand () on NT gives us 15 random bits...hack together 30 bits.  */
  return ((rand () << 15) | rand ());
}

void
srandom (int seed)
{
  srand (seed);
}

/* Destructively turn backslashes into slashes.  */
void
dostounix_filename (p)
     register char *p;
{
  while (*p)
    {
      if (*p == '\\')
	*p = '/';
      p++;
    }
}

/* Routines that are no-ops on NT but are defined to get Emacs to compile.  */


int 
sigsetmask (int signal_mask) 
{ 
  return 0;
}

int 
sigblock (int sig) 
{ 
  return 0;
}

int 
kill (int pid, int signal) 
{ 
  return 0;
}

int 
setpgrp (int pid, int gid) 
{ 
  return 0;
}

int 
alarm (int seconds) 
{ 
  return 0;
}

int 
unrequest_sigio (void) 
{ 
  return 0;
}

int 
request_sigio (void) 
{ 
  return 0;
}

int 
getuid () 
{ 
  char buffer[256];
  int size = 256;

  if (!GetUserName (buffer, &size))
    /* Assume all powers upon failure.  */
    return 0;

  if (!stricmp ("administrator", buffer))
    return 0;
  else
    /* A complete fabrication...is there anything to base it on? */
    return 123;
}

int 
geteuid () 
{ 
  /* I could imagine arguing for checking to see whether the user is
     in the Administrators group and returning a UID of 0 for that
     case, but I don't know how wise that would be in the long run.  */
  return getuid (); 
}

/* Remove all CR's that are followed by a LF.
   (From msdos.c...probably should figure out a way to share it,
   although this code isn't going to ever change.)  */
int
crlf_to_lf (n, buf)
     register int n;
     register unsigned char *buf;
{
  unsigned char *np = buf;
  unsigned char *startp = buf;
  unsigned char *endp = buf + n;

  if (n == 0)
    return n;
  while (buf < endp - 1)
    {
      if (*buf == 0x0d)
	{
	  if (*(++buf) != 0x0a)
	    *np++ = 0x0d;
	}
      else
	*np++ = *buf++;
    }
  if (buf < endp)
    *np++ = *buf++;
  return np - startp;
}

#define REG_ROOT "SOFTWARE\\GNU\\Emacs\\"

LPBYTE 
nt_get_resource (key, lpdwtype)
    char *key;
    LPDWORD lpdwtype;
{
  LPBYTE lpvalue;
  HKEY hrootkey = NULL;
  DWORD cbData;
  BOOL ok = FALSE;
  
  /* Check both the current user and the local machine to see if 
     we have any resources.  */
  
  if (RegOpenKeyEx (HKEY_CURRENT_USER, REG_ROOT, 0, KEY_READ, &hrootkey) == ERROR_SUCCESS)
    {
      lpvalue = NULL;

      if (RegQueryValueEx (hrootkey, key, NULL, NULL, NULL, &cbData) == ERROR_SUCCESS 
	  && (lpvalue = (LPBYTE) xmalloc (cbData)) != NULL 
	  && RegQueryValueEx (hrootkey, key, NULL, lpdwtype, lpvalue, &cbData) == ERROR_SUCCESS)
	{
	  return (lpvalue);
	}

      if (lpvalue) xfree (lpvalue);
	
      RegCloseKey (hrootkey);
    } 
  
  if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_ROOT, 0, KEY_READ, &hrootkey) == ERROR_SUCCESS)
    {
      lpvalue = NULL;
	
      if (RegQueryValueEx (hrootkey, key, NULL, NULL, NULL, &cbData) == ERROR_SUCCESS &&
	  (lpvalue = (LPBYTE) xmalloc (cbData)) != NULL &&
	  RegQueryValueEx (hrootkey, key, NULL, lpdwtype, lpvalue, &cbData) == ERROR_SUCCESS)
	{
	  return (lpvalue);
	}
	
      if (lpvalue) xfree (lpvalue);
	
      RegCloseKey (hrootkey);
    } 
  
  return (NULL);
}

void
init_environment ()
{
  /* Open a console window to display messages during dumping. */
  if (!initialized)
    AllocConsole ();

  /* Check for environment variables and use registry if they don't exist */
  {
      int i;
      LPBYTE lpval;
      DWORD dwType;

      static char * env_vars[] = 
      {
	  "emacs_path",
	  "EMACSLOADPATH",
	  "SHELL",
	  "EMACSDATA",
	  "EMACSPATH",
	  "EMACSLOCKDIR",
	  "INFOPATH",
	  "EMACSDOC",
	  "TERM",
      };

      for (i = 0; i < (sizeof (env_vars) / sizeof (env_vars[0])); i++) 
	{
	  if (!getenv (env_vars[i]) &&
	      (lpval = nt_get_resource (env_vars[i], &dwType)) != NULL)
	    {
	      if (dwType == REG_EXPAND_SZ)
		{
		  char buf1[500], buf2[500];

		  ExpandEnvironmentStrings ((LPSTR) lpval, buf1, 500);
		  _snprintf (buf2, 499, "%s=%s", env_vars[i], buf1);
		  putenv (strdup (buf2));
		}
	      else if (dwType == REG_SZ)
		{
		  char buf[500];
		  
		  _snprintf (buf, 499, "%s=%s", env_vars[i], lpval);
		  putenv (strdup (buf));
		}

	      xfree (lpval);
	    }
	}
    }
}

#ifdef HAVE_TIMEVAL
#include <sys/timeb.h>

/* Emulate gettimeofday (Ulrich Leodolter, 1/11/95).  */
void 
gettimeofday (struct timeval *tv, struct timezone *tz)
{
  struct _timeb tb;
  _ftime (&tb);

  tv->tv_sec = tb.time;
  tv->tv_usec = tb.millitm * 1000L;
  if (tz) 
    {
      tz->tz_minuteswest = tb.timezone;	/* minutes west of Greenwich  */
      tz->tz_dsttime = tb.dstflag;	/* type of dst correction  */
    }
}
#endif /* HAVE_TIMEVAL */


#ifdef PIGSFLY
Keep this around...we might need it later.
#ifdef WINDOWSNT
{
  /*
   * Find the user's real name by opening the process token and looking
   * up the name associated with the user-sid in that token.
   */

  char            b[256], Name[256], RefD[256];
  DWORD           length = 256, rlength = 256, trash;
  HANDLE          Token;
  SID_NAME_USE    User;

  if (1)
    Vuser_real_login_name = build_string ("foo");
  else if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &Token))
    {
      Vuser_real_login_name = build_string ("unknown");
    }
  else if (!GetTokenInformation (Token, TokenUser, (PVOID)b, 256,
				 &trash))
    {
      CloseHandle (Token);
      Vuser_real_login_name = build_string ("unknown");
    }
  else if (!LookupAccountSid ((void *)0, (PSID)b, Name, &length, RefD,
			      &rlength, &User))
    {
      CloseHandle (Token);
      Vuser_real_login_name = build_string ("unknown");
    }
  else
    Vuser_real_login_name = build_string (Name);
}
#else   /* not WINDOWSNT */
#endif  /* not WINDOWSNT */
#endif  /* PIGSFLY */