Red Antigua Logo
Yet another piece of web.
Ads by Goooooogle
Search this site (by Google)
Tools    (top)
Your IP
Check a site for broken links
(W3C)

Perl modules    (top)
Tree::Numbered::Tools
(CPAN)
Perl tutorials    (top)
Perl modules
HTML::Template
CGI::Application
Mail::POP3Client
Mail::Send
MIME::Tools
Cookies with CGI::Application
Upload files with CGI::Application
Download files with CGI::Application
Redirect with CGI::Application
CPAN shell
Install DBD::mysql from the CPAN shell
Perl trim function
Validate an IP with Perl
Run suid Perl scripts under Apache
Perl taint mode
Perl date functions with Date::Calc

In Spanish
Curso de Perl

C tutorials    (top)
C - Introduction
C - Absolute beginner's Emacs
C - Examples for beginners
C - Makefile examples
C - Autotools examples
Server configurations    (top)
DNS
Apache
Apache Authentication and Access Control
mod_perl on FreeBSD
MySQL
MySQL add account
phpMyAdmin
Squid
DHCP

UNIX on Windows    (top)
Apache setup on Windows
MySQL setup on Windows
PHP setup on Windows
Perl setup on Windows
Emacs setup on Windows
UnxUtils
PuTTY
WinSCP
GIMP on Windows
MinGW - gcc on Windows
MSYS - UNIX-styled shell on Windows
msysDTK - autotools on Windows
GDB for MinGW on Windows

Misc. FreeBSD/UNIX    (top)
CD and DVD creation on FreeBSD using 'k3b' on FreeBSD
'portupgrade' on FreeBSD
'ipf' on FreeBSD
'pf' on FreeBSD
'su' on FreeBSD
Mount an ISO image under FreeBSD
Load the correct sound driver under FreeBSD without knowing what sound card you are using
Simultaneous sound channels on FreeBSD
FreeBSD network stuff
DOS-to-UNIX file conversion
favicon.ico on UNIX
Emacs tips
Sendmail tips
GKrellm
Command Line Calculator
Save multimedia streams with 'mplayer'
xargs - solution to 'Argument list too long'
Process multiple images from the command line using 'ImageMagick'
Turn the system bell off under X Windows
Process each line in an input file from the command line (or in a shell script)
How to keep a program running in the background using 'nohup'
How to remove symbolic links in the current directory using 'find' and 'rm'
How to remove Emacs backup files in the current directory and all subdirectories using 'find' and 'rm'
How to execute .profile without logging in
Configure X to handle non-English characters
How to move /var to /usr/var

Redirect a web page    (top)
Redirect to another web page
Apache redirect
C redirect
Perl redirect
PHP redirect
HTML redirect
JavaScript redirect

Javascript    (top)
Trim function
Login form
Register form
Popup window

Virus list    (top)
Latest 10 viruses - Sophos

Off topic    (top)
Personal home links

C - copy-strings.c
C - copy-strings.c
IndexPreviousNext
Using char pointers:
#include <stdio.h>
/* Compile: cc -g -ansi -pedantic -Wall -O2 -o copy-strings copy-strings.c */
/* Run: ./copy-strings */
int
main(int argc, char *argv[])
{
  char *program_name;
  char *your_name;
  program_name = argv[0];
  if (argc != 2) 
    {
      printf("Usage: %s <your_name>\n", program_name);
      return 1;
    }
  printf("This program's name is %s.\n", program_name);
  your_name = argv[1];
  printf("Your name is '%s'.\n", your_name);
  return 0;
}
Using char arrays and strcpy(3):
#include <stdio.h>
#include <string.h>
/* Compile: cc -g -Wall -o copy-strings copy-strings.c */
/* Run: ./copy-strings */
int
main(int argc, char *argv[])
{
  char program_name[100];
  char your_name[100];
  strcpy(program_name, argv[0]);
  if (argc != 2) 
    {
      printf("Usage: %s <your_name>\n", program_name);
      return 1;
  }
  printf("This program's name is %s.\n", program_name);
  strcpy(your_name, argv[1]);
  printf("Your name is '%s'.\n", your_name);
  return 0;
}

Using strncpy(3) is consider safer than strcpy(3):
(for details see the man page, section 'SECURITY CONSIDERATIONS'):
#include <stdio.h>
#include <string.h>
/* Compile: cc -g -ansi -pedantic -Wall -O2 -o copy-strncpy copy-strncpy.c */
/* Run: ./copy-strncpy */

#define MAX_LEN_PROGRAM_NAME 100
#define MAX_LEN_YOUR_NAME 10

int
main(int argc, char *argv[])
{
  char program_name[MAX_LEN_PROGRAM_NAME], your_name[MAX_LEN_YOUR_NAME];

  /* Command line validation. */
  if (argc != 2)
  {
      printf("Usage: %s <your_name>\n", argv[0]);
      if (argc == 1)
      {
          printf("Tip: Add your name as the 2nd argument. ;-)\n");
      }
      return 1;
  }
  if (strlen(argv[0]) > MAX_LEN_PROGRAM_NAME)
  {
      /* Error: program name is too long. */
      printf("Error: The program name must be %i characters or less.\n", MAX_LEN_PROGRAM_NAME);
      return 1;
  }
  if (strlen(argv[1]) > MAX_LEN_YOUR_NAME)
  {
      /* Error: your name is too long. */
      printf("Sorry, this program only accepts names shorter than %i characters.\n", MAX_LEN_YOUR_NAME);
      return 1;
  }
  /* Copy program name string. */
  (void)strncpy(program_name, argv[0], sizeof(program_name) - 1);
  /* NULL-terminate string. */
  program_name[sizeof(program_name) - 1] = '\0';
  /* Print program name. */
  printf("This program's name is %s.\n", program_name);
  /* Copy your name string. */
  (void)strncpy(your_name, argv[1], sizeof(your_name) - 1);
  /* NULL-terminate string. */
  your_name[sizeof(your_name) - 1] = '\0';
  /* Print your name. */
  printf("Your name is '%s'.\n", your_name);
  printf("Hello, %s!\n", your_name);
  return 0;
}

Using strlcpy(3) is considered even safer than both strcpy(3) and strncpy(3), but less portable between platforms:
#include <stdio.h>
#include <string.h>
/* Compile: cc -g -ansi -pedantic -Wall -O2 -o copy-strlcpy copy-strlcpy.c */
/* Run: ./copy-strlcpy */

#define MAX_LEN_PROGRAM_NAME 100
#define MAX_LEN_YOUR_NAME 10

int
main(int argc, char *argv[])
{
  char program_name[MAX_LEN_PROGRAM_NAME], your_name[MAX_LEN_YOUR_NAME];
  if (strlcpy(program_name, argv[0], sizeof(program_name)) >= sizeof(program_name))
    {
      /* Error: destination string 'program_name' was truncated. */
      printf("Error: The program name must be %i characters or less.\n", MAX_LEN_PROGRAM_NAME);
      return 1;
  }
  if (argc != 2)
    {
      printf("Usage: %s <your_name>\n", program_name);
      return 1;
    }
  printf("This program's name is %s.\n", program_name);
  if (strlcpy(your_name, argv[1], sizeof(your_name)) >= sizeof(your_name))
    {
      /* Error: destination string 'your_name' was truncated. */
      printf("Sorry, this program only accepts names shorter than %i characters.\n", MAX_LEN_YOUR_NAME);
      return 1;
    }
  printf("Your name is '%s'.\n", your_name);
  return 0;
}
IndexPreviousNext
Last modified: Thu Oct 11 17:51:53 Romance Daylight Time 2007