/* vi:set cindent tabstop=2 shiftwidth=2: */ /* * libvxfs - library for reading Veritas Journaled FileSystem (VxFS) * Copyright (c) 1999 Martin Hinner * * cwd.c: Current working directory routines * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id$ */ #include #include #include #include #include #define STATEFNAME ".vxcwd" int vxsavecwd (char *device, int subpart, char *dir) { FILE *f; char *path; char *home; home = getenv ("HOME"); if (home == NULL) home = ""; path = malloc (strlen (home) + 1 + sizeof (STATEFNAME)); if (path == 0) return -1; strcpy (path, home); strcat (path, "/" STATEFNAME); f = fopen (path, "w"); free (path); if (!f) { if (vxverbose) perror ("vxsavecwd: Can't open state file"); return -1; } fprintf (f, "%s:%u %s\n", device, subpart, dir); fclose (f); return 0; } int vxreadcwd (char *device, int *subpart, char *dir) { FILE *f; char *path; char *home; char buf[0x100]; char *_dir, *_subp; home = getenv ("HOME"); if (home == NULL) home = ""; path = malloc (strlen (home) + 1 + sizeof (STATEFNAME)); if (path == 0) return -1; strcpy (path, home); strcat (path, "/" STATEFNAME); f = fopen (path, "r"); free (path); if (!f) { if (vxverbose) perror ("vxsavecwd: Can't open state file (no filesystem mounted?)"); return -1; } fgets (buf, sizeof (buf), f); fclose (f); if (buf[strlen (buf) - 1] == '\n') buf[strlen (buf) - 1] = 0; _dir = strchr (buf, ' '); if (!_dir) return -1; *_dir++ = 0; _subp = strchr (buf, ':'); if (_subp) { *_subp++ = 0; *subpart = atoi (_subp); } else *subpart = 0; strcpy (device, buf); strcpy (dir, _dir); return 0; } int vxdeletecwd () { char *path; char *home; home = getenv ("HOME"); if (home == NULL) home = ""; path = malloc (strlen (home) + 1 + sizeof (STATEFNAME)); if (path == 0) return -1; strcpy (path, home); strcat (path, "/" STATEFNAME); if (unlink (path) < 0) { free (path); return -1; } free (path); return 0; }