0

I need to put a stamp on a file consisting of the creation or modification (both are fine) date and the md5 hash. In order to get these parameters in fortran (fortran 2003 for that matter) for a file data I was using

  ! -- execute shell commands 
  call execute_command_line("md5 -q "//trim(data)//">"//trim(data)//".md5")
  call execute_command_line("date -r "//trim(data)//">>"//trim(data)//".md5")

  ! -- read extracted info from a tmp file
  open(10,file=trim(data)//".md5")
  read(10,"(A)") data_file_md5
  read(10,"(A)") data_file_mtime  
  close(10)

  ! -- clean up
  call execute_command_line("rm -rf "//trim(data)//".md5")

However, recently I learnt that this is not a portable approach. In fact, it is not even portable under Linux or macOS, mainly because date -r is not available everywhere, but I also have concerns about execute_command_line and md5 -q.

Therefore, my question is how to make the determination of the creation date and md5 hash more reliable.

yarchik
  • 336
  • 1
  • 8
  • Is your concern with `execute_command_line` that it doesn't exist in Fortran 2003? – francescalus May 06 '21 at 10:46
  • You can use an external library to compute the md5 sum using C as [in this answer](https://stackoverflow.com/a/3395709/4433869) and then you can interface it with Fortran. For the creation date I suppose there are some libraries for C too that you can interface with Fortran – Emilio May 06 '21 at 11:26
  • @Emilio I am not sure it will increase portability. Together with the fortran code, I need to deliver also the C libraries you mentioned. What if they get broken, I cannot support 3rd party libraries and I do not personally know their developers. Relying on the shell commands seems to be more universal, but not quite... – yarchik May 06 '21 at 11:33
  • @yarchik There's certainly no native Fortran way of doing what you want, so you're going to have to interface with something. – veryreverie May 06 '21 at 12:26
  • execute_command_line is part of standard Fortran 2003. As for the rest with what you want it looks like you'll have to implement them yourself - MD5 doesn't look too hard, and the file creation time should be a standard unix system call, which should be fine if unix like systems is all you are interested in. – Ian Bush May 06 '21 at 12:57
  • Don't know off the top of my head, but have a meeting now. Google should find it pretty quickly though. Possible a tiny C wrapper and you should be sorted. – Ian Bush May 06 '21 at 13:00
  • @yarchik stat or fstat look like ways to go - https://man7.org/linux/man-pages/man2/stat.2.html – Ian Bush May 06 '21 at 14:21
  • @IanBush Is `stat` more common than `date`? – yarchik May 06 '21 at 14:47
  • 1
    I am talking about the system call stat rather than the command line version. This is part of Posix so I would hope it is pretty common across unices - but this is not my area really at all. – Ian Bush May 06 '21 at 15:01
  • [Here](http://www.zedwood.com/article/cpp-md5-function) you can find a C++ example for calculating md5 sums. You can copy that source in your code so that you don't have to depend on an external library. I haven't tested it but I think this source does not depend on external libraries. If this example is good for you then you just have to create a Fortran interface. – Emilio May 07 '21 at 11:01

0 Answers0