mchinni@ARDEC.arpa (System Administrator for ARDEC-CC1) (12/16/87)
HELP! 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 Mike Chinni <mchinni@ardec.arpa>
rjd@occrsh.ATT.COM (12/16/87)
> 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'ing <sys/types.h> and <sys/stat.h> 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
drears@ARDEC.arpa (FSAC) (12/17/87)
MIke Chinni writes: MIKE>HELP! MIKE> MIKE> I am in the process of converting a bourne-shell script into C. To do this MIKE>I need to know how to do the following in C: MIKE> 1) Check whether a directory exists MIKE> 2) Given two files: filea and fileb MIKE> Check if filea has been modified more recently than fileb MIKE> MIKE> Mike Chinni <mchinni@ardec.arpa> You can do both with the stat(2) call. Stat returns a structure like : struct stat { dev_t st_dev; ino_t st_ino; u_short st_mode; short st_nlink; short st_uid; short st_gid; dev_t st_rdev; off_t st_size; time_t st_atime; int st_spare1; time_t st_mtime; int st_spare2; time_t st_ctime; int st_spare3; long st_blksize; long st_blocks; long st_spare4[2]; }; If st_mode has bits S_IFDIR then it is a directory. To answer the question about modifed the st_atime, st_mtime, and st_ctime fields will let you know depending if you are looking for access, modification, or inode modification time. It returns a value of -1 if it fails or 0 if it is good. Give me a call if (X6639) you need working examples. I have some old programs on ardec-ac4 that does this. Dennis ------------------------------------------------------------ ARPA: drears@ardec.ac4 UUCP: ...!uunet!drears@ARDEC.arpa #if all else fails AT&T: 201-724-6639 Snailmail: Box 210, Wharton, NJ 07885 ------------------------------------------------------------
levy@ttrdc.UUCP (Daniel R. Levy) (12/18/87)
In article <142700021@occrsh.ATT.COM>, rjd@occrsh.ATT.COM writes: > The syntax is > > stat(filepathname, buf); > or > fstat(filedescriptor, buf); > > buf is defined as "struct stat *buf;" after #include'ing <sys/types.h> and > <sys/stat.h> Ahem, "buf" in this case had better point to some storage, e.g. struct stat *buf; struct stat buff; buf = &buff; retval=stat(name,buf); or better yet omit the pointer and retval=stat(name,&buff); If you just declare struct stat *buf; and that's it, buf will "point" to an undefined place. When stat() sees this, if you're lucky buf will contain an address outside of your process space and your program will immediately core dump, saving you from further harm. Or, more mysteriously, the program may seem to work correctly but quite likely will subtly corrupt other data in the program. N.B.: Lint will catch this error, saying that buf has been used before set. -- |------------Dan Levy------------| Path: ..!{akgua,homxb,ihnp4,ltuxa,mvuxa, | an Engihacker @ | <most AT&T machines>}!ttrdc!ttrda!levy | AT&T Computer Systems Division | Disclaimer? Huh? What disclaimer??? |--------Skokie, Illinois--------|