SATOHOST
No Result
View All Result
Thứ Bảy, Tháng Một 16, 2021
  • Login
  • vps giá rẻ
  • cpanel nulled
  • bitcoin exchange script
  • Home 1
Subscribe
SATOHOST
  • vps giá rẻ
  • cpanel nulled
  • bitcoin exchange script
  • Home 1
No Result
View All Result
SATOHOST
No Result
View All Result
Home Blog

HowTo Apply a Patch File To My Linux / UNIX Source Code

admin by admin
Tháng Sáu 3, 2019
in Blog
5 min read
0
152
SHARES
1.9k
VIEWS
Share on FacebookShare on Twitter

I am a new Linux and Unix system user. I also know that I can patch binary package using up2date or yum command in Linux. I was wondering is if there’s a way to apply a patch file to downloaded source code on a Linux / UNIX like operating system source tree?

Linux and UNIX source software often comes with security and other patches. You can download them from Internet or project home page. There is a command called patch that apply a diff file or patch to an original source tree.
[donotprint][/donotprint]

The patch command takes a patch file patchfile containing a difference listing produced by the diff program and applies those differences to one or more original files, producing patched versions. Normally the patched versions are put in place of the originals.

patch command syntax

The basic syntax is as follows:
$ patch < patch.file $ patch source.code.file < patch.file $ patch -p LEVEL < {/path/to/patch/file}

To apply a patch, one could run the following command in a shell:
$ patch < /path/to/file

In this example, patch foo.c with patch.diff file:
$ patch foo.c < patch.diff

Patches can be undone, or reversed, with the '-R' option:
$ patch -R < /path/to/file

How do I create a patch?

To create a patch, one could run the following diff command:
$ diff -u oldfile-name-here newfile-name-here > patch.diff

Example: Creating and applying the patch for hello.c sample program on a Linux or Unix like system

Create a hello.c as follows:

#include<stdio.h>
 
main(){
	printf("Hello, world!n");
}

#include<stdio.h> main(){
printf("Hello, world!n");
}

Make a copy of the hello.c (or file you wish to modify); both files must be in the same directory, though it may be any directory using cp command:
$ cp hello.c hello-new.c
Edit the file hello-new.c to make it as you want it. In this example, I am fixing a few compiler warning by adding return value from main():

#include<stdio.h>
 
int main(void){
	printf("Hello, world!n");
	return 0;
}

#include<stdio.h> int main(void){
printf("Hello, world!n");
return 0;
}

Next, use command diff to create a unified diff patch file called hello.patch:
$ diff -u hello.c hello-new.c > hello.patch
To see patch use cat command as follows:
$ cat hello.patch
Sample outputs:

--- hello.c	2014-04-29 17:59:49.000000000 +0530
+++ hello-new.c	2014-04-29 18:00:43.000000000 +0530
@@ -1,5 +1,6 @@
 #include<stdio.h>
 
-main(){
+int main(void){
 	printf("Hello, world!n");
+	return 0;
 }

--- hello.c 2014-04-29 17:59:49.000000000 +0530
+++ hello-new.c 2014-04-29 18:00:43.000000000 +0530
@@ -1,5 +1,6 @@
#include<stdio.h>
-main(){
+int main(void){
printf("Hello, world!n");
+ return 0;
}

To apply the patch from hello.patch, enter:

### The hello.patch patchfile knows the name of the file to be patched ##
patch < hello.patch

### The hello.patch patchfile knows the name of the file to be patched ##
patch < hello.patch

Sample outputs:

patching file hello.c

Finally, here is my updated and patched hello.c:
$ cat hello.c
Sample outputs:

#include<stdio.h>
 
int main(void){
	printf("Hello, world!n");
	return 0;
}

#include<stdio.h> int main(void){
printf("Hello, world!n");
return 0;
}

You can now compile program:
$ cc hello.c -o hello
Run it as follows:
$ ./hello
Sample outputs:

Hello, world!

A note about working on an entire source tree

First, make a copy of the source tree:
## Original source code is in lighttpd-1.4.35/ directory ##
$ cp -R lighttpd-1.4.35/ lighttpd-1.4.35-new/

Cd to lighttpd-1.4.35-new directory and make changes as per your requirements:
$ cd lighttpd-1.4.35-new/
$ vi geoip-mod.c
$ vi Makefile

Finally, create a patch with the following command:
$ cd ..
$ diff -rupN lighttpd-1.4.35/ lighttpd-1.4.35-new/ > my.patch

You can use my.patch file to patch lighttpd-1.4.35 source code on a different computer/server using patch command as discussed above:
patch -p1 < my.patch
See the man page of patch and other command for more information and usage - bash(1)

Share on Facebook Twitter

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Tags: blog
admin

admin

Related Posts

Blog

LINUX BACKUP & RESTORE FULL OS

Tháng Mười Hai 16, 2020
Blog

VPS Ram 4GB Chỉ 199k/tháng Miễn Phí Gsuite,

Tháng Tám 29, 2020
Blog

Tài Khoản Google Drive Unlimited 2020 50k – Google Drive Không Giới Hạn – Google drive unlimited 2020

Tháng Bảy 11, 2020
Bán tài khoản MOVO CASH USA ACCOUNT (VCC+VBA)
Blog

Bán tài khoản MOVO CASH USA ACCOUNT (VCC+VBA)

Tháng Bảy 11, 2020
Blog

Hướng dẫn kích hoạt bản quyền Windows Server 2012 R2 không cần crack

Tháng Sáu 10, 2020
Hướng dẫn cài đặt và cấu hình Openstack toàn tập từ A-Z
Blog

Hướng dẫn cài đặt và cấu hình Openstack toàn tập từ A-Z

Tháng Sáu 4, 2020
Hướng dẫn Tự Tạo Ứng Dụng Họp Online Phòng Học Online Miễn Phí với Jitsi
Blog

Hướng dẫn Tự Tạo Ứng Dụng Họp Online Phòng Học Online Miễn Phí với Jitsi

Tháng Tư 13, 2020
Hướng dẫn sử dụng Remote Desktop để truy cập VPS
Blog

Hướng dẫn sử dụng Remote Desktop để truy cập VPS

Tháng Tư 12, 2020
Download Kali Linux 2020.1
Blog

Download Kali Linux 2020.1

Tháng Tư 12, 2020
vps giá rẻ
  • Trending
  • Comments
  • Latest
Hướng dẫn cài DirectAdmin Nulled mới nhất 2020

Hướng dẫn cài DirectAdmin Nulled mới nhất 2020

Tháng Sáu 10, 2020
Wowza Streaming Engine 4.7.7 Crack Linux/Windows

Wowza Streaming Engine 4.7.7 Crack Linux/Windows

Tháng Mười Một 15, 2020
Nén và giải nén trong linux centos 6 centos 7 nén zip, tar.gz và tar.bz2.

Khuyến mãi vps 50k/tháng SSD 50Gb

Tháng Sáu 15, 2019
Bán tài khoản MOVO CASH USA ACCOUNT (VCC+VBA)

Bán tài khoản MOVO CASH USA ACCOUNT (VCC+VBA)

Tháng Bảy 11, 2020

Cpanel Nulled Cpanel/whm Nulled on Centos VPS/Dedicated Servers

Tháng Mười 8, 2020
Hướng dẫn cài đặt và cấu hình Openstack toàn tập từ A-Z

Hướng dẫn cài đặt và cấu hình Openstack toàn tập từ A-Z

Tháng Sáu 4, 2020

LINUX BACKUP & RESTORE FULL OS

0

Milestones & Timeline

0

The Economics of Websites

0

The Landing Page Guide

0

The Next Big Thing

0

UX Design Is Easy

0

LINUX BACKUP & RESTORE FULL OS

Tháng Mười Hai 16, 2020

VPS Ram 4GB Chỉ 199k/tháng Miễn Phí Gsuite,

Tháng Tám 29, 2020

Tài Khoản Google Drive Unlimited 2020 50k – Google Drive Không Giới Hạn – Google drive unlimited 2020

Tháng Bảy 11, 2020
Bán tài khoản MOVO CASH USA ACCOUNT (VCC+VBA)

Bán tài khoản MOVO CASH USA ACCOUNT (VCC+VBA)

Tháng Bảy 11, 2020

Hướng dẫn kích hoạt bản quyền Windows Server 2012 R2 không cần crack

Tháng Sáu 10, 2020
Hướng dẫn cài đặt và cấu hình Openstack toàn tập từ A-Z

Hướng dẫn cài đặt và cấu hình Openstack toàn tập từ A-Z

Tháng Sáu 4, 2020
  • vps giá rẻ
  • cpanel nulled
  • bitcoin exchange script
  • Home 1
Call us: 0975757375

© 2020

No Result
View All Result
  • vps giá rẻ
  • cpanel nulled
  • bitcoin exchange script
  • Home 1

© 2020

Welcome Back!

Login to your account below

Forgotten Password?

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In