summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Colomar <alx.manpages@gmail.com>2022-09-11 00:54:53 +0200
committerAlex Colomar <alx.manpages@gmail.com>2022-09-12 17:08:28 +0200
commit0f6f10d52afa01c97b8366e452c4e299ef86b276 (patch)
tree938044ec16b4068db9885b5f69044f1f941f5151
parent038bd2a80fcb802693333f0062159c8e3d0693b3 (diff)
Various pages: EXAMPLES: Separate variable declarations from the function body
Reported-by: checkpatch(1) Signed-off-by: Alex Colomar <alx.manpages@gmail.com>
-rw-r--r--man3/__ppc_get_timebase.33
-rw-r--r--man3/bsearch.31
-rw-r--r--man3/fread.310
-rw-r--r--man3/insque.34
-rw-r--r--man3/malloc_info.35
-rw-r--r--man3/pthread_create.311
-rw-r--r--man3/rtime.313
-rw-r--r--man3/setaliasent.31
-rw-r--r--man3/setbuf.31
-rw-r--r--man3/shm_open.332
-rw-r--r--man3/tsearch.31
11 files changed, 50 insertions, 32 deletions
diff --git a/man3/__ppc_get_timebase.3 b/man3/__ppc_get_timebase.3
index 70390cb01..9710ea04c 100644
--- a/man3/__ppc_get_timebase.3
+++ b/man3/__ppc_get_timebase.3
@@ -69,8 +69,9 @@ int
main(void)
{
uint64_t tb1, tb2, diff;
+ uint64_t freq;
- uint64_t freq = __ppc_get_timebase_freq();
+ freq = __ppc_get_timebase_freq();
printf("Time Base frequency = %"PRIu64" Hz\en", freq);
tb1 = __ppc_get_timebase();
diff --git a/man3/bsearch.3 b/man3/bsearch.3
index dbb0a2fa5..d6f5ff1d8 100644
--- a/man3/bsearch.3
+++ b/man3/bsearch.3
@@ -106,6 +106,7 @@ compmi(const void *m1, const void *m2)
{
const struct mi *mi1 = m1;
const struct mi *mi2 = m2;
+
return strcmp(mi1\->name, mi2\->name);
}
diff --git a/man3/fread.3 b/man3/fread.3
index 2fcceaa5f..01d6c2373 100644
--- a/man3/fread.3
+++ b/man3/fread.3
@@ -122,15 +122,17 @@ Class: 0x02
int
main(void)
{
- FILE *fp = fopen("/bin/sh", "rb");
+ FILE *fp;
+ size_t ret;
+ unsigned char buffer[4];
+
+ fp = fopen("/bin/sh", "rb");
if (!fp) {
perror("fopen");
return EXIT_FAILURE;
}
- unsigned char buffer[4];
-
- size_t ret = fread(buffer, sizeof(*buffer), ARRAY_SIZE(buffer), fp);
+ ret = fread(buffer, sizeof(*buffer), ARRAY_SIZE(buffer), fp);
if (ret != ARRAY_SIZE(buffer)) {
fprintf(stderr, "fread() failed: %zu\en", ret);
exit(EXIT_FAILURE);
diff --git a/man3/insque.3 b/man3/insque.3
index da690b222..15b6f9544 100644
--- a/man3/insque.3
+++ b/man3/insque.3
@@ -165,7 +165,9 @@ struct element {
static struct element *
new_element(void)
{
- struct element *e = malloc(sizeof(*e));
+ struct element *e;
+
+ e = malloc(sizeof(*e));
if (e == NULL) {
fprintf(stderr, "malloc() failed\en");
exit(EXIT_FAILURE);
diff --git a/man3/malloc_info.3 b/man3/malloc_info.3
index bf1c41e42..0755674be 100644
--- a/man3/malloc_info.3
+++ b/man3/malloc_info.3
@@ -198,7 +198,8 @@ thread_func(void *arg)
int
main(int argc, char *argv[])
{
- int sleepTime;
+ int sleepTime;
+ pthread_t *thr;
if (argc < 4) {
fprintf(stderr,
@@ -212,7 +213,7 @@ main(int argc, char *argv[])
blockSize = atoi(argv[3]);
sleepTime = (argc > 4) ? atoi(argv[4]) : 0;
- pthread_t *thr = calloc(numThreads, sizeof(*thr));
+ thr = calloc(numThreads, sizeof(*thr));
if (thr == NULL)
errExit("calloc");
diff --git a/man3/pthread_create.3 b/man3/pthread_create.3
index 7f5f0ed3d..f551ed4f1 100644
--- a/man3/pthread_create.3
+++ b/man3/pthread_create.3
@@ -311,10 +311,11 @@ thread_start(void *arg)
int
main(int argc, char *argv[])
{
- int s, opt, num_threads;
- pthread_attr_t attr;
- ssize_t stack_size;
- void *res;
+ int s, opt, num_threads;
+ void *res;
+ ssize_t stack_size;
+ pthread_attr_t attr;
+ struct thread_info *tinfo;
/* The "\-s" option specifies a stack size for our threads. */
@@ -348,7 +349,7 @@ main(int argc, char *argv[])
/* Allocate memory for pthread_create() arguments. */
- struct thread_info *tinfo = calloc(num_threads, sizeof(*tinfo));
+ tinfo = calloc(num_threads, sizeof(*tinfo));
if (tinfo == NULL)
handle_error("calloc");
diff --git a/man3/rtime.3 b/man3/rtime.3
index e17cfda16..f8616a1d3 100644
--- a/man3/rtime.3
+++ b/man3/rtime.3
@@ -122,11 +122,12 @@ static const char servername[] = "linux";
int
main(void)
{
- struct sockaddr_in name;
- struct rpc_timeval time1 = {0,0};
- struct rpc_timeval timeout = {1,0};
- struct hostent *hent;
- int ret;
+ int ret;
+ time_t t;
+ struct hostent *hent;
+ struct rpc_timeval time1 = {0,0};
+ struct rpc_timeval timeout = {1,0};
+ struct sockaddr_in name;
memset(&name, 0, sizeof(name));
sethostent(1);
@@ -137,7 +138,7 @@ main(void)
if (ret < 0)
perror("rtime error");
else {
- time_t t = time1.tv_sec;
+ t = time1.tv_sec;
printf("%s\en", ctime(&t));
}
diff --git a/man3/setaliasent.3 b/man3/setaliasent.3
index 6a203dfad..978ac53ff 100644
--- a/man3/setaliasent.3
+++ b/man3/setaliasent.3
@@ -153,6 +153,7 @@ int
main(void)
{
struct aliasent *al;
+
setaliasent();
for (;;) {
al = getaliasent();
diff --git a/man3/setbuf.3 b/man3/setbuf.3
index d2381de2f..138e9af7c 100644
--- a/man3/setbuf.3
+++ b/man3/setbuf.3
@@ -205,6 +205,7 @@ int
main(void)
{
char buf[BUFSIZ];
+
setbuf(stdout, buf);
printf("Hello, world!\en");
return 0;
diff --git a/man3/shm_open.3 b/man3/shm_open.3
index 8ddad52b3..6aa3dff2c 100644
--- a/man3/shm_open.3
+++ b/man3/shm_open.3
@@ -344,18 +344,21 @@ to tell the "send" program that it may now access the shared memory.
int
main(int argc, char *argv[])
{
+ int fd;
+ char *shmpath;
+ struct shmbuf *shmp;
+
if (argc != 2) {
fprintf(stderr, "Usage: %s /shm\-path\en", argv[0]);
exit(EXIT_FAILURE);
}
- char *shmpath = argv[1];
+ shmpath = argv[1];
/* Create shared memory object and set its size to the size
of our structure. */
- int fd = shm_open(shmpath, O_CREAT | O_EXCL | O_RDWR,
- S_IRUSR | S_IWUSR);
+ fd = shm_open(shmpath, O_CREAT | O_EXCL | O_RDWR, 0600);
if (fd == \-1)
errExit("shm_open");
@@ -364,9 +367,8 @@ main(int argc, char *argv[])
/* Map the object into the caller\(aqs address space. */
- struct shmbuf *shmp = mmap(NULL, sizeof(*shmp),
- PROT_READ | PROT_WRITE,
- MAP_SHARED, fd, 0);
+ *shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE,
+ MAP_SHARED, fd, 0);
if (shmp == MAP_FAILED)
errExit("mmap");
@@ -434,14 +436,19 @@ on standard output.
int
main(int argc, char *argv[])
{
+ int fd;
+ char *shmpath, *string;
+ size_t len;
+ struct shmbuf *shmp;
+
if (argc != 3) {
fprintf(stderr, "Usage: %s /shm\-path string\en", argv[0]);
exit(EXIT_FAILURE);
}
- char *shmpath = argv[1];
- char *string = argv[2];
- size_t len = strlen(string);
+ shmpath = argv[1];
+ string = argv[2];
+ len = strlen(string);
if (len > BUF_SIZE) {
fprintf(stderr, "String is too long\en");
@@ -451,13 +458,12 @@ main(int argc, char *argv[])
/* Open the existing shared memory object and map it
into the caller\(aqs address space. */
- int fd = shm_open(shmpath, O_RDWR, 0);
+ fd = shm_open(shmpath, O_RDWR, 0);
if (fd == \-1)
errExit("shm_open");
- struct shmbuf *shmp = mmap(NULL, sizeof(*shmp),
- PROT_READ | PROT_WRITE,
- MAP_SHARED, fd, 0);
+ shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE,
+ MAP_SHARED, fd, 0);
if (shmp == MAP_FAILED)
errExit("mmap");
diff --git a/man3/tsearch.3 b/man3/tsearch.3
index cb0485379..0b9e3a185 100644
--- a/man3/tsearch.3
+++ b/man3/tsearch.3
@@ -266,6 +266,7 @@ static void *
xmalloc(size_t n)
{
void *p;
+
p = malloc(n);
if (p)
return p;