Sponsored Content
Top Forums Programming Copy a file using UNIX-system calls Post 302300916 by shamrock on Wednesday 25th of March 2009 11:26:38 AM
Old 03-25-2009
Quote:
Originally Posted by c_d
Code:
sourcefile=open(argv[1],O_RDONLY,0664);

That's not the preferred way to specify file modes in UNIX. By not giving it the O_CREAT file it means that the file exists and the open call will have no affect on it and instead of giving 0644 for the file mode bits use...
Code:
sourcefile=open(argv[1], O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);

That's for creating and opening a file that does not exist because if it does then the open call fails so should check its return value.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

System Calls

What does the system call "dup" do? What is the difference between dup and dup2 I have a fair idea of what it does but I am confused when its coming down to the exact details... Please help me!:confused: (2 Replies)
Discussion started by: clickonline1
2 Replies

2. UNIX for Dummies Questions & Answers

System calls for cp and mv

Which system calls are made for operations cp and mv (2 Replies)
Discussion started by: gaurava99
2 Replies

3. UNIX for Dummies Questions & Answers

System calls?

open, creat, read, write, lseek and close Are they all primitive? :confused: *Another Question: is there a different between a system call, and an i/o system call? (2 Replies)
Discussion started by: PlunderBunny
2 Replies

4. Solaris

System calls ?

where can i find the differences in System calls between solaris and aix? also is it possible to find a comprehensive list of them? (1 Reply)
Discussion started by: TECHRAMESH
1 Replies

5. UNIX Desktop Questions & Answers

Using system calls

Hi, I'm new to UNIX system calls. Can someone share your knowledge as to how exactly system calls should be executed? Can they be typed like commands such as mkdir on the terminal itself? Also, are there any websites which will show me an example of the output to expect when a system call like... (1 Reply)
Discussion started by: ilavenil
1 Replies

6. UNIX for Dummies Questions & Answers

About system calls.

Hi all, I am new here . I want to know about system call in detail. As system calls are also function .How system identifies it.:) (2 Replies)
Discussion started by: vishwasrao
2 Replies

7. UNIX for Dummies Questions & Answers

system calls in C

Hello, how would i be able to call ps in C programming? thanks, ---------- Post updated at 01:39 AM ---------- Previous update was at 01:31 AM ---------- here's the complete system call, ps -o pid -p %d, getpit() (2 Replies)
Discussion started by: l flipboi l
2 Replies

8. UNIX for Dummies Questions & Answers

Unix directory system calls question

I'm currently studying for my exam, and is practicing with sample exam questions. However there is a question asking "Name THREE UNIX Directory system calls" and the answer given is "opendir, closedir and readdir", however the next question ask "Why is a write directory system call not included... (1 Reply)
Discussion started by: Izzy123
1 Replies

9. UNIX for Dummies Questions & Answers

How to Export Glance "Global System Calls" data to a file

Hello... I'm trying to setup a cronjob to record system data using glance at certain times of the day. My question is, how would one export the "Global System Calls" information to a file? Below is the command I have been using and it works to export CPU information. glance -f ... (0 Replies)
Discussion started by: fumus
0 Replies

10. UNIX for Dummies Questions & Answers

System calls in UNIX

Hi i am very new to programming in UNIX and don't understand the difference between a system call and a normal function call. Also can I implement system calls from within a program? If so could someone please give me an example of a system call from within a program. Lastly, when creating a... (1 Reply)
Discussion started by: bjhum33
1 Replies
shm_open(3C)						   Standard C Library Functions 					      shm_open(3C)

NAME
shm_open - open a shared memory object SYNOPSIS
#include <sys/mman.h> int shm_open(const char *name, int oflag, mode_t mode); DESCRIPTION
The shm_open() function establishes a connection between a shared memory object and a file descriptor. It creates an open file description that refers to the shared memory object and a file descriptor that refers to that open file description. The file descriptor is used by other functions to refer to that shared memory object. The name argument points to a string naming a shared memory object. It is unspeci- fied whether the name appears in the file system and is visible to other functions that take pathnames as arguments. The name argument con- forms to the construction rules for a pathname. The first character of name must be a slash (/) character and the remaining characters of name cannot include any slash characters. For maximum portability, name should include no more than 14 characters, but this limit is not enforced. If successful, shm_open() returns a file descriptor for the shared memory object that is the lowest numbered file descriptor not currently open for that process. The open file description is new, and therefore the file descriptor does not share it with any other processes. It is unspecified whether the file offset is set. The FD_CLOEXEC file descriptor flag associated with the new file descriptor is set. The file status flags and file access modes of the open file description are according to the value of oflag. The oflag argument is the bitwise inclusive OR of the following flags defined in the header <fcntl.h>. Applications specify exactly one of the first two values (access modes) below in the value of oflag: O_RDONLY Open for read access only. O_RDWR Open for read or write access. Any combination of the remaining flags may be specified in the value of oflag: O_CREAT If the shared memory object exists, this flag has no effect, except as noted under O_EXCL below. Otherwise the shared memory object is created; the user ID of the shared memory object will be set to the effective user ID of the process; the group ID of the shared memory object will be set to a system default group ID or to the effective group ID of the process. The permission bits of the shared memory object will be set to the value of the mode argument except those set in the file mode creation mask of the process. When bits in mode other than the file permission bits are set, the effect is unspecified. The mode argument does not affect whether the shared memory object is opened for reading, for writing, or for both. The shared memory object has a size of zero. O_EXCL If O_EXCL and O_CREAT are set, shm_open() fails if the shared memory object exists. The check for the existence of the shared memory object and the creation of the object if it does not exist is atomic with respect to other processes executing shm_open() naming the same shared memory object with O_EXCL and O_CREAT set. If O_EXCL is set and O_CREAT is not set, the result is undefined. O_TRUNC If the shared memory object exists, and it is successfully opened O_RDWR, the object will be truncated to zero length and the mode and owner will be unchanged by this function call. The result of using O_TRUNC with O_RDONLY is undefined. When a shared memory object is created, the state of the shared memory object, including all data associated with the shared memory object, persists until the shared memory object is unlinked and all other references are gone. It is unspecified whether the name and shared memory object state remain valid after a system reboot. RETURN VALUES
Upon successful completion, the shm_open() function returns a non-negative integer representing the lowest numbered unused file descriptor. Otherwise, it returns -1 and sets errno to indicate the error condition. ERRORS
The shm_open() function will fail if: EACCES The shared memory object exists and the permissions specified by oflag are denied, or the shared memory object does not exist and permission to create the shared memory object is denied, or O_TRUNC is specified and write permission is denied. EEXIST O_CREAT and O_EXCL are set and the named shared memory object already exists. EINTR The shm_open() operation was interrupted by a signal. EINVAL The shm_open() operation is not supported for the given name. EMFILE Too many file descriptors are currently in use by this process. ENAMETOOLONG The length of the name string exceeds PATH_MAX, or a pathname component is longer than NAME_MAX while _POSIX_NO_TRUNC is in effect. ENFILE Too many shared memory objects are currently open in the system. ENOENT O_CREAT is not set and the named shared memory object does not exist. ENOSPC There is insufficient space for the creation of the new shared memory object. ENOSYS The shm_open() function is not supported by the system. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ |ATTRIBUTE TYPE |ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Committed | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ |Standard |See standards(5). | +-----------------------------+-----------------------------+ SEE ALSO
close(2), dup(2), exec(2), fcntl(2), mmap(2), umask(2), shm_unlink(3C), sysconf(3C), fcntl.h(3HEAD), attributes(5), standards(5) NOTES
Solaris 2.6 was the first release to support the Asynchronous Input and Output option. Prior to this release, this function always returned -1 and set errno to ENOSYS. SunOS 5.11 5 Feb 2008 shm_open(3C)
All times are GMT -4. The time now is 07:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy