forked from gsoosk/POS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.c
More file actions
55 lines (50 loc) · 1.15 KB
/
sort.c
File metadata and controls
55 lines (50 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "types.h"
#include "stat.h"
#include "fcntl.h" // for using file defines
#include "user.h" // for using from strlen
void insertionSort(char *arr[], int n)
{
int i, j;
char *key;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;
while (j >= 0 && atoi(arr[j]) > atoi(key))
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
int main(int argc, char *argv[])
{
if(argc != 6){
printf(2, "sort: enter 5 numbers please.\n");
exit();
}
int i;
char* nums[5];
for(i = 0 ; i < 5 ; i++)
nums[i] = argv[i+1];
insertionSort(nums, 5);
printf(2,"proccess id is %d \n", getpid());
int fd;
if((fd = open("sorted.txt", O_CREATE | O_WRONLY)) < 0){
printf(2, "sort: cannot open file");
exit();
}
for(i = 0 ; i < 5 ; i++)
{
if(write(fd, nums[i], strlen(nums[i])) != strlen(nums[i])) {
printf(2, "sort: write error\n");
exit();
}
if(write(fd, "\n", 1) != 1) {
printf(2, "sort: write error\n");
exit();
}
}
exit();
}