From 4462150b2ab9086e2412d4499634cff7cd5d02db Mon Sep 17 00:00:00 2001 From: Chris Pressey Date: Thu, 6 Jan 2005 03:21:00 +0000 Subject: [PATCH] Raise WARNS of newfs to 6: - Use static char buffer instead of string constant; - Move and add some function prototypes; - Don't reuse a scratch variable, use a real pid_t to hold the result of a fork(); - Cast variables as needed (mainly to u_long and u_int32) to avoid signed-unsigned comparison warnings; - Rename some variables to avoid name clashes; - Mark some parameters as unused; - Add MOPT_NULL as a struct mntopt constant for terminating a list. And apply some style(9): - Move out some loop invariant code, for clarity; - Remove `register' keywords; - Make explicit some checks against NULL. Based-on-patch-by: Peter Schuller Tested-by: typical usage of installer --- sbin/mount/mntopts.h | 5 +- sbin/newfs/fscopy.c | 10 ++-- sbin/newfs/mkfs.c | 109 ++++++++++++++++++++++++------------------- sbin/newfs/newfs.c | 22 ++++----- 4 files changed, 81 insertions(+), 65 deletions(-) diff --git a/sbin/mount/mntopts.h b/sbin/mount/mntopts.h index d0617ee973..6245d37872 100644 --- a/sbin/mount/mntopts.h +++ b/sbin/mount/mntopts.h @@ -32,7 +32,7 @@ * * @(#)mntopts.h 8.7 (Berkeley) 3/29/95 * $FreeBSD: src/sbin/mount/mntopts.h,v 1.16 1999/10/09 11:54:07 phk Exp $ - * $DragonFly: src/sbin/mount/mntopts.h,v 1.3 2003/11/01 17:16:00 drhodus Exp $ + * $DragonFly: src/sbin/mount/mntopts.h,v 1.4 2005/01/06 03:21:00 cpressey Exp $ */ struct mntopt { @@ -64,6 +64,9 @@ struct mntopt { #define MOPT_RO { "ro", 0, MNT_RDONLY, 0 } #define MOPT_RW { "rw", 1, MNT_RDONLY, 0 } +/* NULL option; used to terminate arrays of options */ +#define MOPT_NULL { NULL, 0, 0, 0 } + /* This is parsed by mount(8), but is ignored by specific mount_*(8)s. */ #define MOPT_AUTO { "auto", 0, 0, 0 } diff --git a/sbin/newfs/fscopy.c b/sbin/newfs/fscopy.c index d7ccb3c38a..766c7d7e8d 100644 --- a/sbin/newfs/fscopy.c +++ b/sbin/newfs/fscopy.c @@ -23,7 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/sbin/newfs/fscopy.c,v 1.4 2004/06/26 22:54:01 dillon Exp $ + * $DragonFly: src/sbin/newfs/fscopy.c,v 1.5 2005/01/06 03:21:00 cpressey Exp $ */ #include @@ -51,6 +51,8 @@ struct FSNode { char fs_Name[4]; }; +static char empty_string[] = ""; + static fsnode_t fsmknode(const char *path) @@ -72,7 +74,7 @@ fsmknode(const char *path) return(node); } -fsnode_t +static fsnode_t fsgethlink(fsnode_t hlinks, fsnode_t node) { fsnode_t scan; @@ -87,7 +89,7 @@ fsgethlink(fsnode_t hlinks, fsnode_t node) return(NULL); } -char * +static char * fshardpath(fsnode_t hlink, fsnode_t node) { fsnode_t scan; @@ -221,7 +223,7 @@ FSCopy(fsnode_t *phlinks, const char *path) node->fs_Data[n] = 0; } } else if (n == 0) { - node->fs_Data = ""; + node->fs_Data = empty_string; node->fs_Bytes = 0; } else { fprintf(stderr, "Unable to read link: %s\n", path); diff --git a/sbin/newfs/mkfs.c b/sbin/newfs/mkfs.c index 7a59f2c610..d9989ccd73 100644 --- a/sbin/newfs/mkfs.c +++ b/sbin/newfs/mkfs.c @@ -32,7 +32,7 @@ * * @(#)mkfs.c 8.11 (Berkeley) 5/3/95 * $FreeBSD: src/sbin/newfs/mkfs.c,v 1.29.2.6 2001/09/21 19:15:21 dillon Exp $ - * $DragonFly: src/sbin/newfs/mkfs.c,v 1.9 2004/12/18 21:43:39 swildner Exp $ + * $DragonFly: src/sbin/newfs/mkfs.c,v 1.10 2005/01/06 03:21:00 cpressey Exp $ */ #include "defs.h" @@ -43,13 +43,14 @@ extern int atoi(char *); extern char * getenv(char *); -#endif #ifdef FSIRAND extern long random(void); extern void srandomdev(void); #endif +#endif /* STANDALONE */ + /* * make file system for cylinder-group style file systems */ @@ -128,17 +129,19 @@ static fsnode_t copyhlinks; #ifdef FSIRAND int randinit; #endif -daddr_t alloc(); -long calcipg(); -static int charsperline(); +daddr_t alloc(int, int); +long calcipg(long, long, off_t *); +static int charsperline(void); void clrblock(struct fs *, unsigned char *, int); void fsinit(time_t); void initcg(int, time_t); int isblock(struct fs *, unsigned char *, int); void iput(struct dinode *, ino_t); int makedir(struct direct *, int); +void parentready(int); void rdfs(daddr_t, int, char *); void setblock(struct fs *, unsigned char *, int); +void started(int); void wtfs(daddr_t, int, char *); void wtfsflush(void); @@ -158,8 +161,8 @@ int parentready_signalled; void mkfs(struct partition *pp, char *fsys, int fi, int fo, const char *mfscopy) { - register long i, mincpc, mincpg, inospercg; - long cylno, rpos, blk, j, warn = 0; + long i, mincpc, mincpg, inospercg; + long cylno, rpos, blk, j, emitwarn = 0; long used, mincpgcnt, bpcg; off_t usedb; long mapcramped, inodecramped; @@ -167,8 +170,6 @@ mkfs(struct partition *pp, char *fsys, int fi, int fo, const char *mfscopy) int status, fd; time_t utime; quad_t sizepb; - void started(); - void parentready(); int width; char tmpbuf[100]; /* XXX this will break in about 2,500 years */ @@ -183,17 +184,18 @@ mkfs(struct partition *pp, char *fsys, int fi, int fo, const char *mfscopy) #endif if (mfs) { int omask; + pid_t child; mfs_ppid = getpid(); signal(SIGUSR1, parentready); - if ((i = fork())) { - if (i == -1) + if ((child = fork()) != 0) { + if (child == -1) err(10, "mfs"); if (mfscopy) copyroot = FSCopy(©hlinks, mfscopy); signal(SIGUSR1, started); - kill(i, SIGUSR1); - if (waitpid(i, &status, 0) != -1 && WIFEXITED(status)) + kill(child, SIGUSR1); + if (waitpid(child, &status, 0) != -1 && WIFEXITED(status)) exit(WEXITSTATUS(status)); exit(11); /* NOTREACHED */ @@ -207,17 +209,20 @@ mkfs(struct partition *pp, char *fsys, int fi, int fo, const char *mfscopy) #else raise_data_limit(); #endif - if(filename) { + if (filename != NULL) { unsigned char buf[BUFSIZ]; - unsigned long l,l1; - fd = open(filename,O_RDWR|O_TRUNC|O_CREAT,0644); + unsigned long l, l1; + ssize_t w; + + fd = open(filename, O_RDWR|O_TRUNC|O_CREAT, 0644); if(fd < 0) err(12, "%s", filename); - for(l=0;l< fssize * sectorsize;l += l1) { - l1 = fssize * sectorsize; - if (BUFSIZ < l1) - l1 = BUFSIZ; - if (l1 != write(fd,buf,l1)) + l1 = fssize * sectorsize; + if (l1 > BUFSIZ) + l1 = BUFSIZ; + for (l = 0; l < (u_long)fssize * (u_long)sectorsize; l += l1) { + w = write(fd, buf, l1); + if (w < 0 || (u_long)w != l1) err(12, "%s", filename); } membase = mmap( @@ -234,7 +239,8 @@ mkfs(struct partition *pp, char *fsys, int fi, int fo, const char *mfscopy) #ifndef STANDALONE get_memleft(); #endif - if (fssize * sectorsize > (memleft - 131072)) + if ((u_long)fssize * (u_long)sectorsize > + (memleft - 131072)) fssize = (memleft - 131072) / sectorsize; if ((membase = malloc(fssize * sectorsize)) == NULL) errx(13, "malloc failed"); @@ -375,7 +381,7 @@ mkfs(struct partition *pp, char *fsys, int fi, int fo, const char *mfscopy) if (maxcontig > 1) sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG); mapcramped = 0; - while (CGSIZE(&sblock) > sblock.fs_bsize) { + while (CGSIZE(&sblock) > (uint32_t)sblock.fs_bsize) { mapcramped = 1; if (sblock.fs_bsize < MAXBSIZE) { sblock.fs_bsize <<= 1; @@ -422,7 +428,7 @@ mkfs(struct partition *pp, char *fsys, int fi, int fo, const char *mfscopy) sblock.fs_fragshift -= 1; mincpc >>= 1; sblock.fs_cpg = roundup(mincpgcnt, mincpc); - if (CGSIZE(&sblock) > sblock.fs_bsize) { + if (CGSIZE(&sblock) > (uint32_t)sblock.fs_bsize) { sblock.fs_bsize <<= 1; break; } @@ -478,7 +484,7 @@ mkfs(struct partition *pp, char *fsys, int fi, int fo, const char *mfscopy) /* * Must ensure there is enough space to hold block map. */ - while (CGSIZE(&sblock) > sblock.fs_bsize) { + while (CGSIZE(&sblock) > (uint32_t)sblock.fs_bsize) { mapcramped = 1; sblock.fs_cpg -= mincpc; sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb); @@ -518,7 +524,7 @@ mkfs(struct partition *pp, char *fsys, int fi, int fo, const char *mfscopy) sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc; if (fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) { sblock.fs_ncyl++; - warn = 1; + emitwarn = 1; } if (sblock.fs_ncyl < 1) { printf("file systems must have at least one cylinder\n"); @@ -630,9 +636,9 @@ next: sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg; sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc / NSPF(&sblock); - warn = 0; + emitwarn = 0; } - if (warn && !mfs) { + if (emitwarn && !mfs) { printf("Warning: %d sector(s) in last cylinder unallocated\n", sblock.fs_spc - (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1) @@ -768,9 +774,10 @@ initcg(int cylno, time_t utime) { daddr_t cbase, d, dlower, dupper, dmax, blkno; long i; - register struct csum *cs; + unsigned long k; + struct csum *cs; #ifdef FSIRAND - long j; + uint32_t j; #endif /* @@ -823,15 +830,19 @@ initcg(int cylno, time_t utime) exit(37); } acg.cg_cs.cs_nifree += sblock.fs_ipg; - if (cylno == 0) - for (i = 0; i < ROOTINO; i++) { - setbit(cg_inosused(&acg), i); + if (cylno == 0) { + for (k = 0; k < ROOTINO; k++) { + setbit(cg_inosused(&acg), k); acg.cg_cs.cs_nifree--; } + } for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag) { #ifdef FSIRAND - for (j = 0; j < sblock.fs_bsize / sizeof(struct dinode); j++) + for (j = 0; + j < sblock.fs_bsize / sizeof(struct dinode); + j++) { zino[j].di_gen = random(); + } #endif wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i), sblock.fs_bsize, (char *)zino); @@ -1020,7 +1031,7 @@ fsinit(time_t utime) * return size of directory. */ int -makedir(register struct direct *protodir, int entries) +makedir(struct direct *protodir, int entries) { char *cp; int i, spcleft; @@ -1094,7 +1105,7 @@ goth: * Calculate number of inodes per group. */ long -calcipg(long cpg, long bpcg, off_t *usedbp) +calcipg(long cylspg, long bpcg, off_t *usedbp) { int i; long ipg, new_ipg, ncg, ncyl; @@ -1105,7 +1116,7 @@ calcipg(long cpg, long bpcg, off_t *usedbp) * Note that fssize is still in sectors, not filesystem blocks. */ ncyl = howmany(fssize, (u_int)secpercyl); - ncg = howmany(ncyl, cpg); + ncg = howmany(ncyl, cylspg); /* * Iterate a few times to allow for ipg depending on itself. */ @@ -1113,8 +1124,8 @@ calcipg(long cpg, long bpcg, off_t *usedbp) for (i = 0; i < 10; i++) { usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock)) * NSPF(&sblock) * (off_t)sectorsize; - new_ipg = (cpg * (quad_t)bpcg - usedb) / density * fssize - / ncg / secpercyl / cpg; + new_ipg = (cylspg * (quad_t)bpcg - usedb) / density * fssize + / ncg / secpercyl / cylspg; new_ipg = roundup(new_ipg, INOPB(&sblock)); if (new_ipg == ipg) break; @@ -1128,9 +1139,9 @@ calcipg(long cpg, long bpcg, off_t *usedbp) * Allocate an inode on the disk */ void -iput(register struct dinode *ip, register ino_t ino) +iput(struct dinode *ip, ino_t ino) { - struct dinode buf[MAXINOPB]; + struct dinode inobuf[MAXINOPB]; daddr_t d; int c; @@ -1150,14 +1161,14 @@ iput(register struct dinode *ip, register ino_t ino) (char *)&acg); sblock.fs_cstotal.cs_nifree--; fscs[0].cs_nifree--; - if (ino >= sblock.fs_ipg * sblock.fs_ncg) { + if (ino >= (uint32_t)sblock.fs_ipg * (uint32_t)sblock.fs_ncg) { printf("fsinit: inode value out of range (%d).\n", ino); exit(32); } d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino)); - rdfs(d, sblock.fs_bsize, (char *)buf); - buf[ino_to_fsbo(&sblock, ino)] = *ip; - wtfs(d, sblock.fs_bsize, (char *)buf); + rdfs(d, sblock.fs_bsize, (char *)inobuf); + inobuf[ino_to_fsbo(&sblock, ino)] = *ip; + wtfs(d, sblock.fs_bsize, (char *)inobuf); } /* @@ -1167,7 +1178,7 @@ iput(register struct dinode *ip, register ino_t ino) * parent forked the child otherwise). */ void -parentready(void) +parentready(__unused int signo) { parentready_signalled = 1; } @@ -1178,7 +1189,7 @@ parentready(void) * We have to wait until the mount has actually completed! */ void -started(void) +started(__unused int signo) { int retry = 100; /* 10 seconds, 100ms */ @@ -1207,7 +1218,7 @@ started(void) * Replace libc function with one suited to our needs. */ caddr_t -malloc(register u_long size) +malloc(u_long size) { char *base, *i; static u_long pgsz; @@ -1306,7 +1317,7 @@ get_memleft(void) pgsz = getpagesize() - 1; dstart = ((u_long)&etext) &~ pgsz; - freestart = ((u_long)(sbrk(0) + pgsz) &~ pgsz); + freestart = ((u_long)((char *)sbrk(0) + pgsz) &~ pgsz); if (getrlimit(RLIMIT_DATA, &rlp) < 0) warn("getrlimit"); memused = freestart - dstart; diff --git a/sbin/newfs/newfs.c b/sbin/newfs/newfs.c index b3bb8c6866..fba020a71b 100644 --- a/sbin/newfs/newfs.c +++ b/sbin/newfs/newfs.c @@ -33,7 +33,7 @@ * @(#) Copyright (c) 1983, 1989, 1993, 1994 The Regents of the University of California. All rights reserved. * @(#)newfs.c 8.13 (Berkeley) 5/1/95 * $FreeBSD: src/sbin/newfs/newfs.c,v 1.30.2.9 2003/05/13 12:03:55 joerg Exp $ - * $DragonFly: src/sbin/newfs/newfs.c,v 1.11 2004/12/18 21:43:39 swildner Exp $ + * $DragonFly: src/sbin/newfs/newfs.c,v 1.12 2005/01/06 03:21:00 cpressey Exp $ */ /* @@ -77,7 +77,7 @@ struct mntopt mopts[] = { MOPT_STDOPTS, MOPT_ASYNC, - { NULL }, + MOPT_NULL, }; void fatal(const char *fmt, ...); @@ -211,16 +211,16 @@ static void mfsintr(int signo); int main(int argc, char **argv) { - register int ch; - register struct partition *pp; - register struct disklabel *lp; + int ch; + struct partition *pp; + struct disklabel *lp; struct disklabel mfsfakelabel; - struct disklabel *getdisklabel(); struct partition oldpartition; struct stat st; struct statfs *mp; int fsi = 0, fso, len, n, vflag; - char *cp = NULL, *s1, *s2, *special, *opstring; + char *cp = NULL, *s1, *s2, *special; + const char *opstring; #ifdef MFS struct vfsconf vfc; int error; @@ -485,7 +485,7 @@ main(int argc, char **argv) havelabel: if (fssize == 0) fssize = pp->p_size; - if (fssize > pp->p_size && !mfs) + if ((uint32_t)fssize > pp->p_size && !mfs) fatal("%s: maximum file system size on the `%c' partition is %d", argv[0], *cp, pp->p_size); if (rpm == 0) { @@ -560,7 +560,7 @@ havelabel: * case (4096 sectors per cylinder) is intended to disagree * with the disklabel. */ - if (t_or_u_flag && secpercyl != lp->d_secpercyl) + if (t_or_u_flag && (uint32_t)secpercyl != lp->d_secpercyl) fprintf(stderr, "%s (%d) %s (%lu)\n", "Warning: calculated sectors per cylinder", secpercyl, "disagrees with disk label", (u_long)lp->d_secpercyl); @@ -663,7 +663,7 @@ havelabel: #ifdef MFS static void -mfsintr(int signo) +mfsintr(__unused int signo) { if (filename) munmap(membase, fssize * sectorsize); @@ -686,7 +686,7 @@ getdisklabel(char *s, int fd) if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { #ifdef COMPAT if (disktype) { - struct disklabel *lp, *getdiskbyname(); + struct disklabel *lp; unlabeled++; lp = getdiskbyname(disktype); -- 2.41.0