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*);
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.
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)
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");
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.