Professional Web Applications Themes

string concatenation - UNIX Programming

hi all, i am writing a code in which i have a char buffer "cwdir[]" which hold the current working directory by calling the function getcwd(). later i change the directory to "/" as i have to make my code Deamon. and later again i want to run some other executable available at the path holded by the "cwdir[]" using the system() system call. presently i concatenate program name (to be executed) to the "cwdir[]" and use system(chdir)to run the program. do we have any facility to automate this process as i need to run many other programs also using ...

  1. #1

    Default string concatenation

    hi all,

    i am writing a code in which i have a char buffer "cwdir[]"
    which hold the current working directory by calling the function
    getcwd(). later i change the directory to "/" as i have to make my
    code Deamon. and later again i want to run some other executable
    available at the path holded by the "cwdir[]" using the system()
    system call. presently i concatenate program name (to be executed) to
    the "cwdir[]" and use system(chdir)to run the program.

    do we have any facility to automate this process as i need to run
    many other programs also using the system() system call for ex as we
    can use the ## symbol to cancatenate in macro ...

    --shri
    Shri Guest

  2. #2

    Default Re: string concatenation

    com (Shri) wrote in
    news:google.com:
     

    Does strcat() help? If you need fancier concatenation, sprintf() might be
    an option.

    --
    - Mark ->
    --
    Mark Guest

  3. #3

    Default Re: string concatenation

    In article <google.com>,
    com (Shri) wrote:
     

    I've taken comp.lang.c and comp.std.c out of the newsgroups, as this is
    entirely Unix-specific; the C language says nothing about working
    directories or how programs are found by system().

    The simplest solution I think would be for you to prepend the directory
    in cwdir[] to your PATH environment variable.

    oldpath = getenv("PATH");
    newpath = malloc(strlen(oldpath) + strlen(cwdir) + 2);
    sprintf(newpath, "%s:%s", cwdir, oldpath);
    setenv("PATH", newpath, 1);

    --
    Barry Margolin, mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    Barry Guest

  4. #4

    Default Re: string concatenation

    Shri wrote: 

    I *think* that what you are asking for is strcpy(), strcat(), etc.
    You might also want to use strdup(), which is not specified in the
    C standard but is available in every modern Unix system's C library.
    And of course in the Unix (POSIX) system C library is also a family
    of exec() functions. Learn to use "man -k", and also study a good
    Unix programming text such as Kernighan & Pike.

    It should also be noted that you shouldn't be writing programs that
    run with elevated privilege (set-UID, daemon) without knowing a lot
    more about security loopholes than you seem to be aware of.
    Douglas Guest

  5. #5

    Default Re: string concatenation

    Sean Burke <org> writes:
     
    >
    > Since you mention security (e.g. buffer overflows), it's worth
    > adding that strcpy, strcat have long since been deprecated in favor
    > of strlcpy, strlcat.[/ref]

    I can't find those anywhere on my system, only strncpy and strncat.
    The latter are defined by ISO C.

    --
    Måns Rullgård
    se
    Måns Guest

  6. #6

    Default Re: string concatenation

    In message <individual.net> of Fri, 13 Feb 2004
    08:06:19 in comp.std.c, Richard Bos <nl> writes 
    >
    >Not in ISO C, they haven't. Maybe in POSIX.[/ref]

    Neither there!
    (http://www.opengroup.org/onlinepubs/007904975/basedefs/string.h.html)

    They ARE doented at http://www.courtesan.com/todd/papers/strlcpy.html

    My summary of that is
    size_t strlcpy(char *dst, const char *src, size_t size);
    Copy a string of up to size-1 bytes from src to dst. Return strlen(src).

    size_t strlcat(char *dst, const char *src, size_t size);
    Concatenate bytes from src to dst to form a string with strlen(dst) <
    size. Return strlen(dst) /* before concatenation */ + strlen(src).

    The functions allow buffer overflow elimination.
    For a pathological case given, strlcpy is slightly slower than strcpy
    and much faster than strncpy because strncpy pads with nul bytes.

    The authors added them to OpenBSD in 1996 and report their approval for
    a future Solaris version in their undated paper. Source code is said to
    be available at ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string

    Have the functions been submitted for C standardisation?
    --
    Walter Briscoe
    Walter Guest

  7. #7

    Default Re: string concatenation

    Walter Briscoe <demon.co.uk> wrote:
     
    > >
    > >Not in ISO C, they haven't. Maybe in POSIX.[/ref]
    >
    > Neither there!
    > (http://www.opengroup.org/onlinepubs/007904975/basedefs/string.h.html)
    >
    > They ARE doented at http://www.courtesan.com/todd/papers/strlcpy.html
    >
    > My summary of that is
    > size_t strlcpy(char *dst, const char *src, size_t size);
    > Copy a string of up to size-1 bytes from src to dst. Return strlen(src).
    >
    > size_t strlcat(char *dst, const char *src, size_t size);
    > Concatenate bytes from src to dst to form a string with strlen(dst) <
    > size. Return strlen(dst) /* before concatenation */ + strlen(src).
    >
    > The functions allow buffer overflow elimination.[/ref]

    Nice, meaningless statement; so does strncat(), and so does using
    strlen() and inserting a '\0' at the right place before using strcpy().
     

    No. Might be a nice addition, but I wouldn't describe them as vital.
    Mind you, I won't complain if they _are_ added to C20XX.

    Richard
    Richard Guest

  8. #8

    Default Re: string concatenation

    Sean Burke <org> wrote in message news:<xenadyne.com>... 
    >
    > Since you mention security (e.g. buffer overflows),
    > it's worth adding that strcpy, strcat have long since
    > been deprecated in favor of strlcpy, strlcat.[/ref]

    Have they?

    They are not present in many frequently used operating systems.

    They are not AFAIK part of the C standard, nor are they AFAIK part of
    the POSIX standard.
    Rob Guest

  9. #9

    Default Re: string concatenation

    In <xenadyne.com> Sean Burke <org> writes:
     

    On the contrary, any C program using these identifiers with external
    linkage invokes undefined behaviour.

    Whoever wanted to improve upon strcpy and friends should have had enough
    clues to avoid identifiers belonging to the implementation name space.

    lstrcpy() and lstrcat() would have been ideal names for this purpose.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email: de
    Dan Guest

  10. #10

    Default Re: string concatenation

    ch (Dan Pop) writes:
     
    >
    > On the contrary, any C program using these identifiers with external
    > linkage invokes undefined behaviour.[/ref]

    Maybe Mr. Burke misspelled "strncpy" and "strncat", mentioned in
    ISO/IEC 9899:1999 under 7.21.2.4 and 7.21.3.2 ? Their prototypes (in
    <string.h>) are

    char *strncpy(char * restrict, const char * restrict, size_t);
    char *strncat(char * restrict, const char * restrict, size_t);

    --
    Maurizio Loreti http://www.pd.infn.it/~loreti/mlo.html
    Dept. of Physics, Univ. of Padova, Italy ROT13: vasa.vg
    Maurizio Guest

  11. #11

    Default Re: string concatenation

    Maurizio Loreti <it> writes:
     
    >>
    >> On the contrary, any C program using these identifiers with external
    >> linkage invokes undefined behaviour.[/ref]
    >
    > Maybe Mr. Burke misspelled "strncpy" and "strncat", mentioned in
    > ISO/IEC 9899:1999 under 7.21.2.4 and 7.21.3.2 ? Their prototypes (in
    > <string.h>) are
    >
    > char *strncpy(char * restrict, const char * restrict, size_t);
    > char *strncat(char * restrict, const char * restrict, size_t);[/ref]

    Maybe he meant those, but the non-standard strl* variants are more
    useful. The return values from the standard str* functions are
    utterly useless, since you already knew the value. Returning the
    number bytes copied is much more useful. BTW, str*cat should be used
    sparingly. In most cases you have, or can easily get (cheaper than
    scanning), a pointer to the end of the destination string.

    --
    Måns Rullgård
    se
    Måns Guest

  12. #12

    Default Re: string concatenation


    Maurizio Loreti <it> writes:
     
    > >
    > > On the contrary, any C program using these identifiers with external
    > > linkage invokes undefined behaviour.[/ref]
    >
    > Maybe Mr. Burke misspelled "strncpy" and "strncat", mentioned in
    > ISO/IEC 9899:1999 under 7.21.2.4 and 7.21.3.2 ? Their prototypes (in
    > <string.h>) are
    >
    > char *strncpy(char * restrict, const char * restrict, size_t);
    > char *strncat(char * restrict, const char * restrict, size_t);[/ref]

    No, I meant strlcpy/strlcat, and I find them to be better
    than the strn* calls. I'm astonished to learn that they
    aren't yet standardised.

    -SEan



    Sean Guest

  13. #13

    Default Re: string concatenation


    nl (Richard Bos) writes:
     
    > >
    > > Neither there!
    > > (http://www.opengroup.org/onlinepubs/007904975/basedefs/string.h.html)
    > >
    > > They ARE doented at http://www.courtesan.com/todd/papers/strlcpy.html
    > >
    > > My summary of that is
    > > size_t strlcpy(char *dst, const char *src, size_t size);
    > > Copy a string of up to size-1 bytes from src to dst. Return strlen(src).
    > >
    > > size_t strlcat(char *dst, const char *src, size_t size);
    > > Concatenate bytes from src to dst to form a string with strlen(dst) <
    > > size. Return strlen(dst) /* before concatenation */ + strlen(src).
    > >
    > > The functions allow buffer overflow elimination.[/ref]
    >
    > Nice, meaningless statement; so does strncat(), and so does using
    > strlen() and inserting a '\0' at the right place before using strcpy().

    >
    > No. Might be a nice addition, but I wouldn't describe them as vital.
    > Mind you, I won't complain if they _are_ added to C20XX.[/ref]

    Don't tell me that snprintf, vsnprintf() are also nonstandard?

    -SEan



    Sean Guest

  14. #14

    Default Re: string concatenation

    Sean Burke wrote:
    .... 

    No: see sections 7.19.6.5 and 7.19.6.12.
    James Guest

  15. #15

    Default Re: string concatenation

    On Fri, 13 Feb 2004 17:12:51 +0000, Sean Burke wrote:
     

    Not only are they not in std. C, they aren't available at all on any
    of the major Linux variants. They also are/were implemented subtly
    differently on Solaris.

    You probably also want to read:

    http://www.and.org/vstr/security.html#alloc

    --
    James Antill -- org
    Need an efficient and powerful string library for C?
    http://www.and.org/vstr/

    James Guest

  16. #16

    Default Re: string concatenation

    On Fri, 13 Feb 2004 10:19:47 +0000, Walter Briscoe wrote:

     

    All the doentation, including the above, is laughable compared to
    normal std. C text. Which is probably why there are a couple of different
    implementations and the two major non OpenBSD implementations are
    _doented_ to act differently than the OpenBSD versions do.
     

    IMO they are even more worthless than asprintf(), they are doented
    to act differently on different platforms and (unlike asprintf() you can
    still cause security issues due to truncation of data).
     

    The "undated" paper has references to papers published in 1998, and says:

    "The strlcpy() and strlcat() functions first appeared in OpenBSD 2.4."

    ....which dates them between May 19, and December 1, 1998. People
    _started_ to hear of them after the 1999 Usenix when the paper...

    http://www.usenix.org/events/usenix99/full_papers/millert/millert.pdf

    ....was presented, which even then as way too late for C99 ... and I don't
    remember hearing about them until 2000 (which was after the last std. was
    released).

    --
    James Antill -- org
    Need an efficient and powerful string library for C?
    http://www.and.org/vstr/

    James Guest

  17. #17

    Default Re: string concatenation

    James Antill <org> writes:
     

    They weren't implemented subtly different on Solaris; our implementation
    is a straightforward clone from the original OpenBSD version and behaves
    exactly the same.

    You may be confused with "snprintf()" where Solaris originally
    implemented it differently for "n = 0"; UNIX98 standardized
    snprintf that it should return a value <= 0 for n = 0 and we're
    a bit of a stickler for standards.

    UNIX03 fixes that, fortunately, so the Solaris release will
    have an snprintf which allows you to call it as follows:

    size_t len = snprintf(0, NULL, .....);

    to compute the length needed (note that snprintf can return
    "-1" and set errno to EILSEQ.

    Casper
    --
    Expressed in this posting are my opinions. They are in no way related
    to opinions held by my employer, Sun Microsystems.
    Statements on Sun products included here are not gospel and may
    be fiction rather than truth.
    Casper Guest

  18. #18

    Default Re: string concatenation

    James Antill <org> writes:
     


    Please cite the specific differences between the implementations;
    I don't know of any.


    Casper
    --
    Expressed in this posting are my opinions. They are in no way related
    to opinions held by my employer, Sun Microsystems.
    Statements on Sun products included here are not gospel and may
    be fiction rather than truth.
    Casper Guest

  19. #19

    Default Re: string concatenation

    On Sat, 14 Feb 2004 09:41:31 +0000, Casper H. S. Dik wrote:
     
    >
    > Please cite the specific differences between the implementations; I don't
    > know of any.[/ref]

    The "well known" one is the return value for strlcat(), see...

    http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/library-c.html#STRLCPY
    http://docs.sun.com/db/doc/806-0627/6j9vhfn7h?a=view#indexterm-1028
    http://docs.sun.com/db/doc/816-0213/6m6ne38cf?a=view#indexterm-1099

    ....the later two links might mean that this has changed for 2.9 but
    my reading of it doesn't suggest that. However even though glib commits
    said they followed the Solaris spec. the code is basically the same as
    that in OpenBSD, so i guess there's movement for Solaris to change.

    Of course there's always...

    http://www.oreillynet.com/pub/a/network/2003/05/20/secureprogckbk.html

    ....where the strlcat() function is just broken.

    --
    James Antill -- org
    Need an efficient and powerful string library for C?
    http://www.and.org/vstr/

    James Guest

  20. #20

    Default Re: string concatenation

    On Sat, 14 Feb 2004 09:37:56 +0000, Casper H. S. Dik wrote:
     
    >
    > They weren't implemented subtly different on Solaris; our implementation
    > is a straightforward clone from the original OpenBSD version and behaves
    > exactly the same.[/ref]

    That's what I would have expected, but I'd heard that they were
    reimplemented to follow the "spec" and the return value was different in
    the Solaris version[1].
    I don't have access to a Solaris machine to check, and I apologize if
    that wasn't the case.

    [1]

    http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/library-c.html#STRLCPY

    --
    James Antill -- org
    Need an efficient and powerful string library for C?
    http://www.and.org/vstr/

    James Guest

Page 1 of 2 12 LastLast

Similar Threads

  1. string concatenation
    By Rinoa in forum Macromedia ColdFusion
    Replies: 1
    Last Post: April 22nd, 01:05 PM
  2. String concatenation qn
    By Wolf Blaum in forum PERL Beginners
    Replies: 4
    Last Post: January 26th, 12:21 PM
  3. Replies: 5
    Last Post: September 7th, 11:02 PM
  4. String Concatenation Bug
    By ecow in forum PHP Development
    Replies: 1
    Last Post: August 18th, 02:14 AM
  5. String concatenation with .= <FH>
    By Jay Tilton in forum PERL Miscellaneous
    Replies: 3
    Last Post: July 13th, 01:53 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139