TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: John H. Guillory
from: Pascal Schmidt
date: 2004-05-02 16:34:58
subject: Merits

Hi John! :-)

 JHG> Fact remains, if you
 JHG> know what your doing, you can code *ANYTHING* without having to 
 JHG> "break" out of a loop, or "GOTO" a label, etc. 
You can, no doubt on that. However, sometimes a break or goto can be much
clearer in the end.

Consider:

    FILE *fp;
    int res;

    fp = fopen("foo", "w");
    res = write(fp, ...); /* some data */
    if (res = -1) {
        fclose(fp);
        return -1;
    }
    res = write(fp, ...); /* some other data */
    fclose(fp);
    return res;

and:

    FILE *fp;
    int res;

    fp = fopen("foo", "w");    
    res = write(fp, ...);
    if (res == -1)
        goto out;
    res = write(fp, ...);
out:
    fclose(fp);
    return res;

The latter keeps the cleanup code (in this case only one fclose, but there
could be several lines of cleanup code) in one place and not duplicated at
each possible exit point.

In the end, it's all a matter of personal taste when the language has
several constructs that can do the same job.

Consider this routine and how it would look like if it had to be written
without "break":

/*
 * create directory hierarchy up to dirname of path
 *
 * path: absolute path to object we are about to create
 */
static int mkdirhier(const char *path)
{
        char *work, *last;
        int res = 0;
        struct stat buf;

        /* need working copy */ 
        work = strdup(path);
        if (!work)
                return -1;
                
        last = work;
        do {
                *last = '/';
                last = strchr(last+1, '/');

                /* component left to check? */          
                if (last != NULL) {
                        *last = '\0';
                        res = stat(work, &buf);
                        
                        /* existing directory, ok */
                        if (res == 0 && S_ISDIR(buf.st_mode))
                                continue;
                        
                        /* existing other object, bad */        
                        if (res == 0) {
                                res = -1;
                                break;
                        }
                        
                        /* nothing there, create directory */
                        res = mkdir(work, 0755);
                        if (res != 0)
                                break;
                }
        } while (last);
        
        free(work);
        return res;
}

Ciao
Pascal

--- Msged/LNX 6.1.1
* Origin: sed s/$(echo $HOME | sed 's/[[:punct:]]/\\&/g')/~/ (1:153/401.2)
SEEN-BY: 633/267 270
@PATH: 153/401 307 140/1 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.