Sponsored Content
Top Forums Programming Newline in ANSI-C standard functions Post 303020299 by yifangt on Monday 16th of July 2018 11:49:35 AM
Old 07-16-2018
Thanks Jim and Corona:
I was not sure the NULL terminator was handled correctly.
If str1 precedes str2 in memory and each one is word-aligned, then the last character you write to str1[128] effectively cause the end of of str1 to be the actual end of str2. This is quite twisting to me!! Now my understanding come to this:

1) Doing strncpy() first caused no NUL terminator, and gave me buffer overflow and strcpy() could not run at all.
2) Instead, doing strcpy() first the correct NUL terminator is ensured, then the following strncpy() seems working which still does not provide NUL terminator. But, because the program exits, just the problem did not show up.

Is this correct?
Blindly using strncpy because people call strcpy "bad" is worse than just using strcpy in the first place.

I do not know the risk of strcpy() or the correct use of strncpy(). They just happened to come to my exercise. I thought figure out the details may help understanding what's going on in the memory, which is why I tried memset(). Thanks again.

Last edited by yifangt; 07-16-2018 at 01:06 PM..
 

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
PCC(1)							      General Commands Manual							    PCC(1)

NAME
pcc - APE C compiler driver SYNOPSIS
pcc [ option ... ] [ name ... ] DESCRIPTION
Pcc compiles and loads C programs, using APE (ANSI C/POSIX) include files and libraries. Named files ending with .c are preprocessed with cpp(1), then compiled with one of the compilers described in 2c(1), as specified by the environment variable $objtype. The object files are then loaded using one of the loaders described in 2l(1). The options are: -o out Place loader output in file out instead of the default 2.out, v.out, etc. -P Omit the compilation and loading phases; leave the result of preprocessing name.c in name.i. -c Omit the loading phase. -p Insert profiling code into the executable output. -w Print compiler warning messages. -B Don't complain about functions used without ANSI function prototypes. -v Echo the preprocessing, compiling, and loading commands before they are executed. -Dname=def -Dname Define the name to the preprocessor, as if by If no definition is given, the name is defined as -Uname Undefine the name to the preprocessor, as if by -Idir files whose names do not begin with are always sought first in the directory of the file argument, then in directories named in -I options, then in /$objtype/include/ape. -N Don't optimize compiled code. -S Print an assembly language version of the object code on standard output. -a Instead of compiling, print on standard output acid functions (see acid(1)) for examining structures declared in the source files. -aa Like -a except that functions for structures declared in included header files are omitted. The APE environment contains all of the include files and library routines specified in the ANSI C standard (X3.159-1989), as well as those specified in the IEEE Portable Operating System Interface standard (POSIX, 1003.1-1990, ISO 9945-1). In order to access the POSIX rou- tines, source programs should define the preprocessor constant _POSIX_SOURCE. FILES
/sys/include/ape directory for machine-independent #include files. /$objtype/include/ape directory for machine-dependent #include files. /$objtype/lib/ape/libap.a ANSI C/POSIX library. SEE ALSO
cpp(1), 2c(1), 2a(1), 2l(1), mk(1), nm(1), acid(1), db(1), prof(1) Howard Trickey, ``APE -- The ANSI/POSIX Environment'' SOURCE
/sys/src/cmd/pcc.c BUGS
The locale manipulation functions are minimal. Signal functions and terminal characteristic handlers are only minimally implemented. Link always fails, because Plan 9 doesn't support multiple links to a file. The functions related to setting effective user and group ids can- not be implemented because the concept doesn't exist in Plan 9. PCC(1)
All times are GMT -4. The time now is 08:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy