Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!bionet!agate!garnet.berkeley.edu!andy From: andy@garnet.berkeley.edu (Andy Lieberman) Newsgroups: comp.unix.questions Subject: Re: rename() system call Summary: Just use link() instead Message-ID: <24515@agate.BERKELEY.EDU> Date: 17 May 89 17:43:47 GMT References: <24504@agate.BERKELEY.EDU> Sender: usenet@agate.BERKELEY.EDU Organization: University of California, Berkeley Lines: 71 The original problem was how to do a rename that would return an error if the file already exists. A number of people showed me how simple this is. Thanks for all the help. From chris@mimsy.umd.edu (many others said the same thing): Use link(), and then, iff it succeeds, unlink(). (rename is like the sequence (void) unlink("dst"); if (link("src", "dst") == 0) (void) unlink("src"); and you want to skip the first part.) Chris -------- From barnett@unclejack.crd.ge.com Wed May 17 07:24:13 1989: Rochkind's book "advanced unix programming" has some simple examples of a file locking mechanism. Page 22 i.e.: #include #include #define LOCKDIR "/tmp/" #define MAXTRIES 3 #define NAPTIME 5 #define BOOLEAN int #define FALSE 0 #define TRUE 1 BOOLEAN lock(name) /* acquire lock */ char *name; { char *path, *lockpath(); int fd, tries; extern int errno; path = lockpath(name); tries = 0; while ((fd = create(path,0)) == -1 && errno == EACCES) { if (++tries <= MAXTRIES) return (FALSE); sleep (NAPTIME); } if (fd == -1 || clode(fd) == -1) syserr("lock"); return(TRUE); } void unlock(name) /* free lock */ char *name; { char *lockpath(); if (unlink(lockpath(name)) == -1) syserr("unlock"); } static char *lockpath(name) /* generate lock filepath */ char *name; { static char path[20]; char *strcat(); strcpy(path, LOCKDIR); retrun(strcat(path,name)); }