home *** CD-ROM | disk | FTP | other *** search
- #include "jam.h"
- #include "def.h"
-
- /* Stuff split out of file.c and fileio.c; makes DOS/NT calls
- * which depend (kinda) on FAT as the filesytem type.
- */
- #include <sys/types.h>
- #include <sys/stat.h>
-
- #include "stdio.h"
- #include "direct.h"
- #include "dos.h"
- #include "io.h"
-
- static BOOL details_in_dir = FALSE;
-
- /* Get directory listing from partial spec; *.* added to
- * end, but no checking done otherwise
- */
- void GetDiskDirectory(BUFFER *bp, char *path)
- {
- #define DOSSPEC (8+3+1+1+1) /* "12345678.123 \0" */
- #define DOSLEN (DOSSPEC + 3)
-
- char dosspec[NFILEN + 1];
- #ifdef WIN32
- WIN32_FIND_DATA fileinfo;
- HANDLE context;
- #else
- struct find_t fileinfo;
- #endif
- int numfiles = 0;
- char *wild = "*.*";
- char *unknown = "-";
- char buffer[2*NCOL], newpath[NFILEN];
- register int i, j, k;
- struct stat statbuf;
- BOOL found = FALSE;
- BOOL goodname;
-
- #ifdef WINDOWED
- WindowSleepCursor();
- #endif
-
- /* need wildcard stuff, don't add if supplied
- */
- strcpy(newpath, path);
- k = (int)strlen(newpath);
- for (i = 0; i < k; i++)
- {
- if (newpath[i] == '*')
- {
- found = TRUE;
- break;
- }
- }
- if (!found)
- strcat(newpath, wild);
- if (newpath[k-1] == '*') /* ends with *, is it complete? */
- if (newpath[k-2] != '.')
- strcat(newpath, wild); /* will have **.* after this, but ok */
-
-
- /* Set up names per row - NOTE NEED CHECK FOR
- * NONE-FAT FILESYSTEM NAME LENGTH here else UGHLY!
- */
- if (details_in_dir)
- k = 1;
- else
- k = ncol/(DOSLEN-1);
-
- /* Find the first file which matches the constructed path
- */
- #ifndef WIN32
- if (_dos_findfirst(newpath, _A_NORMAL, &fileinfo) == 0)
- #else
- if (context = FindFirstFile(newpath, &fileinfo))
- #endif
- {
- i = 0;
- buffer[0] = '\0';
- for (; ;)
- {
- numfiles++;
-
- /* do K names per row
- */
- memset(dosspec, '\0', NFILEN + 1); /* zero pad large buffer */
- memset(dosspec, ' ', DOSSPEC+2);
- dosspec[DOSLEN-1] = '\0';
- #ifndef WIN32
- for (j = 0; fileinfo.name[j]; j++)
- dosspec[j+1] = fileinfo.name[j];
- #else
- for (j = 0; fileinfo.cFileName[j]; )
- {
- dosspec[j+1] = fileinfo.cFileName[j];
- j++;
- if ((j == NFILEN) /* buffer full, leave trailing null */
- break;
- }
- #endif
- /* step over these dir (not useful) names (HACK)
- */
- if (dosspec[1] == '.')
- {
- if ((dosspec[2] == '.') || (dosspec[2] == ' '))
- goodname = FALSE;;
- }
- else
- goodname = TRUE;
-
- if (goodname)
- {
- adjustnamecase(dosspec);
- strcat(buffer, dosspec);
-
- /* read/write/exec info in buffer
- */
- if (details_in_dir)
- {
- char temp[NFILEN];
-
- strcpy(temp, path);
- #ifndef WIN32
- strcat(temp, fileinfo.name);
- #else
- strcat(temp, fileinfo.cFileName);
- #endif
- /* use stat - get r/w/x state
- */
- if (!stat(temp, &statbuf))
- {
- if (statbuf.st_mode & S_IREAD)
- strcat(buffer, "r");
- else
- strcat(buffer, unknown);
- if (statbuf.st_mode & S_IWRITE)
- strcat(buffer, "w");
- else
- strcat(buffer, unknown);
- if (statbuf.st_mode & S_IEXEC)
- strcat(buffer, "x");
- else
- strcat(buffer, unknown);
- }
- else
- strcat(buffer, "???"); /* read/write/exec ? */
- }
- i++;
-
- if (i >= k)
- {
- register int n = strlen(buffer) - 1;
-
- while (buffer[n] == ' ')
- n--;
- if (n >= 0)
- buffer[n+1] = 0; /* kill extra blanks at lines end */
-
- addline(bp, buffer);
- buffer[0] = '\0';
- i = 0;
- }
- } /* goodname */
-
- /* Find next file which matches path, else
- * terminate and break loop
- */
- #ifndef WIN32
- if (_dos_findnext(&fileinfo))
- #else
- if (!FindNextFile(context, &fileinfo))
- #endif
- {
- if (i)
- addline(bp, buffer); /* flush last line */
- break;
- }
- }
- }
-
- ewprintf("%d files found", numfiles);
- #ifdef WINDOWED
- WindowNormalCursor();
- #endif
- }
-
- /* Both were in fileio.c but I moved them here
- * because they make dos calls (JAM).
- *
- * NOTE the hack for returning disk drive on NT.
- * 'wdir' is current path and so better have
- * a drive letter in first char! If not, this code
- * will break.
- *
- * DOS has notion of current directory per drive...
- */
- unsigned short getdisk()
- {
- #ifdef WIN32
- if (wdir[0])
- return ((unsigned short)(wdir[0] - 'a'));
- #else
- unsigned int currentdrive;
-
- _dos_getdrive(¤tdrive);
- return currentdrive-1;
- #endif
- }
-
- /* DOS has notion of current directory per drive...
- */
- void setdisk(char c)
- {
- #ifndef WIN32 /* NOOP for NT */
- unsigned number_of_drives;
-
- _dos_setdrive((unsigned short)(c - 'a' + 1), &number_of_drives);
- #endif
- }
-
- /* Return into buffer of the requested drive
- * in the string; note it does a hacky thing in DOS
- * to set that drive, then fetch the cwd and then resets
- * the drive.
- *
- * DOS has notion of current directory per drive...
- */
- void getcurdir(unsigned short drivenumber, char *buf)
- {
- #ifdef WIN32
- char *temp;
- temp = getwd(wdir);
- strcpy(&buf[3], &temp[3]);
- #else
- unsigned int currentdrive = (int)(getdisk()+1);
- unsigned int number_of_drives;
- char bufr[NFILEN];
-
- _dos_setdrive(drivenumber, &number_of_drives);
- getcwd(&bufr[0], NFILEN-1);
- _dos_setdrive(currentdrive, &number_of_drives);
- strcpy(buf, &bufr[3]);
- #endif
- }
-
- /* Should always use stat? No, DOS is weird
- * and stat fails sometimes. This works best
- * and there is a NT call for it also.
- */
- BOOL fileisrdonly(char *s)
- {
- BOOL result = FALSE;
- #ifndef WIN32
- unsigned int attr = 0;
-
- if (s && *s)
- _dos_getfileattr(s, &attr);
- if (attr & _A_RDONLY)
- result = TRUE;
- #else
- DWORD foo;
-
- if ((foo = GetFileAttributes(s)) >= 0)
- result = (foo & FILE_ATTRIBUTE_READONLY ? TRUE: FALSE);
-
- #endif
- return(result);
- }
-
- /* Dummie routines I was playing with and are not used at
- * all anymore. I should delete them...
- */
- #if 0
- dosdir(f, n)
- int f, n;
- {
- extern BOOL details_in_dir;
- char dirn[NFILEN];
- int s;
- BUFFER *bp;
- extern BUFFER *dired_(char *s);
-
- epreload(dirpath());
- if ((s=eread("DOS directory: ", dirn, NFILEN, EFNEW|EFBUF) == ABORT))
- return FALSE;
- strcat(dirn, "\\");
- details_in_dir = TRUE;
- if (bp = dired_(dirn))
- {
- bp->b_flag |= BFVIEW;
- curbp = bp;
- bp->b_dotp = lforw(bp->b_linep); /* go to first line */
- }
- details_in_dir = FALSE;
- return(bp ? showbuffer(bp, curwp, WFHARD | WFMODE) : FALSE);
- }
- dosset(f, n)
- int f, n;
- {
- char bufn[NFILEN];
- int s;
-
- if ((s=eread("File to change read/write access: ", bufn,
- NFILEN, EFNEW|EFBUF) == ABORT))
- return FALSE;
- if ((s=eyesno("Set readonly")) != TRUE)
- if (s == ABORT)
- return FALSE;
- if (s)
- _dos_setfileattr(bufn, _A_RDONLY);
- else
- _dos_setfileattr(bufn, _A_NORMAL);
-
- return TRUE;
- }
- dosget(f, n)
- int f, n;
- {
- char bufn[NFILEN];
- int s;
- unsigned int attr = (unsigned int)(~_A_NORMAL);
-
- s=eread("Try to open for write: ", bufn, NFILEN, EFNEW|EFBUF);
- _dos_getfileattr(bufn, &attr);
-
- if (attr & _A_RDONLY)
- ewprintf("file is readonly");
- else if (attr & _A_HIDDEN)
- ewprintf("file is readonly");
- else if (attr == _A_NORMAL)
- ewprintf("file is normal");
- else if (attr & _A_SYSTEM)
- ewprintf("file is system");
- else if (attr & _A_VOLID)
- ewprintf("file is vol id!?");
- else if (attr & _A_SUBDIR)
- ewprintf("file is subdir");
- else if (attr & _A_ARCH)
- ewprintf("file is archive");
-
- return TRUE;
- }
- #endif
-
-