diff --git a/defs.h b/defs.h index cbe0dd4ba876aa6399db39877a61bb2cdf704401..cbacf6112af0208a834a986f36e94388b21bf908 100644 --- a/defs.h +++ b/defs.h @@ -127,6 +127,7 @@ void* memset(void*, int, uint); char* safestrcpy(char*, const char*, int); int strlen(const char*); int strncmp(const char*, const char*, uint); +char* strncpy(char*, const char*, int); // syscall.c int argint(int, int*); diff --git a/fs.c b/fs.c index bc3cccd57fc6b8a0cd4d1bd034417992063b7ca1..eef20e89e9c1002c3a6fc570afdf15073a2c277c 100644 --- a/fs.c +++ b/fs.c @@ -455,15 +455,7 @@ writei(struct inode *ip, char *src, uint off, uint n) int namecmp(const char *s, const char *t) { - int i; - - for(i=0; i<DIRSIZ; i++){ - if(s[i] != t[i]) - return s[i] - t[i]; - if(s[i] == 0) - break; - } - return 0; + return strncmp(s, t, DIRSIZ); } // Look for a directory entry in a directory. @@ -500,18 +492,6 @@ dirlookup(struct inode *dp, char *name, uint *poff) return 0; } -// Copy one name to another. -static void -namecpy(char *s, const char *t) -{ - int i; - - for(i=0; i<DIRSIZ && t[i]; i++) - s[i] = t[i]; - for(; i<DIRSIZ; i++) - s[i] = 0; -} - // Write a new directory entry (name, ino) into the directory dp. int dirlink(struct inode *dp, char *name, uint ino) @@ -534,7 +514,7 @@ dirlink(struct inode *dp, char *name, uint ino) break; } - namecpy(de.name, name); + strncpy(de.name, name, DIRSIZ); de.inum = ino; if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) panic("dirwrite"); diff --git a/string.c b/string.c index 0a92ccae96274eedf530237e8e98d68220fcd251..2f9fc258affdc7d82a4a51727523d84c65f189f8 100644 --- a/string.c +++ b/string.c @@ -56,8 +56,20 @@ strncmp(const char *p, const char *q, uint n) n--, p++, q++; if(n == 0) return 0; - else - return (int) ((uchar) *p - (uchar) *q); + return (uchar)*p - (uchar)*q; +} + +char* +strncpy(char *s, const char *t, int n) +{ + char *os; + + os = s; + while(n-- > 0 && (*s++ = *t++) != 0) + ; + while(n-- > 0) + *s++ = 0; + return os; } // Like strncpy but guaranteed to NUL-terminate.