Sponsored Content
Homework and Emergencies Homework & Coursework Questions Using C language to use system calls Post 302734105 by femchi on Wednesday 21st of November 2012 03:09:13 PM
Old 11-21-2012
Using C language to use system calls

I need to write a C program (not a C++ program) that allows some tasks to be preformed automatically from a menu-driven interface. Automate the following tasks:
1. Rename a file
2. Delete a file or a directory
3. Output Process Information
4. Execute “mode 644 ls -l”
5. Exit

I need to know what steps should I follow to achieve the result. is it possible to ask the user to enter all the names and commands in 1 line or I should ask him one by one.
thank you

Code:
#include <stdio.h>
#include <fcntl.h>
#include <sys/errno.h>

int main (void) 
{
	int ch;
	char oldfn;
	char newfn;
	
	printf("\tMenu:\n");
	printf("\t1: Rename a file\n");
	printf("\t2: Delete a file or a directory\n");
	printf("\t3: Output Process Information\n");
	printf("\t4: Execute \"mode 644 ls -l\"\n");
	printf("\t5: Exit\n");
	printf("\t Enter your choice: \n");
	scanf("%d",&ch); 
	do
	{
		switch (ch)
		{
			case 1:printf("Enter the name of file and the new name :\n");
			printf("old file name: ");
			scanf("%s",&oldfn);
			printf("new file name: ");
			scanf("%s",&newfn);
			if ( rename(const char *oldfn, const char *newfn) == 0  )
			{
				printf("file is renamed");
			}
			break;
			case 2:printf("case 2\n");
			break;
			case 3:printf("case 3\n");
			break;
			case 4:printf("case 4\n");
			break;
			case 5:printf("exit\n");
			break;
			default:printf("Enter a number between 1-5\n");
			exit(0);
		}
		scanf("%d",&ch);
		
	}
	while ((ch>=1)||(ch<=5));
		
}

Langara college, Vancouver, Canada - Miss Hengame Hamavand - CPSC 1280
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. Programming

System calls

why user is not able to switch from user to kernel mode by writing the function whose code is identical to system call. (1 Reply)
Discussion started by: joshighanshyam
1 Replies

6. BSD

system calls

what is the functions and relationship between fork,exec,wait system calls as i am a beginer just want the fundamentals. (1 Reply)
Discussion started by: sangramdas
1 Replies

7. 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

8. 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

9. Programming

Are system calls in c language only????

Hi friends, I have three questions. 1) What are system calls? 2) Is it necessary that system calls be in c language (in unix operating system)? 3) Importance of c language when programming in unix environment??? Looking forward to your wonderful replies! ... (2 Replies)
Discussion started by: gabam
2 Replies

10. Programming

System calls and C language low-level qualities???

Hi friends, I hope everyone is fine and doing well. I queried in my previous thread about the low-level qualities of C/C++ languages.I really thank you people for explaining, it was really helpful. One more ambiquity that I have in my mind is regarding the unix system calls like open, creat,... (1 Reply)
Discussion started by: gabam
1 Replies
memalloc_attr(3)					     Library Functions Manual						  memalloc_attr(3)

NAME
memalloc_attr - Query the memory allocation policy and attributes (libnuma library) SYNOPSIS
#include <numa.h> int memalloc_attr( vm_offset_t va, memalloc_attr_t *attr ); PARAMETERS
The user virtual address for which the memory allocation policy is requested. Points to a buffer to receive the memory allocation policy and attributes for the page containing the specified virtual address. DESCRIPTION
The memalloc_attr() function returns the current memory allocation policy and associated attributes in the buffer pointed to by attr for the address specified by va. If radset information about the memory allocation policy is desired, a radset must be allocated through the radsetcreate() function, and the mattr_radset element of the attr argument must point to that radset. Otherwise, a 0 must be specified for the mattr_radset. EXAMPLE
#include <numa.h> main() { vm_offset_t va; memalloc_attr_t attr; int id; int flags = SET_CURSOR_CONSUME; rad_cursor_t cursor = SET_CURSOR_INIT; radsetcreate(&attr.mattr_radset); va = (vm_offset_t)&attr; /* no policy in effect - return zeroes */ if (memalloc_attr(va, &attr) == -1) { perror("memalloc_attr"); radsetdestroy(&attr.mattr_radset); return 0; } printf("mattr_policy = 0x%lx ", attr.mattr_policy); printf("mattr_rad = 0x%lx ", attr.mattr_rad); printf("mattr_stride = 0x%lx ", attr.mattr_stride); printf("mattr_distance = 0x%lx ", attr.mattr_distance); printf("mattr_pagesz = 0x%lx ", attr.mattr_pagesz); /* set policy */ attr.mattr_policy = MPOL_DIRECTED; attr.mattr_rad = 0; if (nmadvise((void *)va, sizeof(memalloc_attr_t), 0, &attr) == -1) { perror("nmadvise"); radsetdestroy(&attr.mattr_radset); return 0; } if (memalloc_attr(va, &attr) == -1) { perror("memalloc_attr"); radsetdestroy(&attr.mattr_radset); return 0; } printf("mattr_policy = 0x%lx ", attr.mattr_policy); printf("mattr_rad = 0x%lx ", attr.mattr_rad); printf("mattr_stride = 0x%lx ", attr.mattr_stride); printf("mattr_distance = 0x%lx ", attr.mattr_distance); printf("mattr_pagesz = 0x%lx ", attr.mattr_pagesz); /* enumerate the mattr_radset */ printf(" Enumerating radset members: "); while ((id = rad_foreach(attr.mattr_radset, flags, &cursor)) != RAD_NONE) { if ((id % 8) == 0) printf(" "); printf("%3d, ", id); } printf(" "); } RETURN VALUES
Success. In this case, the function stores the requested memory allocation policy and attributes in the buffer pointed to by attr. If no memory allocation policy has been set for the specified virtual address (e.g., madvise() or nmadvise()) not called for that address), a zeroed attr structure is returned. Failure. In this case, the function sets errno to indicate the error. ERRORS
If the memalloc_attr() function fails, it sets errno to one of the following values: The address pointed to by va, attr, or mattr_radset is invalid. The mattr_radset element of the attr argument points to an invalid RAD set, possibly one that has not been created by a rad- setcreate() call. SEE ALSO
Functions: numa_intro(3) Files: numa_types(4) memalloc_attr(3)
All times are GMT -4. The time now is 08:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy