lintian doesn't like orphan packages with uploaders...
[debian/amanda] / client-src / getfsent.c
index 0c18fd5dda2b9ef9075ce8cfae710ca59116763d..2c8a7e185a8cb1f777595d00e69891a036046cbf 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
  * Copyright (c) 1991-1998, 2001 University of Maryland at College Park
+ * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
  * All Rights Reserved.
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * file named AUTHORS, in the root directory of this distribution.
  */
 /*
- * $Id: getfsent.c,v 1.20.4.1.2.2.2.6 2003/03/17 18:50:11 martinea Exp $
+ * $Id: getfsent.c,v 1.38 2006/07/19 17:41:14 martinea Exp $
  *
  * generic version of code to read fstab
  */
 
 #include "amanda.h"
+#include "util.h"
 
 #ifdef TEST
 #  include <stdio.h>
 #  include <sys/types.h>
-#  undef P
-#  define P(x) x
 #endif
 
 #include "getfsent.h"
 
+static char *dev2rdev(char *);
+
 /*
  * You are in a twisty maze of passages, all alike.
  * Geesh.
 
 #include <fstab.h>
 
-int open_fstab()
+int
+open_fstab(void)
 {
     return setfsent();
 }
 
-void close_fstab()
+void
+close_fstab(void)
 {
     endfsent();
 }
 
 
-int get_fstab_nextentry(fsent)
-generic_fsent_t *fsent;
+int
+get_fstab_nextentry(
+    generic_fsent_t *  fsent)
 {
     struct fstab *sys_fsent = getfsent();
     static char *xfsname = NULL, *xmntdir = NULL;
@@ -82,7 +87,7 @@ generic_fsent_t *fsent;
     fsent->mntopts = xmntopts = newstralloc(xmntopts, sys_fsent->fs_opts);
 #else
 #if defined(_AIX)
-    fsent->fstype  = xfstype  = newstralloc(xfstype,  "unknown");
+    fsent->fstype  = xfstype  = newstralloc(xfstype,  _("unknown"));
     fsent->mntopts = xmntopts = newstralloc(xmntopts, sys_fsent->fs_type);
 #else
     fsent->fstype  = xfstype  = newstralloc(xfstype,  sys_fsent->fs_vfstype);
@@ -103,24 +108,28 @@ generic_fsent_t *fsent;
 
 static FILE *fstabf = NULL;
 
-int open_fstab()
+int
+open_fstab(void)
 {
     close_fstab();
     return (fstabf = fopen(VFSTAB, "r")) != NULL;
 }
 
-void close_fstab()
+void
+close_fstab(void)
 {
     if(fstabf)
        afclose(fstabf);
     fstabf = NULL;
 }
 
-int get_fstab_nextentry(fsent)
-generic_fsent_t *fsent;
+int
+get_fstab_nextentry(
+    generic_fsent_t *  fsent)
 {
     struct vfstab sys_fsent;
 
+    memset(&sys_fsent, 0, SIZEOF(sys_fsent));
     if(getvfsent(fstabf, &sys_fsent) != 0)
        return 0;
 
@@ -153,7 +162,8 @@ static FILE *fstabf1 = NULL;                /* /proc/mounts */
 static FILE *fstabf2 = NULL;           /* MOUNTED */
 static FILE *fstabf3 = NULL;           /* MNTTAB */
 
-int open_fstab()
+int
+open_fstab(void)
 {
     close_fstab();
 #if defined(HAVE_SETMNTENT)
@@ -172,7 +182,8 @@ int open_fstab()
     return (fstabf1 != NULL || fstabf2 != NULL || fstabf3 != NULL);
 }
 
-void close_fstab()
+void
+close_fstab(void)
 {
     if (fstabf1) {
        AMCLOSE_MNTENT(fstabf1);
@@ -188,8 +199,9 @@ void close_fstab()
     }
 }
 
-int get_fstab_nextentry(fsent)
-generic_fsent_t *fsent;
+int
+get_fstab_nextentry(
+    generic_fsent_t *  fsent)
 {
     struct mntent *sys_fsent = NULL;
 
@@ -242,13 +254,15 @@ generic_fsent_t *fsent;
 
 static FILE *fstabf = NULL;
 
-int open_fstab()
+int
+open_fstab(void)
 {
     close_fstab();
     return (fstabf = fopen(FSTAB, "r")) != NULL;
 }
 
-void close_fstab()
+void
+close_fstab(void)
 {
     if(fstabf)
        afclose(fstabf);
@@ -257,8 +271,9 @@ void close_fstab()
 
 static generic_fsent_t _fsent;
 
-int get_fstab_nextentry(fsent)
-generic_fsent_t *fsent;
+int
+get_fstab_nextentry(
+    generic_fsent_t *  fsent)
 {
     static char *lfsnam = NULL;
     static char *opts = NULL;
@@ -268,6 +283,8 @@ generic_fsent_t *fsent;
 
     amfree(cp);
     for (; (cp = agets(fstabf)) != NULL; free(cp)) {
+       if (cp[0] == '\0')
+           continue;
        fsent->fsname = strtok(cp, " \t");
        if ( fsent->fsname && *fsent->fsname != '#' )
            break;
@@ -299,10 +316,8 @@ generic_fsent_t *fsent;
     }
     fsent->fstype = lfsnam;
 
-#define sc "hs"
-    if (strncmp(fsent->fstype, sc, sizeof(sc)-1) == 0)
+    if (strncmp_const(fsent->fstype, "hs") == 0)
        fsent->fstype = "iso9660";
-#undef sc
 
     fsent->freq = 0;
     fsent->passno = 0;
@@ -330,13 +345,15 @@ generic_fsent_t *fsent;
 
 static FILE *fstabf = NULL;
 
-int open_fstab()
+int
+open_fstab(void)
 {
     close_fstab();
     return (fstabf = fopen(MNTTAB, "r")) != NULL;
 }
 
-void close_fstab()
+void
+close_fstab(void)
 {
     if(fstabf)
        afclose(fstabf);
@@ -345,22 +362,23 @@ void close_fstab()
 
 static generic_fsent_t _fsent;
 
-int get_fstab_nextentry(fsent)
-generic_fsent_t *fsent;
+int
+get_fstab_nextentry(
+    generic_fsent_t *fsent)
 {
     struct statfs fsd;
     char typebuf[FSTYPSZ];
     static struct mnttab mnt;
     char *dp, *ep;
 
-    if(!fread (&mnt, sizeof mnt, 1, fstabf))
+    if(!fread (&mnt, SIZEOF(mnt), 1, fstabf))
       return 0;
 
     fsent->fsname  = mnt.mt_dev;
     fsent->mntdir  = mnt.mt_filsys;
     fsent->fstype = "";
 
-    if (statfs (fsent->mntdir, &fsd, sizeof fsd, 0) != -1
+    if (statfs (fsent->mntdir, &fsd, SIZEOF(fsd), 0) != -1
         && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1) {
        dp = typebuf;
        ep = fsent->fstype = malloc(strlen(typebuf)+2);
@@ -395,7 +413,7 @@ generic_fsent_t *fsent;
  * Convert either a block or character device name to a character (raw)
  * device name.
  *
- * char *dev2rdev(char *name);
+ * static char *dev2rdev(const char *name);
  *
  * entry:      name - device name to convert
  * exit:       matching character device name if found,
@@ -408,15 +426,16 @@ generic_fsent_t *fsent;
  *=====================================================================
  */
 
-static char *dev2rdev(name)
-char *name;
+static char *
+dev2rdev(
+    char *     name)
 {
   char *fname = NULL;
   struct stat st;
   char *s;
   int ch;
 
-  if(stat(name, &st) == 0 && S_ISCHR(st.st_mode)) {
+  if(stat(name, &st) == 0 && !S_ISBLK(st.st_mode)) {
     /*
      * If the input is already a character device, just return it.
      */
@@ -440,7 +459,7 @@ char *name;
     if (ch == '/') {
       s[-1] = '\0';
       fname = newvstralloc(fname, name, "/r", s, NULL);
-      s[-1] = ch;
+      s[-1] = (char)ch;
       if(stat(fname, &st) == 0 && S_ISCHR(st.st_mode)) return fname;
     }
     ch = *s++;
@@ -449,8 +468,13 @@ char *name;
   return stralloc(name);                       /* no match */
 }
 
-static int samefile(stats, estat)
-struct stat stats[3], *estat;
+#ifndef IGNORE_FSTAB
+static int samefile(struct stat[3], struct stat *);
+
+static int
+samefile(
+    struct stat stats[3],
+    struct stat *estat)
 {
   int i;
   for(i = 0; i < 3; ++i) {
@@ -460,16 +484,21 @@ struct stat stats[3], *estat;
   }
   return 0;
 }
+#endif /* !IGNORE_FSTAB */
 
-int search_fstab(name, fsent, check_dev)
-     char *name;
-     generic_fsent_t *fsent;
-     int check_dev;
+int
+search_fstab(
+     char *            name,
+     generic_fsent_t * fsent,
+     int               check_dev)
 {
 #ifdef IGNORE_FSTAB
   /* There is no real mount table so this will always fail and
    * we are using GNU tar so we can just return here.
    */
+  (void)name;          /* Quiet unused parameter warning */
+  (void)fsent;         /* Quiet unused parameter warning */
+  (void)check_dev;     /* Quiet unused parameter warning */
   return 0;
 #else
   struct stat stats[3];
@@ -480,21 +509,22 @@ int search_fstab(name, fsent, check_dev)
   if (!name)
     return 0;
 
-  stats[0].st_dev = stats[1].st_dev = stats[2].st_dev = -1;
+  memset(stats, 0, SIZEOF(stats));
+  stats[0].st_dev = stats[1].st_dev = stats[2].st_dev = (dev_t)-1;
 
   if (stat(name, &stats[0]) == -1)
-    stats[0].st_dev = -1;
+    stats[0].st_dev = (dev_t)-1;
   if (name[0] != '/') {
     fullname = stralloc2(DEV_PREFIX, name);
     if (stat(fullname, &stats[1]) == -1)
-      stats[1].st_dev = -1;
+      stats[1].st_dev = (dev_t)-1;
     fullname = newstralloc2(fullname, RDEV_PREFIX, name);
     if (stat(fullname, &stats[2]) == -1)
-      stats[2].st_dev = -1;
+      stats[2].st_dev = (dev_t)-1;
     amfree(fullname);
   }
   else if (stat((rdev = dev2rdev(name)), &stats[1]) == -1)
-    stats[1].st_dev = -1;
+    stats[1].st_dev = (dev_t)-1;
 
   amfree(rdev);
 
@@ -510,9 +540,8 @@ int search_fstab(name, fsent, check_dev)
 
     amfree(rdev);
 
-    if(fsent->mntdir != NULL &&
-       (smnt = stat(fsent->mntdir, &mntstat)) == -1)
-      continue;
+    if(fsent->mntdir != NULL)
+       smnt = stat(fsent->mntdir, &mntstat);
 
     if(fsent->fsname != NULL) {
       sfs = stat(fsent->fsname, &fsstat);
@@ -540,8 +569,9 @@ int search_fstab(name, fsent, check_dev)
 #endif /* !IGNORE_FSTAB */
 }
 
-int is_local_fstype(fsent)
-generic_fsent_t *fsent;
+int
+is_local_fstype(
+    generic_fsent_t *  fsent)
 {
     if(fsent->fstype == NULL)  /* unknown, assume local */
        return 1;
@@ -557,8 +587,9 @@ generic_fsent_t *fsent;
 }
 
 
-char *amname_to_devname(str)
-char *str;
+char *
+amname_to_devname(
+    char *     str)
 {
     generic_fsent_t fsent;
 
@@ -570,8 +601,9 @@ char *str;
     return dev2rdev(str);
 }
 
-char *amname_to_dirname(str)
-char *str;
+char *
+amname_to_dirname(
+    char *     str)
 {
     generic_fsent_t fsent;
 
@@ -583,8 +615,8 @@ char *str;
     return stralloc(str);
 }
 
-char *amname_to_fstype(str)
-char *str;
+char *amname_to_fstype(
+    char *     str)
 {
     generic_fsent_t fsent;
 
@@ -596,134 +628,133 @@ char *str;
 
 #ifdef TEST
 
+void print_entry(generic_fsent_t *fsent);
+
 void
-print_entry(fsent)
-generic_fsent_t *fsent;
+print_entry(
+    generic_fsent_t *  fsent)
 {
 #define nchk(s)        ((s)? (s) : "<NULL>")
-    printf("%-20.20s %-14.14s %-7.7s %4d %5d %s\n",
+    g_printf("%-20.20s %-14.14s %-7.7s %4d %5d %s\n",
           nchk(fsent->fsname), nchk(fsent->mntdir), nchk(fsent->fstype),
           fsent->freq, fsent->passno, nchk(fsent->mntopts));
 }
 
-int main(argc, argv)
-    int argc;
-    char **argv;
+int
+main(
+    int                argc,
+    char **    argv)
 {
     generic_fsent_t fsent;
-    int fd;
     char *s;
     char *name = NULL;
-    unsigned long malloc_hist_1, malloc_size_1;
-    unsigned long malloc_hist_2, malloc_size_2;
-
-    for(fd = 3; fd < FD_SETSIZE; fd++) {
-       /*
-        * Make sure nobody spoofs us with a lot of extra open files
-        * that would cause an open we do to get a very high file
-        * descriptor, which in turn might be used as an index into
-        * an array (e.g. an fd_set).
-        */
-       close(fd);
-    }
+
+    /*
+     * Configure program for internationalization:
+     *   1) Only set the message locale for now.
+     *   2) Set textdomain for all amanda related programs to "amanda"
+     *      We don't want to be forced to support dozens of message catalogs.
+     */  
+    setlocale(LC_MESSAGES, "C");
+    textdomain("amanda"); 
+
+    safe_fd(-1, 0);
 
     set_pname("getfsent");
 
-    malloc_size_1 = malloc_inuse(&malloc_hist_1);
+    dbopen(NULL);
+
+    /* Don't die when child closes pipe */
+    signal(SIGPIPE, SIG_IGN);
 
     if(!open_fstab()) {
-       fprintf(stderr, "getfsent_test: could not open fstab\n");
+       g_fprintf(stderr, _("getfsent_test: could not open fstab\n"));
        return 1;
     }
 
-    printf("getfsent (%s)\n",GETFSENT_TYPE);
-    printf("l/r fsname               mntdir         fstype  freq pass# mntopts\n");
+    g_printf("getfsent (%s)\n",GETFSENT_TYPE);
+    g_printf("l/r fsname               mntdir         fstype  freq pass# mntopts\n");
     while(get_fstab_nextentry(&fsent)) {
-       printf("%c  ",is_local_fstype(&fsent)? 'l' : 'r');
+       g_printf("%c  ",is_local_fstype(&fsent)? 'l' : 'r');
        print_entry(&fsent);
     }
-    printf("--------\n");
+    g_printf("--------\n");
 
     close_fstab();
 
     name = newstralloc(name, "/usr");
     if(search_fstab(name, &fsent, 1) || search_fstab(name, &fsent, 0)) {
-       printf("Found %s mount for %s:\n",
-              is_local_fstype(&fsent)? "local" : "remote", name);
+       g_printf(_("Found %s mount for %s:\n"),
+              is_local_fstype(&fsent)? _("local") : _("remote"), name);
        print_entry(&fsent);
     }
     else 
-       printf("Mount for %s not found\n", name);
+       g_printf(_("Mount for %s not found\n"), name);
 
     name = newstralloc(name, "/");
     if(search_fstab(name, &fsent, 1) || search_fstab(name, &fsent, 0)) {
-       printf("Found %s mount for %s:\n",
-              is_local_fstype(&fsent)? "local" : "remote", name);
+       g_printf(_("Found %s mount for %s:\n"),
+              is_local_fstype(&fsent)? _("local") : _("remote"), name);
        print_entry(&fsent);
     }
     else 
-       printf("Mount for %s not found\n", name);
+       g_printf(_("Mount for %s not found\n"), name);
 
     name = newstralloc(name, "/");
     s = amname_to_fstype(name);
-    printf("fstype of `%s': %s\n", name, s);
+    g_printf(_("fstype of `%s': %s\n"), name, s);
     amfree(s);
     name = newstralloc(name, "/dev/root");
     s = amname_to_fstype(name);
-    printf("fstype of `%s': %s\n", name, s);
+    g_printf(_("fstype of `%s': %s\n"), name, s);
     amfree(s);
     name = newstralloc(name, "/usr");
     s = amname_to_fstype(name);
-    printf("fstype of `%s': %s\n", name, s);
+    g_printf(_("fstype of `%s': %s\n"), name, s);
     amfree(s);
     name = newstralloc(name, "c0t3d0s0");
     s = amname_to_fstype(name);
-    printf("fstype of `%s': %s\n", name, s);
+    g_printf(_("fstype of `%s': %s\n"), name, s);
     amfree(s);
 
     name = newstralloc(name, "/tmp/foo");
     s = amname_to_devname(name);
-    printf("device of `%s': %s\n", name, s);
+    g_printf(_("device of `%s': %s\n"), name, s);
     amfree(s);
     s = amname_to_dirname(name);
-    printf("dirname of `%s': %s\n", name, s);
+    g_printf(_("dirname of `%s': %s\n"), name, s);
     amfree(s);
     s = amname_to_fstype(name);
-    printf("fstype of `%s': %s\n", name, s);
+    g_printf(_("fstype of `%s': %s\n"), name, s);
     amfree(s);
 
     name = newstralloc(name, "./foo");
     s = amname_to_devname(name);
-    printf("device of `%s': %s\n", name, s);
+    g_printf(_("device of `%s': %s\n"), name, s);
     amfree(s);
     s = amname_to_dirname(name);
-    printf("dirname of `%s': %s\n", name, s);
+    g_printf(_("dirname of `%s': %s\n"), name, s);
     amfree(s);
     s = amname_to_fstype(name);
-    printf("fstype of `%s': %s\n", name, s);
+    g_printf(_("fstype of `%s': %s\n"), name, s);
     amfree(s);
 
     while (--argc > 0) {
        name = newstralloc(name, *++argv);
        s = amname_to_devname(name);
-       printf("device of `%s': %s\n", name, s);
+       g_printf(_("device of `%s': %s\n"), name, s);
        amfree(s);
        s = amname_to_dirname(name);
-       printf("dirname of `%s': %s\n", name, s);
+       g_printf(_("dirname of `%s': %s\n"), name, s);
        amfree(s);
        s = amname_to_fstype(name);
-       printf("fstype of `%s': %s\n", name, s);
+       g_printf(_("fstype of `%s': %s\n"), name, s);
        amfree(s);
     }
 
     amfree(name);
 
-    malloc_size_2 = malloc_inuse(&malloc_hist_2);
-
-    if(malloc_size_1 != malloc_size_2) {
-       malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);
-    }
-
+    dbclose();
     return 0;
 }