Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!occrsh!occrsh.ATT.COM!rjd From: rjd@occrsh.ATT.COM Newsgroups: comp.unix.wizards Subject: Re: HELP! File status checking in C Message-ID: <142700021@occrsh.ATT.COM> Date: 16 Dec 87 14:02:00 GMT References: <10869@brl-adm.ARPA> Lines: 36 Nf-ID: #R:brl-adm.ARPA:-1086900:occrsh.ATT.COM:142700021:000:1174 Nf-From: occrsh.ATT.COM!rjd Dec 16 08:02:00 1987 > I am in the process of converting a bourne-shell script into C. To do this >I need to know how to do the following in C: > 1) Check whether a directory exists > 2) Given two files: filea and fileb > Check if filea has been modified more recently than fileb (What did we do - Lose our manual?) Both of your questions are answered by the stat(2) or fstat(2) system calls, depending on whether you want to check the file by name or open file descriptor. The syntax is stat(filepathname, buf); or fstat(filedescriptor, buf); buf is defined as "struct stat *buf;" after #include'ingand For AT&T System V: Question 1) After the call, the return status of -1 and errno set to ENOENT means the directory/fifo/character special/block special/ordinary file does not exist. If it does exist, then checking the buf.st_mode by and'ing it with 0040000 will tell you whether or not it is a directory. Question 2) stat("filea", buf); fatime=buf.st_mtime; stat("fileb", buf); if(fatime > buf.st_mtime) printf("Filea has been modified more recently then fileb\n"); else printf("Fileb has been modified more recently then filea\n"); Randy