|
Home > Archive > Unix Programming > September 2007 > Is there a difference between return -1 vs return 1 ?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Is there a difference between return -1 vs return 1 ?
|
|
| K-mart Cashier 2007-09-27, 2:30 am |
| The following code snippet is taken from OpenBSD
/*
* term_chk - check that a terminal exists, and get the message bit
* and the access time
*/
int
term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror)
{
struct stat s;
char path[MAXPATHLEN];
(void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
if (stat(path, &s) < 0) {
if (showerror)
warn("%s", path);
return(1);
}
*msgsokP = (s.st_mode & S_IWGRP) != 0; /* group write bit */
*atimeP = s.st_atime;
return(0);
}
On failure, the author returns 1. I think I might know the answer to
this, but I'm still going to ask. Could he have returned -1?
| |
| user923005 2007-09-27, 2:30 am |
| On Sep 26, 6:21 pm, K-mart Cashier <cdal...@gmail.com> wrote:
> The following code snippet is taken from OpenBSD
>
> /*
> * term_chk - check that a terminal exists, and get the message bit
> * and the access time
> */
> int
> term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror)
> {
> struct stat s;
> char path[MAXPATHLEN];
>
> (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
> if (stat(path, &s) < 0) {
> if (showerror)
> warn("%s", path);
> return(1);
> }
> *msgsokP = (s.st_mode & S_IWGRP) != 0; /* group write bit */
> *atimeP = s.st_atime;
> return(0);
>
> }
>
> On failure, the author returns 1. I think I might know the answer to
> this, but I'm still going to ask. Could he have returned -1?
Sure. But the burning question is: 'how is the return of the function
used?"
I guess it is just zero for 'worked' and non-zero for 'failed' in
which case -1 or 1 would both signal failure.
But you should look at the code to be sure.
| |
| K-mart Cashier 2007-09-27, 2:30 am |
| On Sep 26, 6:52 pm, user923005 <dcor...@connx.com> wrote:
> On Sep 26, 6:21 pm, K-mart Cashier <cdal...@gmail.com> wrote:
>
>
>
>
>
>
>
>
> Sure. But the burning question is: 'how is the return of the function
> used?"
>
> I guess it is just zero for 'worked' and non-zero for 'failed' in
> which case -1 or 1 would both signal failure.
> But you should look at the code to be sure.
Here is how part of it is used
int
main(int argc, char *argv[])
{
char tty[MAXPATHLEN], *mytty, *cp;
int msgsok, myttyfd;
time_t atime;
uid_t myuid;
/* check that sender has write enabled */
if (isatty(fileno(stdin)))
myttyfd = fileno(stdin);
else if (isatty(fileno(stdout)))
myttyfd = fileno(stdout);
else if (isatty(fileno(stderr)))
myttyfd = fileno(stderr);
else
errx(1, "can't find your tty");
if (!(mytty = ttyname(myttyfd)))
errx(1, "can't find your tty's name");
if ((cp = strrchr(mytty, '/')))
mytty = cp + 1;
if (term_chk(mytty, &msgsok, &atime, 1))
exit(1);
if (!msgsok)
warnx("you have write permission turned off");
myuid = getuid();
/* check args */
switch (argc) {
case 2:
search_utmp(argv[1], tty, sizeof tty, mytty, myuid);
do_write(tty, mytty, myuid);
break;
case 3:
if (!strncmp(argv[2], _PATH_DEV, sizeof(_PATH_DEV) -
1))
argv[2] += sizeof(_PATH_DEV) - 1;
if (utmp_chk(argv[1], argv[2]))
errx(1, "%s is not logged in on %s",
argv[1], argv[2]);
if (term_chk(argv[2], &msgsok, &atime, 1))
exit(1);
if (myuid && !msgsok)
errx(1, "%s has messages disabled on %s",
argv[1], argv[2]);
do_write(argv[2], mytty, myuid);
break;
default:
(void)fprintf(stderr, "usage: write user [tty]\n");
exit(1);
}
done(0);
/* NOTREACHED */
return (0);
}
I guess I'm just wondering if there is a difference between something
like:
/*Function returns 1 on failure*/
if (term_chk(argv[2], &msgsok, &atime, 1))
exit(1);
vs
/*Function returns -1 on failure*/
if (term_chk(argv[2], &msgsok, &atime, 1) <1)
exit(1);
| |
| Scott Lurndal 2007-09-27, 2:31 am |
| K-mart Cashier <cdalten@gmail.com> writes:
>
>I guess I'm just wondering if there is a difference between something
>like:
>
>/*Function returns 1 on failure*/
> if (term_chk(argv[2], &msgsok, &atime, 1))
> exit(1);
>
>vs
>
>
>/*Function returns -1 on failure*/
> if (term_chk(argv[2], &msgsok, &atime, 1) <1)
> exit(1);
>
The former will generate slightly better code and is the conventional
way in C to use boolean arithmetic (the value zero is false and a non-zero
value is true).
System calls generally (but not always)[*] return -1 for error; yet most code
checks as you do, above (i.e. < 1) which is technically not correct. If the
error code is -1, then you should be checking for -1, not any negative value.
The lseek system call, in particular, can return negative values other than -1
as successful return values (e.g. when lseeking in a > 2GB file, or seeking in
a special file like a /proc-based memory image, or /dev/mem.
scott
[*] multiple threads and a global errno don't play well together so most newly
defined systems calls (e.g. pthread_*) return errno directly rather than -1.
(and most libc implementations map the global errno to a thread-specific data
area to support the legacy system calls use of errno).
| |
| Barry Margolin 2007-09-27, 2:31 am |
| In article <1190859455.981696.252970@50g2000hsm.googlegroups.com>,
K-mart Cashier <cdalten@gmail.com> wrote:
> I guess I'm just wondering if there is a difference between something
> like:
>
> /*Function returns 1 on failure*/
> if (term_chk(argv[2], &msgsok, &atime, 1))
> exit(1);
>
> vs
>
>
> /*Function returns -1 on failure*/
> if (term_chk(argv[2], &msgsok, &atime, 1) <1)
> exit(1);
If the function is intended to return a specific value on failure, then
you should check specifically for that value, e.g. if it returns -1 then
you should use:
if (term_chk(...) == -1)
But returning specific values is generally only done if different values
have different meanings. If the return value is just to indicate
success/failure, and it's intended to be used in an if() statement, then
it's probably best to return a boolean -- 0 for false/failure, anything
else for true/success.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Ben Pfaff 2007-09-27, 2:31 am |
| scott@slp53.sl.home (Scott Lurndal) writes:
> The lseek system call, in particular, can return negative
> values other than -1 as successful return values (e.g. when
> lseeking in a > 2GB file, or seeking in a special file like a
> /proc-based memory image, or /dev/mem.
It's true that lseek is allowed to return a negative value on
success for special files, but SUSv3 says specifically that "The
lseek() function shall fail if: ... The resulting file offset
would be a value which cannot be represented correctly in an
object of type off_t." POSIX.1-1990 was apparently looser,
according to the SUSv3 rationale.
--
Ben Pfaff
http://benpfaff.org
|
|
|
|
|