Linux equivalent

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vermarajeev
    New Member
    • Aug 2006
    • 180

    Linux equivalent

    Hi guys,
    What are the changes to be made to make this code run on linux.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    {
      FILE *inFile, *outFile;
      char * buffer;
      size_t inResult;
      size_t outResult;
    
      char tempFilePath[L_tmpnam_s];
      errno_t err;
    
      tmpfile_s(&outFile);
    	
      if( outFile )
      {	  
    	  err = tmpnam_s( tempFilePath, L_tmpnam_s );
          if (err)
          {
             printf("Error occurred creating unique filename.\n");
             exit(1);
          }
          else
          {
             printf( "%s is safe to use as a temporary file.\n", tempFilePath );
          }
    	
    
    	  fopen_s(&inFile, "test.txt", "rb");
    	  if( !inFile )
    		{
            printf("Error Opening inFile\n");
    		return 0;
    		}
    
    	  // obtain file size:
    	  fseek (inFile , 0 , SEEK_END);
    	  long lSize = ftell (inFile);
    	  rewind (inFile);
    	  buffer = (char*) malloc (sizeof(char)*lSize);
    	  if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
    
    	  inResult = fread (buffer, 1, lSize, inFile);
    	  if (inResult != lSize)
    	  {
    		  fputs ("Reading error",stderr); 
    		  exit (3);
    	  }	
    		
    	  outResult = fwrite( buffer, 1, inResult, outFile );
    	  if (outResult != inResult)
    	  {
    		  fputs ("Reading error",stderr); 
    		  exit (3);
    	  }	
    
      }
      free(buffer);
      fclose (inFile);
      fclose (outFile);	
      return 0;
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    so long as you use standard libraries (which you do) and are careful about operating system specific filename naming conventions you should be OK.
    However, the code as posted won't compile, e.g. L_tmpnam_s, errno_t and lSize' are undeclared identifiers.

    Comment

    • vermarajeev
      New Member
      • Aug 2006
      • 180

      #3
      Originally posted by horace1
      so long as you use standard libraries (which you do) and are careful about operating system specific filename naming conventions you should be OK.
      However, the code as posted won't compile, e.g. L_tmpnam_s, errno_t and lSize' are undeclared identifiers.
      I know about that error and tried on my system too and get the same error. I think I was not clear. The above code runs well on windows system but on linux it gives me errors. But I somehow fixed the problem.

      Now my another question is
      I have two files 'file1.txt' file2.enc'.

      'file1.txt' contains plain data and 'file2.enc' contains encoded data.

      Presently I'm deleting 'file1.txt' and retaining back 'file2.enc'.

      What I want is, use 'file1.txt' to both encode and decode?

      I have a solution for this
      Solution-->
      Let two files 'file1.txt' and 'file2.enc' be created. Delete 'file.txt' and then rename 'file2.enc' to 'file1.txt.'
      But the question is since I'm deleting and renaming the files. Will it not make my application too slow????? I think renaming and deleting the files is at operating system level, so it might result in performace problems.

      Can you provide me a better idea to do the same. My code should be portable on both windows and linux.

      Thanks in advance

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        I have not found using remove() and rename() causing any significant delay

        Comment

        • DeMan
          Top Contributor
          • Nov 2006
          • 1799

          #5
          it won't make the process too slow but (and I assume this is kinda where your questions leading).....
          if you encode file1.txt in file2.enc.....
          then delete file1.txt (which is a good idea, why keep the plaintext, we encrypted for a reason).....
          then rename file2.enc to be file1.txt....
          then your encoded file will be called file1.txt.....
          but I think you are more concerned with OVERWRITING the data that previously was file1.txt???
          renaming the encoded file to the plaintext file does not do this - is this what you want to do?

          Comment

          • vermarajeev
            New Member
            • Aug 2006
            • 180

            #6
            Originally posted by DeMan
            it won't make the process too slow but (and I assume this is kinda where your questions leading).....
            if you encode file1.txt in file2.enc.....
            then delete file1.txt (which is a good idea, why keep the plaintext, we encrypted for a reason).....
            then rename file2.enc to be file1.txt....
            then your encoded file will be called file1.txt.....
            but I think you are more concerned with OVERWRITING the data that previously was file1.txt???
            renaming the encoded file to the plaintext file does not do this - is this what you want to do?
            Yes my encoded file will be called file1.txt. No I'm not all bothered about overwriting the data.

            The user gives me a plain text file. I encode the data then the same file should have the encoded data. That is my target.

            One more doubt-->
            When I delete a file using ::remove( fileName ) Will the file get deleted from hard drive or will it be on memory??? Is there any way for the hacker to get the deleted file??? If yes what should I do to not allow the hacker to hack my file...

            Waiting eagerly....

            Comment

            • DeMan
              Top Contributor
              • Nov 2006
              • 1799

              #7
              Hi Again,

              Overwriting the information is one way to make it a little harder for hackers BUT
              These days, people have devised mehtods to find even overwritten data. Some companies (depending on the sensitivity of their data) overwrite eleven (some probably more, but I don't know) times with random bitstrings....( the less pattern there is, the more difficult it is for someone to tell at which layer that bit was a given value).

              Depending on what you are using the information for (and more importantly where), it may be sufficient to simply rwemove the file as you do....
              If you are encrypting military secrets you would want to overwrite no matter what. If you are encrypting account information for a bank (especially where several people have access to the computer/network you ar e using) you probably want to overwrite. If you are encrypting a telephone/Addressbook on a home computer that is only used by your family, I wouldn't bother overwriting (unless you have hackers in the family).....

              Hope I haven't confused you further

              Comment

              Working...