1
#include <fstab.h>

struct fstab *getfsent(void);

http://man7.org/linux/man-pages/man3/getfsent.3.html

getfsent reads a line from /etc/fstab file and return a variable of type struct fstab*. Do I need to free it? Or it's managed by someone else? If it's managed by someone else, why isn't the return type const struct fstab*? I check the reference above but couldn't find anything useful.

  • 1
    It's probably implementation-dependent, but the original is static – Thomas Dickey Aug 13 '18 at 9:28
2

At least for glibc, you shouldn't. The source indicates that the pointer is to a member of an internal state struct, so it's not something you can directly free.

The docs also hint at this:

To read the entire content of the of the fstab file the GNU C Library contains a set of three functions which are designed in the usual way.

The "usual" way here being something like getpwent:

The return value may point to a static area, and may be overwritten by subsequent calls to getpwent(), getpwnam(3), or getpwuid(3). (Do not pass the returned pointer to free(3).)

Also, the glibc docs specifically for getfsent:

The function returns a pointer to a variable of type struct fstab. This variable is shared by all threads and therefore this function is not thread-safe. If an error occurred getfsent returns a NULL pointer.

That that variable is shared is a strong indication you should not mess with memory management.

If you want to free the resources, use endfsent(), which will clear the internal state.

  • 1
    Thanks. I wasn't aware glibc has such comprehensive documentation. It rocks! – JohnKoch Aug 13 '18 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.