home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / the25.zip / thesrc251.zip / nonansi.c < prev    next >
C/C++ Source or Header  |  1998-07-23  |  26KB  |  797 lines

  1. /***********************************************************************/
  2. /* NONANSI.C -                                                         */
  3. /* This file contains all calls to non-ansi conforming routines.       */
  4. /***********************************************************************/
  5. /*
  6.  * THE - The Hessling Editor. A text editor similar to VM/CMS xedit.
  7.  * Copyright (C) 1991-1997 Mark Hessling
  8.  *
  9.  * This program is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU General Public License as
  11.  * published by the Free Software Foundation; either version 2 of
  12.  * the License, or any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17.  * General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to:
  21.  *
  22.  *    The Free Software Foundation, Inc.
  23.  *    675 Mass Ave,
  24.  *    Cambridge, MA 02139 USA.
  25.  *
  26.  *
  27.  * If you make modifications to this software that you feel increases
  28.  * it usefulness for the rest of the community, please email the
  29.  * changes, enhancements, bug fixes as well as any and all ideas to me.
  30.  * This software is going to be maintained and enhanced as deemed
  31.  * necessary by the community.
  32.  *
  33.  * Mark Hessling                 Email:             M.Hessling@qut.edu.au
  34.  * PO Box 203                    Phone:                    +617 3802 0800
  35.  * Bellara                       http://www.gu.edu.au/gext/the/markh.html
  36.  * QLD 4507                      **** Maintainer PDCurses & REXX/SQL ****
  37.  * Australia                     ************* Author of THE ************
  38.  */
  39.  
  40. /*
  41. $Id: nonansi.c 2.1 1995/06/24 16:30:30 MH Rel MH $
  42. */
  43.  
  44. #include <the.h>
  45. #include <proto.h>
  46.  
  47. #if defined(UNIX) || defined(__EMX__)
  48. # include <pwd.h>
  49. #endif
  50. #include <errno.h>
  51.  
  52. /*#define DEBUG 1*/
  53. #ifdef __EMX__                /* prevent DOS- and OS2-include's    */
  54. #elif defined(DOS)
  55. #  include <dos.h>
  56. #  if !defined(GO32)
  57. #    include <direct.h>
  58. #  endif
  59. #elif defined(OS2)
  60. #  include <direct.h>
  61. #  include <io.h>
  62. #  ifndef S_IFMT
  63. #    define S_IFMT 0xF000
  64. #  endif
  65. #endif
  66.  
  67. #if defined(WIN32) && defined(_MSC_VER)
  68. # include <direct.h>
  69. #endif
  70.  
  71. /***********************************************************************/
  72. #ifdef HAVE_PROTO
  73. short file_readable(CHARTYPE *filename)
  74. #else
  75. short file_readable(filename)
  76. CHARTYPE *filename;
  77. #endif
  78. /***********************************************************************/
  79. {
  80. /*--------------------------- local data ------------------------------*/
  81. /*--------------------------- processing ------------------------------*/
  82. #ifdef TRACE
  83.  trace_function("nonansi.c: file_readable");
  84. #endif
  85.  if (access((DEFCHAR *)filename,R_OK) == (-1))
  86.    {
  87. #ifdef TRACE
  88.     trace_return();
  89. #endif
  90.     return(FALSE);
  91.    }
  92. #ifdef TRACE
  93.     trace_return();
  94. #endif
  95.  return(TRUE);
  96. }
  97. /***********************************************************************/
  98. #ifdef HAVE_PROTO
  99. short file_writable(CHARTYPE *filename)
  100. #else
  101. short file_writable(filename)
  102. CHARTYPE *filename;
  103. #endif
  104. /***********************************************************************/
  105. {
  106. /*--------------------------- local data ------------------------------*/
  107. /*--------------------------- processing ------------------------------*/
  108. #ifdef TRACE
  109.  trace_function("nonansi.c: file_writable");
  110. #endif
  111.  if (!file_exists(filename))
  112.    {
  113. #ifdef TRACE
  114.     trace_return();
  115. #endif
  116.     return(TRUE);
  117.    }
  118.  if (access((DEFCHAR *)filename,W_OK) == (-1))
  119.    {
  120. #ifdef TRACE
  121.     trace_return();
  122. #endif
  123.     return(FALSE);
  124.    }
  125. #ifdef TRACE
  126.  trace_return();
  127. #endif
  128.  return(TRUE);
  129. }
  130. /***********************************************************************/
  131. #ifdef HAVE_PROTO
  132. short file_exists(CHARTYPE *filename)
  133. #else
  134. short file_exists(filename)
  135. CHARTYPE *filename;
  136. #endif
  137. /***********************************************************************/
  138. {
  139. /*--------------------------- local data ------------------------------*/
  140. /*--------------------------- processing ------------------------------*/
  141. #ifdef TRACE
  142.  trace_function("nonansi.c: file_exists");
  143. #endif
  144.  if (access((DEFCHAR *)filename,F_OK) == (-1))
  145.    {
  146. #ifdef TRACE
  147.     trace_return();
  148. #endif
  149.     return(FALSE);
  150.    }
  151. #ifdef TRACE
  152.  trace_return();
  153. #endif
  154.  return(TRUE);
  155. }
  156. /***********************************************************************/
  157. #ifdef HAVE_PROTO
  158. short remove_file(CHARTYPE *filename)
  159. #else
  160. short remove_file(filename)
  161. CHARTYPE *filename;
  162. #endif
  163. /***********************************************************************/
  164. {
  165. /*--------------------------- local data ------------------------------*/
  166. /*--------------------------- processing ------------------------------*/
  167. #ifdef TRACE
  168.  trace_function("nonansi.c: remove_file");
  169. #endif
  170. if(filename == NULL)
  171.      return(RC_OK);
  172. #ifdef VMS
  173.  if (delete(filename) == (-1))
  174. #else
  175.  if (unlink((DEFCHAR *)filename) == (-1))
  176. #endif
  177.    {
  178. #ifdef TRACE
  179.     trace_return();
  180. #endif
  181.     return(RC_ACCESS_DENIED);
  182.    }
  183. #ifdef TRACE
  184.  trace_return();
  185. #endif
  186.  return(RC_OK);
  187. }
  188. #if defined(DOS) || defined(OS2) || defined(WIN32) || defined(__EMX__)
  189. /***********************************************************************/
  190. #ifdef HAVE_PROTO
  191. short splitpath(CHARTYPE *filename)
  192. #else
  193. short splitpath(filename)
  194. CHARTYPE *filename;
  195. #endif
  196. /***********************************************************************/
  197. {
  198. /*-------------------------- external data ----------------------------*/
  199.  extern CHARTYPE curr_path[MAX_FILE_NAME+1];
  200.  extern CHARTYPE sp_fname[MAX_FILE_NAME+1];
  201.  extern CHARTYPE sp_path[MAX_FILE_NAME+1];
  202. /*--------------------------- local data ------------------------------*/
  203.  short len=0;
  204.  CHARTYPE work_filename[MAX_FILE_NAME+1] ;
  205.  CHARTYPE current_dir[MAX_FILE_NAME+1] ;
  206. #ifdef __EMX__
  207.  int new_dos_disk=0,current_dos_disk=0,temp_disk=0;
  208. #elif defined(DOS)
  209.  short new_dos_disk=0,current_dos_disk=0;        /* 1 - A,2 - B... */
  210.  short temp_disk=0;
  211. #elif defined (OS2)
  212.  ULONG logical_os2_drives=0L;
  213. #  if defined( __32BIT__) || defined(__386__)
  214.  ULONG new_dos_disk=0L,current_dos_disk=0L,temp_disk=0L;
  215. #  else
  216.  USHORT new_dos_disk=0,current_dos_disk=0,temp_disk=0;
  217. #  endif
  218. #elif defined(WIN32)
  219.  unsigned int new_dos_disk=0,current_dos_disk=0;        /* 1 - A,2 - B... */
  220.  unsigned int temp_disk=0;
  221. #endif
  222. /*--------------------------- processing ------------------------------*/
  223. #ifdef TRACE
  224.  trace_function("nonansi.c: splitpath");
  225. #endif
  226.  strcpy(sp_path,"");
  227.  strcpy(sp_fname,"");
  228.  strcpy(work_filename,filename);
  229. /*---------------------------------------------------------------------*/
  230. /* If the supplied filename is empty, set the path = cwd and filename  */
  231. /* equal to blank.                                                     */
  232. /*---------------------------------------------------------------------*/
  233.  if (strcmp(filename,"") == 0)
  234.    {
  235. #if defined(__EMX__)
  236.     _getcwd2(sp_path,MAX_FILE_NAME);
  237. #else
  238.     getcwd(sp_path,MAX_FILE_NAME);
  239. #endif
  240.     strcpy(sp_fname,"");
  241.    }
  242. /*---------------------------------------------------------------------*/
  243. /* For DOS and OS/2, get current drive.                                */
  244. /*---------------------------------------------------------------------*/
  245. #ifdef __EMX__
  246.  current_dos_disk = (_getdrive() - 'A') + 1;
  247. #elif defined(DOS)
  248. # if defined(TC) || defined(GO32)
  249.  current_dos_disk = (short)(getdisk() + 1);
  250. # endif
  251. # if defined(MSC)
  252.  _dos_getdrive(¤t_dos_disk);
  253. # endif
  254. #elif defined (OS2)
  255.  DosQueryCurrentDisk(¤t_dos_disk,&logical_os2_drives);
  256. #else                 /* WIN32                 */
  257. # if defined(__WATCOMC__)
  258.  _dos_getdrive(¤t_dos_disk);
  259. # else                    /* assume MSC                 */
  260.  current_dos_disk = _getdrive();
  261. # endif
  262. #endif
  263.  
  264.  new_dos_disk = current_dos_disk;
  265. /*---------------------------------------------------------------------*/
  266. /* For DOS and OS/2, if a drive specified determine the drive number.  */
  267. /*---------------------------------------------------------------------*/
  268.  if (*(filename+1) == ':')/* we assume this means a drive secification */
  269.     new_dos_disk = (toupper(*(filename)) - 'A') + 1;
  270. /*---------------------------------------------------------------------*/
  271. /* For DOS and OS/2, change to the specified disk (if supplied and     */
  272. /* different). Validate the drive number.                              */
  273. /*---------------------------------------------------------------------*/
  274.  if (new_dos_disk != current_dos_disk)
  275.    {
  276. #if defined(__EMX__)
  277.     _chdrive((char)((new_dos_disk-1)+'A'));
  278.     temp_disk = (_getdrive() - 'A') + 1;
  279. #elif defined(DOS)
  280. # if defined(TC) || defined(GO32)
  281.     setdisk((short)(new_dos_disk-1));
  282.     temp_disk = getdisk()+1;
  283. # else                    /* assume MSC                 */
  284.     _dos_setdrive(new_dos_disk,&temp_disk);
  285.     _dos_getdrive(&temp_disk);
  286. # endif
  287. #elif defined(OS2)
  288.     DosSetDefaultDisk(new_dos_disk);
  289.     DosQueryCurrentDisk(&temp_disk,&logical_os2_drives);
  290. #else                 /* assume WIN32              */
  291. # if defined(__WATCOMC__)
  292.     _dos_setdrive(new_dos_disk,&temp_disk);
  293.     _dos_getdrive(&temp_disk);
  294. # else                    /* assume MSC                 */
  295.     _chdrive(new_dos_disk);
  296.     temp_disk = _getdrive();
  297. # endif
  298. #endif
  299.  
  300.     if (temp_disk != new_dos_disk)  /* invalid drive */
  301.       {
  302. #ifdef TRACE
  303.        trace_return();
  304. #endif
  305.        return (RC_BAD_DRIVE);
  306.       }
  307.    }
  308. /*---------------------------------------------------------------------*/
  309. /* Save the current working directory on the specified drive, or the   */
  310. /* current drive if not specified.                                     */
  311. /*---------------------------------------------------------------------*/
  312. #if defined(__EMX__)
  313.  _getcwd2(current_dir,MAX_FILE_NAME);
  314. #else
  315.  getcwd(current_dir,MAX_FILE_NAME);
  316. #endif
  317. /*---------------------------------------------------------------------*/
  318. /* If the work_filename contains a drive specifier, special handling is*/
  319. /* needed.                                                             */
  320. /*---------------------------------------------------------------------*/
  321.  switch(strlen(filename))
  322.    {
  323.     case 1:
  324.          break;
  325. /*---------------------------------------------------------------------*/
  326. /* If the filename consists only of a drive specifier, copy the current*/
  327. /* directory for the now new drive into work_filename.                 */
  328. /*---------------------------------------------------------------------*/
  329.     case 2:
  330.          if (*(filename+1) == ':')
  331.             strcpy(work_filename,current_dir);
  332.          break;
  333.     default:
  334.          if (*(filename+1) == ':'
  335.          &&  *(filename+2) != ISLASH)
  336.            {
  337.             strcpy(work_filename,current_dir);
  338.             if (current_dir[strlen(current_dir)-1] != ISLASH)
  339.                strcat(work_filename,ISTR_SLASH);
  340.             strcat(work_filename,filename+2);
  341.            }
  342.          break;
  343.    }
  344. /*---------------------------------------------------------------------*/
  345. /* First determine if the supplied filename is a directory.            */
  346. /*---------------------------------------------------------------------*/
  347.  if (chdir(work_filename) == 0)  /* valid directory */
  348.    {
  349. #if defined(__EMX__)
  350.     _getcwd2(sp_path,MAX_FILE_NAME);
  351. #else
  352.     getcwd(sp_path,MAX_FILE_NAME);
  353. #endif
  354.     strcpy(sp_fname,"");
  355.    }
  356.  else          /* here if the file is not a directory */
  357.    {
  358.     len = strzreveq(work_filename,ISLASH);
  359.     switch(len)
  360.       {
  361.        case (-1):
  362. #if defined(__EMX__)
  363.             _getcwd2(sp_path,MAX_FILE_NAME);
  364. #else
  365.             getcwd(sp_path,MAX_FILE_NAME);
  366. #endif
  367.             strcpy(sp_fname,work_filename);
  368.             break;
  369.        case 0:
  370.             strcpy(sp_path,work_filename);
  371.             sp_path[1] = '\0';
  372.             strcpy(sp_fname,work_filename+1+len);
  373.             break;
  374.       default:
  375.             strcpy(sp_path,work_filename);
  376.             sp_path[len] = '\0';
  377.             strcpy(sp_fname,work_filename+1+len);
  378.             break;
  379.      }
  380.    }
  381.  if (strlen(sp_path) == 2
  382.  && sp_path[1] == ':')
  383.     strcat(sp_path,ISTR_SLASH);
  384. /*---------------------------------------------------------------------*/
  385. /* Change directory to the supplied path, if possible and store the    */
  386. /* expanded path.                                                      */
  387. /* If an error, restore the current path.                              */
  388. /*---------------------------------------------------------------------*/
  389.  if (chdir(sp_path) != 0)
  390.    {
  391.     if (new_dos_disk != current_dos_disk)
  392.       {
  393.        chdir(current_dir);
  394. #if defined(__EMX__)
  395.        _chdrive((char)((current_dos_disk-1)+'A'));
  396. #elif defined(DOS)
  397. # if defined(TC) || defined(GO32)
  398.        setdisk((short)(current_dos_disk-1));
  399. # else                    /* assume MSC                 */
  400.        _dos_setdrive(current_dos_disk,&temp_disk);
  401. # endif
  402. #elif defined OS2
  403.        DosSetDefaultDisk(new_dos_disk);
  404. #else                 /* assume WIN32              */
  405. # if defined(__WATCOMC__)
  406.        _dos_setdrive(current_dos_disk,&temp_disk);
  407. # else                    /* assume MSC                 */
  408.        _chdrive(current_dos_disk);
  409. # endif
  410. #endif
  411.       }
  412.     chdir(curr_path);
  413. #ifdef TRACE
  414.     trace_return();
  415. #endif
  416.     return(RC_FILE_NOT_FOUND);
  417.    }
  418. /*---------------------------------------------------------------------*/
  419. /* We are now in a valid directory, get the fully qualified directory  */
  420. /* name.                                                               */
  421. /*---------------------------------------------------------------------*/
  422. #if defined(__EMX__)
  423.  _getcwd2(sp_path,MAX_FILE_NAME);
  424. #else
  425.  getcwd(sp_path,MAX_FILE_NAME);
  426. #endif
  427. /*---------------------------------------------------------------------*/
  428. /* For DOS or OS/2, change back to the current directory of the now    */
  429. /* current disk and then change back to the original disk.             */
  430. /*---------------------------------------------------------------------*/
  431.  if (new_dos_disk != current_dos_disk)
  432.    {
  433.     if (chdir(current_dir) != 0)
  434.       {
  435. #ifdef TRACE
  436.        trace_return();
  437. #endif
  438.        return(RC_FILE_NOT_FOUND);
  439.       }
  440. #if defined(__EMX__)
  441.     _chdrive((char)((current_dos_disk-1)+'A'));
  442. #elif defined(DOS)
  443. # if defined(TC) || defined(GO32)
  444.     setdisk((short)(current_dos_disk-1));
  445. # else                    /* assume MSC                 */
  446.     _dos_setdrive(current_dos_disk,&temp_disk);
  447. # endif
  448. #elif defined(OS2)
  449.     DosSetDefaultDisk(current_dos_disk);
  450. #else                 /* assume WIN32              */
  451. # if defined(__WATCOMC__)
  452.     _dos_setdrive(current_dos_disk,&temp_disk);
  453. # else                    /* assume MSC                 */
  454.     _chdrive(current_dos_disk);
  455. # endif
  456. #endif
  457.    }
  458.  
  459.  chdir(curr_path);
  460. /*---------------------------------------------------------------------*/
  461. /* Append the OS directory character to the path if it doesn't already */
  462. /* end in the character.                                               */
  463. /*---------------------------------------------------------------------*/
  464. #ifndef VMS
  465.  len = strlen(sp_path);
  466.  if (len > 0)
  467.     if (sp_path[len-1] != ISLASH)
  468.        strcat(sp_path,(CHARTYPE *)ISTR_SLASH);
  469. #endif
  470. #ifdef TRACE
  471.  trace_return();
  472. #endif
  473.  return(RC_OK);
  474. }
  475. #else
  476. /***********************************************************************/
  477. #ifdef HAVE_PROTO
  478. short splitpath(CHARTYPE *filename)
  479. #else
  480. short splitpath(filename)
  481. CHARTYPE *filename;
  482. #endif
  483. /***********************************************************************/
  484. {
  485. /*-------------------------- external data ----------------------------*/
  486.  extern CHARTYPE curr_path[MAX_FILE_NAME+1];
  487.  extern CHARTYPE sp_fname[MAX_FILE_NAME+1];
  488.  extern CHARTYPE sp_path[MAX_FILE_NAME+1];
  489.  extern struct stat stat_buf;
  490. /*--------------------------- local data ------------------------------*/
  491.  short len=0;
  492.  CHARTYPE work_filename[MAX_FILE_NAME+1] ;
  493. /*--------------------------- processing ------------------------------*/
  494. #ifdef TRACE
  495.  trace_function("nonansi.c: splitpath");
  496. #endif
  497.  strcpy((DEFCHAR *)sp_path,"");
  498.  strcpy((DEFCHAR *)sp_fname,"");
  499.  strcpy((DEFCHAR *)work_filename,(DEFCHAR *)filename);
  500. /*---------------------------------------------------------------------*/
  501. /* If the supplied filename is empty, set the path = cwd and filename  */
  502. /* equal to blank.                                                     */
  503. /*---------------------------------------------------------------------*/
  504.  if (strcmp((DEFCHAR *)filename,"") == 0)
  505.    {
  506.     getcwd((DEFCHAR *)sp_path,MAX_FILE_NAME);
  507.     strcpy((DEFCHAR *)sp_fname,"");
  508.    }
  509. /*---------------------------------------------------------------------*/
  510. /* Check if the first character is tilde; translate HOME env variable  */
  511. /* if there is one. Obviously only applicable to UNIX.                 */
  512. /*---------------------------------------------------------------------*/
  513.  if (*(work_filename) == '~')
  514.    {
  515.     if (*(work_filename+1) == ISLASH)
  516.       {
  517.        strcpy((DEFCHAR *)work_filename,(DEFCHAR *)getenv("HOME"));
  518.        strcat((DEFCHAR *)work_filename,(DEFCHAR *)(filename+1));
  519.       }
  520.     else
  521.       {
  522.        struct passwd *pwd;
  523.  
  524.        strcpy((DEFCHAR *)sp_path,(DEFCHAR *)filename+1);
  525.        if ((len = strzeq(sp_path,ISLASH)) != (-1))
  526.           sp_path[len] = '\0';
  527.        if ((pwd = getpwnam((DEFCHAR *)sp_path)) == NULL)
  528.           return(RC_BAD_FILEID);
  529.        strcpy((DEFCHAR *)work_filename,pwd->pw_dir);
  530.        if (len != (-1))
  531.           strcat((DEFCHAR *)work_filename,(DEFCHAR *)(filename+1+len));
  532.       }
  533.    }
  534. /*---------------------------------------------------------------------*/
  535. /* First determine if the supplied filename is a directory.            */
  536. /*---------------------------------------------------------------------*/
  537.  if ((stat((DEFCHAR *)work_filename,&stat_buf) == 0)
  538.  &&  (stat_buf.st_mode & S_IFMT) == S_IFDIR)
  539.    {
  540.     strcpy((DEFCHAR *)sp_path,(DEFCHAR *)work_filename);
  541.     strcpy((DEFCHAR *)sp_fname,"");
  542.    }
  543.  else          /* here if the file doesn't exist or is not a directory */
  544.    {
  545.     len = strzreveq(work_filename,ISLASH);
  546.     switch(len)
  547.       {
  548.        case (-1):
  549.             getcwd((DEFCHAR *)sp_path,MAX_FILE_NAME);
  550.             strcpy((DEFCHAR *)sp_fname,(DEFCHAR *)work_filename);
  551.             break;
  552.        case 0:
  553.             strcpy((DEFCHAR *)sp_path,(DEFCHAR *)work_filename);
  554.             sp_path[1] = '\0';
  555.             strcpy((DEFCHAR *)sp_fname,(DEFCHAR *)work_filename+1+len);
  556.             break;
  557.       default:
  558.             strcpy((DEFCHAR *)sp_path,(DEFCHAR *)work_filename);
  559.             sp_path[len] = '\0';
  560.             strcpy((DEFCHAR *)sp_fname,(DEFCHAR *)work_filename+1+len);
  561.             break;
  562.      }
  563.    }
  564. /*---------------------------------------------------------------------*/
  565. /* Change directory to the supplied path, if possible and store the    */
  566. /* expanded path.                                                      */
  567. /* If an error, restore the current path.                              */
  568. /*---------------------------------------------------------------------*/
  569.  if (chdir((DEFCHAR *)sp_path) != 0)
  570.    {
  571.     chdir((DEFCHAR *)curr_path);
  572. #ifdef TRACE
  573.     trace_return();
  574. #endif
  575.     return(RC_FILE_NOT_FOUND);
  576.    }
  577.  getcwd((DEFCHAR *)sp_path,MAX_FILE_NAME);
  578.  chdir((DEFCHAR *)curr_path);
  579. /*---------------------------------------------------------------------*/
  580. /* Append the OS directory character to the path if it doesn't already */
  581. /* end in the character.                                               */
  582. /*---------------------------------------------------------------------*/
  583. #ifndef VMS
  584.  len = strlen((DEFCHAR *)sp_path);
  585.  if (len > 0)
  586.     if (sp_path[len-1] != ISLASH)
  587.        strcat((DEFCHAR *)sp_path,(DEFCHAR *)ISTR_SLASH);
  588. #endif
  589. #ifdef TRACE
  590.  trace_return();
  591. #endif
  592.  return(RC_OK);
  593. }
  594. #endif
  595.  
  596. #ifndef HAVE_RENAME
  597. /***********************************************************************/
  598. #ifdef HAVE_PROTO
  599. short rename(CHARTYPE *path1,CHARTYPE *path2)
  600. #else
  601. short rename(path1,path2)
  602. CHARTYPE *path1;
  603. CHARTYPE *path2;
  604. #endif
  605. /***********************************************************************/
  606. /* Function  : Emulate missing rename() function missing from SystemV  */
  607. /* Parameters: path1    - old filename                                 */
  608. /*             path2    - new_filename                                 */
  609. /* Return    : 0 on success, -1 on error                               */
  610. /***********************************************************************/
  611. {
  612. #ifdef TRACE
  613.  trace_function("nonansi.c: rename");
  614. #endif
  615.   if (link(path1,path2) != 0)
  616.     {
  617. #ifdef TRACE
  618.      trace_return();
  619. #endif
  620.      return(-1);
  621.     }
  622.   if (unlink(path1) != 0)
  623.     {
  624. #ifdef TRACE
  625.      trace_return();
  626. #endif
  627.      return(-1);
  628.     }
  629. #ifdef TRACE
  630.  trace_return();
  631. #endif
  632.  return(0);
  633. }
  634. #endif
  635.  
  636. #if defined(OS2) || (defined(__EMX__) && !defined(MSDOS))
  637. /***********************************************************************/
  638. /* Function  : Determine if file system allows the supplied filename.  */
  639. /* Parameters: PathFn - directory path and filename without ext.     */
  640. /* Note      : replaces function LongFileNames                   */
  641. /* Return    : 1 if file system allows this filename               */
  642. /***********************************************************************/
  643. bool IsPathAndFilenameValid(CHARTYPE *PathFn)
  644. {
  645.  CHARTYPE FullFn[CCHMAXPATH];
  646.  CHARTYPE *buf;
  647.  ULONG rc;
  648. #ifdef TRACE
  649.  trace_function("nonansi.c: IsPathAndFilenameValid");
  650. #endif
  651.  
  652. #ifdef __EMX__
  653.  if (_osmode == DOS_MODE)
  654.     return(0);
  655. #endif
  656.  if ((buf = malloc(strlen(PathFn) + 5)) == NULL)
  657.     return(0);                /* Fake an error             */
  658.  strcpy(buf,PathFn);
  659.  strcat(buf,".xxx");                    /* any extension                     */
  660. #if defined(__32BIT__) || defined(__386__)
  661.  rc = DosQueryPathInfo(buf,FIL_QUERYFULLNAME,FullFn,sizeof(FullFn));
  662. #else
  663.  rc = DosQPathInfo(buf,FIL_QUERYFULLNAME,FullFn,sizeof(FullFn),0ul);
  664. #endif
  665.  free(buf);
  666.  if (rc == 0)
  667.     return(1);
  668.  return(0);
  669. }
  670. #endif
  671.  
  672. #ifdef USE_OLD_LONGFILENAMES        /* FGC: previous ifdef OS2         */
  673. #  if defined(__32BIT__) || defined(__386__)
  674. #  define FSQBUFFERSIZE 64
  675. /***********************************************************************/
  676. bool LongFileNames(CHARTYPE *path)
  677. /***********************************************************************/
  678. /* Function  : Determine if file system allows long file names. (HPFS) */
  679. /*             This is the 32-bit version.                             */
  680. /* Parameters: path     - directory path                               */
  681. /* Return    : 1 if file system is HPFS                                */
  682. /***********************************************************************/
  683. {
  684. /*--------------------------- local data ------------------------------*/
  685.         ULONG nDrive=0L;
  686.         ULONG lMap=0L;
  687.         char buffer[FSQBUFFERSIZE];
  688.         FSQBUFFER2 *bData = (FSQBUFFER2 *) buffer;
  689.         char bName[3];
  690.         ULONG bDataLen=0L;
  691. /*--------------------------- processing ------------------------------*/
  692. #ifdef TRACE
  693.  trace_function("nonansi.c: LongFileNames");
  694. #endif
  695.  if ((strlen (path) > 0) && path [1] == ':')
  696.     bName[0] = path[0];
  697.  else
  698.    {
  699.     DosQueryCurrentDisk(&nDrive, &lMap);
  700.     bName[0] = (char) (nDrive + 'A' - 1);
  701.    }
  702.  bName[1] = ':';
  703.  bName[2] = 0;
  704.  bDataLen = FSQBUFFERSIZE;
  705.  DosQueryFSAttach(bName, 0, FSAIL_QUERYNAME, bData, &bDataLen);
  706.  return(strcmp(bData->szFSDName + bData->cbName, "HPFS") == 0);
  707. }
  708. #  else
  709.  
  710. /***********************************************************************/
  711. bool LongFileNames(CHARTYPE *path)
  712. /***********************************************************************/
  713. /* Function  : Determine if file system allows long filenames. (HPFS)  */
  714. /*             This is the 16-bit version.                             */
  715. /* Parameters: path     - directory path                               */
  716. /* Return    : 1 if file system is HPFS                                */
  717. /***********************************************************************/
  718. {
  719. typedef struct _FSNAME {
  720.         USHORT cbName;
  721.         UCHAR  szName[1];
  722. } FSNAME;
  723. typedef struct _FSQINFO {
  724.         USHORT iType;
  725.         FSNAME Name;
  726.         UCHAR  rgFSAData[59];
  727. } FSQINFO;
  728. typedef FSQINFO FAR *PFSQINFO;
  729. /*--------------------------- local data ------------------------------*/
  730.  USHORT nDrive=0,cbData=0;
  731.  ULONG lMap=0L;
  732.  FSQINFO bData;
  733.  BYTE bName[3];
  734.  FSNAME *pFSName=NULL;
  735. /*--------------------------- processing ------------------------------*/
  736. #ifdef TRACE
  737.  trace_function("nonansi.c: LongFileNames");
  738. #endif
  739.  if ((strlen(path) > 0) && path[1] == ':')
  740.     bName[0] = path[0];
  741.  else
  742.    {
  743.     DosQueryCurrentDisk(&nDrive, &lMap);
  744.     bName[0] = (char)(nDrive + '@');
  745.    }
  746.  bName[1] = ':';
  747.  bName[2] = 0;
  748.  cbData = sizeof(bData);
  749.  DosQFSAttach((PSZ)bName,0,1,(PBYTE)&bData,&cbData,0L);
  750.  pFSName = &bData.Name;
  751.  (CHARTYPE *)pFSName += pFSName->cbName + sizeof(pFSName->cbName)+1;
  752.  return(strcmp((CHARTYPE *)&(pFSName->szName[0]),"HPFS") == 0);
  753. }
  754. #  endif
  755. #endif
  756.  
  757. #ifdef PRE_111_GO32
  758. /***********************************************************************/
  759. #ifdef HAVE_PROTO
  760. short getdisk(void)
  761. #else
  762. short getdisk()
  763. #endif
  764. /***********************************************************************/
  765. /* Function  : Determine the drive letter for the current disk.        */
  766. /* Parameters: nil                                                     */
  767. /* Return    : 0 for drive A:, 1 for drive B:, etc...                  */
  768. /***********************************************************************/
  769. {
  770. /*--------------------------- local data ------------------------------*/
  771. /*--------------------------- processing ------------------------------*/
  772.  regs.h.ah = 0x19;
  773.  int86(0x21, ®s, ®s);
  774.  return ((short) regs.h.al);
  775. }
  776. /***********************************************************************/
  777. #ifdef HAVE_PROTO
  778. short setdisk(short drive)
  779. #else
  780. short setdisk(drive)
  781. short drive;
  782. #endif
  783. /***********************************************************************/
  784. /* Function  : Change the current drive to that specified.             */
  785. /* Parameters: 0 for drive A:, 2 for drive B:, etc...                  */
  786. /* Return    : number of drives present                                */
  787. /***********************************************************************/
  788. {
  789. /*--------------------------- local data ------------------------------*/
  790. /*--------------------------- processing ------------------------------*/
  791.  regs.h.ah = 0x0e;
  792.  regs.h.dl = (char)drive;
  793.  int86(0x21, ®s, ®s);
  794.  return ((short) regs.h.al);
  795. }
  796. #endif
  797.