-2

Can someone point me to the right direction on how to append multiple files to an archive file in C?

Below is what I'm planning to do. Can you please let me know if it's the right way (if not, can you let me know which is a better way)? Thanks a lot!

-Use OPEN to open the archive file (O_RDONLY | O_WRONLY | O_APPEND) -Use WRITE to write the regular file to archive

user2203774
  • 609
  • 4
  • 13
  • 25

2 Answers2

1

You should hopefully have a system header file named ar.h (normally located in /usr/include/), if you check that you will see the format of the archive files.

Each file in the archive is a ar_hdr structure followed by the data of the file contents. So to append a file to the archive, you just write a correct structure followed by the contents of the file you want to append.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks for the tip! I am really struggling with my homework assignment. Would you mind to tell me what you mean by "write a correct structure followed by the contents of the file you want to append"? – user2203774 Jul 19 '13 at 18:44
  • Yeah, that's only almost right. While I haven't studied the file format on Linux (presumably the questioner's OS), on Solaris if the filename is more than a certain length, it gets stored in the symbol table instead of the file header entry. – This isn't my real name Jul 19 '13 at 18:50
0

In order to add files (or even one file) to an archive file, you first need to know the format of the archive. If you've studied the documentation for the file format as provided by your OS, (as was advised in this answer to your previous question,) then you should already know enough about the file format that you shouldn't need to ask us how to do it.

C does not have functions named OPEN or WRITE. there are open() and write(), however, be advised that O_RDONLY|O_WRONLY is not equivalent to O_RDWR. You could use write() to write the content of the file to the end of the archive, yes, but (a) don't forget to write the file header first, and (b) are you sure you don't need to update the archive's symbol table? (If you do, you may need to rewrite the entire file.)

Why aren't you just using the ar command-line utility, again?

Community
  • 1
  • 1
This isn't my real name
  • 4,869
  • 3
  • 17
  • 30
  • @ Elchonon Edelson: Thank u for answering my questions. For my school assignment, I need to write some code in C that works just like the ar commands (e.g., ar -t, and ar -q). Sadly, none of my class lecture or class notes talk about ar.h and I got really confused. I spent a lot of time looking for an answer. I looked at the ar.h file on my laptop. With the limited experience in programming (3 quarters in school), I really have a difficult time understanding how to work with archive files. If you know any tutorial on working with archive files and ar.h, please let me know. Thank you. – user2203774 Jul 19 '13 at 19:15
  • Working with archive files is basically the same as working with any file that has a defined structure. You need documentation explaining the structure of the file content, and you write code to conform to that structure. In this case the file structure is proprietary, and I was not able to find documentation for the file structure used by the GNU library. The documentation for this file as used by Oracle Solaris 10 is [here](http://docs.oracle.com/cd/E26505_01/html/816-5173/ar-3head.html), the GNU library may be different. – This isn't my real name Jul 19 '13 at 19:37
  • The only way to really get into the file structure is to use the `ar` command to create a bunch of different archives, then based on the documentation, write code to open the files and list the contents, and check to see if you got it right. (Then, write code to update or modify the file based on what you know about the file structure, and compare the results with what you get when you do the same thing with `ar`...) – This isn't my real name Jul 19 '13 at 19:48
  • The POSIX documentation for the `ar` command ([here](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ar.html)) also provides an example of the format of an archive file, a quick read-through shows several significant differences from what Solaris does, nicely illustrating my point about the format not being standardized. – This isn't my real name Jul 19 '13 at 19:52
  • Thanks Elchonon! I'm supposed to write something that does the same thing as "ar -q" in C - To quickly append a file to the archive. That's the main thing I want to do. How can I use an ar_adr struct in this case? I think I should use fopen to open the archive file..set "a+" as the mode..then use fwrite to add the text file that I want to append to the archive file. Should I put this text file in the ar-adr struct? Sorry I know I'm not making much sense here and my questions sound dumb. I'm doing my best to learn. Thank you for your patience.. – user2203774 Jul 19 '13 at 19:57
  • Sorry I hope you don't mind that I keep bothering you. I spent a lot of time on my homework assignment, but I feel stuck. For this assignment, we need to write some functions in C which do the same thing as the ar commands (e.g., -q, -x, -d). By any chance, do you mind to tell me what would be the best approach to tackle this homework? I feel like I'm missing something and that's why I'm stuck. – user2203774 Jul 19 '13 at 20:02
  • The `-q` command-line argument simply turns off duplicate-checking and the "insert this into the middle" options. You still have to know how to add a file, which you have already been told, and how to handle the case of the filename being more than 16 bytes long, which may vary depending on the file format. Please re-read the answers already provided, and you should have everything you need. After thorough study of the documentation, the only thing left is implementation, testing, re-implementation, re-testing, and the fix-a-bug/test/fix-a-bug/test loop. – This isn't my real name Jul 19 '13 at 20:06