Sponsored Content
Top Forums Programming Newline in ANSI-C standard functions Post 303020298 by Corona688 on Monday 16th of July 2018 11:30:53 AM
Old 07-16-2018
Stop that! That's not how you use strncpy! And also not why people use it.

Blindly using strncpy because people call strcpy "bad" is worse than just using strcpy in the first place. You are not correcting the risks strcpy allows (unlimited input lengths despite limited buffer size) and causing problems strcpy didn't have had in the first place.

Grasp the basics first and you'll understand what they're talking about.

Last edited by Corona688; 07-16-2018 at 12:40 PM..
This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. Programming

Ansi C

Dear All, I have to develope some C functions in Unix for a Magic program. The original MSE code which compiles the attached C program uses a +z option, but the cc compiler don't know this. The complete command in the compiler script is 'cc -c -Aa +z myfile.c'. The warning message is 'The -z... (4 Replies)
Discussion started by: Frankie
4 Replies

2. Programming

ANSI C vs POSIX

can somebody explain about the ANSI C vs POSIX. say i was using open and fopen, i know that open is POSIX, and fopen is ANSI C. i read that that POSIX is a system call and ANSI C is like a standard library function. wouldn't the fopen function has to call on open function anyway to open any kind... (2 Replies)
Discussion started by: bb00y
2 Replies

3. Shell Programming and Scripting

Convert file from Unix - ANSI to PC - ANSI

Hi, I am creating a file in Unix using a shell script. The file is getting created in the Unix - ANSI format. My requirement is to convert it to the PC - ANSI format. Can anyone tell me how to do this? Thanks, Sunil (0 Replies)
Discussion started by: ssmallya
0 Replies

4. Programming

using c++ and c standard I/O functions

Is it not a healthy practice to mix C and C++ standard I/O functions together e.g. string name; // this is a declared instance of the string class in C++ printf("\nPlease enter your name: "); cin >> name; I did something similar in a program Im designing, and used it several... (1 Reply)
Discussion started by: JamesGoh
1 Replies

5. Programming

Standard UNIX functions

Hi everybody, first of all i apologize if my thread's title doesn't make much sense,but i coudn't find a more appropriate name :) Then i apologize about my question,which probably will sound trivial for you :) :) I am working on a program which is being tested in Linux but the final target is... (2 Replies)
Discussion started by: Zipi
2 Replies

6. Shell Programming and Scripting

standard error to standard out question

Hi there how can i get the result of a command to not give me its error. For example, on certain systems the 'zfs' command below is not available, but this is fine becaues I am testing against $? so i dont want to see the message " command not found" Ive tried outputting to /dev/null 2>&1 to no... (5 Replies)
Discussion started by: hcclnoodles
5 Replies

7. UNIX for Dummies Questions & Answers

Redirect Standard output and standard error into spreadsheet

Hey, I'm completely new at this and I was wondering if there is a way that I would be able to redirect the log files in a directories standard output and standard error into and excel spreadsheet in anyway? Please remember don't use too advanced of terminology as I just started using shell... (6 Replies)
Discussion started by: killaram
6 Replies

8. Shell Programming and Scripting

Standard out and standard error

I need to run a cronjob and in the cronjob I execute a script that if there is an error produces standard error so I do /RUNMYSCRIPT 2> mylogfile.log However, if it runs correctly, I don't get a standard error output, I get a standard out output. How do I redirect both standard error and... (2 Replies)
Discussion started by: guessingo
2 Replies

9. Programming

why the implementatoin of Bakery algorithm in ANSI C does not work in ANSI C

I follow the description of wiki (Lamport's bakery algorithm - Wikipedia, the free encyclopedia), then implement that algorithm in C, but it doesn't work, Starving is still here, is the implementation worry? Only print out: Thread ID: 0 START! Thread ID: 0 END! Thread ID: 0 START!... (2 Replies)
Discussion started by: sehang
2 Replies

10. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies
STRCPY(3)						   BSD Library Functions Manual 						 STRCPY(3)

NAME
stpcpy, stpncpy, strcpy, strncpy -- copy strings LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <string.h> char * stpcpy(char *dst, const char *src); char * stpncpy(char *restrict dst, const char *restrict src, size_t n); char * strcpy(char *restrict dst, const char *restrict src); char * strncpy(char *restrict dst, const char *restrict src, size_t n); DESCRIPTION
The stpcpy() and strcpy() functions copy the string src to dst (including the terminating '' character). The stpncpy() and strncpy() functions copy at most n characters from src into dst. If src is less than n characters long, the remainder of dst is filled with '' characters. Otherwise, dst is not terminated. The source and destination strings should not overlap, as the behavior is undefined. RETURN VALUES
The strcpy() and strncpy() functions return dst. The stpcpy() and stpncpy() functions return a pointer to the terminating '' character of dst. If stpncpy() does not terminate dst with a NUL character, it instead returns a pointer to dst[n] (which does not necessarily refer to a valid memory location.) EXAMPLES
The following sets chararray to ``abc'': char chararray[6]; (void)strncpy(chararray, "abc", sizeof(chararray)); The following sets chararray to ``abcdef'': char chararray[6]; (void)strncpy(chararray, "abcdefgh", sizeof(chararray)); Note that it does not NUL terminate chararray, because the length of the source string is greater than or equal to the length argument. The following copies as many characters from input to buf as will fit and NUL terminates the result. Because strncpy() does not guarantee to NUL terminate the string itself, this must be done explicitly. char buf[1024]; (void)strncpy(buf, input, sizeof(buf) - 1); buf[sizeof(buf) - 1] = ''; This could be better achieved using strlcpy(3), as shown in the following example: (void)strlcpy(buf, input, sizeof(buf)); SECURITY CONSIDERATIONS
The strcpy(), strncpy(), stpcpy(), and stpncpy() functions are easily misused in a manner which enables malicious users to arbitrarily change a running program's functionality through a buffer overflow attack. (See the FSA and EXAMPLES.) It is recommended that strlcpy(3) be used instead as a way to avoid such problems. strlcpy(3) is not defined in any standards, but it has been adopted by most major libc implementations. SEE ALSO
bcopy(3), memccpy(3), memcpy(3), memmove(3), strlcpy(3), wcscpy(3) STANDARDS
The strcpy() and strncpy() functions conform to ISO/IEC 9899:1990 (``ISO C90''). The stpcpy() and stpncpy() functions conform to IEEE Std 1003.1-2008 (``POSIX.1''). HISTORY
The stpcpy() function first appeared in FreeBSD 4.4, and stpncpy() was added in FreeBSD 8.0. BSD
February 28, 2009 BSD
All times are GMT -4. The time now is 11:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy