1

I was wondering if strcpy or strcat like functions causes any system call or they are handled internally by the OS?

Asish Biswas
  • 221
  • 1
  • 4
  • 17
  • 1
    When in doubt, you can run your binary through `strace` which will show you system calls that the binary performs. – smani Feb 07 '14 at 14:27
  • 1
    yes. i did that. and strace doesn't show any system call for those functions. that made me ask you guys... how this kind of operation is handled? is there any way to catch those events? – Asish Biswas Feb 07 '14 at 14:31
  • 4
    You can try the [ltrace](http://ltrace.alioth.debian.org/) for library traces. The str functions don't make system calls. – Keith Feb 07 '14 at 14:33

3 Answers3

6

No system call is involved. In fact, the source code of most if not all implementations would look like this:

char * 
strcpy(char *s1, const char *s2) { 
    char *s = s1; 
    while ((*s++ = *s2++) != 0) ;
    return (s1);
}

strcat is similar:

char *
strcat(char *s1, const char *s2)
{
    strcpy(&s1[strlen(s1)], s2);
    return s1;
}
Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
4

On Linux, those calls are implemented by the standard library (and those are part of the standard C library). See also glibc. System calls are invocations from user code to kernel code for hardware access (e.g. memory allocation); they are accomplished with an interrupt 0x80.

Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
4

No OS calls are REQUIRED for such simple operations - they can be performed easily in the libraries.

Note that the OS may be entered during such calls, eg. because they generate a page-fault or some other hardware interrupt occurs.

Martin James
  • 24,453
  • 3
  • 36
  • 60