Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #382973

Re: getOpsFromSelf() (Long Post)

From Spiros Bousbouras <spibou@gmail.com>
Newsgroups comp.lang.c
Subject Re: getOpsFromSelf() (Long Post)
Date 2024-02-24 17:06 +0000
Organization A noiseless patient Spider
Message-ID <1HyZeCKHsTViXCSYj@bongo-ra.co> (permalink)
References <ur1ie5$2cgfo$1@dont-email.me> <jh8EdBXIFQu4WLaKt@bongo-ra.co>

Show all headers | View raw


On Sat, 24 Feb 2024 16:19:11 -0000 (UTC)
Spiros Bousbouras <spibou@gmail.com> wrote:
> On Tue, 20 Feb 2024 06:56:06 -0000 (UTC)
> porkchop@invalid.foo (Mike Sanders) wrote:
> >     char *path_env = getenv("PATH");
> >     if (!path_env) return NULL;
> > 
> >     char path_env_copy[MAX_SIZE];
> >     strncpy(path_env_copy, path_env, sizeof(path_env_copy));
> >     path_env_copy[sizeof(path_env_copy) - 1] = '\0';
> > 
> >     char full_path[MAX_SIZE];
> >     char *dir = strtok(path_env_copy, PATH_SEPARATOR);
> > 
> >     while (dir) {
> >         snprintf(full_path, sizeof(full_path), "%s%s%s", dir, 
> >             (dir[strlen(dir) - 1] == DIR_SEPARATOR[0] ? "" :
> >                 DIR_SEPARATOR), fname);
> >         if (stat(full_path, &buffer) == 0) {
> >             char *result = strdup(full_path);
> >             if (!result) return NULL; // memory allocation error <--
> This line is redundant.
> 
> >             return result;
> >         }
> >         dir = strtok(NULL, PATH_SEPARATOR);

If I remeber correctly , the convention is that if  ::  exists in PATH
then it's the same as  :.:  i.e. the current working directory. Your
code does not handle this case. Yet another reason to do the searching
in the string yourself instead of using  strtok() .

> >     }
> > 
> >     return NULL;
> > }
> 
> I wouldn't use  strtok()  at all for this but rather  strchr()  and keep
> track myself of the position in the string and not modify the string at
> all. This way
> 
> - you can use the string returned by  getenv()  directly instead of having
>   to copy it.
> 
> - you don't have to call  strlen()  when you have already traversed the
>   string to find the separator. Traversing the string for a second time
>   probably won't have a noticeable effect for performance in this context
>   but this kind of thing where the code has done a certain somewhat costly
>   computation (traversing part of the string) and then immediately does
>   again almost the same computation instead of using the result found by
>   the first computation , grates me.
> 
> > 
> > #endif

-- 
God grant me serenity to accept the code I cannot change, courage to
change the code I can, and wisdom to know the difference.
  Erik Naggum

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

getOpsFromSelf() (Long Post) porkchop@invalid.foo (Mike Sanders) - 2024-02-20 06:56 +0000
  Re: getOpsFromSelf() (Long Post) Spiros Bousbouras <spibou@gmail.com> - 2024-02-24 16:19 +0000
    Re: getOpsFromSelf() (Long Post) Spiros Bousbouras <spibou@gmail.com> - 2024-02-24 17:06 +0000
      Re: getOpsFromSelf() (Long Post) porkchop@invalid.foo (Mike Sanders) - 2024-02-24 21:25 +0000
        Re: getOpsFromSelf() (Long Post) Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-02-24 17:59 -0800
          Re: getOpsFromSelf() (Long Post) porkchop@invalid.foo (Mike Sanders) - 2024-02-25 11:49 +0000
            Re: getOpsFromSelf() (Long Post) Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-02-25 14:54 -0800
              Re: getOpsFromSelf() (Long Post) Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2024-02-25 22:58 +0000
              Re: getOpsFromSelf() (Long Post) dave_thompson_2@comcast.net - 2024-03-01 18:37 -0500
                Re: getOpsFromSelf() (Long Post) porkchop@invalid.foo (Mike Sanders) - 2024-03-02 01:10 +0000
                Re: getOpsFromSelf() (Long Post) Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2024-03-02 02:36 +0000
                Re: getOpsFromSelf() (Long Post) porkchop@invalid.foo (Mike Sanders) - 2024-03-02 12:13 +0000
        Re: getOpsFromSelf() (Long Post) Spiros Bousbouras <spibou@gmail.com> - 2024-02-26 15:07 +0000
          Re: getOpsFromSelf() (Long Post) porkchop@invalid.foo (Mike Sanders) - 2024-03-02 12:16 +0000
    Re: getOpsFromSelf() (Long Post) porkchop@invalid.foo (Mike Sanders) - 2024-02-24 21:22 +0000
      Re: getOpsFromSelf() (Long Post) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2024-02-25 00:48 +0000
        Re: getOpsFromSelf() (Long Post) porkchop@invalid.foo (Mike Sanders) - 2024-02-25 01:51 +0000
      Re: getOpsFromSelf() (Long Post) Spiros Bousbouras <spibou@gmail.com> - 2024-02-26 14:50 +0000
        Re: getOpsFromSelf() (Long Post) porkchop@invalid.foo (Mike Sanders) - 2024-02-26 15:15 +0000
        Re: getOpsFromSelf() (Long Post) Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-02-26 13:38 -0800
        Re: getOpsFromSelf() (Long Post) Spiros Bousbouras <spibou@gmail.com> - 2024-02-26 21:40 +0000
        Re: getOpsFromSelf() (Long Post) scott@slp53.sl.home (Scott Lurndal) - 2024-02-27 16:17 +0000
        Re: getOpsFromSelf() (Long Post) Spiros Bousbouras <spibou@gmail.com> - 2024-02-27 17:04 +0000
        Re: getOpsFromSelf() (Long Post) David Brown <david.brown@hesbynett.no> - 2024-02-27 18:20 +0100
    Re: getOpsFromSelf() (Long Post) scott@slp53.sl.home (Scott Lurndal) - 2024-02-25 18:42 +0000
      Re: getOpsFromSelf() (Long Post) porkchop@invalid.foo (Mike Sanders) - 2024-02-26 15:20 +0000
  Re: getOpsFromSelf() (Long Post) immibis <news@immibis.com> - 2024-02-28 22:21 +0100
    Re: getOpsFromSelf() (Long Post) porkchop@invalid.foo (Mike Sanders) - 2024-03-01 00:38 +0000

csiph-web