void fetch_info(struct list *lst) { struct fsmntinfo *fmi; int nummnt; statst *entbuf; statst vfsbuf, **fs; /* init fsmntinfo */ if ((fmi = malloc(sizeof(struct fsmntinfo))) == NULL) { (void)fputs("Error while allocating memory to fmi", stderr); exit(EXIT_FAILURE); /* NOTREACHED */ } *fmi = fmi_init(); if ((nummnt = getmntinfo(&entbuf, MNT_NOWAIT)) <= 0) err(EXIT_FAILURE, "Error while getting the list of mountpoints"); /* NOTREACHED */ for (fs = &entbuf; nummnt--; (*fs)++) { vfsbuf = **fs; if ((fmi->fsnameog = strdup(entbuf->f_mntfromname)) == NULL) fmi->fsnameog = g_unknown_str; if ((fmi->mntdirog = strdup(entbuf->f_mntonname)) == NULL) fmi->mntdirog = g_unknown_str; if ((fmi->fstypeog = strdup(entbuf->f_fstypename)) == NULL) fmi->fstypeog = g_unknown_str; if (Wflag) { /* Wflag to avoid name truncation */ fmi->fsname = fmi->fsnameog; fmi->mntdir = fmi->mntdirog; fmi->fstype = fmi->fstypeog; } else { if ((fmi->fsname = strdup(shortenstr( entbuf->f_mntfromname, STRMAXLEN))) == NULL) { fmi->fsname = g_unknown_str; } if ((fmi->mntdir = strdup(shortenstr( entbuf->f_mntonname, STRMAXLEN))) == NULL) { fmi->mntdir = g_unknown_str; } if ((fmi->fstype = strdup(shortenstr( entbuf->f_fstypename, STRMAXLEN))) == NULL) { fmi->fstype = g_unknown_str; } } /* infos from statvfs */ fmi->flags = GET_FLAGS(vfsbuf); fmi->bsize = vfsbuf.f_bsize; fmi->frsize = GET_FRSIZE(vfsbuf); fmi->blocks = vfsbuf.f_blocks; fmi->bfree = vfsbuf.f_bfree; fmi->bavail = vfsbuf.f_bavail; fmi->files = vfsbuf.f_files; fmi->ffree = vfsbuf.f_ffree; fmi->favail = GET_FAVAIL(vfsbuf); if ((fmi->mntopts = statfs_flags_to_str(fmi)) == NULL) fmi->mntopts = g_none_str; /* compute, available, % used, etc. */ compute_fs_stats(fmi); /* pointer to the next element */ fmi->next = NULL; /* enqueue the element into the queue */ enqueue(lst, *fmi); update_maxwidth(fmi); } free(fmi); }
void fetch_info(struct list *lst) { struct fsmntinfo *fmi; FILE *mnttab; struct mnttab mnttabbuf; struct statvfs vfsbuf; int ret; /* init fsmntinfo */ if ((fmi = malloc(sizeof(struct fsmntinfo))) == NULL) { (void)fputs("Error while allocating memory to fmi", stderr); exit(EXIT_FAILURE); /* NOTREACHED */ } *fmi = fmi_init(); /* open mnttab file */ if ((mnttab = fopen("/etc/mnttab", "r")) == NULL) { perror("Error while opening mnttab file "); exit(EXIT_FAILURE); /* NOTREACHED */ } /* loop to get infos from all the mounted fs */ while ((ret = getmntent(mnttab, &mnttabbuf)) == 0) { if (statvfs(mnttabbuf.mnt_mountp, &vfsbuf) == -1) { (void)fprintf(stderr, _("WARNING: %s was skipped " "because it could not be stated"), mnttabbuf.mnt_mountp); perror(" "); continue; } if (Wflag) { /* Wflag to avoid name truncation */ if ((fmi->fsname = strdup(mnttabbuf.mnt_special)) == NULL) fmi->fsname = g_unknown_str; if ((fmi->mntdir = strdup(mnttabbuf.mnt_mountp))== NULL) fmi->mntdir = g_unknown_str; if ((fmi->fstype = strdup(mnttabbuf.mnt_fstype)) == NULL) fmi->fstype = g_unknown_str; } else { if ((fmi->fsname = strdup(shortenstr( mnttabbuf.mnt_special, STRMAXLEN))) == NULL) { fmi->fsname = g_unknown_str; } if ((fmi->mntdir = strdup(shortenstr (mnttabbuf.mnt_mountp, STRMAXLEN))) == NULL) { fmi->mntdir = g_unknown_str; } if ((fmi->fstype = strdup(shortenstr( mnttabbuf.mnt_fstype, STRMAXLEN))) == NULL) { fmi->fstype = g_unknown_str; } } if ((fmi->mntopts = strdup(mnttabbuf.mnt_mntopts)) == NULL) fmi->mntopts = g_none_str; fmi->bsize = vfsbuf.f_bsize; fmi->frsize = vfsbuf.f_frsize; fmi->blocks = vfsbuf.f_blocks; fmi->bfree = vfsbuf.f_bfree; fmi->bavail = vfsbuf.f_bavail; fmi->files = vfsbuf.f_files; fmi->ffree = vfsbuf.f_ffree; fmi->favail = vfsbuf.f_favail; compute_fs_stats(fmi); fmi->next = NULL; enqueue(lst, *fmi); } if (ret > 0) { (void)fprintf(stderr, "An error occured while reading the " "mnttab file\n"); } /* we need to close the mnttab file now */ if (fclose(mnttab) == EOF) perror("Could not close mnttab file "); free(fmi); }