Xref: utzoo comp.unix.questions:23400 comp.sys.pyramid:862 Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!usc!apple!snorkelwacker!spdcc!merk!alliant!linus!nixbur!nixpbe!peun33!mboen From: mboen@nixpbe.UUCP (Martin Boening) Newsgroups: comp.unix.questions,comp.sys.pyramid Subject: Re: getting 'df' info from inside a program? Message-ID: Date: 29 Jun 90 05:42:06 GMT References: <797@massey.ac.nz> <3394@sactoh0.UUCP> Sender: news@nixpbe.UUCP Followup-To: comp.unix.questions Lines: 116 In <3394@sactoh0.UUCP> jak@sactoh0.UUCP (Jay A. Konigsberg) writes: >In article <797@massey.ac.nz> GEustace@massey.ac.nz (Glen Eustace) writes: >>We are trying to write some code that will check whether a file will >>fit on a given file system prior to copying it. It would be great if >>the information given in a 'df' were available with some system call. >>Does anyone know whether there is one and if so how one uses it. >> >This looks like a job for the stat() system call. (Sys V) >#include >#include >#include >main() >{ >struct stat *buf; >buf = (struct stat *)malloc(sizeof(struct stat)); /* error check removed */ >if ( stat("filename", buf) == -1 ) > { > fprintf("stat: error=%d\n", errno); > exit(2); > } >else > { > printf("%s is %ld bytes.\n", "filename", buf->st_size); > } >} >-- >------------------------------------------------------------- >Jay @ SAC-UNIX, Sacramento, Ca. UUCP=...pacbell!sactoh0!jak >If something is worth doing, its worth doing correctly. I think you missed the point, there. The question was how to find out if a file system still provides enough room for a file prior to copying it, not to get the size of the file to copy. Of course the above program will determine the size of the file to copy. Now, to get the remaining space of the target file system, you have to use ustat(2). Man Page excerpt: USTAT(2-att) TARGON /35 Operating System USTAT(2-att) NAME ustat - get file system statistics SYNOPSIS #include #include int ustat (dev, buf) int dev; struct ustat *buf; DESCRIPTION Ustat returns information about a mounted file system. Dev is a device number identifying a device containing a mounted file system. Buf is a pointer to a ustat structure that includes the following elements: daddr_t f_tfree; /* Total free blocks */ ino_t f_tinode; /* Number of free inodes */ char f_fname[6]; /* Filsys name */ char f_fpack[6]; /* Filsys pack name */ ... and so on ... Question is: how to get the number for dev. I think the stat call comes to new honor here: do a stat on the directory you wish to copy the file to, then use the struct element st_dev. So, extending your program a bit, I think the following might work: #include #include #include #include main() { struct stat *buf; struct ustat *ubuf; buf = (struct stat *)malloc(sizeof(struct stat)); /* error check removed */ ubuf = (struct ustat *)malloc(sizeof(struct ustat)); /* ditto */ if ( stat("target-dir", buf) ) { perror("stat failed"); exit (errno); } else if ( ustat(buf->st_dev, ubuf) ) { perror("ustat failed"); exit(errno); } else { fprintf(stdout, "%ld free blocks and %ld free inodes remain on target fs\n", ubuf->f_tfree, ubuf->f_inode); exit(0); } } Something similar is done in the nntpd, BTW. I have not tried if it works, yet. I hope this helps and isn't improper to post to the net (enough general inter- est??) Martin -- Email: in the USA -> ...!uunet!philabs!linus!nixbur!mboening.pad outside USA -> {...!mcvax}!unido!nixpbe!mboening.pad Paper Mail: Martin Boening, Nixdorf Computer AG, DS-CC22, Pontanusstr. 55, 4790 Paderborn, W.-Germany