www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/06/04/09:52:33

Xref: news2.mv.net comp.os.msdos.djgpp:4555
From: carl DOT johansson AT helsinki DOT fi (Carl Johansson)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: File sharing?
Date: 3 Jun 1996 19:49:17 GMT
Organization: University of Helsinki
Lines: 159
Message-ID: <4ovfjt$egp@oravannahka.Helsinki.FI>
References: <4osc1v$9ec AT oravannahka DOT Helsinki DOT FI>
NNTP-Posting-Host: hanu2.pc.helsinki.fi
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <4osc1v$9ec AT oravannahka DOT Helsinki DOT FI>, carl DOT johansson AT helsinki DOT fiä 
says...
>
>How come that DJGPP v2 appears to ignore 
>file sharing completely? It so happens that I
>actually need this, and I'm trying to get my
>other compiler (VC++ 1.5) to look the other
>way. I use only the _dos_ calls in both, at 
>least the VC++ documentation says it uses the
>system calls directly. The code is as good as
>identical for both.
>
>

Mr. Zaretskii asked me to post sample code, so
I will do that. I am trying to make a small DOS 
program which will make the system files of Windows 95
read-only while it's running, in order to have some
rudimentary protection against rogue installation 
programs (the reason for this is that installation of 
Paint Shop Pro 32 eventually forced me to uninstall
the OS, now I had no problems. This is not the full version,
and another version compares dates and only protects newer
files.). 

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys\stat.h>  
#include <sys\types.h>
#include <io.h>
#include <dos.h>
#include <errno.h>  
#include <dir.h>

int unset_file( const char *);

int set_file( const char *);

int walk_dir( const char *, int (*func) ( const char *) );


int main( int argc, char *argv[] )
{
  char *sys_path, *win_path;

  win_path = getenv("windir");
  if ( win_path == NULL )
  {
    fprintf( stderr, "Failed to find the Windows directory\n");
    return -1;
  }

  sys_path = malloc( strlen(win_path)+8 );
  if ( sys_path == NULL )
  {
    fprintf( stderr, "Out of memory\n");
    return -1;
  }
  sys_path[0] = '\0';
  sys_path = strcpy( sys_path, win_path);
  sys_path = strcat( sys_path, "\\system");

  if ( _dos_setfileattr( sys_path, _A_RDONLY ) != 0)
    fprintf( stderr, "Error setting %s, error: %s\n", sys_path, 
strerror(errno));

  walk_dir( sys_path, set_file);
  
  printf("Hit Enter when installation is complete\n");
  getchar(); 

  if ( _dos_setfileattr( sys_path, _A_ARCH ) != 0)
    fprintf( stderr, "Error setting %s, error: %s\n", sys_path, 
strerror(errno));

  walk_dir( sys_path, unset_file);

  return 0;
}  


int unset_file( const char *path )
{
  if ( _dos_setfileattr( path, _A_ARCH ) != 0)
    fprintf( stderr, "Warning! Failed to unset %s, error: %s\n", path, 
strerror(errno));
  return 0;
}


int set_file( const char *path )
{  
  if ( _dos_setfileattr( path, _A_RDONLY ) != 0)
    fprintf( stderr, "Error setting %s, error: %s\n", path, strerror(errno));
  return 0;
}

int walk_dir( const char *path, int (*func) ( const char *) )
{
  struct _find_t ff;

  chdir( path );

  if (_dos_findfirst( "*.*", _A_ARCH|_A_HIDDEN|_A_SYSTEM|_A_RDONLY, &ff) == 0)
  {
    func( ff.name );
    while (_dos_findnext( &ff ) == 0)
      func( ff.name );
  }

  if (_dos_findfirst( "*", _A_SUBDIR, &ff) == 0) 
  {
    if (ff.name[0] != '.')
      walk_dir( ff.name, func);
    while (_dos_findnext( &ff ) == 0 && ff.name[0] != '.')
      walk_dir( ff.name, func );
  }

  chdir("..");

  return 0;
}     

Many header files are redundant, but needed by VC++. 
The recursion doesn't work here for some reason either, 
but that's not the point.

With VC++ I get the following error message:

Sharing violation reading drive D
Abort, Retry, Fail?
Error setting somefile.ext, error: 

for each file Windows uses (ex. KEYBOARD:DRV, USER:EXE etc.) 
at the time the program attempts to access it (note that
strerror fails to return anything, not even "No error").  
With DJGPP I get nothing, it works perfectly from my point of 
view here. 

Now I'm not sure whether this is a bug in VC++ or DJGPP, as I know 
practically nothing about file sharing in Windows. But since VC is 
a (16-bit) Windows compiler I can embed icons into the executables
which makes them easier to distinguish, and that's why I would like
to port the code to it. But first I need to know whether this is at
all possible, whether DJGPP is overlooking something here or whether
VC++ is being too picky. I will try to post in 
comp.os.ms-windows.programmer.misc if no one knows the answer here.


cj


..




- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019