Resolved Parsing /etc/hosts

Discussion in 'Mac Programming' started by raison, Jan 14, 2016.

  1. raison macrumors member


    Nov 13, 2009
    I want to parse /etc/hosts and manipulate it by adding/removing hosts. I'm using Swift to build an OSX application, but before I build a parser for /etc/hosts, I want to make sure there's nothing out there already available so I don't have to reinvent the wheels. From my researches, I found:

    1. mDNSResponder seems to parse /etc/hosts according to the hosts manpage, but I couldn't find an exposed API to manipulate the hosts file;

    2. gethostent seems to be able to do that, but not sure how to use standard C code on Swift and link libc to the project.

    Any help is appreciated, thanks! :)
  2. ArtOfWarfare macrumors G3


    Nov 26, 2007
    Use the best tool for the job. If other languages can do this out of the box, what makes you think Swift is the best?

    Use something that can talk with C more effortlessly. Like Obj-C.
  3. mrichmon macrumors 6502a

    Jun 17, 2003
  4. raison thread starter macrumors member


    Nov 13, 2009
    Thanks for all the replies, I ended up building my own parser using enumerateSubstringsInRange, it worked like a charm according to my unit tests. Fingers crossed :)

Share This Page