Deleting a line from a file
Saket Sinha
saket.sinha89 at gmail.com
Wed May 14 11:57:06 EDT 2014
Please find my response inline-
On Wed, May 14, 2014 at 8:04 PM, Bernd Petrovitsch
<bernd at petrovitsch.priv.at> wrote:
> Hi!
>
> The original mail is off-topic as it has nothing to do wotj the Linux
> kernel development as such, but:
>
I wanted to isolate the problem hence did not give the full context.
Actually I have written a file-system utility which mounts my
filesystem on a specific path and update it in /etc/fstab. When I
umount it, I delete the path from the /etc/fstab.
I had earlier written a C function to do that but it is corrupting the fstab.
SO now I am trying to run the above sed command this way
system(sed -i 's#^/opt/new1.*$##g' /etc/fstab).
The C function that was currupting the filesystem is listed below -
int removeEntryFromFSTAB(const char * fullPath, const char * fileName)
{
FILE *tabFileOld = NULL;
FILE *tabFileNew = NULL;
struct mntent *m;
char newFileName[PATH_MAX];
int rc = -1;
tabFileOld = setmntent( fileName, "r" ); // Open for writing now
if (tabFileOld == NULL )
goto end;
tabFileNew = setmntent(newFileName, "w");
if (tabFileNew == NULL )
goto end;
while ((m = getmntent(tabFileOld)) != NULL)
{
if (tcscmp(MY_FS_TYPE, m->mnt_type) == 0)
{
if ((tcscmp(fullPath, m->mnt_dir) == 0))
continue;
}
if (addmntent(tabFileNew, m) != 0)
goto end;
}
endmntent(tabFileOld);
endmntent(tabFileNew);
tabFileNew = NULL;
tabFileOld = NULL;
rename(newFileName, fileName))
rc = 0;
end:
if (tabFileNew != NULL)
endmntent(tabFileNew);
if (tabFileOld != NULL)
endmntent(tabFileOld);
sync();
return rc;
}
Kindly let me know, if you think there is a better way.
Regards,
Saket Sinha
More information about the Kernelnewbies
mailing list